Example #1
0
        private void ReceiveAttributes(MqttApplicationMessageReceivedEventArgs e)
        {
            var tps           = e.ApplicationMessage.Topic.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            var rpcmethodname = tps[2];
            var rpcdevicename = tps[1];
            var rpcrequestid  = tps[4];

            LogInformation?.Invoke($"rpcmethodname={rpcmethodname} ");
            LogInformation?.Invoke($"rpcdevicename={rpcdevicename } ");
            LogInformation?.Invoke($"rpcrequestid={rpcrequestid}   ");

            if (!string.IsNullOrEmpty(rpcmethodname) && !string.IsNullOrEmpty(rpcdevicename) && !string.IsNullOrEmpty(rpcrequestid))
            {
                if (e.ApplicationMessage.Topic.Contains("/attributes/"))
                {
                    OnReceiveAttributes?.Invoke(Client, new AttributeResponse()
                    {
                        KeyName    = rpcmethodname,
                        DeviceName = rpcdevicename,
                        Id         = rpcrequestid,
                        Data       = e.ApplicationMessage.ConvertPayloadToString()
                    });
                }
            }
        }
Example #2
0
 private void Client_ApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e)
 {
     LogDebug?.Invoke($"ApplicationMessageReceived Topic {e.ApplicationMessage.Topic}  QualityOfServiceLevel:{e.ApplicationMessage.QualityOfServiceLevel} Retain:{e.ApplicationMessage.Retain} ");
     try
     {
         if (e.ApplicationMessage.Topic.StartsWith($"/devices/") && e.ApplicationMessage.Topic.Contains("/response/"))
         {
             ReceiveAttributes(e);
         }
         else if (e.ApplicationMessage.Topic.StartsWith($"/devices/") && e.ApplicationMessage.Topic.Contains("/rpc/request/"))
         {
             var tps           = e.ApplicationMessage.Topic.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
             var rpcmethodname = tps[4];
             var rpcdevicename = tps[1];
             var rpcrequestid  = tps[5];
             LogInformation?.Invoke($"rpcmethodname={rpcmethodname} ");
             LogInformation?.Invoke($"rpcdevicename={rpcdevicename } ");
             LogInformation?.Invoke($"rpcrequestid={rpcrequestid}   ");
             if (!string.IsNullOrEmpty(rpcmethodname) && !string.IsNullOrEmpty(rpcdevicename) && !string.IsNullOrEmpty(rpcrequestid))
             {
                 OnExcRpc?.Invoke(Client, new RpcRequest()
                 {
                     Method    = rpcmethodname,
                     DeviceId  = rpcdevicename,
                     RequestId = rpcrequestid,
                     Params    = e.ApplicationMessage.ConvertPayloadToString()
                 });
             }
         }
     }
     catch (Exception ex)
     {
         LogError?.Invoke($"ClientId:{e.ClientId} Topic:{e.ApplicationMessage.Topic},Payload:{e.ApplicationMessage.ConvertPayloadToString()}", ex);
     }
 }
Example #3
0
        public async Task <bool> ConnectAsync(Uri uri, string username, string password)
        {
            if (BrokerUri == null && uri != null)
            {
                BrokerUri = uri;
            }
            if (BrokerUri != null && uri == null)
            {
                uri = BrokerUri;
            }
            bool initok = false;

            try
            {
                var factory = new MqttFactory();
                Client = factory.CreateMqttClient( );
                var clientOptions = new MqttClientOptionsBuilder()
                                    .WithClientId(uri.ToString() + Guid.NewGuid().ToString())
                                    .WithTcpServer(uri.Host, uri.Port)
                                    .WithCredentials(username, password)
                                    .Build();
                Client.ApplicationMessageReceivedAsync += Client_ApplicationMessageReceived;
                Client.ConnectedAsync += e => {
                    Client.SubscribeAsync($"/devices/{DeviceId}/rpc/request/+/+");
                    Client.SubscribeAsync($"/devices/{DeviceId}/attributes/update/", MqttQualityOfServiceLevel.ExactlyOnce);
                    LogInformation?.Invoke($"CONNECTED WITH SERVER ");
                    return(Task.CompletedTask);
                };
                Client.DisconnectedAsync += async e =>
                {
                    try
                    {
                        await Client.ConnectAsync(clientOptions);
                    }
                    catch (Exception exception)
                    {
                        LogError?.Invoke("CONNECTING FAILED", exception);
                    }
                };

                try
                {
                    var result = await Client.ConnectAsync(clientOptions);

                    initok = result.ResultCode == MqttClientConnectResultCode.Success;
                }
                catch (Exception exception)
                {
                    LogError?.Invoke("CONNECTING FAILED", exception);
                }
                LogInformation?.Invoke("WAITING FOR APPLICATION MESSAGES");
            }
            catch (Exception exception)
            {
                LogError?.Invoke("CONNECTING FAILED", exception);
            }
            return(initok);
        }
Example #4
0
        public async Task <bool> ConnectAsync(Uri uri, string username, string password)
        {
            if (BrokerUri == null && uri != null)
            {
                BrokerUri = uri;
            }
            if (BrokerUri != null && uri == null)
            {
                uri = BrokerUri;
            }
            bool initok = false;

            try
            {
                var           factory       = new MqttFactory();
                MqttNetLogger mqttNetLogger = new MqttNetLogger();
                mqttNetLogger.LogMessagePublished += MqttNetLogger_LogMessagePublished;;
                Client = factory.CreateMqttClient(mqttNetLogger);
                var clientOptions = new MqttClientOptionsBuilder()
                                    .WithClientId(uri.ToString() + Guid.NewGuid().ToString())
                                    .WithTcpServer(uri.Host, uri.Port)
                                    .WithCredentials(username, password)
                                    .Build();
                Client.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(args => Client_ApplicationMessageReceived(Client, args));
                Client.ConnectedHandler    = new MqttClientConnectedHandlerDelegate(args => Client_ConnectedAsync(Client, args));
                Client.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(async e =>
                {
                    try
                    {
                        await Client.ConnectAsync(clientOptions);
                    }
                    catch (Exception exception)
                    {
                        LogError?.Invoke("CONNECTING FAILED", exception);
                    }
                });

                try
                {
                    var result = await Client.ConnectAsync(clientOptions);

                    initok = result.ResultCode == MqttClientConnectResultCode.Success;
                }
                catch (Exception exception)
                {
                    LogError?.Invoke("CONNECTING FAILED", exception);
                }
                LogInformation?.Invoke("WAITING FOR APPLICATION MESSAGES");
            }
            catch (Exception exception)
            {
                LogError?.Invoke("CONNECTING FAILED", exception);
            }
            return(initok);
        }
Example #5
0
 public override void Information(string message)
 {
     LogInformation.Invoke(logtype, message);
 }
Example #6
0
 private void Client_ConnectedAsync(object sender, MqttClientConnectedEventArgs e)
 {
     Client.SubscribeAsync($"/devices/{DeviceId}/rpc/request/+/+");
     Client.SubscribeAsync($"/devices/{DeviceId}/attributes/update/", MqttQualityOfServiceLevel.ExactlyOnce);
     LogInformation?.Invoke($"CONNECTED WITH SERVER ");
 }