Пример #1
0
        public void publish(string[] topic, string[] payload, string mqttBrokerAddress)
        {
            if (mqttClient != null)
            {
                if (!mqttBrokerAddress.Equals(this.MqttBrokerAddressPublish) & mqttClient.IsConnected)
                {
                    mqttClient.Disconnect();
                }
            }
            if (topic.Length != payload.Length)
            {
                throw new ArgumentOutOfRangeException("Array topic and payload must be the same size");
            }
            mqttClient = new uPLibrary.Networking.M2Mqtt.MqttClient(mqttBrokerAddress);
            string clientID = Guid.NewGuid().ToString();

            if (!mqttClient.IsConnected)
            {
                mqttClient.Connect(clientID);
            }
            for (int i = 0; i < payload.Length; i++)
            {
                mqttClient.Publish(topic[i], Encoding.UTF8.GetBytes(payload[i]), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
            }
        }
Пример #2
0
        protected void Dispose(bool disposing)
        {
            if (!_disposedValue)
            {
                if (disposing)
                {
                    try
                    {
                        _client.Disconnect();
                    }
                    catch (System.Net.Sockets.SocketException)
                    {
                        Console.Error.WriteLine("Client not disconnected properly");
                    }
                    finally
                    {
                        _IsSetup        = false;
                        _client         = null;
                        _brokerEndPoint = null;
                    }
                }

                _disposedValue = true;
            }
        }
Пример #3
0
        private static async Task <int> _publish()
        {
            Console.WriteLine("Type 'q' to quit");

            var client = new uPLibrary.Networking.M2Mqtt.MqttClient("grissom.klingon");

            client.Connect(Guid.NewGuid().ToString());
            Random r    = new Random((int)(DateTime.Now.Ticks + (long)Environment.CurrentManagedThreadId));
            string text = null;

            do
            {
                text = Console.ReadLine();
                if (text != "q")
                {
                    double d     = r.NextDouble() * 100.00;
                    var    bytes = System.Text.Encoding.UTF8.GetBytes(string.Format("{{ \"temp\":{0} }}", d));

                    client.Publish("house/serverroom/temp", bytes);

                    Console.WriteLine(1);
                }
            } while(text != "q");

            client.Disconnect();

            return(1);
        }
Пример #4
0
        private static void _subscribe(MqttConfig cfg)
        {
            //System.Threading.Timer tmr;
            //"192.168.9.21"

            var client = new uPLibrary.Networking.M2Mqtt.MqttClient(cfg.server);

            client.ProtocolVersion         = uPLibrary.Networking.M2Mqtt.MqttProtocolVersion.Version_3_1_1;
            client.MqttMsgSubscribed      += Client_MqttMsgSubscribed;
            client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
            client.MqttMsgPublished       += Client_MqttMsgPublished;

            if (!string.IsNullOrWhiteSpace(cfg.username))
            {
                client.Connect(Guid.NewGuid().ToString(), cfg.username, cfg.password);
            }
            else
            {
                client.Connect(Guid.NewGuid().ToString());
            }

            var channels = new List <string>();
            var qoss     = new List <byte>();

            {
                foreach (var s in cfg.subscriptions)
                {
                    channels.Add(s.channel);
                    qoss.Add(s.qos);
                }
            }

            /*client.Subscribe(new string[] {"house/serverroom/temp"}, new byte[] { 0});*/
            client.Subscribe(channels.ToArray(), qoss.ToArray());

            Console.WriteLine("Type 'q' to quit");
            string text = null;

            do
            {
                text = Console.ReadLine();
            } while(text != "q");

            client.Disconnect();
        }
Пример #5
0
 public void Disconnect()
 {
     mqttClient.Disconnect();
 }
Пример #6
0
 public void stop()
 {
     this.Disconnect();
     mqttClient.Disconnect();
     this.shouldStop = true;
 }