/// <summary> /// Set the state of a light or group on the bridge. /// </summary> /// <typeparam name="T">Any class the derives from CommonProperties</typeparam> /// <param name="state">New state of the object.</param> /// <param name="id">ID of the specified object.</param> /// <returns>BridgeCommResult</returns> public async Task <bool> SetStateAsyncTask(IBaseProperties state, string id) { string typename = null; string type = null; if (state is State) { typename = "lights"; type = "state"; } if (state is Action) { typename = "groups"; type = "action"; } if (typename == null) { return(false); } string url = BridgeUrl + $@"/{typename}/{id}/{type}"; CommResult comres; if (!Virtual) { comres = await Comm.SendRequestAsyncTask(new Uri(url), WebRequestType.Put, Serializer.SerializeToJson(state)); if (comres.Status == WebExceptionStatus.Success) { LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data)); return(LastCommandMessages.Success); } } else { LastCommandMessages.AddMessage(new Success() { Address = url, value = state.ToString() }); return(LastCommandMessages.Success); } ProcessCommandFailure(url, comres.Status); return(false); }
/// <summary> /// Set the state of a light or group on the bridge. /// </summary> /// <typeparam name="T">Any class the derives from CommonProperties</typeparam> /// <param name="state">New state of the object.</param> /// <param name="id">ID of the specified object.</param> /// <returns>BridgeHttpResult</returns> public async Task <bool> SetStateAsyncTask(IBaseProperties state, string id) { string typename = null; string type = null; if (state is State) { typename = "lights"; type = "state"; } if (state is Action) { typename = "groups"; type = "action"; } if (typename == null) { return(false); } string url = BridgeUrl + $@"/{typename}/{id}/{type}"; HttpResult comres; if (!Virtual) { comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Put, Serializer.ModifyJsonObject(state)); if (comres.Success) { LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data)); return(LastCommandMessages.Success); } } else { LastCommandMessages.AddMessage(new Success() { Address = url, value = state.ToString() }); return(LastCommandMessages.Success); } BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure)); return(false); }
/// <summary> /// Set the light state of a scene async. /// </summary> /// <param name="sceneid">Id of the scene.</param> /// <param name="lightid">Id of the light.</param> /// <param name="state">State of the light.</param> /// <returns>BrideHttpResult</returns> public async Task <bool> SetSceneLightStateAsyncTask(string sceneid, string lightid, IBaseProperties state) { string url = BridgeUrl + $"/scenes/{sceneid}/lightstates/{lightid}"; HttpResult comres; if (!Virtual) { comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Put, Serializer.ModifyJsonObject(state)); if (comres.Success) { LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data)); return(LastCommandMessages.Success); } } else { LastCommandMessages.AddMessage(new Success() { Address = url, value = $"Set Virtual scene state : {sceneid}, {lightid}, {state.ToString()}" }); return(LastCommandMessages.Success); } BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure)); return(false); }
/// <summary> /// Set the light state of a scene async. /// </summary> /// <param name="sceneid">Id of the scene.</param> /// <param name="lightid">Id of the light.</param> /// <param name="state">State of the light.</param> /// <returns>BrideCommResult</returns> public async Task <bool> SetSceneLightStateAsyncTask(string sceneid, string lightid, IBaseProperties state) { string url = BridgeUrl + $"/scenes/{sceneid}/lightstates/{lightid}"; CommResult comres; if (!Virtual) { comres = await Comm.SendRequestAsyncTask(new Uri(url), WebRequestType.Put, Serializer.SerializeToJson(state)); if (comres.Status == WebExceptionStatus.Success) { LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data)); return(LastCommandMessages.Success); } } else { LastCommandMessages.AddMessage(new Success() { Address = url, value = $"Set Virtual scene state : {sceneid}, {lightid}, {state.ToString()}" }); return(LastCommandMessages.Success); } ProcessCommandFailure(url, comres.Status); return(false); }