Example #1
0
        private void MqttClient_MessageReceivedEvent(MQTTnet.MqttApplicationMessageReceivedEventArgs eventArgs)
        {
            var messageAsJson = Encoding.UTF8.GetString(eventArgs.ApplicationMessage.Payload);

            var topic = eventArgs.ApplicationMessage.Topic.Split('/').Last();

            switch (topic)
            {
            case "start":
                StartTimer();
                break;

            case "update":
                _model = JsonConvert.DeserializeObject <ScoreBoardModel>(messageAsJson);
                Dispatcher.Invoke(() => DataContext = _model);
                break;

            case "stop":
                if (!string.IsNullOrWhiteSpace(messageAsJson) && messageAsJson != "empty")
                {
                    var offset = JsonConvert.DeserializeObject <TimeSpan>(messageAsJson);
                    StopTimer(offset);
                }
                else
                {
                    StopTimer();
                }
                break;

                //case "pause":
                //    break;
            }
        }
Example #2
0
        public ScoreBoardWindow()
        {
            InitializeComponent();

            _timer          = new Timer();
            _timer.Interval = 1000;
            _timer.Elapsed += Timer_Elapsed;

            _model           = new ScoreBoardModel();
            this.DataContext = _model;

            _mqttServer = new Mqtt.Server(_options);
            _mqttClient = new Mqtt.Client(_options);
            _mqttClient.MessageReceivedEvent += MqttClient_MessageReceivedEvent;
            _mqttClient.Subscribe($"display/{_options.ClientId}/#");                                                  // subscribe to all updates meant for me!
            Task.Run(async() => await _mqttClient.SendMessageAsync($"display/{_options.ClientId}/status", "online")); // tell the world I'm here!
        }