예제 #1
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;
            }
        }
예제 #2
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;
                }
            }
        }
예제 #3
0
        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);
        }