private bool Subscribe()
        {
            bool success = false;
            int messageId = 0;

            try
            {
                Subscription subscription = null;
                subscription = new Subscription(Topic + "pingresp", QoS.BestEfforts);
                messageId = _mqttService.Subscribe(subscription);

                // Subscribe to any doorbell messages
                string topic = "/" + _houseCode + "/externaldoor/+/doorbell";
                subscription = new Subscription(topic, QoS.BestEfforts);
                messageId = _mqttService.Subscribe(subscription);

                success = true;
            }
            catch (Exception ex)
            {
                _logger.Error("Exception during subscription", ex);
            }
            return success;
        }
 public int Subscribe(Subscription subscription)
 {
     if (_isConnected)
     {
         ushort messageId = MessageId;
         string[] topics = new string[1];
         int[] qos = new int[1];
         topics[0] = subscription.Topic;
         qos[0] = (int)subscription.QualityOfService;
         SubscribeMQTT(_socket, topics, qos, 1,messageId);
         return messageId;
     }
     else
     {
         throw new MqttNotConnectedException("You must connect to a broker before you can call subscribe");
     }
 }
 public int Subscribe(Subscription[] subscriptions)
 {
     throw new NotImplementedException();
 }
        private bool Subscribe()
        {
            bool success = false;
            int messageId = 0;

            try
            {
                Subscription subscription = null; ;
                subscription = new Subscription(Topic + "pingresp", QoS.BestEfforts);
                messageId = _mqttService.Subscribe(subscription);

                // Subscribe to any alarm state changes (away, sleep, off)
                subscription = new Subscription(Topic + "setalarmstate", QoS.BestEfforts);
                messageId = _mqttService.Subscribe(subscription);

                // Subscribe to messages that indicate the code was valid or invalid
                subscription = new Subscription(Topic + "codevalid", QoS.BestEfforts);
                messageId = _mqttService.Subscribe(subscription);

                // Subscribe to messages that indicate the alarm state change was valid or invalid
                subscription = new Subscription(Topic + "alarmstatevalid", QoS.BestEfforts);
                messageId = _mqttService.Subscribe(subscription);

                success = true;
            }
            catch (Exception ex)
            {
                _logger.Error("Exception during subscription", ex);
            }
            return success;
        }
        private bool Subscribe()
        {
            bool success = false;
            int messageId = 0;

            try
            {
                Subscription subscription = new Subscription(Topic + "pingresp", QoS.BestEfforts);
                messageId = _mqttService.Subscribe(subscription);
                success = true;

                subscription = new Subscription(Topic + "setlock", QoS.BestEfforts);
                messageId = _mqttService.Subscribe(subscription);

                subscription = new Subscription(Topic + "codevalid", QoS.BestEfforts);
                messageId = _mqttService.Subscribe(subscription);

            }
            catch (Exception ex)
            {
                _logger.Error("Exception during subscription", ex);
            }
            return success;
        }
Beispiel #6
0
        private static void SetupMqtt()
        {
            try
            {
                _mqtt = MqttClientFactory.CreateClient(_settings.Server, _settings.Key);

                _mqtt.PublishArrived -= new PublishArrivedDelegate(MqttPublishArrived);
                _mqtt.ConnectionLost -= new ConnectionDelegate(MqttConnectionLost);

                _mqtt.Connect();

                Subscription subscription = new Subscription(_settings.Key, QoS.BestEfforts);
                _mqtt.Subscribe(subscription);

                _mqtt.PublishArrived += new PublishArrivedDelegate(MqttPublishArrived);
                _mqtt.ConnectionLost += new ConnectionDelegate(MqttConnectionLost);

                _blinkLedCommand.Execute(CreateLedBlinkCommand(2));
            }
            catch (Exception ex)
            {
                Restart("Setup MQTT has failed", ex);
            }
        }