예제 #1
0
        public static StateResult Parse(JObject evtStateJson)
        {
            var state = new StateResult();

            state.EntityId    = evtStateJson.GetValue <string>("entity_id");
            state.State       = evtStateJson.GetValue <string>("state");
            state.LastChanged = evtStateJson.GetValue <DateTime>("last_changed");
            state.LastUpdated = evtStateJson.GetValue <DateTime>("last_updated");
            state.Attributes  = evtStateJson.GetValue <JObject>("attributes");
            state.Context     = evtStateJson.GetValue <JObject>("context");
            return(state);
        }
예제 #2
0
        public static EventData Parse(JObject dataJson)
        {
            var data = new EventData();

            data.EntityId = dataJson.GetValue <string>("entity_id");
            JObject oldState = dataJson.GetValue <JObject>("old_state");
            JObject newState = dataJson.GetValue <JObject>("new_state");

            if (oldState != null)
            {
                data.OldState = StateResult.Parse(oldState);
            }
            if (newState != null)
            {
                data.NewState = StateResult.Parse(newState);
            }

            return(data);
        }
예제 #3
0
        public static ResponseMessage Parse(WsAPI context, JObject jsonObj)
        {
            string tp = jsonObj.GetValue <string>("type");

            if (string.IsNullOrEmpty(tp))
            {
                throw new ArgumentException($"invalid response format");
            }

            MessageType msgTp = (MessageType)Enum.Parse(typeof(MessageType), tp, true);

            ResponseMessage responseMsg;

            if (msgTp == MessageType.Auth_invalid ||
                msgTp == MessageType.Auth_ok ||
                msgTp == MessageType.Auth_required)
            {
                responseMsg           = new AuthResponseMessage(msgTp);
                responseMsg.IsSuccess = msgTp == MessageType.Auth_invalid;
            }
            else
            {
                int?id = jsonObj.GetValue <int?>("id");

                responseMsg = new ResponseMessage(id, msgTp);

                responseMsg.IsSuccess = jsonObj.GetValue <bool?>("success");

                JObject errObj = jsonObj.GetValue <JObject>("error");

                if (errObj != null)
                {
                    responseMsg.Error = new Error(
                        errObj.GetValue <int>("error"),
                        errObj.GetValue <string>("message"));
                }
            }

            responseMsg.RequestMessage = context?.FindRequestMessageOrDefault(responseMsg.Id.GetValueOrDefault());

            if (responseMsg.RequestMessage?.Type == RequestMessage.MessageType.Get_states)
            {
                responseMsg.States = jsonObj
                                     .GetValue <JObject[]>("result")
                                     ?.Select(state => StateResult.Parse(state))
                                     .ToArray();

                responseMsg.Result = responseMsg.States;
            }
            else if (responseMsg.RequestMessage?.Type == RequestMessage.MessageType.Subscribe_events)
            {
                responseMsg.Event  = EventResult.Parse(jsonObj.GetValue <JObject>("event"));
                responseMsg.Result = responseMsg.Event;
            }
            else
            {
                responseMsg.Result = jsonObj.GetValue <object>("result");
            }

            return(responseMsg);
        }