public async Task SetColor(string availability, Selector selector) { if (!_options.IsLIFXEnabled || string.IsNullOrEmpty(_options.LIFXApiKey)) { return; } _client = await LifxCloudClient.CreateAsync(_options.LIFXApiKey); string color = ""; switch (availability) { case "Available": color = "green"; break; case "Busy": color = "red"; break; case "BeRightBack": color = "yellow"; break; case "Away": color = "yellow"; break; case "DoNotDisturb": color = "purple"; break; case "Offline": color = "white"; break; case "Off": color = "white"; break; default: color = availability; break; } if (_options.Brightness == 0) { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off });; } else { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Brightness = (_options.Brightness / 100), Color = color });; } }
public async Task <ApiResponse> SetColor(string color, int brightness, Selector selector) { if (string.IsNullOrEmpty(_apiKey)) { throw new NotImplementedException("Api Key Not Specified"); } _client = await LifxCloudClient.CreateAsync(_apiKey); if (brightness == 0) { return(await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off, Fast = true })); } else { return(await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Brightness = Convert.ToDouble(brightness) / 100, Color = color, Duration = 0, Fast = true })); } }
public async Task <ApiResponse> SetState(SetStateRequest request) { if (client == null) { return(new ApiResponse()); } return(await client.SetState(this, request)); }
public async Task SetColor(string availability, string lightId, string apiKey = null) { if (string.IsNullOrEmpty(lightId)) { throw new ArgumentNullException(); } Selector selector = new Selector.LightId(lightId); if (!string.IsNullOrEmpty(apiKey)) { _options.LightSettings.LIFX.LIFXApiKey = apiKey; } if (!_options.LightSettings.LIFX.IsLIFXEnabled || string.IsNullOrEmpty(_options.LightSettings.LIFX.LIFXApiKey)) { return; } _client = await LifxCloudClient.CreateAsync(_options.LightSettings.LIFX.LIFXApiKey); string color = ""; switch (availability) { case "Available": if (!_options.LightSettings.LIFX.AvailableStatus.Disabled) { color = $"{_options.LightSettings.LIFX.AvailableStatus.Colour.ToString()}"; } else { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; case "Busy": if (!_options.LightSettings.LIFX.BusyStatus.Disabled) { color = $"{_options.LightSettings.LIFX.BusyStatus.Colour.ToString()}"; } else { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; case "BeRightBack": if (!_options.LightSettings.LIFX.BeRightBackStatus.Disabled) { color = $"{_options.LightSettings.LIFX.BeRightBackStatus.Colour.ToString()}"; } else { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; case "Away": if (!_options.LightSettings.LIFX.AwayStatus.Disabled) { color = $"{_options.LightSettings.LIFX.AwayStatus.Colour.ToString()}"; } else { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; case "DoNotDisturb": if (!_options.LightSettings.LIFX.DoNotDisturbStatus.Disabled) { color = $"{_options.LightSettings.LIFX.DoNotDisturbStatus.Colour.ToString()}"; } else { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; case "Offline": if (!_options.LightSettings.LIFX.OfflineStatus.Disabled) { color = $"{_options.LightSettings.LIFX.OfflineStatus.Colour.ToString()}"; } else { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; case "Off": if (!_options.LightSettings.LIFX.OffStatus.Disabled) { color = $"{_options.LightSettings.LIFX.OffStatus.Colour.ToString()}"; } else { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; default: color = $"{_options.LightSettings.LIFX.OffStatus.Colour.ToString()}"; break; } if (color.Length == 9 && color.Contains("#")) { color = $"#{color.Substring(3)}"; } color = color.Replace("#", ""); switch (color.Length) { case var length when color.Length == 6: // Do Nothing break; case var length when color.Length > 6: // Get last 6 characters color = color.Substring(color.Length - 6); break; default: throw new ArgumentException("Supplied Color had an issue"); } if (availability == "Off") { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Color = color, Power = PowerState.Off }); return; } if (_options.LightSettings.UseDefaultBrightness) { if (_options.LightSettings.DefaultBrightness == 0) { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); } else { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Brightness = Convert.ToDouble(_options.LightSettings.DefaultBrightness) / 100, Color = color, Duration = 0 }); } } else { if (_options.LightSettings.LIFX.LIFXBrightness == 0) { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); } else { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Brightness = Convert.ToDouble(_options.LightSettings.LIFX.LIFXBrightness) / 100, Color = color, Duration = 0 }); } } }
public async Task SetColor(string availability, string activity, string lightId, string apiKey = null) { if (string.IsNullOrEmpty(lightId)) { throw new ArgumentNullException(nameof(lightId), "Selected LIFX Light Not Specified"); } bool useWorkingHours = await _mediator.Send(new WorkingHoursServices.UseWorkingHoursCommand()); bool IsInWorkingHours = await _mediator.Send(new WorkingHoursServices.IsInWorkingHoursCommand()); if (!useWorkingHours || (useWorkingHours && IsInWorkingHours)) { Selector selector = null; if (!lightId.Contains("group")) { selector = new Selector.LightId(lightId.Replace("id:", "")); } else { selector = new Selector.GroupId(lightId.Replace("group_id:", "")); } if (!string.IsNullOrEmpty(apiKey)) { _options.LightSettings.LIFX.LIFXApiKey = apiKey; } if (!_options.LightSettings.LIFX.IsEnabled || string.IsNullOrEmpty(_options.LightSettings.LIFX.LIFXApiKey)) { return; } try { _client = await LifxCloudClient.CreateAsync(_options.LightSettings.LIFX.LIFXApiKey); string color = ""; switch (availability) { case "Available": if (!_options.LightSettings.LIFX.Statuses.AvailabilityAvailableStatus.Disabled) { color = $"{_options.LightSettings.LIFX.Statuses.AvailabilityAvailableStatus.Colour.ToString()}"; } else { _logger.LogInformation($"Turning LIFX Light {lightId} Off - LIFXService:SetColor"); var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; case "Busy": if (!_options.LightSettings.LIFX.Statuses.AvailabilityBusyStatus.Disabled) { color = $"{_options.LightSettings.LIFX.Statuses.AvailabilityBusyStatus.Colour.ToString()}"; } else { _logger.LogInformation($"Turning LIFX Light {lightId} Off - LIFXService:SetColor"); var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; case "BeRightBack": if (!_options.LightSettings.LIFX.Statuses.AvailabilityBeRightBackStatus.Disabled) { color = $"{_options.LightSettings.LIFX.Statuses.AvailabilityBeRightBackStatus.Colour.ToString()}"; } else { _logger.LogInformation($"Turning LIFX Light {lightId} Off - LIFXService:SetColor"); var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; case "Away": if (!_options.LightSettings.LIFX.Statuses.AvailabilityAwayStatus.Disabled) { color = $"{_options.LightSettings.LIFX.Statuses.AvailabilityAwayStatus.Colour.ToString()}"; } else { _logger.LogInformation($"Turning LIFX Light {lightId} Off - LIFXService:SetColor"); var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; case "DoNotDisturb": if (!_options.LightSettings.LIFX.Statuses.AvailabilityDoNotDisturbStatus.Disabled) { color = $"{_options.LightSettings.LIFX.Statuses.AvailabilityDoNotDisturbStatus.Colour.ToString()}"; } else { _logger.LogInformation($"Turning LIFX Light {lightId} Off - LIFXService:SetColor"); var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; case "Offline": if (!_options.LightSettings.LIFX.Statuses.AvailabilityOfflineStatus.Disabled) { color = $"{_options.LightSettings.LIFX.Statuses.AvailabilityOfflineStatus.Colour.ToString()}"; } else { _logger.LogInformation($"Turning LIFX Light {lightId} Off - LIFXService:SetColor"); var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; case "Off": if (!_options.LightSettings.LIFX.Statuses.AvailabilityOffStatus.Disabled) { color = $"{_options.LightSettings.LIFX.Statuses.AvailabilityOffStatus.Colour.ToString()}"; } else { _logger.LogInformation($"Turning LIFX Light {lightId} Off - LIFXService:SetColor"); var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; default: color = $"{_options.LightSettings.LIFX.Statuses.AvailabilityOffStatus.Colour.ToString()}"; break; } if (color.Length == 9 && color.Contains("#")) { color = $"#{color.Substring(3)}"; } color = color.Replace("#", ""); switch (color.Length) { case var length when color.Length == 6: // Do Nothing break; case var length when color.Length > 6: // Get last 6 characters color = color.Substring(color.Length - 6); break; default: throw new ArgumentException("Supplied Color had an issue"); } if (availability == "Off") { _logger.LogInformation($"Turning LIFX Light {lightId} Off - LIFXService:SetColor"); var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } if (_options.LightSettings.UseDefaultBrightness) { if (_options.LightSettings.DefaultBrightness == 0) { _logger.LogInformation($"Turning LIFX Light {lightId} Off - LIFXService:SetColor"); var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); } else { string message = $"Setting LIFX Light {lightId} to {color}"; _logger.LogInformation(message); var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Brightness = Convert.ToDouble(_options.LightSettings.DefaultBrightness) / 100, Color = color, Duration = 0 }); } } else { if (_options.LightSettings.LIFX.Brightness == 0) { _logger.LogInformation($"Turning LIFX Light {lightId} Off - LIFXService:SetColor"); var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); } else { string message = $"Setting LIFX Light {lightId} to {color}"; _logger.LogInformation(message); var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Brightness = Convert.ToDouble(_options.LightSettings.LIFX.Brightness) / 100, Color = color, Duration = 0 }); } } } catch (Exception e) { _logger.LogError(e, "Error Occured Setting Color"); throw; } } }
public async Task SetColor(string availability, Selector selector, string apiKey = null) { if (!string.IsNullOrEmpty(apiKey)) { _options.LightSettings.LIFX.LIFXApiKey = apiKey; } if (!_options.LightSettings.LIFX.IsLIFXEnabled || string.IsNullOrEmpty(_options.LightSettings.LIFX.LIFXApiKey)) { return; } _client = await LifxCloudClient.CreateAsync(_options.LightSettings.LIFX.LIFXApiKey); string color = ""; switch (availability) { case "Available": if (!_options.LightSettings.LIFX.AvailableStatus.Disabled) { color = $"{_options.LightSettings.LIFX.AvailableStatus.Colour.ToString()}"; } else { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; case "Busy": if (!_options.LightSettings.LIFX.BusyStatus.Disabled) { color = $"{_options.LightSettings.LIFX.BusyStatus.Colour.ToString()}"; } else { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; case "BeRightBack": if (!_options.LightSettings.LIFX.BeRightBackStatus.Disabled) { color = $"{_options.LightSettings.LIFX.BeRightBackStatus.Colour.ToString()}"; } else { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; case "Away": if (!_options.LightSettings.LIFX.AwayStatus.Disabled) { color = $"{_options.LightSettings.LIFX.AwayStatus.Colour.ToString()}"; } else { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; case "DoNotDisturb": if (!_options.LightSettings.LIFX.DoNotDisturbStatus.Disabled) { color = $"{_options.LightSettings.LIFX.DoNotDisturbStatus.Colour.ToString()}"; } else { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; case "Offline": if (!_options.LightSettings.LIFX.OfflineStatus.Disabled) { color = $"{_options.LightSettings.LIFX.OfflineStatus.Colour.ToString()}"; } else { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; case "Off": if (!_options.LightSettings.LIFX.OffStatus.Disabled) { color = $"{_options.LightSettings.LIFX.OffStatus.Colour.ToString()}"; } else { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); return; } break; default: color = $"{_options.LightSettings.LIFX.OffStatus.Colour.ToString()}"; break; } if (color.Length == 9 && color.Contains("#")) { color = $"#{color.Substring(3)}"; } if (availability == "Off") { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Color = color, Power = PowerState.Off }); return; } if (_options.LightSettings.UseDefaultBrightness) { if (_options.LightSettings.DefaultBrightness == 0) { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); } else { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Brightness = Convert.ToDouble(_options.LightSettings.DefaultBrightness) / 100, Color = color, Duration = 0 }); } } else { if (_options.LightSettings.LIFX.LIFXBrightness == 0) { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Power = PowerState.Off }); } else { var result = await _client.SetState(selector, new LifxCloud.NET.Models.SetStateRequest { Brightness = Convert.ToDouble(_options.LightSettings.LIFX.LIFXBrightness) / 100, Color = color, Duration = 0 }); } } }
public async Task SetColor(string availability, string activity, string lightId, string apiKey = null) { if (string.IsNullOrEmpty(lightId)) { _logger.LogInformation("Selected LIFX Light Not Specified"); return; } Selector selector = null; if (!lightId.Contains("group")) { selector = new Selector.LightId(lightId.Replace("id:", "")); } else { selector = new Selector.GroupId(lightId.Replace("group_id:", "")); } if (!string.IsNullOrEmpty(apiKey)) { _appState.Config.LightSettings.LIFX.LIFXApiKey = apiKey; } if (!_appState.Config.LightSettings.LIFX.IsEnabled || string.IsNullOrEmpty(_appState.Config.LightSettings.LIFX.LIFXApiKey)) { return; } try { _client = await LifxCloudClient.CreateAsync(_appState.Config.LightSettings.LIFX.LIFXApiKey); var o = await Handle(_appState.Config.LightSettings.LIFX.UseActivityStatus?activity : availability, lightId); if (o.returnFunc) { return; } var color = o.color.Replace("#", ""); var command = o.command; var message = ""; switch (color.Length) { case var length when color.Length == 6: // Do Nothing break; case var length when color.Length > 6: // Get last 6 characters color = color.Substring(0, 6); break; default: throw new ArgumentException("Supplied Color had an issue"); } if (availability == "Off") { _logger.LogInformation($"Turning LIFX Light {lightId} Off - LIFXService:SetColor"); command.Power = PowerState.Off; var result = await _client.SetState(selector, command); return; } if (_appState.Config.LightSettings.UseDefaultBrightness) { if (_appState.Config.LightSettings.DefaultBrightness == 0) { command.Power = PowerState.Off; } else { command.Power = PowerState.On; command.Brightness = Convert.ToDouble(_appState.Config.LightSettings.DefaultBrightness) / 100; command.Duration = 0; } } else { if (_appState.Config.LightSettings.LIFX.Brightness == 0) { command.Power = PowerState.Off; } else { command.Power = PowerState.On; command.Brightness = Convert.ToDouble(_appState.Config.LightSettings.DefaultBrightness) / 100; command.Duration = 0; } } command.Color = color; await _client.SetState(selector, command); message = $"Setting LIFX Light {lightId} to {color}"; _logger.LogInformation(message); } catch (Exception e) { _logger.LogError(e, "Error Occured Setting Color"); throw; } }
private async Task <(string color, SetStateRequest command, bool returnFunc)> Handle(string presence, string lightId) { var props = _appState.Config.LightSettings.LIFX.Statuses.GetType().GetProperties().ToList(); if (_appState.Config.LightSettings.LIFX.UseActivityStatus) { props = props.Where(a => a.Name.ToLower().StartsWith("activity")).ToList(); } else { props = props.Where(a => a.Name.ToLower().StartsWith("availability")).ToList(); } string color = ""; string message; var command = new SetStateRequest(); if (presence.Contains("#")) { // provided presence is actually a custom color color = presence; command.Power = PowerState.On; return(color, command, false); } foreach (var prop in props) { if (presence == prop.Name.Replace("Status", "").Replace("Availability", "").Replace("Activity", "")) { var value = (AvailabilityStatus)prop.GetValue(_appState.Config.LightSettings.LIFX.Statuses); if (!value.Disabled) { command.Power = PowerState.On; color = value.Colour; return(color, command, false); } else { command.Power = PowerState.Off; Selector selector = null; if (!lightId.Contains("group")) { selector = new Selector.LightId(lightId.Replace("id:", "")); } else { selector = new Selector.GroupId(lightId.Replace("group_id:", "")); } await _client.SetState(selector, command); message = $"Turning LIFX Light {lightId} Off"; _logger.LogInformation(message); return(color, command, true); } } } return(color, command, false); }