Esempio n. 1
0
        public static async Task Run(ILogger logger = null)
        {
            if (logger == null)
            {
                var loggerFactory = LoggerFactory.Create(builder =>
                {
                    builder.AddConsole();
                });

                logger = loggerFactory.CreateLogger <LightsCore>();
            }

            httpClient = new HttpClient();
            SunsetClient sunsetClient = new SunsetClient(httpClient);
            LifxClient   lifxClient   = new LifxClient(httpClient);

            var getAllLightsTask = lifxClient.GetAllLights();

            int desiredKelvin = await GetDesiredKelvin(sunsetClient, logger);

            logger.LogInformation($"Desired kelvin: {desiredKelvin}");

            var changes         = new List <State>();
            var allLightsResult = await getAllLightsTask;

            foreach (Light light in allLightsResult)
            {
                if (light.Color.Kelvin == desiredKelvin)
                {
                    logger.LogInformation($"Skipping {light.Label} because Kelvin is already {desiredKelvin}.");
                }
                else if (light.Color.Saturation > 0)
                {
                    logger.LogInformation($"Skipping {light.Label} because saturation is greater than zero ({light.Color.Saturation}) indicating a custom colour is set.");
                }
                else
                {
                    logger.LogInformation($"Changing {light.Label} from {light.Color.Kelvin} to {desiredKelvin}");
                    changes.Add(new State()
                    {
                        Selector = $"label:{light.Label}",
                        Color    = $"kelvin:{desiredKelvin}"
                    });
                }
            }

            if (changes.Any())
            {
                var states = new States()
                {
                    Fast      = true, // See https://api.developer.lifx.com/v1/docs/set-state#fast-mode
                    StateList = changes
                };

                await lifxClient.PutStates(states);
            }
        }