public static void LightwaveRfEventHandler(LightwaveRfWebsocketMessage receivedMessage)
 {
     foreach (var item in receivedMessage.items)
     {
         LightwaveRfFeature feature = null;
         if (_FEATURES.TryGetValue((string)item.payload.featureId, out feature))
         {
             feature.attributes.value = item.payload.value;
             Console.WriteLine("EVENT: {0} Attribute: {1} Value: {2}", (_FEATURESETS[feature.featureId] ?? feature.attributes.name), feature.attributes.type, item.payload.value);
         }
     }
 }
        private static void WebSocketReceiveMessage(string data)
        {
            LightwaveRfWebsocketMessage sentMessage     = null;
            LightwaveRfWebsocketMessage receivedMessage = null;

            try
            {
                receivedMessage = JsonConvert.DeserializeObject <LightwaveRfWebsocketMessage>(data);
            }
            catch
            {
                return;
            }

            if (receivedMessage.opclass == "feature" && (receivedMessage.operation == "read" || receivedMessage.operation == "write"))
            {
                // This is a workaround for the protocol/API bug described below
                var itemId        = receivedMessage.items[0].itemId;
                var transactionId = _TRANSACTIONS.Values.Single(v => v.items.All(x => x.itemId == itemId)).transactionId;
                receivedMessage.transactionId = transactionId;
            }

            if (_TRANSACTIONS.TryGetValue(receivedMessage.transactionId, out sentMessage))
            {
                _TRANSACTIONS.Remove(receivedMessage.transactionId);
                sentMessage.requestTask.SetResult(receivedMessage);
            }
            else
            {
                if (receivedMessage.direction == "notification")
                {
                    if (receivedMessage.opclass == "group" && receivedMessage.operation == "update")
                    {
                        LightwaveRfReadRootGroupsAsync();
                        return;
                    }
                    else if (receivedMessage.operation == "event")
                    {
                        LightwaveRfEventHandler(receivedMessage);
                        return;
                    }
                }

                Console.WriteLine("Unexpected Response: " + data);
            }
        }
        private static async Task <LightwaveRfWebsocketMessage> WebSocketSendMessageAsync(LightwaveRfWebsocketMessage message)
        {
            message.requestTask = new TaskCompletionSource <LightwaveRfWebsocketMessage>();

            _TRANSACTIONS.Add(message.transactionId, message);
            _WEBSOCKET.Send(JsonConvert.SerializeObject(message));

            await message.requestTask.Task;

            return(message.requestTask.Task.Result);
        }