Esempio n. 1
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 bool SetSceneLightState(string sceneid, string lightid, IBaseProperties state)
        {
            string     url = BridgeUrl + $"/scenes/{sceneid}/lightstates/{lightid}";
            CommResult comres;

            if (!Virtual)
            {
                comres = Comm.SendRequest(new Uri(url), WebRequestType.Put, Serializer.SerializeToJson(state));

                if (comres.Status == WebExceptionStatus.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);
            }
            ProcessCommandFailure(url, comres.Status);
            return(false);
        }
Esempio n. 2
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>BrideHttpResult</returns>
        public bool SetSceneLightState(string sceneid, string lightid, IBaseProperties state)
        {
            string     url = BridgeUrl + $"/scenes/{sceneid}/lightstates/{lightid}";
            HttpResult comres;

            if (!Virtual)
            {
                comres = HueHttpClient.SendRequest(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);
        }
Esempio n. 3
0
        private void AppendDescription(IBaseProperties properties)
        {
            var desc = properties[KnownPropertiesNames.Description];

            if (desc.Length > 0)
            {
                this._sb.AppendLine().Append(' ', IndentCell * 10).Append(desc);
            }
        }
Esempio n. 4
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>BridgeHttpResult</returns>
        public async Task <bool> SetStateAsyncTask(IBaseProperties state, string id)
        {
            string typename = null;
            string type     = null;

            if (state is State)
            {
                typename = "lights";
                type     = "state";
            }
            if (state is Action)
            {
                typename = "groups";
                type     = "action";
            }
            if (typename == null)
            {
                return(false);
            }
            string url = BridgeUrl + $@"/{typename}/{id}/{type}";

            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 = state.ToString()
                });
                return(LastCommandMessages.Success);
            }


            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Esempio n. 5
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 async Task <bool> SetStateAsyncTask(IBaseProperties state, string id)
        {
            string typename = null;
            string type     = null;

            if (state is State)
            {
                typename = "lights";
                type     = "state";
            }
            if (state is Action)
            {
                typename = "groups";
                type     = "action";
            }
            if (typename == null)
            {
                return(false);
            }
            string url = BridgeUrl + $@"/{typename}/{id}/{type}";

            CommResult comres;

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

                if (comres.Status == WebExceptionStatus.Success)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = state.ToString()
                });
                return(LastCommandMessages.Success);
            }


            ProcessCommandFailure(url, comres.Status);
            return(false);
        }
Esempio n. 6
0
        /// <summary>
        /// Toggle the state of an object on and off (Light or group)
        /// </summary>
        /// <param name="bridge">Bridge to get the information from.</param>
        /// <param name="obj">Object to toggle.</param>
        /// <param name="tt">Transition Time (Optional)</param>
        /// <param name="dimvalue">Value for the dim (Optional)</param>
        /// <param name="state">New state at toggle (Optional)</param>
        /// <returns>The new image of the object.</returns>
        public async Task <ImageSource> ToggleObjectOnOffStateAsyncTask(IHueObject obj, ushort?tt = null, byte?dimvalue = null, IBaseProperties state = null)
        {
            ImageSource hr = null;

            if (obj is Light)
            {
                Light bresult = await GetObjectAsync <Light>(obj.Id);

                if (bresult == null)
                {
                    return(null);
                }
                Light currentState = bresult;

                if (currentState.state.reachable == false && currentState.manufacturername != "OSRAM")
                {
                    hr = GetImageForLight(LightImageState.Unr, currentState.modelid, currentState.config.archetype);
                }
                else
                {
                    if (currentState.state.@on == true)
                    {
                        log.Debug("Toggling light state : OFF");
                        bool bsetlightstate = await SetStateAsyncTask(new State { @on = false, transitiontime = tt }, obj.Id);

                        if (bsetlightstate)
                        {
                            hr = GetImageForLight(LightImageState.Off, currentState.modelid, currentState.config.archetype);
                        }
                    }
                    else
                    {
                        log.Debug("Toggling light state : ON");

                        State newstate;

                        if (WinHueSettings.settings.SlidersBehavior == 0)
                        {
                            newstate = new State()
                            {
                                on             = true,
                                transitiontime = tt
                            };;
                            if (!WinHueSettings.settings.UseLastBriState && bresult.state.bri != null)
                            {
                                newstate.bri = dimvalue ?? WinHueSettings.settings.DefaultBriLight;
                            }
                        }
                        else
                        {
                            newstate                = state as State ?? new State();
                            newstate.on             = true;
                            newstate.transitiontime = tt;
                        }

                        bool bsetlightstate = await SetStateAsyncTask(newstate, obj.Id);

                        if (bsetlightstate)
                        {
                            hr = GetImageForLight(LightImageState.On, currentState.modelid, currentState.config.archetype);
                        }
                    }
                }
            }
            else
            {
                Group bresult = await GetObjectAsync <Group>(obj.Id);

                if (bresult == null)
                {
                    return(null);
                }
                Group currentstate = bresult;
                if (currentstate.action.@on == true)
                {
                    log.Debug("Toggling group state : ON");
                    bool bsetgroupstate = await SetStateAsyncTask(new Action { @on = false, transitiontime = tt }, obj.Id);

                    if (bsetgroupstate)
                    {
                        hr = GDIManager.CreateImageSourceFromImage(Properties.Resources.HueGroupOff_Large);
                    }
                }
                else
                {
                    log.Debug("Toggling group state : OFF");

                    Action newaction;

                    if (WinHueSettings.settings.SlidersBehavior == 0)
                    {
                        newaction = new Action()
                        {
                            on             = true,
                            transitiontime = tt
                        };

                        if (!WinHueSettings.settings.UseLastBriState)
                        {
                            newaction.bri = dimvalue ?? WinHueSettings.settings.DefaultBriGroup;
                        }
                    }
                    else
                    {
                        newaction                = state as Action ?? new Action();
                        newaction.on             = true;
                        newaction.transitiontime = tt;
                    }

                    bool bsetgroupstate = await SetStateAsyncTask(newaction, obj.Id);

                    if (bsetgroupstate)
                    {
                        hr = GDIManager.CreateImageSourceFromImage(Properties.Resources.HueGroupOn_Large);
                    }
                }
            }

            return(hr);
        }
