Skip to main content

General

Örnek: MQTT Bridge

Harici broker'lara (AWS/HiveMQ) veri köprülemek için MQTT Client kullanımı.

SDK, yönetilen bir MQTT Client içerir. 3. parti cloud'lar veya legacy sistemlerle entegrasyon için kullanabilirsiniz.

using MQTTnet; 
using Proxus.SDK.BaseFunctions;
using Proxus.Common.Messages;
using Proxus.Common;

namespace MyProject.Functions
{
    public class AwsBridge : FunctionBase
    {
        // 1. Konfigürasyon Property'lerini Override et
        protected override string MqttBrokerHost => "a3xyz-ats.iot.us-east-1.amazonaws.com";
        protected override int MqttBrokerPort => 8883;
        protected override string MqttBrokerUser => null; // Prod'da sertifika

        public AwsBridge(object sys, object log, object config) : base(sys, log, config) { }

        protected override void OnStarted()
        {
            // 2. İç veri aboneliği
            Subscriptions?.Add(new SubscriptionContext {
                Type = typeof(TransportData),
                Topics = (HashSet<string>) ["Gen_01"]
            });

            // 3. Cloud komutlarını dinle
            SubscribeToMqttTopic("cmd/reset");

            base.OnStarted();
        }

        // Internal -> External
        protected override void OnMessageReceive(FunctionContext ctx)
        {
            if (ctx.Message is TransportData data)
            {
                var temp = data.GetPayloadValueByName<double>("Temperature");
                string json = $"{{\"device\": \"{data.Topic}\", \"val\": {temp}}}";
                
                PublishMqttMessage("dt/telemetry", json);
            }
        }

        // External -> Internal
        protected override void HandleMqttMessage(MqttApplicationMessage message)
        {
            string cmd = message.ConvertPayloadToString();
            LogInformation($"Cloud Command Received: {cmd}");

            if (cmd == "RESET_COUNTER")
            {
                // Yerel state'i resetleme veya PLC'ye yazma
            }
        }
    }
}