Example #1
0
 private void OnDestroy()
 {
     if (mqttClient != null)
     {
         mqttClient.Disconnect();
     }
 }
Example #2
0
        public override async Task TeardownAsync()
        {
            await base.TeardownAsync();

            if (_mqttclient != null)
            {
                _mqttclient.MqttMsgPublishReceived -= Mqttclient_MqttMsgPublishReceived;
                _mqttclient.ConnectionClosed       -= MqttclientOnConnectionClosed;
                _mqttclient.Disconnect();
                _mqttclient = null;
                _methodRegistrations.Clear();
            }
        }
Example #3
0
        /// <summary>
        /// 브로커 접속 테스트
        /// </summary>
        /// <param name="BrokerIP">브로커 주소</param>
        /// <param name="ClientID">클라이언트 ID</param>
        /// <returns>테스트 결과</returns>
        public static bool Test(string BrokerIP, string ClientID)
        {
            bool ret = false;

            try
            {
                var client = new uPLibrary.Networking.M2Mqtt.MqttClient(BrokerIP);
                if (client.Connect(ClientID) == 0)
                {
                    ret = true;
                    System.Threading.Thread.Sleep(500);
                    client.Disconnect();
                }
            }
            catch (Exception) { }
            return(ret);
        }
Example #4
0
        static void Main(string[] args)
        {
            string AppEUI = @"70B3D57ED0000538"; // TODO: make configurable
            string AccessKey = @"Dj8ebO/p+agcxgKoRMmF0rKd07X7m4idj+M6HiS48EY="; // TODO: make configurable

            string username = AppEUI;
            string password = AccessKey;

            string mqttBrokerHostName = "rene-ttn-test"; // TODO: make configurable
                                                         // TODO: use TLS

            Console.WriteLine("starting up...");
            MqttClient client = new MqttClient(mqttBrokerHostName); // TODO: add error handling!

            // Register event handler for Connection being closed
            client.ConnectionClosed += client_ConnectionClosed;

            // register event handler for received messages
            client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;

            // Register event handler for subscription
            client.MqttMsgSubscribed += client_MqttMsgSubscribed;

            string clientId = "RSTest";
            client.Connect(clientId, username, password);   // TODO: add error handling

            // subscribe to the topic ( all Applications, all Devices
            client.Subscribe(new string[] { @"+/devices/+/up" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });

            Console.WriteLine("Press <Enter> to quit");
            Console.ReadLine();

            // Remove event handler for Connection being closed
            client.ConnectionClosed -= client_ConnectionClosed;
            client.Disconnect();

            Console.WriteLine("Ended!");
        }
Example #5
0
        private void Publish_Click(object sender, RoutedEventArgs e)
        {
            MqttClient Mq;
            string broker = "192.168.42.1";
            //string broker = "127.0.0.1";

            Refresh_Click(this, null);
            Mq = new MqttClient(broker);
            Mq.Connect("pNavPlan3");
            System.Diagnostics.Trace.WriteLine($"Connected to MQTT @ {broker}", "1");
            var jsn = NavPointText;
            Mq.Publish("Navplan/WayPoints", Encoding.ASCII.GetBytes(jsn));
            System.Threading.Thread.Sleep(200);

            Mq.Disconnect();
            StatusText = "NavPoints published";
        }
Example #6
0
        static int Main(string[] args)
        {
            if (args.Any() && args[0] == "TestMode")
            {
                var client = new MqttClient("localhost");
                client.MqttMsgPublishReceived += (s, m) =>
                {
                    Console.WriteLine($"Publish Recieved: {m.Topic} \t {Encoding.ASCII.GetString(m.Message)}");
                };

                client.MqttMsgPublished += (s, m) =>
                {
                    Console.WriteLine("Message Published");
                };

                client.ConnectionClosed += (s, m) =>
                {
                    Console.WriteLine("Connection Closed");
                };

                client.MqttMsgSubscribed += (s, m) =>
                {
                    Console.WriteLine("Message Subscribed");
                };

                client.MqttMsgUnsubscribed += (s, m) =>
                {
                    Console.WriteLine("Message Unsubscribed");
                };

                client.Reconnected += (s, m) =>
                {
                    Console.WriteLine("Reconnected");
                };

                client.Connected += (s, m) =>
                {
                    Console.WriteLine("Connected");
                };

                client.Connected += (s, m) =>
                {
                    Console.WriteLine("I am a fruitcake");
                };

                client.Disconnected += (s, m) =>
                {
                    Console.WriteLine("Disconnected");
                };

                client.Event += (s, m) =>
                {
                    var t = "{\r\n";
                    var q = m.Dict.Select((a) => $"\t\"{a.Key}\" : \"{a.Value}\"");
                    t += String.Join(",\r\n", q);
                    t += "\r\n}";
                    Console.WriteLine(t);
                };

                //client.shouldReconnect = false;
                client.Connect();

                Console.ReadKey();
                Console.WriteLine("Begin attempt publish");
                var do_the_thing = true;
                new Thread(() =>
                {
                    while (do_the_thing)
                    {
                        client.Publish("/banana", Encoding.ASCII.GetBytes("I am a fish"));
                        Thread.Sleep(1000);
                    }
                }).Start();

                Console.ReadKey();
                Console.WriteLine("subscribe to banana");
                client.AddTopic(new Topic.Topic("/banana"));
                Console.ReadKey();
                Console.WriteLine("subscribe to fig");
                client.AddTopic(new Topic.Topic("/fig"));
                Console.ReadKey();
                Console.WriteLine("unsubscribe from fig");
                client.RemoveTopic("/fig");
                Console.ReadKey();
                do_the_thing = false;
                client.Disconnect();
            }
            return(0);
        }