/// <summary> /// Get a list of all schedules. /// </summary> /// <returns>Returns a list of all schedules or NULL when error.</returns> public Dictionary <string, Schedule> GetScheduleList() { Dictionary <string, Schedule> result = new Dictionary <string, Schedule>(); CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/schedules"), WebRequestType.GET); switch (comres.status) { case WebExceptionStatus.Success: result = Serializer.DeserializeToObject <Dictionary <string, Schedule> >(comres.data); if (result != null) { return(result); } result = new Dictionary <string, Schedule>(); List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(Communication.lastjson); lastMessages = lstmsg != null ? new MessageCollection(lstmsg) : new MessageCollection { new UnkownError(comres) }; break; case WebExceptionStatus.Timeout: lastMessages = new MessageCollection { _bridgeNotResponding }; BridgeNotResponding?.Invoke(this, _e); break; default: lastMessages = new MessageCollection { new UnkownError(comres) }; break; } return(result); }
/// <summary> /// Gets a list of lights that were discovered the last time a search for new lights was performed. The list of new lights is always deleted when a new search is started. /// </summary> /// <returns>Returns a list of new lights detected otherwise will return null</returns> public SearchResult GetNewLights() { SearchResult newLights = new SearchResult(); CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/lights/new"), WebRequestType.GET); switch (comres.status) { case WebExceptionStatus.Success: newLights = Serializer.DeserializeSearchResult(comres.data); if (newLights != null) { return(newLights); } newLights = new SearchResult(); List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(Communication.lastjson); lastMessages = lstmsg == null ? new MessageCollection { new UnkownError(comres) } : new MessageCollection(lstmsg); break; case WebExceptionStatus.Timeout: lastMessages = new MessageCollection { _bridgeNotResponding }; BridgeNotResponding?.Invoke(this, _e); break; default: lastMessages = new MessageCollection { new UnkownError(comres) }; break; } return(newLights); }
/// <summary> /// Get a list of specified objects from the bridge. /// </summary> /// <typeparam name="T">HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</typeparam> /// <returns>BridgeCommResult</returns> public List <T> GetListObjects <T>(bool showhidden = false) where T : IHueObject { string typename = typeof(T).Name.ToLower() + "s"; string url = BridgeUrl + $"/{typename}"; HttpResult comres = HueHttpClient.SendRequest(new Uri(url), WebRequestType.Get); if (comres.Success) { Dictionary <string, T> data = Serializer.DeserializeToObject <Dictionary <string, T> >(comres.Data); if (data != null) { List <T> listdata = data.Select(x => { x.Value.Id = x.Key; return(x.Value); }).ToList(); if (!showhidden) { RemoveHiddenObjects(ref listdata, WinHueSettings.bridges.BridgeInfo[Mac].hiddenobjects); } return(listdata); } LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data)); return(null); } BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure)); return(null); }
/// <summary> /// Create a new scene in the bridge. /// </summary> /// <param name="newScene">New Scene to create.</param> /// <returns>The ID of the created scene.</returns> public string CreateScene(Scene newScene) { string sceneId = ""; CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/scenes/"), WebRequestType.POST, Serializer.SerializeToJson <Scene>(newScene)); 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); sceneId = lastMessages.FailureCount >= 1 ? "" : ((CreationSuccess)lastMessages[0]).id; } break; case WebExceptionStatus.Timeout: lastMessages = new MessageCollection { _bridgeNotResponding }; BridgeNotResponding?.Invoke(this, _e); break; default: lastMessages = new MessageCollection { new UnkownError(comres) }; break; } return(sceneId); }
/// <summary> /// Return the desired rule from the bridge. /// </summary> /// <param name="id">ID of the desired rule</param> /// <returns>A rule from the bridge.</returns> public Rule GetRule(string id) { Rule rule = new Rule(); CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/rules/" + id.ToString()), WebRequestType.GET); switch (comres.status) { case WebExceptionStatus.Success: rule = Serializer.DeserializeToObject <Rule>(comres.data); if (rule != null) { return(rule); } rule = new Rule(); List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(Communication.lastjson); lastMessages = lstmsg == null ? new MessageCollection { new UnkownError(comres) } : new MessageCollection(lstmsg); break; case WebExceptionStatus.Timeout: lastMessages = new MessageCollection { _bridgeNotResponding }; BridgeNotResponding?.Invoke(this, _e); break; default: lastMessages = new MessageCollection { new UnkownError(comres) }; break; } return(rule); }
public async Task <bool> FindNewLightsAsync(string serialslist = null) { LightSearchSerial lsl = new LightSearchSerial(); if (serialslist != null) { string[] serials = serialslist.Split(','); foreach (string s in serials) { lsl.deviceid.Add(s); } } HttpResult comres = await HueHttpClient.SendRequestAsyncTask(new Uri(BridgeUrl + $"/lights"), WebRequestType.Post, lsl.deviceid.Count == 0? "" : Serializer.SerializeJsonObject(lsl)); if (comres.Success) { LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data)); return(true); } BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, BridgeUrl + $"/lights", WebExceptionStatus.NameResolutionFailure)); return(false); }
/// <summary> /// Tell the bridge to store the current state of the lights of the scene. /// </summary> /// <param name="id">ID of the scene.</param> /// <returns>BrideCommResult</returns> public CommandResult StoreCurrentLightState(string id) { CommandResult bresult = new CommandResult() { Success = false }; CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + $"/scenes/{id}"), WebRequestType.PUT, Serializer.SerializeToJson(new Scene() { storelightstate = true })); switch (comres.status) { case WebExceptionStatus.Success: lastMessages = new MessageCollection(Serializer.DeserializeToObject <List <Message> >(comres.data)); bresult.Success = lastMessages.FailureCount == 0; bresult.resultobject = lastMessages; break; case WebExceptionStatus.Timeout: lastMessages = new MessageCollection { _bridgeNotResponding }; BridgeNotResponding?.Invoke(this, _e); bresult.resultobject = comres.data; break; default: lastMessages = new MessageCollection { new UnkownError(comres) }; bresult.resultobject = comres.data; break; } return(bresult); }
/// <summary> /// Create a new sensor on the bridge. /// </summary> /// <param name="newSensor"></param> /// <returns>The created sensor id or null.</returns> public string CreateSensor(Sensor newSensor) { string result = ""; CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/sensors"), WebRequestType.POST, Serializer.SerializeToJson <Sensor>(newSensor)); 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); result = lastMessages.SuccessCount == 1 ? ((CreationSuccess)_lastmessages[0]).id : ""; } break; case WebExceptionStatus.Timeout: lastMessages = new MessageCollection { _bridgeNotResponding }; BridgeNotResponding?.Invoke(this, _e); break; default: lastMessages = new MessageCollection { new UnkownError(comres) }; break; } return(result); }
/// <summary> /// Creates a new user / Register a new user. The link button on the bridge must be pressed and this command executed within 30 seconds. /// </summary> /// <returns>Contains a list with a single item that details whether the user was added successfully along with the username parameter. Note: If the requested username already exists then the response will report a success.</returns> /// <param name="deviceType">Description of the type of device associated with this username. This field must contain the name of your app.</param> /// <return>The new API Key.</return> public string CreateUser(string deviceType, bool?generatesteamkey = null) { string url = "http://" + _ipAddress + "/api"; User newuser = new User() { devicetype = deviceType, generateclientkey = generatesteamkey }; Version current = new Version(ApiVersion); if (current < Version.Parse("1.22")) { newuser.generateclientkey = null; } HttpResult comres = HueHttpClient.SendRequest(new Uri(url), WebRequestType.Post, Serializer.CreateJsonObject(newuser)); if (comres.Success) { LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data)); return(LastCommandMessages.Success ? LastCommandMessages.LastSuccess.value : null); } BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure)); return(null); }
/// <summary> /// Update the bridge firmware. /// </summary> /// <returns>True or False command sent succesfully.</returns> public CommandResult DoSwUpdate() { CommandResult bresult = new CommandResult { Success = false }; CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/config"), WebRequestType.PUT, "{\"swupdate\":" + Serializer.SerializeToJson <SwUpdate>(new SwUpdate() { updatestate = 3 }) + "}"); switch (comres.status) { case WebExceptionStatus.Success: lastMessages = new MessageCollection(Serializer.DeserializeToObject <List <Message> >(comres.data)); if (lastMessages.FailureCount == 0) { bresult.Success = true; } break; case WebExceptionStatus.Timeout: lastMessages = new MessageCollection { _bridgeNotResponding }; BridgeNotResponding?.Invoke(this, _e); break; default: lastMessages = new MessageCollection { new UnkownError(comres) }; break; } bresult.resultobject = lastMessages; return(bresult); }
/// <summary> /// Check if there is an update available on the bridge async. (Does not force the bridge to check for an update) /// </summary> /// <returns>Software Update or null.</returns> public async Task <bool> CheckUpdateAvailableAsyncTask() { HttpResult comres = await HueHttpClient.SendRequestAsyncTask(new Uri(BridgeUrl + "/config"), WebRequestType.Get); if (comres.Success) { BridgeSettings brs = Serializer.DeserializeToObject <BridgeSettings>(comres.Data); if (brs != null) { if (brs.swupdate.updatestate == 2) { UpdateAvailable = true; return(true); } return(false); } else { LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(HueHttpClient.LastJson)); } } BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, BridgeUrl + "/config", WebExceptionStatus.NameResolutionFailure)); return(false); }
/// <summary> /// Change the name of Schedule. /// </summary> /// <param name="id">ID of the schedule to rename.</param> /// <param name="newName">New name of the schedule.</param> /// <returns>A collection of messages.</returns> public MessageCollection ChangeScheduleName(string id, string newName) { CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/schedules/" + id.ToString()), WebRequestType.PUT, Serializer.SerializeToJson <Schedule>(new Schedule() { name = newName })); 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); } break; case WebExceptionStatus.Timeout: lastMessages = new MessageCollection { _bridgeNotResponding }; BridgeNotResponding?.Invoke(this, _e); break; default: lastMessages = new MessageCollection { new UnkownError(comres) }; break; } return(lastMessages); }