Ejemplo n.º 1
0
        //--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\
        #region --Set-, Get- Methods--
        protected void setState(ConnectionState state)
        {
            this.state = state;
            ConnectionStateChanged?.Invoke(this, this.state);
            switch (this.state)
            {
            case ConnectionState.DISCONNECTED:
                ConnectionDisconnected?.Invoke(this, new EventArgs());
                break;

            case ConnectionState.CONNECTING:
                ConnectionConnecting?.Invoke(this, new EventArgs());
                break;

            case ConnectionState.CONNECTED:
                ConnectionConnected?.Invoke(this, new EventArgs());
                break;

            case ConnectionState.DISCONNECTING:
                ConnectionDisconnecting?.Invoke(this, new EventArgs());
                break;

            case ConnectionState.ERROR:
                ConnectionError?.Invoke(this, new EventArgs());
                break;

            default:
                throw new InvalidOperationException("Invalid given state: " + state);
            }
        }
Ejemplo n.º 2
0
        public void MqttConnect()
        {
            System.Threading.Thread.Sleep(GetRandom(3000));

            ConnectionConnected?.Invoke();

            _messageId++;
            _queueCancellationTokenSource = new CancellationTokenSource();

            Task t;

            t = Task.Run(() => ProcessMessageQueue(), _queueCancellationTokenSource.Token);
        }
Ejemplo n.º 3
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);
        }