Example #1
0
        /// <summary>
        /// Force the bridge to check for an update.
        /// </summary>
        /// <returns>True or false if the operation is successful (does not return if there is an update)</returns>
        public CommandResult ForceCheckForUpdate()
        {
            CommandResult bresult = new CommandResult {
                Success = false
            };
            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/config"), WebRequestType.PUT, "{\"swupdate\": {\"checkforupdate\":true}}");

            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);
        }
Example #2
0
        /// <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);
        }
Example #3
0
        /// <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);
        }
Example #4
0
        /// <summary>
        /// Remove a user from the whitelist.
        /// </summary>
        /// <param name="username">Username to remove</param>
        /// <returns>True or false if the user has been removed.</returns>
        public CommandResult RemoveUser(string username)
        {
            CommandResult bresult = new CommandResult {
                Success = false
            };
            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/config/whitelist/" + username), WebRequestType.DELETE);

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                MessageCollection mc = new MessageCollection(Serializer.DeserializeToObject <List <Message> >(comres.data));
                lastMessages = mc;
                if (mc.FailureCount == 0)
                {
                    bresult.resultobject = true;
                    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);
        }
Example #5
0
        /// <summary>
        ///  Get all the timezones that are supported by the bridge.
        /// </summary>
        /// <returns>a list of all the timezones supported by the bridge.</returns>
        public async Task <List <string> > GetTimeZonesAsyncTask()
        {
            Version api   = Version.Parse(ApiVersion);
            Version limit = Version.Parse("1.15.0");

            if (api > limit)
            {
                Capabilities cap = await GetBridgeCapabilitiesAsyncTask();

                return(cap?.timezones.values);
            }
            else
            {
                HttpResult comres = await HueHttpClient.SendRequestAsyncTask(new Uri(BridgeUrl + "/info/timezones"), WebRequestType.Get);

                if (comres.Success)
                {
                    List <string> timezones = Serializer.DeserializeToObject <List <string> >(comres.Data);
                    if (timezones != null)
                    {
                        return(timezones);
                    }
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(null);
                }
                BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, BridgeUrl + "/info/timezones", WebExceptionStatus.NameResolutionFailure));
                return(null);
            }
        }
Example #6
0
        /// <summary>
        /// Get the specified object freom the bridge in async.
        /// </summary>
        /// <typeparam name="T">Type of object to deserialize to</typeparam>
        /// <param name="id">Id of the object to get</param>
        /// <returns>BridgeCommResult</returns>
        public async Task <T> GetObjectAsync <T>(string id) where T : IHueObject
        {
            string typename = typeof(T).Name.ToLower() + "s";

            if (typename == string.Empty || typename == "s")
            {
                return(default(T));
            }
            string     url    = BridgeUrl + $"/{typename}/{id}";
            HttpResult comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Get);

            if (comres.Success)
            {
                T data = Serializer.DeserializeToObject <T>(comres.Data);
                if (data != null)
                {
                    data.Id = id;
                    return(data);
                }

                LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                return(default(T));
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(default(T));
        }
Example #7
0
        /// <summary>
        /// Get the specified object freom the bridge.
        /// </summary>
        /// <typeparam name="T">HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</typeparam>
        /// <param name="id">Id of the object to get</param>
        /// <returns>BridgeCommResult</returns>
        public CommandResult GetObject <T>(string id) where T : HueObject
        {
            CommandResult bresult = new CommandResult()
            {
                Success = false
            };
            string ns = typeof(T).Namespace;

            if (ns != null)
            {
                string typename = string.Empty;
                if (typeof(T).BaseType == typeof(HueObject))
                {
                    typename = typeof(T).ToString().Replace(ns, "").Replace(".", "").ToLower() + "s";
                }
                else
                {
                    typename = typeof(T).BaseType.ToString().Replace(ns, "").Replace(".", "").ToLower() + "s";
                }

                CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + $"/{typename}/{id}"), WebRequestType.GET);

                switch (comres.status)
                {
                case WebExceptionStatus.Success:
                    MethodInfo method  = typeof(Serializer).GetMethod("DeserializeToObject");
                    MethodInfo generic = method.MakeGenericMethod(typeof(T));
                    bresult.resultobject = generic.Invoke(this, new object[] { comres.data });
                    bresult.Success      = bresult.resultobject != null;
                    if (bresult.resultobject == null)
                    {
                        bresult.resultobject = new MessageCollection(Serializer.DeserializeToObject <List <Message> >(comres.data));
                        lastMessages         = (MessageCollection)bresult.resultobject;
                    }
                    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;
                }
            }
            else
            {
                bresult.Success      = false;
                bresult.resultobject = "Type of object cannot be null";
            }

            return(bresult);
        }