Esempio n. 7
0
        /// <summary>
        /// Toggle the state of an object on and off (Light or group)
        /// </summary>
        /// <param name="bridge">Bridge to get the information from.</param>
        /// <param name="obj">Object to toggle.</param>
        /// <param name="tt">Transition Time (Optional)</param>
        /// <param name="dimvalue">Value for the dim (Optional)</param>
        /// <param name="state">New state at toggle (Optional)</param>
        /// <returns>The new image of the object.</returns>
        public async Task <bool> ToggleObjectOnOffStateAsyncTask(IHueObject obj, ushort?tt = null, byte?dimvalue = null, IBaseProperties state = null)
        {
            bool result = false;

            if (obj is Light)
            {
                Light bresult = await GetObjectAsync <Light>(obj.Id);

                if (bresult == null)
                {
                    return(false);
                }
                Light currentState = bresult;

                if (currentState.state.@on == true)
                {
                    log.Debug("Toggling light state : OFF");
                    result = await SetStateAsyncTask(new State { @on = false, transitiontime = tt }, obj.Id);
                }
                else
                {
                    log.Debug("Toggling light state : ON");

                    State newstate;

                    if (WinHueSettings.settings.SlidersBehavior == 0)
                    {
                        newstate = new State()
                        {
                            on             = true,
                            transitiontime = tt
                        };;
                        if (!WinHueSettings.settings.UseLastBriState && bresult.state.bri != null)
                        {
                            newstate.bri = dimvalue ?? WinHueSettings.settings.DefaultBriLight;
                        }
                    }
                    else
                    {
                        newstate                = state as State ?? new State();
                        newstate.on             = true;
                        newstate.transitiontime = tt;
                    }

                    result = await SetStateAsyncTask(newstate, obj.Id);
                }
            }
            else
            {
                Group bresult = await GetObjectAsync <Group>(obj.Id);

                if (bresult == null)
                {
                    return(false);
                }
                Group currentstate = bresult;
                if (currentstate.action.@on == true)
                {
                    log.Debug("Toggling group state : ON");
                    result = await SetStateAsyncTask(new Action { @on = false, transitiontime = tt }, obj.Id);
                }
                else
                {
                    log.Debug("Toggling group state : OFF");

                    Action newaction;

                    if (WinHueSettings.settings.SlidersBehavior == 0)
                    {
                        newaction = new Action()
                        {
                            on             = true,
                            transitiontime = tt
                        };

                        if (!WinHueSettings.settings.UseLastBriState)
                        {
                            newaction.bri = dimvalue ?? WinHueSettings.settings.DefaultBriGroup;
                        }
                    }
                    else
                    {
                        newaction                = state as Action ?? new Action();
                        newaction.on             = true;
                        newaction.transitiontime = tt;
                    }

                    result = await SetStateAsyncTask(newaction, obj.Id);
                }
            }

            return(result);
        }