示例#1
0
        /// <summary>
        /// Set the state object of a light.
        /// </summary>
        /// <param name="ID">ID of the light to change state.</param>
        /// <param name="newState">The new state of the light to change.</param>
        /// <returns>A list of MessageCollection from the bridge.</returns>
        public HueResult SetLightState(string ID, State newState)
        {
            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/lights/" + ID + "/state"), WebRequestType.PUT, Serializer.SerializeToJson <State>(newState));
            HueResult  result = new HueResult();

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data);
                lastMessages = lstmsg == null ? new MessageCollection {
                    new UnkownError(comres)
                } : new MessageCollection(lstmsg);
                result.Success = true;
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }
            result.messages = lastMessages;
            return(result);
        }
示例#2
0
        /// <summary>
        /// Delete the selected light from the bridge.
        /// </summary>
        /// <param name="ID">ID of the light to delete.</param>
        /// <returns>True for success or false for error.</returns>
        public HueResult DeleteLight(string ID)
        {
            HueResult result = new HueResult();

            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/lights/" + ID), WebRequestType.DELETE);

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data);
                if (lstmsg == null)
                {
                    goto default;
                }
                else
                {
                    lastMessages = new MessageCollection(lstmsg);
                    if (lastMessages.SuccessCount == 1)
                    {
                        result.Success = lastMessages[0].GetType() == typeof(DeletionSuccess) ? true : false;
                    }
                }
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };

                break;
            }
            result.messages = lastMessages;
            return(result);
        }