Exemple #1
0
        private void MQTTConnectiobn()
        {
            byte returnCode = AppMQTTClient.Connect(ClientId, "ash", "ash", false, BrokerKeepAlivePeriod);

            if (returnCode != 0)
            {
                MakeConnection();
            }
        }
Exemple #2
0
 public string Subscribe(string messgeTopic)
 {
     if (AppMQTTClient != null)
     {
         ushort msgId = AppMQTTClient.Subscribe(new string[] { messgeTopic },
                                                new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }
                                                );
         //Logger.Info(string.Format("Subscription to topic {0}", messgeTopic));
     }
     return("Success");
 }
Exemple #3
0
        private void DefinedMQTTCommunicationEvents()
        {
            AppMQTTClient.MqttMsgPublished       += client_MqttMsgPublished;       //publish
            AppMQTTClient.MqttMsgSubscribed      += client_MqttMsgSubscribed;      //subscribe confirmation
            AppMQTTClient.MqttMsgUnsubscribed    += client_MqttMsgUnsubscribed;
            AppMQTTClient.MqttMsgPublishReceived += client_MqttMsgPublishReceived; //received message.
            AppMQTTClient.ConnectionClosed       += client_ConnectionClosed;

            ushort submsgId = AppMQTTClient.Subscribe(new string[] { "shhello", "sh/configuration", "sh/command", "sh/feedback/#" },
                                                      new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE,
                                                                   MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
        }
Exemple #4
0
 public void MakeDisConnect()
 {
     if (AppMQTTClient != null && AppMQTTClient.IsConnected)
     {
         try
         {
             AppMQTTClient.Disconnect();
         }
         catch (Exception ex)
         {
             Logger.LogError(ex, string.Format("Could not disconnect to MQ broker: {0}", ex.Message));
         }
     }
 }
Exemple #5
0
 public string Publish(string messgeTopic, string publishMessage)
 {
     if (AppMQTTClient != null)
     {
         try
         {
             lock (locker)
             {
                 ushort msgId = AppMQTTClient.Publish(messgeTopic,                            // topic
                                                      Encoding.UTF8.GetBytes(publishMessage), // message body
                                                      MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE,     // QoS level
                                                      false);
             }
         }
         catch (Exception ex)
         {
             //    log.Warn("Error while publishing: " + ex.Message, ex);
         }
     }
     return("Success");
 }
Exemple #6
0
        public void MakeConnection()
        {
            Logger.Info("Make Connection Envoke");

            #region MyRegion

            try
            {
                if (AppMQTTClient == null || !AppMQTTClient.IsConnected)
                {
                    Logger.Info("MQTT process to start connection.");

                    if (BrokerAddress == "192.168.11.236")
                    {
                        LocalBrokerConnection(BrokerAddress);
                    }

                    else if (BrokerAddress == "192.168.11.189")
                    {
                        BrokerConnectionWithoutCertificateForCommand(BrokerAddress);
                    }

                    else if (BrokerAddress == "192.168.11.190")
                    {
                        Logger.Info("Try to connect with broker.");
                        //BrokerConnectionWithoutCertificateForCommand(BrokerAddress);
                        BrokerConnectionWithCertificateForWebBroker(BrokerAddress);
                        Logger.Info("Web broker connection successfull.");
                    }
                    else if (BrokerAddress == "192.168.11.150")
                    {
                        BrokerConnectionWithoutCertificate(BrokerAddress);
                    }
                    else
                    {
                        BrokerConnectionWithCertificate(BrokerAddress);
                    }

                    DefinedMQTTCommunicationEvents();
                    Logger.Info("MQTT successfully stablished connection.");
                }
            }
            catch (Exception ex)
            {
                Logger.Info(string.Format("Could not stablished connection to MQ broker: {0}", ex.Message));

                //don't leave the client connected
                if (AppMQTTClient != null && AppMQTTClient.IsConnected)
                {
                    try
                    {
                        AppMQTTClient.Disconnect();
                    }
                    catch
                    {
                        ////Logger.LogError(ex, string.Format("Could not disconnect to MQ broker: {1}", ex.Message));
                    }
                }
                //ideally through exception and call make connection on hanle it.
                MakeConnection();
                Logger.Info(string.Format("Try to connect with mqtt"));
            }
            #endregion
        }