Esempio n. 1
0
        /// <summary>
        /// Send a json command to a list of lights
        /// </summary>
        /// <param name="command"></param>
        /// <param name="lightList">if null, send command to all lights</param>
        /// <returns></returns>
        public async Task <DeConzResults> SendCommandRawAsync(string command, IEnumerable <string> lightList = null)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            CheckInitialized();

            if (lightList == null || !lightList.Any())
            {
                //Group 0 always contains all the lights
                return(await SendGroupCommandAsync(command).ConfigureAwait(false));
            }
            else
            {
                DeConzResults results = new DeConzResults();

                await lightList.ForEachAsync(_parallelRequests, async (lightId) =>
                {
                    HttpClient client = await GetHttpClient().ConfigureAwait(false);
                    var result        = await client.PutAsync(new Uri(ApiBase + string.Format("lights/{0}/state", lightId)), new JsonContent(command)).ConfigureAwait(false);

                    string jsonResult = await result.Content.ReadAsStringAsync().ConfigureAwait(false);
                    results.AddRange(DeserializeDefaultDeConzResult(jsonResult));
                }).ConfigureAwait(false);

                return(results);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Checks if the json contains errors
        /// </summary>
        /// <param name="json"></param>
        private static DeConzResults DeserializeDefaultDeConzResult(string json)
        {
            DeConzResults result = null;

            try
            {
                result = JsonConvert.DeserializeObject <DeConzResults>(json);
            }

            catch (JsonSerializationException)
            {
                //Ignore JsonSerializationException
            }

            return(result);
        }
Esempio n. 3
0
        /// <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);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        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);
        }