Exemplo n.º 1
0
        public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false)
        {
            IsInitialized = false;

            try
            {
                UpdateTrigger.Stop();

                IList <IRGBDevice> devices = new List <IRGBDevice>();
                UpdateTrigger.ClientGroups = new Dictionary <StreamingHueClient, StreamingGroup>();

                foreach (HueClientDefinition clientDefinition in ClientDefinitions)
                {
                    // Create a temporary for this definition
                    ILocalHueClient client = new LocalHueClient(clientDefinition.Ip);
                    client.Initialize(clientDefinition.AppKey);

                    // Get the entertainment groups, no point continuing without any entertainment groups
                    IReadOnlyList <Group> entertainmentGroups = client.GetEntertainmentGroups().GetAwaiter().GetResult();
                    if (!entertainmentGroups.Any())
                    {
                        continue;
                    }

                    // Get all lights once, all devices can use this list to identify themselves
                    List <Light> lights = client.GetLightsAsync().GetAwaiter().GetResult().ToList();

                    foreach (Group entertainmentGroup in entertainmentGroups.OrderBy(g => int.Parse(g.Id)))
                    {
                        StreamingHueClient streamingClient = new StreamingHueClient(clientDefinition.Ip, clientDefinition.AppKey, clientDefinition.ClientKey);
                        StreamingGroup     streamingGroup  = new StreamingGroup(entertainmentGroup.Locations);
                        streamingClient.Connect(entertainmentGroup.Id).GetAwaiter().GetResult();

                        UpdateTrigger.ClientGroups.Add(streamingClient, streamingGroup);
                        foreach (string lightId in entertainmentGroup.Lights.OrderBy(int.Parse))
                        {
                            HueDeviceInfo deviceInfo = new HueDeviceInfo(entertainmentGroup, lightId, lights);
                            HueDevice     device     = new HueDevice(deviceInfo);
                            device.Initialize(new HueUpdateQueue(UpdateTrigger, lightId, streamingGroup));
                            devices.Add(device);
                        }
                    }
                }

                UpdateTrigger.Start();
                Devices       = new ReadOnlyCollection <IRGBDevice>(devices);
                IsInitialized = true;
            }
            catch
            {
                if (throwExceptions)
                {
                    throw;
                }
                return(false);
            }

            return(true);
        }
        public async Task SwitchDeviceAsync(HueDevice device)
        {
            var hueDefinition = _hueDefinitions.Single(hd => hd.HueBridge.Name == device.BridgeName);

            if (device.IsGroup)
            {
                await SwitchGroupAsync(device, hueDefinition);
            }
            else
            {
                await SwitchLightAsync(device, hueDefinition);
            }
        }
        private async Task SwitchLightAsync(HueDevice device, HueDefinition hueDefinition)
        {
            var light = await hueDefinition.HueClient.GetLightAsync(device.Id.ToString());

            if (light == null)
            {
                throw new InvalidOperationException(
                          $"Light with id [{device.Id}] on bridge [{device.BridgeName}] with host [{hueDefinition.HueBridge.Host}] does not exists.");
            }

            var cmd = new LightCommand()
            {
                On = !light.State.On
            };

            _log.Information("Switching state {on} to {@device}", cmd.On, device);
            await hueDefinition.HueClient.SendCommandAsync(cmd, new[] { device.Id.ToString() });
        }
        public async Task TurnDeviceOffAsync(HueDevice device)
        {
            var hueDefinition = _hueDefinitions.Single(hd => hd.HueBridge.Name == device.BridgeName);
            var cmd           = new LightCommand()
            {
                On = false
            };

            _log.Information("Turning off {@device}", device);
            if (device.IsGroup)
            {
                await hueDefinition.HueClient.SendGroupCommandAsync(cmd, device.Id.ToString());
            }
            else
            {
                await hueDefinition.HueClient.SendCommandAsync(cmd, new[] { device.Id.ToString() });
            }
        }
Exemplo n.º 5
0
 public TurnOnDevice(HueDevice hueDevice)
 {
     HueDevice = hueDevice;
 }
 public SwitchDevice(HueDevice hueDevice)
 {
     HueDevice = hueDevice;
 }