public override string ToString() { return(Serializer.SerializeToJson(this)); }
/// <summary> /// Create a new group. /// </summary> /// <param name="newgroup">Group to create.</param> /// <returns>Return the ID of the group created.</returns> public string CreateGroup(Group newgroup) { string Id = ""; CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/groups"), WebRequestType.POST, Serializer.SerializeToJson <Group>(newgroup)); 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) { Id = (((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(Id); }
/// <summary> /// Change an existing Group. /// </summary> /// <param name="newgroup">Old group with new properties.</param> /// <returns>A collection of messages.</returns> public MessageCollection ChangeGroup(Group newgroup) { CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/groups/" + newgroup.Id), WebRequestType.PUT, Serializer.SerializeToJson <Group>(newgroup)); 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); }
/// <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> /// Set the desired light name. /// </summary> /// <param name="ID">ID of the light.</param> /// <param name="Name">The name of the light.</param> /// <returns>A list of MessageCollection from the bridge.</returns> public MessageCollection ChangeLightName(string ID, string Name) { CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/lights/" + ID), WebRequestType.PUT, Serializer.SerializeToJson <Light>(new Light() { name = Name })); 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); break; case WebExceptionStatus.Timeout: lastMessages = new MessageCollection { _bridgeNotResponding }; BridgeNotResponding?.Invoke(this, _e); break; default: lastMessages = new MessageCollection { new UnkownError(comres) }; break; } return(lastMessages); }
/// <summary> /// Store current light state in the selected scene. /// </summary> /// <param name="id">ID of the scene to change the name.</param> /// <param name="newlightsList">New List of lights</param> /// <returns>the id of the modified scene or null</returns> public string SetScene(string id) { string modifiedid = ""; CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/scenes/" + id), WebRequestType.PUT, Serializer.SerializeToJson <Scene>(new Scene() { storelightstate = true })); 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 == 2) { modifiedid = id; } } lastMessages = new MessageCollection(); break; case WebExceptionStatus.Timeout: lastMessages = new MessageCollection { _bridgeNotResponding }; BridgeNotResponding?.Invoke(this, _e); break; default: lastMessages = new MessageCollection { new UnkownError(comres) }; break; } return(modifiedid); }
/// <summary> /// Set the parameters of a light in a scene. /// </summary> /// <param name="sceneName">The name of the scene.</param> /// <param name="lightId">The light ID.</param> /// <param name="newState">The desired state of the light.</param> /// <returns>A list of MessageCollection from the bridge.</returns> public MessageCollection SetSceneLightState(string sceneName, string lightId, State newState) { CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/scenes/" + sceneName + "/lightstates/" + lightId), WebRequestType.PUT, Serializer.SerializeToJson(newState)); 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); break; case WebExceptionStatus.Timeout: lastMessages = new MessageCollection { _bridgeNotResponding }; BridgeNotResponding?.Invoke(this, _e); break; default: lastMessages = new MessageCollection { new UnkownError(comres) }; break; } return(lastMessages); }
/// <summary> /// Modify an already existing rule. /// </summary> /// <param name="id">ID of the rule to be modified.</param> /// <param name="modifiedRule">The desired modification to the rule.</param> /// <returns>The id of the modified rule if successful</returns> public string ModifyRule(string id, Rule modifiedRule) { string result = ""; CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/rules/" + id.ToString()), WebRequestType.PUT, Serializer.SerializeToJson <Rule>(modifiedRule)); 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 > 0) { result = ((Success)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> /// Change the name of a Rule. /// </summary> /// <param name="id">ID of the rule to change the name.</param> /// <param name="newName">New name of the rule.</param> /// <returns>A message collection.</returns> public MessageCollection ChangeRuleName(string id, string newName) { CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/rules/" + id.ToString()), WebRequestType.PUT, Serializer.SerializeToJson <Rule>(new Rule() { 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); }
/// <summary> /// Set the flag of a sensor to the desired value. /// </summary> /// <param name="id">Id of the sensor.</param> /// <param name="config">Config of the sensor</param> /// <returns></returns> public MessageCollection SetSensorFlag(string id, SensorState config) { CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/sensors/" + id.ToString() + "/state"), WebRequestType.PUT, Serializer.SerializeToJson <SensorState>(config)); 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); }
/// <summary> /// Contact the bridge to register your application and generate an APIKEY. /// </summary> /// <param name="ApplicationName">Name of your application.</param> /// <returns>True or false the Registration has been succesfull. This will automaically populate the ApiKey with the one generated.</returns> public bool RegisterApplication(string ApplicationName) { CommResult comres = Communication.SendRequest(new Uri("http://" + _ipAddress + "/api"), WebRequestType.POST, Serializer.SerializeToJson <User>(new User() { devicetype = ApplicationName })); 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) { ApiKey = ((Success)lastMessages[0]).Value; } } break; case WebExceptionStatus.Timeout: lastMessages = new MessageCollection { _bridgeNotResponding }; BridgeNotResponding?.Invoke(this, _e); break; default: lastMessages = new MessageCollection { new UnkownError(comres) }; break; } return(ApiKey == string.Empty); }
/// <summary> /// Rename a sensor. /// </summary> /// <param name="id">ID of the sensor to rename.</param> /// <param name="sensor">The new value of the sensor.</param> /// <returns>True or False the sensor has been renamed.</returns> public bool UpdateSensor(string id, Sensor sensor) { bool result = false; sensor.state.lastupdated = null; sensor.name = null; sensor.modelid = null; sensor.type = null; sensor.manufacturername = null; sensor.swversion = null; sensor.uniqueid = null; if (sensor.config != null) { sensor.config.on = null; sensor.config.reachable = null; } CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/sensors/" + id.ToString()), WebRequestType.PUT, Serializer.SerializeToJson <Sensor>(sensor)); 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.FailureCount == 0) { result = true; } } break; case WebExceptionStatus.Timeout: lastMessages = new MessageCollection { _bridgeNotResponding }; BridgeNotResponding?.Invoke(this, _e); break; default: lastMessages = new MessageCollection { new UnkownError(comres) }; break; } return(result); }
/// <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> /// Update the bridge firmware. /// </summary> /// <returns>True or False command sent succesfully.</returns> public bool DoSwUpdate() { bool result = 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: List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data); if (lstmsg == null) { goto default; } lastMessages = new MessageCollection(lstmsg); if (lastMessages.SuccessCount == 1) { result = true; } break; case WebExceptionStatus.Timeout: lastMessages = new MessageCollection { _bridgeNotResponding }; BridgeNotResponding?.Invoke(this, _e); break; default: lastMessages = new MessageCollection { new UnkownError(comres) }; break; } return(result); }
/// <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); }
/// <summary> /// Update the specified Schedule /// </summary> /// <param name="id">Id of the scedule to update</param> /// <param name="schedule">new schedule settings</param> /// <returns>true or false of the update was applied</returns> public bool UpdateSchedule(string id, Schedule schedule) { bool result = false; CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/schedules/" + id), WebRequestType.PUT, Serializer.SerializeToJson <Schedule>(schedule)); 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 = true; } } break; case WebExceptionStatus.Timeout: lastMessages = new MessageCollection { _bridgeNotResponding }; BridgeNotResponding?.Invoke(this, _e); break; default: lastMessages = new MessageCollection { new UnkownError(comres) }; break; } return(result); }