Exemplo n.º 1
0
        public override void OnExecute()
        {
            Hue = Context.CredentialProvider.Get <HueCredential, LocalHueClient>();

            var lights = Hue.GetLightsAsync().Result;

            foreach (var light in lights)
            {
                Console.WriteLine(light.Id);
            }
        }
Exemplo n.º 2
0
        public List <LightSource> GetSources()
        {
            var list = new List <LightSource>();

            foreach (var r in _client.GetLightsAsync().Result)
            {
                list.Add(new LightSource(this, r.Id));
            }

            return(list);
        }
Exemplo n.º 3
0
        public void SetBridge(string bridgeIP, string appKey, bool alreadyRegistered)
        {
            _hue = new HueClient(bridgeIP);

            if (alreadyRegistered)
            {
                _hue.Initialize(appKey);
            }
            else
            {
                var retryCount = 0;
                var regOk      = false;
                while (!regOk || retryCount >= RETRY_MAX)
                {
                    var reg = _hue.RegisterAsync(APP_NAME, appKey);
                    reg.Wait();
                    if (!reg.Result)
                    {
                        ShowWarning("Please press the button on the bridge to register the application", 30);
                    }
                    else
                    {
                        regOk = true;
                    }

                    retryCount++;
                    if (!regOk && retryCount < RETRY_MAX)
                    {
                        Thread.Sleep(RETRY_DELAY);
                    }
                }

                if (!regOk)
                {
                    ShowError("Failed to register application with bridge", 30);
                }
            }

            var lights = _hue.GetLightsAsync();

            lights.Wait();

            var choices = lights.Result.Select(x => new Tuple <string, string>(x.Id, string.Format("{0} ({1})", x.Name, x.Type))).ToList();

            UpdateLightChoice(choices);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Called when the package is started.
        /// </summary>
        public override void OnStart()
        {
            PackageHost.WriteInfo("Connecting to " + PackageHost.GetSettingValue <string>("BridgeAddress"));
            hueClient = new HueClient(PackageHost.GetSettingValue <string>("BridgeAddress"));
            hueClient.Initialize(PackageHost.GetSettingValue <string>("BridgeUsername"));

            PackageHost.WriteInfo("Getting configuration");
            var bridge = hueClient.GetBridgeAsync().GetAwaiter().GetResult();

            PackageHost.PushStateObject <BridgeConfig>("BridgeConfig", bridge.Config);
            foreach (Light light in bridge.Lights)
            {
                PackageHost.PushStateObject <Light>(light.Name, light);
            }

            PackageHost.WriteInfo("Starting query process");
            Task.Factory.StartNew(() =>
            {
                int pause = 1000;
                while (PackageHost.IsRunning)
                {
                    try
                    {
                        var lights = hueClient.GetLightsAsync().GetAwaiter().GetResult();
                        foreach (Light light in lights)
                        {
                            PackageHost.PushStateObject <Light>(light.Name, light);
                        }
                        pause = 1000;
                    }
                    catch (Exception ex)
                    {
                        if (pause < 60000)
                        {
                            pause *= 2;
                        }
                        PackageHost.WriteError("Error to query lights : {0}", ex.ToString());
                    }
                    Thread.Sleep(pause);
                }
            });

            PackageHost.WriteInfo("Connected to {0}", hueClient.ApiBase);
        }
Exemplo n.º 5
0
        private static async Task PerformDayNightCycling(HueClient hueClient, int colorTemperature, DateTimeOffset currentTime, AppState appState, Configuration configuration)
        {
            var allLights = await hueClient.GetLightsAsync();

            var lights = allLights.ToList();

            var idToLights = allLights.ToDictionary(x => x.Id, x => x);

            var nameToLights            = allLights.ToDictionary(x => x.Name, x => x);
            var namesOfLightsToExclude  = configuration.NamesOfLightsToExclude.ToHashSet();
            var lightsAvailableToChange = allLights.Where(x => x.State.On && x.State.ColorTemperature != colorTemperature && !namesOfLightsToExclude.Contains(x.Name));

            var lightsToChange = new List <Light>();

            foreach (var light in lights)
            {
                if (!appState.Lights.TryGetValue(light.Id, out var state))
                {
                    state = appState.Lights[light.Id] = new LightControlStatus(DateTimeOffset.MinValue, currentTime, DateTimeOffset.MinValue, DateTimeOffset.MinValue, TimeSpan.Zero, LightControlState.Off, light);
                }
            }

            var sunriseAndSunset = GetSunriseAndSunset(currentTime, configuration);

            var shouldDoSlowTransitionAtSunriseOrSunset = (appState.LastRunTime <sunriseAndSunset.sunrise && currentTime> sunriseAndSunset.sunrise) || (appState.LastRunTime <sunriseAndSunset.sunset && currentTime> sunriseAndSunset.sunset);

            var transitionTimeForBatch = configuration.TransitionTime;

            if (shouldDoSlowTransitionAtSunriseOrSunset)
            {
                Console.WriteLine($"{DateTime.Now}: all lights -> { colorTemperature }.  sunrise: {sunriseAndSunset.sunrise} sunset: {sunriseAndSunset.sunrise}");
                Console.WriteLine();

                transitionTimeForBatch = configuration.TransitionTimeAtSunriseAndSunset;
                foreach (var light in lights)
                {
                    var state = appState.Lights[light.Id];
                    if (!light.State.On)
                    {
                        state.LightControlState           = LightControlState.Off;
                        state.HueShiftTookControlTime     = DateTimeOffset.MinValue;
                        state.TransitionRequestedTime     = DateTimeOffset.MinValue;
                        state.RequestedTransitionDuration = TimeSpan.Zero;
                        state.DetectedOnTime = DateTimeOffset.MinValue;
                    }
                    else
                    {
                        lightsToChange.Add(light);
                        state.LightControlState           = LightControlState.Transitioning;
                        state.HueShiftTookControlTime     = currentTime;
                        state.TransitionRequestedTime     = currentTime;
                        state.RequestedTransitionDuration = configuration.TransitionTimeAtSunriseAndSunset;
                        state.DetectedOnTime = currentTime;
                    }
                }
            }
            else
            {
                foreach (var light in lights)
                {
                    var state = appState.Lights[light.Id];

                    if (!light.State.On)
                    {
                        state.LightControlState           = LightControlState.Off;
                        state.HueShiftTookControlTime     = DateTimeOffset.MinValue;
                        state.TransitionRequestedTime     = DateTimeOffset.MinValue;
                        state.RequestedTransitionDuration = TimeSpan.Zero;
                        state.DetectedOnTime = DateTimeOffset.MinValue;
                    }
                    else
                    {
                        switch (state.LightControlState)
                        {
                        case LightControlState.Off:
                            lightsToChange.Add(light);
                            state.LightControlState           = LightControlState.Transitioning;
                            state.HueShiftTookControlTime     = currentTime;
                            state.TransitionRequestedTime     = currentTime;
                            state.RequestedTransitionDuration = configuration.TransitionTime;
                            state.DetectedOnTime = currentTime;
                            break;

                        case LightControlState.Transitioning:
                            TimeSpan transitionTimeElapsed = currentTime - state.TransitionRequestedTime;
                            var      allowedTolerance      = TimeSpan.FromSeconds(10);

                            bool hasTransitionTimeElapsed = transitionTimeElapsed > (state.RequestedTransitionDuration + allowedTolerance);

                            if ((light.State.ColorTemperature == colorTemperature) || hasTransitionTimeElapsed)
                            {
                                state.LightControlState           = LightControlState.HueShiftAutomated;
                                state.TransitionRequestedTime     = DateTimeOffset.MinValue;
                                state.RequestedTransitionDuration = TimeSpan.Zero;
                            }
                            break;

                        case LightControlState.HueShiftAutomated:
                            if (light.State.ColorTemperature != colorTemperature)
                            {
                                state.LightControlState = LightControlState.ManualControl;
                            }
                            break;

                        case LightControlState.ManualControl:
                            break;
                        }
                    }
                }
            }

            appState.LastRunTime = currentTime;
            foreach (var onLight in lightsToChange)
            {
                Console.WriteLine($"{DateTime.Now}: Light { onLight.Name } switching { onLight.State.ColorTemperature } -> { colorTemperature }");
            }

            if (lightsToChange.Count > 0)
            {
                var command = new LightCommand();
                command.ColorTemperature = colorTemperature;
                command.TransitionTime   = transitionTimeForBatch;

                await hueClient.SendCommandAsync(command, lightsToChange.Select(x => x.Id).ToList());
            }
        }
Exemplo n.º 6
0
        private async void GetLightsAction()
        {
            var result = await _hueClient.GetLightsAsync();

            GetLights = string.Format("Found {0} lights", result.Count());
        }