Ejemplo n.º 1
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));
         }
     }
 }
Ejemplo n.º 2
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
        }