Ejemplo n.º 1
0
 /**
  * \fn changeStateMqttFlow
  * \brief change the state of the mqtt workflow
  * \param [in] state : target input state
  */
 private void changeStateMqttFlow(bool state)
 {
     if (true == state)
     {
         this.listConsole.Items.Clear();
         //
         this.m_currentSubscribeInformation = new MqttSubscribeInfo(new string[] { MQTT_MAIN_TOPIC }, new byte[] { 0x00 });
         int port = 0;
         if (true == int.TryParse(this.tbBrokerPortInput.Text, out port))
         {
             this.m_currentBrokerInfos = new MqttBrokerInfo(this.tbBrokerHostnameInput.Text, port);
             //
             this.m_client = new SampleMqttClient(this.m_currentBrokerInfos, false, null, null, uPLibrary.Networking.M2Mqtt.MqttSslProtocols.None);
             this.m_client.Connect();
             this.m_client.Subscribe(this.m_currentSubscribeInformation, SampleMqttClient_MqttMsgPublishReceived);
             //
             this.btnManageWorkflow.Content = DISPLAY_STOP_WF;
             this.listConsole.Items.Add($"=========> : {DISPLAY_START_WF}");
         }
     }
     else
     {
         this.m_client.Unsubscribe(this.m_currentSubscribeInformation, SampleMqttClient_MqttMsgPublishReceived);
         this.m_client.Disconnect();
         this.m_client = null;
         //
         this.m_currentSubscribeInformation = null;
         this.m_currentBrokerInfos          = null;
         //
         this.btnManageWorkflow.Content = DISPLAY_START_WF;
         this.listConsole.Items.Add($"=========> : {DISPLAY_STOP_WF}");
     }
     this.m_bWorkflowStarted = state;
 }
Ejemplo n.º 2
0
        /**
         * \fn Unsubscribe
         * \brief unsubscribe from mqtt flow
         * \param [in] mqttSubscribeInfo : mqtt subscribe informations
         * \param [in] us_topics : target topics for unsubscription
         * \return success or failed
         */
        public bool Unsubscribe(MqttSubscribeInfo mqttSubscribeInfo, MqttMsgPublishEventHandler callBack)
        {
            try
            {
                //unsubscribe
                if (null != callBack)
                {
                    MqttMsgPublishReceived -= callBack;
                }
                Unsubscribe(mqttSubscribeInfo.Topics);

                return(true);
            }
            catch (Exception ex)
            {
                throw new Exception($"Exception while trying to unsubscribe from broker (host={BrokerInfo.broker_address}, port={BrokerInfo.broker_port}) : {ex.Message}", ex);
            }
        }
Ejemplo n.º 3
0
        /**
         * \fn Subscribe
         * \brief subscribe to mqtt flow
         * \param [in] subscribeInfo : mqtt subscribe informations
         * \param [in] callBack : callback for event association
         * \return success or failed
         */
        public bool Subscribe(MqttSubscribeInfo subscribeInfo, MqttMsgPublishEventHandler callBack)
        {
            try
            {
                // register to message received
                if (null != callBack)
                {
                    MqttMsgPublishReceived += callBack;
                }

                // subscribe to the topic
                Subscribe(subscribeInfo.Topics, subscribeInfo.QoS);
                return(true);
            }
            catch (Exception ex)
            {
                throw new Exception($"[ElaMqttSubscriber]\t[Subscribe]\tException while subscribing to broker (host={BrokerInfo.broker_address}, port={BrokerInfo.broker_port}): {ex.Message}", ex);
            }
        }