/// <summary> /// Create a group for a list of lights /// </summary> /// <param name="lights">List of lights in the group</param> /// <param name="name">Optional name</param> /// <returns></returns> public async Task <string> CreateGroupAsync(string name = null) { CheckInitialized(); CreateGroupRequest jsonObj = new CreateGroupRequest(); if (!string.IsNullOrEmpty(name)) { jsonObj.Name = name; } string jsonString = JsonConvert.SerializeObject(jsonObj, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); HttpClient client = await GetHttpClient().ConfigureAwait(false); //Create group with the lights we want to target var response = await client.PostAsync(new Uri(String.Format("{0}groups", ApiBase)), new JsonContent(jsonString)).ConfigureAwait(false); var jsonResult = await response.Content.ReadAsStringAsync().ConfigureAwait(false); DeConzResults groupResult = DeserializeDefaultDeConzResult(jsonResult); if (groupResult.Count > 0 && groupResult[0].Success != null && !string.IsNullOrEmpty(groupResult[0].Success.Id)) { return(groupResult[0].Success.Id.Replace("/groups/", string.Empty)); } if (groupResult.HasErrors()) { throw new Exception(groupResult.Errors.First().Error.Description); } return(null); }
public async Task <string> CreateRule(string name, IEnumerable <RuleCondition> conditions, IEnumerable <InternalBridgeCommand> actions) { CheckInitialized(); if (conditions == null || !conditions.Any()) { throw new ArgumentNullException(nameof(conditions)); } if (actions == null || !actions.Any()) { throw new ArgumentNullException(nameof(actions)); } if (conditions.Count() > 8) { throw new ArgumentException("Max 8 conditions allowed", nameof(conditions)); } if (actions.Count() > 8) { throw new ArgumentException("Max 8 actions allowed", nameof(actions)); } JObject jsonObj = new JObject(); if (conditions != null && conditions.Any()) { jsonObj.Add("conditions", JToken.FromObject(conditions, new JsonSerializer { NullValueHandling = NullValueHandling.Ignore })); } if (actions != null && actions.Any()) { jsonObj.Add("actions", JToken.FromObject(actions, new JsonSerializer { NullValueHandling = NullValueHandling.Ignore })); } if (!string.IsNullOrEmpty(name)) { jsonObj.Add("name", name); } string jsonString = JsonConvert.SerializeObject(jsonObj, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); HttpClient client = await GetHttpClient().ConfigureAwait(false); //Create group with the lights we want to target var response = await client.PostAsync(new Uri(String.Format("{0}rules", ApiBase)), new JsonContent(jsonString)).ConfigureAwait(false); var jsonResult = await response.Content.ReadAsStringAsync().ConfigureAwait(false); DeConzResults rulesResult = DeserializeDefaultDeConzResult(jsonResult); if (rulesResult.Count > 0 && rulesResult[0].Success != null && !string.IsNullOrEmpty(rulesResult[0].Success.Id)) { return(rulesResult[0].Success.Id); } if (rulesResult.HasErrors()) { throw new Exception(rulesResult.Errors.First().Error.Description); } return(null); }
public async Task <string> CreateSceneAsync(Scene scene) { CheckInitialized(); if (scene == null) { throw new ArgumentNullException(nameof(scene)); } if (scene.Lights == null) { throw new ArgumentNullException(nameof(scene.Lights)); } if (scene.Name == null) { throw new ArgumentNullException(nameof(scene.Name)); } //It defaults to false, but fails when omitted //https://github.com/Q42/Q42.HueApi/issues/56 if (!scene.Recycle.HasValue) { scene.Recycle = false; } //Filter non updatable properties //Set these fields to null var sceneJson = JObject.FromObject(scene, new JsonSerializer() { NullValueHandling = NullValueHandling.Ignore }); sceneJson.Remove("Id"); sceneJson.Remove("version"); sceneJson.Remove("lastupdated"); sceneJson.Remove("locked"); sceneJson.Remove("owner"); sceneJson.Remove("lightstates"); string jsonString = JsonConvert.SerializeObject(sceneJson, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); HttpClient client = await GetHttpClient().ConfigureAwait(false); var response = await client.PostAsync(new Uri(String.Format("{0}scenes", ApiBase)), new JsonContent(jsonString)).ConfigureAwait(false); var jsonResult = await response.Content.ReadAsStringAsync().ConfigureAwait(false); DeConzResults sceneResult = DeserializeDefaultDeConzResult(jsonResult); if (sceneResult.Count > 0 && sceneResult[0].Success != null && !string.IsNullOrEmpty(sceneResult[0].Success.Id)) { return(sceneResult[0].Success.Id); } if (sceneResult.HasErrors()) { throw new Exception(sceneResult.Errors.First().Error.Description); } return(null); }