Ejemplo n.º 1
0
        private HueSelectedSceneEntity GetExistingSceneEntity(string groupId, HueSelectedSceneContext context)
        {
            var query = from item in context.HueSelectedScenes
                        where item.HueId == bridge.Config.BridgeId && item.RoomId == groupId
                        select item;

            if (query.Count() > 1)
            {
                context.RemoveRange(query);
            }

            return(query.FirstOrDefault());
        }
Ejemplo n.º 2
0
        public async Task SendCommandAsync(Q42.HueApi.Models.Groups.Group group)
        {
            var command = new LightCommand();

            command.On         = group.Action.On;
            command.Brightness = group.Action.Brightness;

            await client.SendGroupCommandAsync(command, group.Id);

            // Clear current scene
            using (var context = new HueSelectedSceneContext())
            {
                HueSelectedSceneEntity exist = GetExistingSceneEntity(group.Id, context);
                if (exist != null)
                {
                    context.Remove(exist);
                    await context.SaveChangesAsync();
                }
            }
        }
Ejemplo n.º 3
0
        public async Task SendCommandAsync(Q42.HueApi.Models.Scene scene)
        {
            await client.RecallSceneAsync(scene.Id, scene.Group);

            // get status after update
            await Task.Delay(300);

            var lights = new List <Models.Hue.JsonLight>();

            foreach (var light in scene.Lights)
            {
                var lightItem = await client.GetLightAsync(light);

                lights.Add(new Models.Hue.JsonLight(lightItem));
            }

            var lightStateJson = JsonSerializer.Serialize(lights);

            // Save current scene
            using (var context = new HueSelectedSceneContext())
            {
                HueSelectedSceneEntity exist = GetExistingSceneEntity(scene.Group, context);
                if (exist != null)
                {
                    exist.SceneId        = scene.Id;
                    exist.LightStateJson = lightStateJson;
                    context.Update(exist);
                }
                else
                {
                    var entity = new HueSelectedSceneEntity();
                    entity.HueId          = bridge.Config.BridgeId;
                    entity.RoomId         = scene.Group;
                    entity.SceneId        = scene.Id;
                    entity.LightStateJson = lightStateJson;
                    await context.HueSelectedScenes.AddAsync(entity);
                }

                await context.SaveChangesAsync();
            }
        }
Ejemplo n.º 4
0
        public async Task <List <IDevice> > GetHueDevicesAsync()
        {
            var hueDevices = new List <IDevice>();
            var groups     = await client.GetGroupsAsync();

            var rooms = (from item in groups
                         where item.Type == Q42.HueApi.Models.Groups.GroupType.Room
                         select item).ToList();

            var scenes = await client.GetScenesAsync();

            foreach (var room in rooms)
            {
                var hueDevice = new Models.Hue.Group(room);
                var lights    = new List <IAppliance>();
                foreach (var lightId in room.Lights)
                {
                    lights.Add(new Models.Hue.Light(await client.GetLightAsync(lightId), user));
                }
                hueDevice.Appliances = lights;

                if (scenes != null)
                {
                    hueDevice.HueScenes.AddRange(from scene in scenes
                                                 where scene.Type != null &&
                                                 scene.Type == Q42.HueApi.Models.SceneType.GroupScene &&
                                                 scene.Group == room.Id
                                                 select scene);

                    // check if scene matches
                    using (var context = new HueSelectedSceneContext())
                    {
                        await context.Database.EnsureCreatedAsync();

                        HueSelectedSceneEntity exist = GetExistingSceneEntity(room.Id, context);
                        if (exist != null)
                        {
                            var match = true;
                            try
                            {
                                var cachedLights = JsonSerializer.Deserialize <List <Models.Hue.JsonLight> >(exist.LightStateJson);
                                foreach (var cachedLight in cachedLights)
                                {
                                    var light = (from item in hueDevice.Appliances
                                                 where ((Models.Hue.Light)item).HueLight.Id == cachedLight.Id
                                                 select((Models.Hue.Light)item).HueLight).FirstOrDefault();
                                    if (light != null)
                                    {
                                        if (!light.State.CheckEquals(cachedLight.State))
                                        {
                                            match = false;
                                            break;
                                        }
                                    }
                                }
                            }
                            catch (Exception)
                            {
                                match = false;
                            }

                            if (match)
                            {
                                hueDevice.SelectedHueScene = (from item in hueDevice.HueScenes
                                                              where item.Id == exist.SceneId
                                                              select item).FirstOrDefault();
                            }
                            else
                            {
                                context.Remove(exist);
                                await context.SaveChangesAsync();
                            }
                        }
                    }
                }

                hueDevices.Add(hueDevice);
            }

            return(hueDevices);
        }