Example #8
0
        /// <summary>
        /// Activate a scene.
        /// </summary>
        /// <param name="id">Id of the scene.</param>
        /// <returns>BridgeCommResult</returns>
        public CommandResult ActivateScene(string id)
        {
            CommandResult bresult = new CommandResult()
            {
                Success = false
            };
            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/groups/0/action"), WebRequestType.PUT, "{\"scene\":\"" + id + "\"}");

            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);
        }
Example #9
0
        /// <summary>
        /// Change the sensor's state.
        /// </summary>
        /// <param name="id">id of the sensor</param>
        /// <param name="newstate">New state of the sensor</param>
        /// <returns>BridgeCommResult</returns>
        public CommandResult ChangeSensorState(string id, SensorState newstate)
        {
            CommandResult bresult = new CommandResult();
            CommResult    comres  = Communication.SendRequest(new Uri(BridgeUrl + $@"/sensors/{id}/state"), WebRequestType.PUT, Serializer.SerializeToJson(newstate));

            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);
        }
Example #10
0
        /// <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 CommandResult SetState <T>(CommonProperties state, string id) where T : HueObject
        {
            CommandResult bresult = new CommandResult()
            {
                Success = false
            };

            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + $@"/" + (typeof(T) == typeof(Light) ? "lights" : "groups") + $@"/{id}/" + (typeof(T) == typeof(Light) ? "state" : "action")), WebRequestType.PUT, Serializer.SerializeToJson(state));

            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);
        }
Example #11
0
        /// <summary>
        /// Set the light state of a scene.
        /// </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 CommandResult SetSceneLightState(string sceneid, string lightid, CommonProperties state)
        {
            CommandResult bresult = new CommandResult()
            {
                Success = false
            };
            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + $"/scenes/{sceneid}/lightstates/{lightid}"), WebRequestType.PUT, Serializer.SerializeToJson(state));

            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);
        }
Example #12
0
        /// <summary>
        /// Process the failure of a command to the bridge.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="exception"></param>
        private void ProcessCommandFailure(string url, WebExceptionStatus exception)
        {
            switch (exception)
            {
            case WebExceptionStatus.Timeout:
                LastCommandMessages.AddMessage(new Error()
                {
                    type = -1, address = url, description = "A Timeout occured."
                });
                break;

            case WebExceptionStatus.ConnectFailure:
                LastCommandMessages.AddMessage(new Error()
                {
                    type = -1, address = url, description = "Unable to contact the bridge"
                });
                break;

            default:
                LastCommandMessages.AddMessage(new Error()
                {
                    type = -2, address = url, description = "A unknown error occured."
                });
                break;
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, exception));
        }
Example #13
0
        /// <summary>
        /// Get a list of specified objects from the bridge async.
        /// </summary>
        /// <typeparam name="T">HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</typeparam>
        /// <returns>BridgeCommResult</returns>
        public async Task <List <T> > GetListObjectsAsync <T>(bool showmyhidden = false, bool getgroupzero = false) where T : IHueObject
        {
            string     typename = typeof(T).Name.ToLower() + "s";
            string     url      = BridgeUrl + $"/{typename}";
            HttpResult comres   = await HueHttpClient.SendRequestAsyncTask(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 (!showmyhidden)
                    {
                        RemoveHiddenObjects(ref listdata, WinHueSettings.bridges.BridgeInfo[Mac].hiddenobjects);
                    }

                    if (typeof(T) == typeof(Group) && getgroupzero)
                    {
                        listdata.Add(await GetObjectAsync <T>("0"));
                    }

                    if (typeof(T) == typeof(Scene) && !WinHueSettings.settings.ShowHiddenScenes)
                    {
                        listdata.RemoveAll(x => x.GetType() == typeof(Scene) && x.name.StartsWith("HIDDEN"));
                    }

                    return(data.Select(x => { x.Value.Id = x.Key; return x.Value; }).ToList());
                }
                LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                return(null);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(null);
        }
