Ejemplo n.º 1
0
        public void Mqtt1()
        {
            var    rnd     = new Random();
            var    client1 = MqttClientFactory.CreateConnectedClient();
            var    client2 = MqttClientFactory.CreateConnectedClient();
            var    lck     = new object();
            string mqttMsg = null;

            var topic = "opentrigger/tests/D59979C42762488F8B570A1F16BE3AB6/" + DateTime.Now.Ticks;
            var msg   = "asdf-" + DateTime.Now.Ticks;

            client1.MqttMsgSubscribed += (sender, args) =>
            {
                Debug.WriteLine($"subscribed {args.MessageId}");
            };

            client1.Subscribe(topic);
            client1.Subscribe(topic + "/ll");
            client1.MqttMsgPublishReceived += (sender, args) =>
            {
                var inMsg  = Encoding.UTF8.GetString(args.Message);
                var dbgMsg = $"msg '{inMsg}' from '{args.Topic}'";
                Debug.WriteLine(dbgMsg);
                lock (lck)
                {
                    mqttMsg = inMsg;
                }
            };

            client2.Publish(topic, Encoding.UTF8.GetBytes(msg));

            var sw = new Stopwatch();

            sw.Start();

            while (true)
            {
                lock (lck)
                {
                    if (mqttMsg == msg || sw.ElapsedMilliseconds > 5000)
                    {
                        break;
                    }
                }
                System.Threading.Thread.Sleep(500);
            }
            client1.Disconnect();
            client2.Disconnect();

            Assert.AreEqual(msg, mqttMsg);
        }
Ejemplo n.º 2
0
        private MqttClient GetMqttClient()
        {
            if (_channels == null)
            {
                var prefix    = AppSettings.Get("MqttPrefix").TrimEnd('/');
                var websocket = AppSettings.Get("MqttWebsocket");
                _channels = new Channels
                {
                    Prefix             = prefix,
                    Websocket          = websocket,
                    ScreenN            = $"{prefix}/N",
                    ScreenS            = $"{prefix}/S",
                    ScreenW            = $"{prefix}/W",
                    ScreenO            = $"{prefix}/O",
                    Vote               = $"{prefix}/Vote",
                    Debug              = $"{prefix}/Debug",
                    Command            = $"{prefix}/Command",
                    OpenTriggerRelease = AppSettings.Get <string>("OpenTriggerTopic", null),
                };
            }
            if (_mqttClient == null || _mqttClient.IsConnected == false)
            {
                _mqttClient = MqttClientFactory.CreateConnectedClient();

                _mqttClient.MqttMsgSubscribed += (sender, args) =>
                {
                    var client = (MqttClient)sender;
                    var msg    = new
                    {
                        @event = "MqttMsgSubscribed",
                        client.ClientId,
                        args.MessageId,
                        args.GrantedQoSLevels,
                    };
                    PublishMessage(_channels.Debug, msg);
                };

                _mqttClient.Subscribe(_channels.Vote);
                if (!string.IsNullOrWhiteSpace(_channels.OpenTriggerRelease))
                {
                    _mqttClient.Subscribe(_channels.OpenTriggerRelease);
                }

                _mqttClient.MqttMsgPublishReceived += (sender, args) =>
                {
                    var msg = JToken.Parse(_encoding.GetString(args.Message));
                    MessageReceived(args.Topic, msg, args.QosLevel, args.Retain, args.DupFlag);
                };
            }
            return(_mqttClient);
        }