// the interrupt handler for the button
        void button_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            if (upstreamMQTT != null)
            {
                try
                {
                    upstreamMQTT.UnsubscribeFromEvents();
                }
                catch
                {
                }

                upstreamMQTT.Disconnect();
                upstreamMQTT = null;
                NDLogger.Log("MQTT connection cancelled", LogLevel.Verbose);
            }
            else
            {
                try
                {
                    startMQTT();
                }
                catch
                {
                }
            }
        }
Beispiel #2
0
 public MQTTLogger(MQTTCloudPlatform platform)
 {
     this.platform = platform;
     this.handler = logToMQTT;
 }
        void startMQTT()
        {
            IPHostEntry hostEntry = null;
            try
            {
                hostEntry = Dns.GetHostEntry(Configuration.MQTT.HostName);
            }
            catch (SocketException se)
            {
                NDLogger.Log("Unable to get host entry by DNS error: " + se, LogLevel.Error);
                return;
            }

            upstreamMQTT = new NDMQTT();

            int returnCode = upstreamMQTT.Connect(hostEntry, Configuration.MQTT.UserName, Configuration.MQTT.Password, Configuration.MQTT.HostPort);

            if (returnCode == 0)
            {
                NDLogger.AddLogger(new MQTTLogger(upstreamMQTT));
            }
            else
            {
                upstreamMQTT = null;
                return;
            }

            upstreamMQTT.SubscribeToEvents(new int[] { 0 }, new String[] { Configuration.MQTT.SensorDataTopic });
        }