Example #1
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 #2
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 #3
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 #4
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 #5
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}";
            CommResult comres;

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

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

            ProcessCommandFailure(url, comres.Status);
            return(false);
        }
Example #6
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>BridgeCommResult</returns>
        public bool ModifyObject(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);
            }

            CommResult comres;

            if (!Virtual)
            {
                comres = Comm.SendRequest(new Uri(url), WebRequestType.Put, Serializer.ModifyJsonObject(clone));
                if (comres.Status == WebExceptionStatus.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);
            }
            ProcessCommandFailure(url, comres.Status);
            return(false);
        }
Example #7
0
        /// <summary>
        /// Create a new object on the bridge async. 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 async Task <bool> CreateObjectAsyncTask(IHueObject newobject)
        {
            string     typename = newobject.GetType().Name.ToLower() + "s";
            IHueObject clone    = (IHueObject)newobject.Clone();
            string     url      = BridgeUrl + $@"/{typename}";

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

            CommResult comres;

            if (!Virtual)
            {
                comres = await Comm.SendRequestAsyncTask(new Uri(url), WebRequestType.Post, Serializer.CreateJsonObject(clone));

                if (comres.Status == WebExceptionStatus.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);
            }

            ProcessCommandFailure(url, comres.Status);
            return(false);
        }
Example #8
0
        /// <summary>
        /// Rename a specified object on the bridge async.
        /// </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>BridgeCommResult</returns>
        public async Task <bool> RenameObjectASyncTask(IHueObject hueobj)
        {
            string typename = hueobj.GetType().Name.ToLower() + "s";
            string url      = BridgeUrl + $@"/{typename}/{hueobj.Id}";

            CommResult comres;

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

                if (comres.Status == WebExceptionStatus.Success)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = $"Rename Virtual object : {hueobj.ToString()}"
                });
                return(LastCommandMessages.Success);
            }
            ProcessCommandFailure(url, comres.Status);
            return(false);
        }
Example #9
0
        private async void _refreshTimer_Tick(object sender, EventArgs e)
        {
            IHueObject        selected = SelectedObject;
            List <IHueObject> obj      = await SelectedBridge.GetAllObjectsAsync(false, true);

            List <IHueObject> diff = obj.Where(x => !_listCurrentBridgeHueObjects.Any(y => y.Id == x.Id && y.GetType() == x.GetType())).ToList();

            foreach (IHueObject ho in diff)
            {
                CurrentBridgeHueObjectsList.Remove(x => x.Id == ho.Id && x.GetType() == ho.GetType());
            }

            foreach (IHueObject o in obj)
            {
                IHueObject oldo = _listCurrentBridgeHueObjects.FirstOrDefault(x => x.Id == o.Id && x.GetType() == o.GetType());
                if (oldo == null)
                {
                    _listCurrentBridgeHueObjects.Add(o);
                }
                else
                {
                    RefreshHueObject(ref oldo, o);
                }
            }

            if (SelectedObject is null)
            {
                return;
            }
            if (obj.Any(x => x.Id == selected.Id && x.GetType() == selected.GetType()))
            {
                SelectedObject = selected;
            }
        }
Example #10
0
        public async Task RefreshCurrentObject(bool logging = false)
        {
            int index = CurrentBridgeHueObjectsList.FindIndex(x => x.Id == SelectedObject.Id && x.GetType() == SelectedObject.GetType());

            if (index == -1)
            {
                return;
            }

            IHueObject hr = await SelectedBridge.GetObjectAsync(SelectedObject.Id, SelectedObject.GetType());

            if (hr == null)
            {
                return;
            }
            IHueObject newobj = hr;

            CurrentBridgeHueObjectsList[index].Image = newobj.Image;
            List <PropertyInfo> pi = newobj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly).ToList();

            foreach (PropertyInfo p in pi)
            {
                if (CurrentBridgeHueObjectsList[index].HasProperty(p.Name))
                {
                    PropertyInfo prop = CurrentBridgeHueObjectsList[index].GetType().GetProperty(p.Name);
                    if (prop != null)
                    {
                        p.SetValue(CurrentBridgeHueObjectsList[index], prop.GetValue(newobj));
                    }
                }
            }
        }
Example #11
0
        private void RefreshHueObject(ref IHueObject obj, IHueObject newobject)
        {
            if (obj == null || newobject == null)
            {
                return;
            }
            if (obj.GetType() != newobject.GetType())
            {
                return;
            }
            if (obj.Id != newobject.Id)
            {
                return;
            }

            PropertyInfo[] pi = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            foreach (PropertyInfo p in pi)
            {
                object value = p.GetValue(newobject);
                p.SetValue(obj, value);
            }
        }