Example #14
0
        /// <summary>
        /// Set Entertrainment Group light location
        /// </summary>
        /// <param name="id">ID of the group</param>
        /// <param name="loc">Location information</param>
        /// <returns></returns>
        public bool SetEntertrainementLightLocation(string id, Location loc)
        {
            string     url = BridgeUrl + $@"/groups/{id}";
            HttpResult comres;

            if (!Virtual)
            {
                comres = HueHttpClient.SendRequest(new Uri(url), WebRequestType.Put, Serializer.SerializeJsonObject(loc));
                if (comres.Success)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = $"Update Light location for group : {id}"
                });
                return(LastCommandMessages.Success);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Example #15
0
        /// <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);
        }
Example #16
0
        /// <summary>
        /// Modify the specified object in the bridge.
        /// </summary>
        /// <typeparam name="T">HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</typeparam>
        /// <param name="modifiedobject">The new modified object.</param>
        /// <param name="id">Id of the object.</param>
        /// <returns>BridgeHttpResult</returns>
        public async Task <bool> ModifyObjectAsyncTask(IHueObject modifiedobject)
        {
            string     typename = modifiedobject.GetType().Name.ToLower() + "s";
            IHueObject clone    = (IHueObject)modifiedobject.Clone();
            string     url      = BridgeUrl + $@"/{typename}/{modifiedobject.Id}";

            if (typename == null)
            {
                return(false);
            }

            HttpResult comres;

            if (!Virtual)
            {
                comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Put, Serializer.ModifyJsonObject(modifiedobject));

                if (comres.Success)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = $"Modified Virtual object : {modifiedobject.ToString()}"
                });
                return(LastCommandMessages.Success);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Example #17
0
        /// <summary>
        /// Change the sensor's state.
        /// </summary>
        /// <param name="id">id of the sensor</param>
        /// <param name="newstate">New state of the sensor</param>
        /// <returns>Success or error</returns>
        public async Task <bool> ChangeSensorStateAsyncTask(string id, object newstate)
        {
            string     url = BridgeUrl + $@"/sensors/{id}/state";
            HttpResult comres;

            if (!Virtual)
            {
                comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Put, Serializer.ModifyJsonObject(newstate));

                if (comres.Success)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = $"Modified Virtual sensor state : {id},{newstate.ToString()}"
                });
                return(LastCommandMessages.Success);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Example #18
0
        /// <summary>
        /// Remove the specified object from the bridge.
        /// </summary>
        /// <typeparam name="T">HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</typeparam>
        /// <param name="obj">Object to modify</param>
        /// <returns>HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</returns>
        public bool RemoveObject(string Id, HueObjectType type)
        {
            string typename = type.ToString();
            string url      = BridgeUrl + $@"/{typename}/{Id}";

            HttpResult comres;

            if (!Virtual)
            {
                comres = HueHttpClient.SendRequest(new Uri(url), WebRequestType.Delete);
                if (comres.Success)
                {
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = $"Deleted Virtual object : {Id},{type.ToString()}"
                });
                return(LastCommandMessages.Success);
            }

            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Example #19
0
        /// <summary>
        /// Remove the specified object from the bridge async.
        /// </summary>
        /// <typeparam name="T">HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</typeparam>
        /// <param name="obj">Object to modify</param>
        /// <returns>HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</returns>
        public async Task <bool> RemoveObjectAsyncTask(IHueObject obj)
        {
            string     typename = obj.GetType().Name.ToLower() + "s";
            string     url      = BridgeUrl + $@"/{typename}/{obj.Id}";
            HttpResult comres;

            if (!Virtual)
            {
                comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Delete);

                if (comres.Success)
                {
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = $"Deleted Virtual object : {obj.ToString()}"
                });
                return(LastCommandMessages.Success);
            }

            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Example #20
0
        /// <summary>
        /// Rename a specified object on the bridge.
        /// </summary>
        /// <typeparam name="T">HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</typeparam>
        /// <param name="id">ID of the specified object to rename.</param>
        /// <param name="newname">New name of the object.</param>
        /// <returns>BridgeHttpResult</returns>
        public bool RenameObject(IHueObject hueobj)
        {
            string typename = hueobj.GetType().Name.ToLower() + "s";
            string url      = BridgeUrl + $@"/{typename}/{hueobj.Id}";

            HttpResult comres;

            if (!Virtual)
            {
                comres = HueHttpClient.SendRequest(new Uri(url), WebRequestType.Put, $@"{{""name"":""{hueobj.name}""}}");
                if (comres.Success)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = $"Rename object : {hueobj.ToString()}"
                });
                return(LastCommandMessages.Success);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Example #21
