コード例 #1
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;
            }
        }
コード例 #2
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.GetHueType();
            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);
        }
コード例 #3
0
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            Schedule sc = scvm.Schedule;
            bool     result;

            if (actualobj is Schedule)
            {
                sc.Id  = actualobj.Id;
                result = _bridge.ModifyObject(sc);
            }
            else
            {
                result = _bridge.CreateObject(sc);
            }

            if (result)
            {
                DialogResult = true;
                actualobj    = new Schedule()
                {
                    Id = _bridge.LastCommandMessages.LastSuccess.value
                };
                Close();
            }
            else
            {
                MessageBox.Show($"{GlobalStrings.Error_ErrorHasOccured} : {_bridge.LastCommandMessages}", GlobalStrings.Error, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
コード例 #4
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 async Task <bool> ModifyObjectAsyncTask(IHueObject modifiedobject)
        {
            string     typename = modifiedobject.GetHueType();
            IHueObject clone    = (IHueObject)modifiedobject.Clone();
            string     url      = BridgeUrl + $@"/{typename}/{modifiedobject.Id}";

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

            CommResult comres;

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

                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);
        }
コード例 #5
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.GetHueType();
            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.SerializeToJson(ClearNotAllowedCreationProperties(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);
        }
コード例 #6
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.GetHueType();
            string url      = BridgeUrl + $@"/{typename}/{hueobj.Id}";

            CommResult comres;

            if (!Virtual)
            {
                comres = await Comm.SendRequestAsyncTask(new Uri(url), WebRequestType.Put, Serializer.SerializeToJson(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);
        }
コード例 #7
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));
                    }
                }
            }
        }
コード例 #8
0
 public Form_RenameObject(Bridge bridge, IHueObject obj)
 {
     InitializeComponent();
     _bridge        = bridge;
     _obj           = obj;
     Title          = string.Format(Title, obj.name);
     tbNewName.Text = obj.name;
 }
コード例 #9
0
        public async Task Initialize(Bridge bridge, IHueObject obj)
        {
            _bridge = bridge;
            await scvm.Initialize(bridge, obj);

            actualobj = obj;

            Title = obj is Schedule ? GUI.ScheduleCreatorForm_Title_Modify + obj.name : GUI.ScheduleCreatorForm_Title_Create + obj.name;
        }
コード例 #10
0
        private async Task Initialize()
        {
            List <IHueObject> lo = await BridgesManager.Instance.SelectedBridge.GetAllObjectsAsync(true);

            HiddenObjects.ListObjects = new ObservableCollection <IHueObject>(lo);
            foreach (Tuple <string, string> t in WinHueSettings.bridges.BridgeInfo[BridgesManager.Instance.SelectedBridge.Mac].hiddenobjects)
            {
                if (HiddenObjects.ListObjects.Any(x => x.Id == t.Item1 && x.GetType().Name == t.Item2))
                {
                    IHueObject obj = HiddenObjects.ListObjects.FirstOrDefault(x => x.Id == t.Item1 && x.GetType().Name == t.Item2);
                    HiddenObjects.ListObjects.Remove(obj);
                    HiddenObjects.HiddenObjects.Add(obj);
                }
            }

            HiddenObjects.AcceptChanges();
        }
コード例 #11
0
        private async Task Initialize()
        {
            List <IHueObject> lo = await HueObjectHelper.GetBridgeDataStoreAsyncTask(_bridge, false);

            List <IHueObject> ls = new List <IHueObject>();

            HiddenObjects.ListObjects = new ObservableCollection <IHueObject>(lo);
            foreach (Tuple <string, string> t in WinHueSettings.bridges.BridgeInfo[_bridge.Mac].hiddenobjects)
            {
                if (HiddenObjects.ListObjects.Any(x => x.Id == t.Item1 && x.GetHueType() == t.Item2))
                {
                    IHueObject obj = HiddenObjects.ListObjects.FirstOrDefault(x => x.Id == t.Item1 && x.GetHueType() == t.Item2);
                    HiddenObjects.ListObjects.Remove(obj);
                    HiddenObjects.HiddenObjects.Add(obj);
                }
            }

            HiddenObjects.AcceptChanges();
        }
コード例 #12
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);
            }
        }
コード例 #13
0
        /// <summary>
        /// Get the specified object freom the bridge.
        /// </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 IHueObject GetObject(string id, Type objecttype)
        {
            string typename = objecttype.GetHueType();

            if (typename == null)
            {
                return(null);
            }
            string     url    = BridgeUrl + $"/{typename}/{id}";
            CommResult comres = Comm.SendRequest(new Uri(url), WebRequestType.Get);

            if (comres.Status == WebExceptionStatus.Success)
            {
                IHueObject data = (IHueObject)Serializer.DeserializeToObject(comres.Data, objecttype);
                if (data != null)
                {
                    return(data);
                }
                LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                return(null);
            }
            ProcessCommandFailure(url, comres.Status);
            return(null);
        }
コード例 #14
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);
        }
コード例 #15
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);
        }