Exemple #1
0
 private void BtnSend_Click(object sender, EventArgs e)
 {
     if (TopicField.TextLength == 0 || MessageField.TextLength == 0)
     {
         MessageBox.Show("Wprowadź dane", "Błąd",
                         MessageBoxButtons.OK,
                         MessageBoxIcon.Warning);
         return;
     }
     Client.SendMessage(TopicField.Text, MessageField.Text);
 }
        private void HandlingSensorResource(WebsocketEvent message)
        {
            _logger.Debug("Received websocket message of type {0}", message.EventType);
            if (message.EventType.ToLowerInvariant() == "changed")
            {
                if (message.State == null)
                {
                    _logger.Warn("Received empty message state in websocket msg. Dismiss message");
                    return;
                }

                var entity = _sensorRepository.GetById(message.Id);
                _sensorRepository.RefreshState(entity, message.State);

                if (entity != null)
                {
                    var name = entity.Name;
                    foreach (var state in message.State)
                    {
                        var key = state.Key.ToLowerInvariant();
                        if (key == "lastupdated")
                        {
                            continue;
                        }


                        var mqttMessage = new DeconzItemStateChangedMqttMessage
                        {
                            Entity       = entity,
                            PropertyName = key,
                            Content      = entity.State.Data[state.Key].ToString().ToLowerInvariant()
                        };
                        _logger.Debug("Publishing change for entity '{0}' on state '{1}' to value '{2}'",
                                      mqttMessage.Entity.Name,
                                      mqttMessage.PropertyName,
                                      mqttMessage.Content);
                        _mqttClient.SendMessage(mqttMessage);
                    }
                }
                else
                {
                    _logger.Warn("Got websocket message with invalid entity id {0}", message.Id);
                }

                _sensorRepository.NotifyChange(message.Id);
            }
        }
        private void Run()
        {
            while (!_cancelationToken.IsCancellationRequested)
            {
                _logger.Info("Sending telemetry");
                _metricRecorder.CountEvent(DeconzToMqttIdentifier.SendInterval());
                try
                {
                    foreach (var repository in _repositories)
                    {
                        foreach (var item in repository.GetAllDeconzItems())
                        {
                            try
                            {
                                var msg = new DeconzItemTelemetryMqttMessage
                                {
                                    Content = repository.Serialize(item),
                                    Entity  = item
                                };
                                _mqttClient.SendMessage(msg);
                            }
                            catch (Exception ex)
                            {
                                _logger.Error(ex, "Sending of message for sensor {0} failed", item.Name);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, "Error in telemetry processing occurred");
                }

                Thread.Sleep(TimeSpan.FromSeconds(60));
            }

            _task = null;
        }