Exemplo n.º 1
0
        /// <summary>
        /// WebSocket Server event handler, fires the event.
        /// </summary>
        /// <param name="arg">Arguments containing event arguments.</param>
        public void OnWebsocketEvent(object sender, WebSocketEventArg arg)
        {
            if (arg.isClosed)
            {
                WebSocketClientInfo key = null;
                bool found = false;

                foreach (var connection in _ListWithConnections)
                {
                    if (connection.Key.clientId == arg.clientId)
                    {
                        key   = connection.Key;
                        found = true;
                        break;
                    }
                }
                if (found)
                {
                    _ListWithConnections.Remove(key);
                }
            }


            WebsocketEvent?.Invoke(this, arg);
        }
Exemplo n.º 2
0
 public void Handle(WebsocketEvent message)
 {
     if (message.ResourceType.ToLowerInvariant() == "sensors")
     {
         HandlingSensorResource(message);
     }
 }
Exemplo n.º 3
0
        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);
            }
        }
Exemplo n.º 4
0
 public BaseWebsocketEvent(WebsocketEvent websocketEvent)
 {
     WebsocketEvent = websocketEvent;
 }