Пример #1
0
        public async Task ModifySceneLightState()
        {
            IHueRequest request = new ModifySceneRequest(new ModifySceneParameters
            {
                SceneId          = _sceneId,
                LightIds         = new[] { 7 },
                UseCurrentStatus = true
            });

            _client.GetResponseAsync(request).Wait();

            request = new ModifySceneLightStateRequest(_sceneId, 7)
            {
                LightState = new LightState
                {
                    IsOn   = true,
                    Effect = LightEffect.ColorLoop
                }
            };

            var response = await _client.GetResponseAsync(request);

            Assert.True(response is SuccessResponse);
            OnLog(response);
        }
Пример #2
0
        public async Task ModifySceneTest()
        {
            IHueRequest request = new ModifySceneRequest(new ModifySceneParameters
            {
                SceneId          = _sceneId,
                Name             = "new Name",
                LightIds         = new [] { 7 },
                UseCurrentStatus = true
            });

            var response = await _client.GetResponseAsync(request);

            Assert.True(response is SuccessResponse);
            OnLog(response);
        }
Пример #3
0
        private JsonResult ModifySceneProperty(ModifySceneRequest newScene, Scene scene)
        {
            scene.Name            = newScene.Name ?? scene.Name;
            scene.Lights          = newScene.Lights ?? scene.Lights;
            scene.StoreLightState = newScene.StoreLightState ?? scene.StoreLightState;
            var scenes = _grp.DatabaseInstance.GetCollection <Scene>("scenes");

            scenes.Update(scene);
            return(Json(new List <Dictionary <string, object> >
            {
                new Dictionary <string, object>
                {
                    ["success"] = new Dictionary <string, string>
                    {
                        [$"/scenes/{scene.Id}/name"] = $"{newScene.Name}",
                        [$"/scenes/{scene.Id}/lights"] = $"{newScene.Lights}",
                        [$"/scenes/{scene.Id}/storelightstate"] = $"{newScene.StoreLightState}"
                    }
                }
            }));
        }
Пример #4
0
        public JsonResult ModifyScene(string user, [FromBody] ModifySceneRequest newScene, string sceneId, string lightId)
        {
            // authentication
            if (!_grp.AuthenticatorInstance.IsValidUser(user))
            {
                return(Json(_grp.AuthenticatorInstance.ErrorResponse(Request.Path.ToString())));
            }

            // caller want to modify scene name/lights/storelightstate only
            var scenes = _grp.DatabaseInstance.GetCollection <Scene>("scenes");
            var scene  = scenes.FindById(Convert.ToInt64(sceneId));

            if (scene == null)
            {
                return(Json(new List <Dictionary <string, object> >
                {
                    new Dictionary <string, object>
                    {
                        ["failure"] = "scene not exist"
                    }
                }));
            }

            if (newScene.Name != null || newScene.Lights != null || newScene.StoreLightState != null)
            {
                return(ModifySceneProperty(newScene, scene));
            }
            // caller want to modify light state
            else
            {
                // check if lightId is valid
                if (scene.Lights.Count(x => x == lightId) > 0)
                {
                    if (scene.LightStates.Count(x => x.Key == lightId) == 0)
                    {
                        scene.LightStates[lightId] = new GroupAction();
                    }
                    var pp         = typeof(GroupAction).GetProperties().ToList();
                    var lightstate = scene.LightStates[lightId];

                    foreach (var p in typeof(ModifySceneRequest).GetProperties())
                    {
                        switch (p.Name)
                        {
                        case "Name":
                        case "Lights":
                        case "StoreLightState":
                            break;

                        case "XY":     // have to make a special case for nullable array
                            if (newScene.XY != null)
                            {
                                lightstate.XY[0] = newScene.XY[0] ?? lightstate.XY[0];
                                lightstate.XY[1] = newScene.XY[1] ?? lightstate.XY[1];
                            }
                            break;

                        default:
                            var pv = p.GetValue(newScene, null);
                            if (pv != null)
                            {
                                pp.Find(x => x.Name == p.Name).SetValue(lightstate, pv);
                            }
                            break;
                        }
                    }
                    scenes.Update(scene);
                }
            }

            return(Json(new List <Dictionary <string, object> >
            {
                new Dictionary <string, object>
                {
                    ["success"] = new Dictionary <string, string>
                    {
                        ["id"] = $"0"
                    }
                }
            }));
        }