/// <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); }
public void EditSchedule(Schedule sc) { Header.Description = sc.description; Header.Name = sc.name; Header.Recycle = sc.recycle; Header.Autodelete = sc.autodelete; Header.Enabled = sc.status; if (sc.localtime.Contains("PT")) { Header.ScheduleType = "PT"; Regex timerRegex = new Regex(@"(^R(\d{2})\/?)?PT(\d\d:\d\d:\d\d)(A\d\d:\d\d:\d\d)?$"); Match mc = timerRegex.Match(sc.localtime); Header.Datetime = DateTime.Parse(mc.Groups[3].Value); if (mc.Groups[2].Value != string.Empty) { Repetitions = Convert.ToInt32(mc.Groups[2].Value); } if (mc.Groups[4].Value != string.Empty) { Header.Randomize = true; } } else if (sc.localtime.Contains("W")) { Header.ScheduleType = "W"; Regex alarmRegex = new Regex(@"(^W(\d{3})//?)T(\d{2}:\d{2}:\d{2})(A\d{2}:\d{2}:\d{2})?$"); Match mc = alarmRegex.Match(sc.localtime); Header.Datetime = DateTime.Parse(mc.Groups[3].Value); if (mc.Groups[2].Value != string.Empty) { ScheduleMask = mc.Groups[2].Value; } if (mc.Groups[4].Value != string.Empty) { Header.Randomize = true; } } else { Header.ScheduleType = "T"; Regex scheduleRegex = new Regex(@"^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(A\d{2}:\d{2}:\d{2})?$"); Match mc = scheduleRegex.Match(sc.localtime); Header.Datetime = DateTime.Parse(mc.Groups[1].Value, CultureInfo.InvariantCulture); if (mc.Groups[2].Value != string.Empty) { Header.Randomize = true; } } if (sc.command?.address?.objecttype == null) { return; } if (!sc.command.body.Contains("scene")) { switch (sc.command.address.objecttype) { case "lights": Content = ContentTypeVm.Light; break; case "groups": Content = ContentTypeVm.Group; break; case "schedules": Content = ContentTypeVm.Schedule; break; case "sensors": Content = ContentTypeVm.Sensor; break; default: break; } } else { Content = ContentTypeVm.Scene; } if (Content != ContentTypeVm.Scene) { SelectedTarget = _listTargetHueObject.FirstOrDefault(x => x.Id == sc.command.address.id); } else { Action scene = Serializer.DeserializeToObject <Action>(sc.command.body); SelectedTarget = _listTargetHueObject.FirstOrDefault(x => x.Id == scene.scene); } if (SelectedTarget == null) { MessageBox.Show(GlobalStrings.Object_Does_Not_Exists, GlobalStrings.Error, MessageBoxButton.OK, MessageBoxImage.Error); } else { switch (SelectedTarget) { case Sensor _: { ((ScheduleCreatorPropertyGridViewModel)_selectedViewModel).SelectedObject = Serializer.DeserializeToObject(sc.command.body, HueSensorStateFactory.CreateSensorStateFromSensorType(((Sensor)SelectedTarget).type).GetType()); break; } case Schedule _: { ((ScheduleCreatorPropertyGridViewModel)_selectedViewModel).SelectedObject = Serializer.DeserializeToObject <Schedule>(sc.command.body); break; } case Scene _: { SelectedViewModel = null; break; } case Light _: case Group _: { if (_propGridLG) { ((ScheduleCreatorPropertyGridViewModel)_selectedViewModel).SelectedObject = Serializer.DeserializeToObject <State>(sc.command.body); } else { SelectedViewModel = Serializer.DeserializeToObject <ScheduleCreatorSlidersViewModel>(sc.command.body); if (SelectedViewModel == null) { SetEmptyViewModel(); } } break; } } } }
/// <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); }