0
        /// <summary>
        /// Create a new object on the bridge. Won't work for Lights.
        /// </summary>
        /// <typeparam name="T">HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</typeparam>
        /// <param name="newobject">New object to create on the bridge.</param>
        /// <returns>HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</returns>
        public bool CreateObject(IHueObject newobject)
        {
            string     typename = newobject.GetType().Name.ToLower() + "s";
            IHueObject clone    = (IHueObject)newobject.Clone();
            string     url      = BridgeUrl + $@"/{typename}";

            HttpResult comres;

            if (!Virtual)
            {
                comres = HueHttpClient.SendRequest(new Uri(url), WebRequestType.Post, Serializer.CreateJsonObject(clone));
                if (comres.Success)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = $"Created Virtual object : {newobject.ToString()}"
                });
                return(LastCommandMessages.Success);
            }

            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Example #22
0
        /// <summary>
        /// Activate a scene async.
        /// </summary>
        /// <param name="id">Id of the scene.</param>
        /// <returns>BridgeHttpResult</returns>
        public async Task <bool> ActivateSceneAsyncTask(string id)
        {
            string     url = BridgeUrl + "/groups/0/action";
            HttpResult comres;

            if (!Virtual)
            {
                comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Put, "{\"scene\":\"" + id + "\"}");

                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 : {id}"
                });
                return(LastCommandMessages.Success);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Example #23
0
        /// <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);
        }
Example #24
0
        /// <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);
        }
Example #25
0
        /// <summary>
        /// Set Entertrainment Group stream status
        /// </summary>
        /// <param name="id">ID of the group</param>
        /// <param name="status">Status of the stream</param>
        /// <returns></returns>
        public async Task <bool> SetEntertrainementGroupStreamStatus(string id, bool status)
        {
            string url = BridgeUrl + $@"/groups/{id}";

            HttpResult comres;

            if (!Virtual)
            {
                comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Put, "{\"stream\":{\"active\":" + status.ToString().ToLower() + "}}");

                if (comres.Success)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = $"Update Light location for group : {id}"
                });
                return(LastCommandMessages.Success);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Example #26
0
        /// <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);
        }
Example #27
0
        /// <summary>
        /// Allows the user to set some configuration values.
        /// </summary>
        /// <param name="settings">Settings of the bridge.</param>
        /// <return>The new settings of the bridge.</return>
        public async Task <bool> SetBridgeSettingsAsyncTask(BridgeSettings settings)
        {
            HttpResult comres = await HueHttpClient.SendRequestAsyncTask(new Uri(BridgeUrl + "/config"), WebRequestType.Put, Serializer.ModifyJsonObject(settings));

            if (comres.Success)
            {
                LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                return(LastCommandMessages.Success);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, BridgeUrl + "/config", WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Example #28
0
        public async Task <bool> TouchLink()
        {
            HttpResult comres = await HueHttpClient.SendRequestAsyncTask(new Uri(BridgeUrl + "/config"), WebRequestType.Put, "{\"touchlink\":true}");

            if (comres.Success)
            {
                LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                return(true);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, BridgeUrl + "/config", WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Example #29
0
        /// <summary>
        /// Check if there is an update for the bridge.
        /// </summary>
        /// <returns>Software Update or null.</returns>
        public CommandResult GetSwUpdate()
        {
            SwUpdate      swu     = new SwUpdate();
            CommandResult bresult = new CommandResult {
                Success = false
            };
            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/config"), WebRequestType.GET);

            switch (comres.status)
            {
            case WebExceptionStatus.Success:

                BridgeSettings brs = Serializer.DeserializeToObject <BridgeSettings>(comres.data);
                if (brs != null)
                {
                    bresult.Success      = true;
                    bresult.resultobject = brs.swupdate;
                }
                else
                {
                    List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(Communication.lastjson);
                    if (lstmsg == null)
                    {
                        goto default;
                    }
                    else
                    {
                        lastMessages         = new MessageCollection(lstmsg);
                        bresult.resultobject = lastMessages;
                    }

                    bresult.resultobject = Serializer.DeserializeToObject <List <Message> >(comres.data);
                }
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                bresult.resultobject = lastMessages;
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                bresult.resultobject = lastMessages;
                break;
            }

            return(bresult);
        }
Example #30
0
        /// <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);
        }