Пример #1
0
        private void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            var msg = MqttMessageBuilder
                      .NewMessage()
                      .AddTopic(e.Topic)
                      .SetMessage(Encoding.UTF8.GetString(e.Message))
                      .SetRawMessage(e.Message)
                      .SetMessageType(MqttMessageType.MQTT_PUBLISH)
                      .Build();

            MessageReceivedString?.Invoke(msg);
        }
Пример #2
0
        public void MqttConnect()
        {
            if ((_autoReconnect) && (!_reconnectTimerStarted))
            {
                _reconnectTimer.Start();
                _reconnectTimerStarted = true;
            }

            if (client.IsConnected)
            {
                client.Disconnect();
            }

            System.Threading.Thread.Sleep(10);

            var offlineWill = MqttMessageBuilder
                              .NewMessage()
                              .AddDeviceIdToTopic
                              .AddTopic(_mqttSettings.will.topic)
                              .Build();

            byte conn = client.Connect(_mqttSettings.deviceId, _mqttSettings.user, _mqttSettings.password, true, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE,
                                       _mqttSettings.will.enabled, offlineWill.GetRawTopic(), _mqttSettings.will.offlineMessage, false, _mqttSettings.will.keepAlive);

            if (client.IsConnected)
            {
                var onlineWill = MqttMessageBuilder
                                 .NewMessage()
                                 .AddDeviceIdToTopic
                                 .AddTopic(_mqttSettings.will.topic)
                                 .SetMessage(_mqttSettings.will.onlineMessage)
                                 .SetMessageType(MqttMessageType.MQTT_PUBLISH)
                                 .Build();

                Publish(onlineWill);
                ConnectionConnected?.Invoke();
            }
            else
            {
                switch (conn)
                {
                case MqttMsgConnack.CONN_REFUSED_IDENT_REJECTED:
                    ConnectionClosed?.Invoke("Ident rejected by server", conn);
                    break;

                case MqttMsgConnack.CONN_REFUSED_NOT_AUTHORIZED:
                    ConnectionClosed?.Invoke("User not authorized", conn);
                    break;

                case MqttMsgConnack.CONN_REFUSED_PROT_VERS:
                    ConnectionClosed?.Invoke("Protocol version mismatch", conn);
                    break;

                case MqttMsgConnack.CONN_REFUSED_SERVER_UNAVAILABLE:
                    ConnectionClosed?.Invoke("Server unavailable", conn);
                    break;

                case MqttMsgConnack.CONN_REFUSED_USERNAME_PASSWORD:
                    ConnectionClosed?.Invoke("User/Pass error", conn);
                    break;

                default:
                    ConnectionClosed?.Invoke("Unknown error", conn);
                    break;
                }
            }

            _queueCancellationTokenSource = new CancellationTokenSource();

            Task t;

            t = Task.Run(() => ProcessMessageQueue(), _queueCancellationTokenSource.Token);
        }