public async Task <ActionResult> ProcessAlexaRequest([FromBody] SkillRequest request) { using (Serilog.Context.LogContext.PushProperty("Request", JsonConvert.SerializeObject(request))) { var requestType = request.GetRequestType(); _logger.LogDebug($"Beginning Alexa Request: {requestType.Name}"); SkillResponse response = null; if (requestType == typeof(LaunchRequest)) { response = ResponseBuilder.Tell("Welcome to Presence Light!"); response.Response.ShouldEndSession = false; } else if (requestType == typeof(IntentRequest)) { var intentRequest = request.Request as IntentRequest; if (intentRequest.Intent.Name == "Teams") { _appState.SetLightMode("Graph"); response = ResponseBuilder.Tell("Presence Light set to Teams!"); } else if (intentRequest.Intent.Name == "Custom") { _appState.SetLightMode("Custom"); _appState.SetCustomColor("#FFFFFF"); response = ResponseBuilder.Tell("Presence Light set to custom!"); } if (_appState.LightMode == "Custom") { if (!string.IsNullOrEmpty(Config.LightSettings.Hue.HueApiKey) && !string.IsNullOrEmpty(Config.LightSettings.Hue.HueIpAddress) && !string.IsNullOrEmpty(Config.LightSettings.Hue.SelectedItemId)) { await _mediator.Send(new Core.HueServices.SetColorCommand() { Availability = _appState.CustomColor, Activity = "", LightID = Config.LightSettings.Hue.SelectedItemId }); } if (Config.LightSettings.LIFX.IsEnabled && !string.IsNullOrEmpty(Config.LightSettings.LIFX.LIFXApiKey)) { await _mediator.Send(new Core.LifxServices.SetColorCommand() { Availability = _appState.CustomColor, Activity = "", LightId = Config.LightSettings.LIFX.SelectedItemId }); } } } return(new OkObjectResult(response)); } }
public async Task SendCommand(ICommand command) { if (command is null) { throw new ArgumentNullException(nameof(command)); } await _mediator.Send(command); if (_auditConfiguration.AuditCommands) { await _mediator.Send(new CreateAuditCommand { Command = command }); } }
public async Task <IActionResult> GetAllOrders() { //MediatR //Ideally, every method in the controller should only have this 3 lines //the query or command var query = new GetAllOrdersQuery(); //the send method var result = await _mediator.Send(query); //the result return(Ok(result)); //var orders:List<OrderDto> = await _ordersRepository.GetOrdersAsync(); //var orderResponse:List < OrderResponse > = _mapper.MapOrderDtosToOrderResponse(orders); //return Ok(orderResponse); }
public async Task <ActionResult> Index(PagedRequest parameter) { var result = await _mediator.Send(new QueryEmailAccounts { CurrentPage = parameter.Page ?? 1, PageSize = parameter.Size ?? 20 }); return(View(result)); }
public async Task <string> SetColor(string availability, string?activity, CancellationToken cancellationToken = default) { bool useWorkingHours = await _mediator.Send(new WorkingHoursServices.UseWorkingHoursCommand()); bool IsInWorkingHours = await _mediator.Send(new WorkingHoursServices.IsInWorkingHoursCommand()); if (!useWorkingHours || (useWorkingHours && IsInWorkingHours)) { // If we are outside of working hours we should signal that we are off availability = activity = availability; } string result = await SetAvailability(availability, cancellationToken); result += await SetActivity(activity, cancellationToken); return(result); }
public async Task <IActionResult> GetDetails(Guid id) { var query = new ProcessedRequestById(id); var result = await _mediator.Send(query); if (result is null) { return(NotFound()); } return(Ok(result)); }
public async void UpdateLight(string command) { using (Serilog.Context.LogContext.PushProperty("Command", command)) { if (command == "Teams") { _logger.LogDebug("Set Light Mode: Graph"); _appState.SetLightMode("Graph"); } else { _logger.LogDebug("Set Light Mode: Custom"); _logger.LogDebug("Set Custom Color: Offline"); _appState.SetLightMode("Custom"); _appState.SetCustomColor("Offline"); } } if (_appState.LightMode == "Custom") { if (!string.IsNullOrEmpty(Config.LightSettings.Hue.HueApiKey) && !string.IsNullOrEmpty(Config.LightSettings.Hue.HueIpAddress) && !string.IsNullOrEmpty(Config.LightSettings.Hue.SelectedItemId)) { await _mediator.Send(new Core.HueServices.SetColorCommand() { Availability = _appState.CustomColor, Activity = "", LightID = Config.LightSettings.Hue.SelectedItemId }); } if (Config.LightSettings.LIFX.IsEnabled && !string.IsNullOrEmpty(Config.LightSettings.LIFX.LIFXApiKey)) { await _mediator.Send(new Core.LifxServices.SetColorCommand() { Availability = _appState.CustomColor, Activity = "", LightId = Config.LightSettings.LIFX.SelectedItemId }); } } }
public async Task Consume(ConsumeContext <UserCheckoutAccepted> context) { var result = false; var eventMsg = context.Message; if (eventMsg.RequestId != Guid.Empty) { // 创建订单指令 var createOrderCommand = new OrderCreateCommand(eventMsg.Basket.Items, eventMsg.UserId, eventMsg.UserName, eventMsg.City, eventMsg.Street, eventMsg.State, eventMsg.Country, eventMsg.ZipCode, eventMsg.CardNumber, eventMsg.CardHolderName, eventMsg.CardExpiration, eventMsg.CardSecurityNumber, eventMsg.CardTypeId); // 请求创建订单 var requestCreateOrder = new IdentifiedCommand <OrderCreateCommand, bool>(eventMsg.RequestId, createOrderCommand); result = await _mediator.Send(requestCreateOrder); } _logger.LogTrace(result ? $"UserCheckoutAccepted integration event has been received and a create new order process is started with requestId: {eventMsg.RequestId}" : $"UserCheckoutAccepted integration event has been received but a new order process has failed with requestId: {eventMsg.RequestId}"); }
public async void UpdateLight(string command) { if (await _userAuthService.IsUserAuthenticated()) { string availability = ""; string activity = ""; using (Serilog.Context.LogContext.PushProperty("Command", command)) { if (command == "Teams") { _logger.LogDebug("Set Light Mode: Graph"); _appState.SetLightMode("Graph"); availability = _appState.Presence.Availability; activity = _appState.Presence.Activity; } else { _logger.LogDebug("Set Light Mode: Custom"); _logger.LogDebug("Set Custom Color: Offline"); _appState.SetLightMode("Custom"); _appState.SetCustomColor("Offline"); availability = _appState.CustomColor; activity = _appState.CustomColor; } } if (Config.LightSettings.Hue.IsEnabled && !Config.LightSettings.Hue.UseRemoteApi && !string.IsNullOrEmpty(Config.LightSettings.Hue.HueApiKey) && !string.IsNullOrEmpty(Config.LightSettings.Hue.HueIpAddress) && !string.IsNullOrEmpty(Config.LightSettings.Hue.SelectedItemId)) { await _mediator.Send(new Core.HueServices.SetColorCommand() { Availability = availability, Activity = activity, LightID = Config.LightSettings.Hue.SelectedItemId }).ConfigureAwait(true); } if (Config.LightSettings.Hue.IsEnabled && Config.LightSettings.Hue.UseRemoteApi && !string.IsNullOrEmpty(Config.LightSettings.Hue.HueApiKey) && !string.IsNullOrEmpty(Config.LightSettings.Hue.HueIpAddress) && !string.IsNullOrEmpty(Config.LightSettings.Hue.SelectedItemId)) { await _mediator.Send(new Core.RemoteHueServices.SetColorCommand { Availability = availability, LightId = Config.LightSettings.Hue.SelectedItemId, BridgeId = Config.LightSettings.Hue.RemoteBridgeId }).ConfigureAwait(true); } if (Config.LightSettings.LIFX.IsEnabled && !string.IsNullOrEmpty(Config.LightSettings.LIFX.LIFXApiKey) && !string.IsNullOrWhiteSpace(Config.LightSettings.LIFX.SelectedItemId)) { await _mediator.Send(new Core.LifxServices.SetColorCommand() { Availability = availability, Activity = activity, LightId = Config.LightSettings.LIFX.SelectedItemId }).ConfigureAwait(true); } if (Config.LightSettings.Wiz.IsEnabled && !string.IsNullOrWhiteSpace(Config.LightSettings.Wiz.SelectedItemId)) { await _mediator.Send(new Core.WizServices.SetColorCommand() { Availability = availability, Activity = activity, LightID = Config.LightSettings.Wiz.SelectedItemId }).ConfigureAwait(true); } if (Config.LightSettings.Yeelight.IsEnabled && !string.IsNullOrWhiteSpace(Config.LightSettings.Yeelight.SelectedItemId)) { await _mediator.Send(new Core.YeelightServices.SetColorCommand() { Availability = availability, Activity = activity, LightId = Config.LightSettings.Yeelight.SelectedItemId }).ConfigureAwait(true); } } }
private async Task CallGraph() { lightMode = "Graph"; lightColors.syncTeamsButton.IsEnabled = false; lightColors.syncThemeButton.IsEnabled = true; if (!await _mediator.Send(new Core.GraphServices.GetIsInitializedCommand())) { await _mediator.Send(new Core.GraphServices.InitializeCommand() { Client = _graphservice.GetAuthenticatedGraphClient() }); } landingPage.signInPanel.Visibility = Visibility.Collapsed; lightColors.lblTheme.Visibility = Visibility.Collapsed; landingPage.loadingPanel.Visibility = Visibility.Visible; try { var(profile, presence) = await _mediator.Send(new Core.GraphServices.GetProfileAndPresenceCommand()); var photo = await GetPhoto().ConfigureAwait(true); if (photo == null) { MapUI(presence, profile, new BitmapImage(new Uri("pack://application:,,,/PresenceLight;component/images/UnknownProfile.png"))); } else { MapUI(presence, profile, LoadImage(photo)); } if (Config.LightSettings.SyncLights) { if (!await _mediator.Send(new Core.WorkingHoursServices.UseWorkingHoursCommand())) { if (lightMode == "Graph") { await SetColor(presence.Availability, presence.Activity).ConfigureAwait(true); } } else { bool previousWorkingHours = await _mediator.Send(new Core.WorkingHoursServices.IsInWorkingHoursCommand()); if (previousWorkingHours) { if (lightMode == "Graph") { await SetColor(presence.Availability, presence.Activity).ConfigureAwait(true); } } else { // check to see if working hours have passed if (previousWorkingHours) { if (lightMode == "Graph") { switch (Config.LightSettings.HoursPassedStatus) { case "Keep": await SetColor(presence.Availability, presence.Activity).ConfigureAwait(true); break; case "White": await SetColor("Offline", presence.Activity).ConfigureAwait(true); break; case "Off": await SetColor("Off", presence.Activity).ConfigureAwait(true); break; default: await SetColor(presence.Availability, presence.Activity).ConfigureAwait(true); break; } } } } } } landingPage.loadingPanel.Visibility = Visibility.Collapsed; landingPage.signInPanel.Visibility = Visibility.Collapsed; landingPage.dataPanel.Visibility = Visibility.Visible; await _settingsService.SaveSettings(Config).ConfigureAwait(true); landingPage.turnOffButton.Visibility = Visibility.Visible; landingPage.turnOnButton.Visibility = Visibility.Collapsed; await InteractWithLights().ConfigureAwait(true); } catch (Exception e) { } }
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, string activity, string lightId) { if (string.IsNullOrEmpty(lightId)) { throw new ArgumentOutOfRangeException("Wiz Selected Light Id Invalid"); } try { bool useWorkingHours = await _mediator.Send(new WorkingHoursServices.UseWorkingHoursCommand()); bool IsInWorkingHours = await _mediator.Send(new WorkingHoursServices.IsInWorkingHoursCommand()); if (!useWorkingHours || (useWorkingHours && IsInWorkingHours)) { var o = Handle(_options.LightSettings.Wiz.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(color.Length - 6); break; default: throw new ArgumentException("Supplied Color had an issue"); } var rgb = new RGBColor(color); command.R = Convert.ToInt32(rgb.R); command.B = Convert.ToInt32(rgb.B); command.G = Convert.ToInt32(rgb.G); if (availability == "Off") { command.State = false; UpdateLight(command, lightId); message = $"Turning Wiz Light {lightId} Off"; _logger.LogInformation(message); return; } if (_options.LightSettings.UseDefaultBrightness) { if (_options.LightSettings.DefaultBrightness == 0) { command.State = false; } else { command.State = true; command.Dimming = _options.LightSettings.DefaultBrightness; command.Speed = 0; } } else { if (_options.LightSettings.Wiz.Brightness == 0) { command.State = false; } else { command.State = true; command.Dimming = _options.LightSettings.Wiz.Brightness; command.Speed = 0; } } UpdateLight(command, lightId); message = $"Setting Wiz Light {lightId} to {color}"; _logger.LogInformation(message); } } catch (Exception e) { _logger.LogError(e, "Error Occurred Setting Color"); throw; } }
private async Task InteractWithLights() { bool previousWorkingHours = false; while (await _userAuthService.IsUserAuthenticated()) { bool useWorkingHours = await _mediator.Send(new Core.WorkingHoursServices.UseWorkingHoursCommand()); bool IsInWorkingHours = await _mediator.Send(new Core.WorkingHoursServices.IsInWorkingHoursCommand()); try { await Task.Delay(Convert.ToInt32(Config.LightSettings.PollingInterval * 1000)).ConfigureAwait(true); bool touchLight = false; string newColor = ""; if (Config.LightSettings.SyncLights) { if (!useWorkingHours) { if (_appState.LightMode == "Graph") { touchLight = true; } } else { if (IsInWorkingHours) { previousWorkingHours = IsInWorkingHours; if (_appState.LightMode == "Graph") { touchLight = true; } } else { // check to see if working hours have passed if (previousWorkingHours) { switch (Config.LightSettings.HoursPassedStatus) { case "Keep": break; case "White": newColor = "Offline"; break; case "Off": newColor = "Off"; break; default: break; } touchLight = true; } } } } if (touchLight) { switch (_appState.LightMode) { case "Graph": _logger.LogInformation("PresenceLight Running in Teams Mode"); _appState.Presence = await System.Threading.Tasks.Task.Run(() => GetPresence()).ConfigureAwait(true); if (newColor == string.Empty) { await SetColor(_appState.Presence.Availability, _appState.Presence.Activity).ConfigureAwait(true); } else { await SetColor(newColor, newColor).ConfigureAwait(true); } break; default: break; } } } catch (Exception e) { _logger.LogError(e, "Error Occured Interacting with Lights"); } } }
public async Task SetColor(string availability, string activity, string lightId) { if (string.IsNullOrEmpty(lightId)) { throw new ArgumentOutOfRangeException("Hue Selected Light Id Invalid"); } try { bool useWorkingHours = await _mediator.Send(new WorkingHoursServices.UseWorkingHoursCommand()); bool IsInWorkingHours = await _mediator.Send(new WorkingHoursServices.IsInWorkingHoursCommand()); if (!useWorkingHours || (useWorkingHours && IsInWorkingHours)) { _client = new LocalHueClient(_options.LightSettings.Hue.HueIpAddress); _client.Initialize(_options.LightSettings.Hue.HueApiKey); var o = await Handle(_options.LightSettings.Hue.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(color.Length - 6); break; default: throw new ArgumentException("Supplied Color had an issue"); } command.SetColor(new RGBColor(color)); if (availability == "Off") { command.On = false; if (lightId.Contains("group_id:")) { await _client.SendGroupCommandAsync(command, lightId.Replace("group_id:", "")); } else { await _client.SendCommandAsync(command, new List <string> { lightId.Replace("id:", "") }); } message = $"Turning Hue Light {lightId} Off"; _logger.LogInformation(message); return; } if (_options.LightSettings.UseDefaultBrightness) { if (_options.LightSettings.DefaultBrightness == 0) { command.On = false; } else { command.On = true; command.Brightness = Convert.ToByte(((Convert.ToDouble(_options.LightSettings.DefaultBrightness) / 100) * 254)); command.TransitionTime = new TimeSpan(0); } } else { if (_options.LightSettings.Hue.Brightness == 0) { command.On = false; } else { command.On = true; command.Brightness = Convert.ToByte(((Convert.ToDouble(_options.LightSettings.Hue.Brightness) / 100) * 254)); command.TransitionTime = new TimeSpan(0); } } if (lightId.Contains("group_id:")) { await _client.SendGroupCommandAsync(command, lightId.Replace("group_id:", "")); } else { await _client.SendCommandAsync(command, new List <string> { lightId.Replace("id:", "") }); } message = $"Setting Hue Light {lightId} to {color}"; _logger.LogInformation(message); } } catch (Exception e) { _logger.LogError(e, "Error Occurred Setting Color"); throw; } }
protected Task <TResponse> SendAsync <TResponse>(MediatR.IRequest <TResponse> request) { return(MediatR.Send(request)); }
public async Task SetColor(string availability, string activity, string lightId) { string message = ""; if (string.IsNullOrEmpty(lightId)) { _logger.LogError($"Yeelight Selected Light Id {lightId} Invalid"); throw new ArgumentOutOfRangeException(nameof(lightId), $"Yeelight Selected Light Id {lightId} Invalid"); } bool useWorkingHours = await _mediator.Send(new WorkingHoursServices.UseWorkingHoursCommand()); bool IsInWorkingHours = await _mediator.Send(new WorkingHoursServices.IsInWorkingHoursCommand()); if (!useWorkingHours || (useWorkingHours && IsInWorkingHours)) { var device = this.deviceGroup.FirstOrDefault(x => x.Id == lightId); if (device == null) { message = $"Yeelight Device {lightId} Not Found"; _logger.LogError(message); throw new ArgumentOutOfRangeException(nameof(lightId), message); } device.OnNotificationReceived += Device_OnNotificationReceived; device.OnError += Device_OnError; if (!await device.Connect()) { message = $"Unable to Connect to Yeelight Device {lightId}"; _logger.LogError(message); throw new ArgumentOutOfRangeException(nameof(lightId), message); } try { string color = ""; if (_options.LightSettings.Yeelight.UseActivityStatus) { } else { switch (availability) { case "Available": if (!_options.LightSettings.Yeelight.Statuses.AvailabilityAvailableStatus.Disabled) { color = _options.LightSettings.Yeelight.Statuses.AvailabilityAvailableStatus.Colour; } else { message = $"Turning Yeelight Light {lightId} Off"; _logger.LogInformation(message); await device.SetPower(false); return; } break; case "Busy": if (!_options.LightSettings.Yeelight.Statuses.AvailabilityBusyStatus.Disabled) { color = _options.LightSettings.Yeelight.Statuses.AvailabilityBusyStatus.Colour; } else { message = $"Turning Yeelight Light {lightId} Off"; _logger.LogInformation(message); await device.SetPower(false); return; } break; case "BeRightBack": if (!_options.LightSettings.Yeelight.Statuses.AvailabilityBeRightBackStatus.Disabled) { color = _options.LightSettings.Yeelight.Statuses.AvailabilityBeRightBackStatus.Colour; } else { message = $"Turning Yeelight Light {lightId} Off"; _logger.LogInformation(message); await device.SetPower(false); return; } break; case "Away": if (!_options.LightSettings.Yeelight.Statuses.AvailabilityAwayStatus.Disabled) { color = _options.LightSettings.Yeelight.Statuses.AvailabilityAwayStatus.Colour; } else { message = $"Turning Yeelight Light {lightId} Off"; _logger.LogInformation(message); await device.SetPower(false); return; } break; case "DoNotDisturb": if (!_options.LightSettings.Yeelight.Statuses.AvailabilityDoNotDisturbStatus.Disabled) { color = _options.LightSettings.Yeelight.Statuses.AvailabilityDoNotDisturbStatus.Colour; } else { message = $"Turning Yeelight Light {lightId} Off"; _logger.LogInformation(message); await device.SetPower(false); return; } break; case "Offline": if (!_options.LightSettings.Yeelight.Statuses.AvailabilityOfflineStatus.Disabled) { color = _options.LightSettings.Yeelight.Statuses.AvailabilityOfflineStatus.Colour; } else { message = $"Turning Yeelight Light {lightId} Off"; _logger.LogInformation(message); await device.SetPower(false); return; } break; case "Off": if (!_options.LightSettings.Yeelight.Statuses.AvailabilityOffStatus.Disabled) { color = _options.LightSettings.Yeelight.Statuses.AvailabilityOffStatus.Colour; } else { message = $"Turning Yeelight Light {lightId} Off"; _logger.LogInformation(message); await device.SetPower(false); return; } break; default: color = availability; break; } } 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 (_options.LightSettings.UseDefaultBrightness) { if (_options.LightSettings.DefaultBrightness == 0) { await device.TurnOff(); } else { await device.TurnOn(); await device.SetBrightness(Convert.ToInt32(_options.LightSettings.DefaultBrightness)); } } else { if (_options.LightSettings.Hue.Brightness == 0) { await device.TurnOff(); } else { await device.TurnOn(); await device.SetBrightness(Convert.ToInt32(_options.LightSettings.Yeelight.Brightness)); } } var rgb = new RGBColor(color); await device.SetRGBColor((int)rgb.R, (int)rgb.G, (int)rgb.B); return; } catch (Exception e) { _logger.LogError(e, "Error Occured Finding Lights"); throw; } } }
private async Task LoadSettings() { try { _logger.LogInformation("Load Settings Initialized"); if (!(await _settingsService.IsFilePresent().ConfigureAwait(true))) { await _settingsService.SaveSettings(_options).ConfigureAwait(true); } _appState.SetConfig(await _settingsService.LoadSettings().ConfigureAwait(true) ?? throw new NullReferenceException("Settings Load Service Returned null")); bool useWorkingHours = await _mediator.Send(new Core.WorkingHoursServices.UseWorkingHoursCommand()); bool IsInWorkingHours = await _mediator.Send(new Core.WorkingHoursServices.IsInWorkingHoursCommand()); _logger.LogInformation("Load Settings Successfull"); } catch (Exception e) { _logger.LogError(e, "Error occured Loading Settings"); _diagClient.TrackException(e); } }
public async Task <ActionResult> ProcessAlexaRequest([FromBody] SkillRequest request) { string availability = ""; string activity = ""; using (Serilog.Context.LogContext.PushProperty("Request", JsonConvert.SerializeObject(request))) { var requestType = request.GetRequestType(); _logger.LogDebug($"Beginning Alexa Request: {requestType.Name}"); SkillResponse response = null; if (requestType == typeof(LaunchRequest)) { response = ResponseBuilder.Tell("Welcome to Presence Light!"); response.Response.ShouldEndSession = false; } else if (requestType == typeof(IntentRequest)) { var intentRequest = request.Request as IntentRequest; if (intentRequest.Intent.Name == "Teams") { _logger.LogDebug("Set Light Mode: Graph"); _appState.SetLightMode("Graph"); availability = _appState.Presence.Availability; activity = _appState.Presence.Activity; } else { _logger.LogDebug("Set Light Mode: Custom"); _logger.LogDebug("Set Custom Color: Offline"); _appState.SetLightMode("Custom"); _appState.SetCustomColor("Offline"); availability = _appState.CustomColor; activity = _appState.CustomColor; } if (Config.LightSettings.Hue.IsEnabled && !Config.LightSettings.Hue.UseRemoteApi && !string.IsNullOrEmpty(Config.LightSettings.Hue.HueApiKey) && !string.IsNullOrEmpty(Config.LightSettings.Hue.HueIpAddress) && !string.IsNullOrEmpty(Config.LightSettings.Hue.SelectedItemId)) { await _mediator.Send(new Core.HueServices.SetColorCommand() { Availability = availability, Activity = activity, LightID = Config.LightSettings.Hue.SelectedItemId }).ConfigureAwait(true); } if (Config.LightSettings.Hue.IsEnabled && Config.LightSettings.Hue.UseRemoteApi && !string.IsNullOrEmpty(Config.LightSettings.Hue.HueApiKey) && !string.IsNullOrEmpty(Config.LightSettings.Hue.HueIpAddress) && !string.IsNullOrEmpty(Config.LightSettings.Hue.SelectedItemId)) { await _mediator.Send(new Core.RemoteHueServices.SetColorCommand { Availability = availability, LightId = Config.LightSettings.Hue.SelectedItemId, BridgeId = Config.LightSettings.Hue.RemoteBridgeId }).ConfigureAwait(true); } if (Config.LightSettings.LIFX.IsEnabled && !string.IsNullOrEmpty(Config.LightSettings.LIFX.LIFXApiKey) && !string.IsNullOrWhiteSpace(Config.LightSettings.LIFX.SelectedItemId)) { await _mediator.Send(new Core.LifxServices.SetColorCommand() { Availability = availability, Activity = activity, LightId = Config.LightSettings.LIFX.SelectedItemId }).ConfigureAwait(true); } if (Config.LightSettings.Wiz.IsEnabled && !string.IsNullOrWhiteSpace(Config.LightSettings.Wiz.SelectedItemId)) { await _mediator.Send(new Core.WizServices.SetColorCommand() { Availability = availability, Activity = activity, LightID = Config.LightSettings.Wiz.SelectedItemId }).ConfigureAwait(true); } if (Config.LightSettings.Yeelight.IsEnabled && !string.IsNullOrWhiteSpace(Config.LightSettings.Yeelight.SelectedItemId)) { await _mediator.Send(new Core.YeelightServices.SetColorCommand() { Availability = availability, Activity = activity, LightId = Config.LightSettings.Yeelight.SelectedItemId }).ConfigureAwait(true); } } return(new OkObjectResult(response)); } }