public void CreateOnOffLightStateUpdateFromLightState_StateNull()
        {
            IHueLightStateUpdateFactory lightStateUpdateFactory = new HueLightStateUpdateFactory();
            LightStateUpdate            lightStateUpdate        = null;

            var result = lightStateUpdateFactory.CreateOnOffLightStateUpdateFromLightState(lightStateUpdate);

            Assert.Null(result);
        }
        public void CreateFromLightState_StateNull()
        {
            IHueLightStateUpdateFactory lightStateUpdateFactory = new HueLightStateUpdateFactory();
            LightStateUpdate            lightStateUpdate        = null;

            var result = lightStateUpdateFactory.CreateFromLightState(LightType.HueDimmableLight, lightStateUpdate);

            Assert.Null(result);
        }
        public async Task <IActionResult> SetLightState(int id, [FromBody] LightStateUpdate stateUpdate)
        {
            bool success = await hueLightProvider.SetLightStateAsync(id, stateUpdate);

            if (success)
            {
                return(Ok());
            }
            else
            {
                return(NotFound());
            }
        }
        /// <summary>
        /// Creates a new instance of HueLightStateUpdate from the given LightStateUpdate object
        /// </summary>
        /// <param name="lightType">type of the light</param>
        /// <param name="stateUpdate">light state update</param>
        /// <returns>new instance of HueLightStateUpdate</returns>
        public HueLightStateUpdate CreateFromLightState(LightType lightType, LightStateUpdate stateUpdate)
        {
            if (stateUpdate == null)
            {
                return(null);
            }

            return(lightType switch
            {
                LightType.HueDimmableLight => CreateDimmableLightStateUpdate(stateUpdate),
                LightType.HueExtendedColorLight => CreateExtendedColorLightStateUpdate(stateUpdate),
                LightType.HueColorTemperatureLight => CreateColorTemperatureLightStateUpdate(stateUpdate),
                _ => null
            });
        public void CreateOnOffLightStateUpdateFromLightState_ColorTemperature()
        {
            IHueLightStateUpdateFactory lightStateUpdateFactory = new HueLightStateUpdateFactory();
            var lightStateUpdate = new LightStateUpdate()
            {
                On               = true,
                Brightness       = 90,
                ColorTemperature = 55,
                Saturation       = 27,
                Hue              = 8000
            };

            var result = lightStateUpdateFactory.CreateOnOffLightStateUpdateFromLightState(lightStateUpdate);

            Assert.True(result.On);
        }
        public void CreateFromLightState_Dimmable()
        {
            IHueLightStateUpdateFactory lightStateUpdateFactory = new HueLightStateUpdateFactory();

            var lightStateUpdate = new LightStateUpdate()
            {
                On               = true,
                Brightness       = 90,
                ColorTemperature = 55,
                Saturation       = 27,
                Hue              = 8000
            };

            var result = lightStateUpdateFactory.CreateFromLightState(LightType.HueDimmableLight, lightStateUpdate) as HueLightStateUpdateDimmable;

            Assert.True(result.On);
            Assert.Equal(90, result.Brightness);
        }
Esempio n. 7
0
        public async Task <bool> SetLightStateAsync(int lightId, LightStateUpdate stateUpdate)
        {
            string url = $"{apiUrl}/lights/{lightId}/state";

            bool success = false;

            try
            {
                var light = await GetLightByIdAsync(lightId);

                var httpClient = clientFactory.CreateClient();

                // first send only on/off state to Hue bridge, because otherwise Hue will respond with error:
                var hueOnOffLightStateUpdate = lightStateUpdateFactory.CreateOnOffLightStateUpdateFromLightState(stateUpdate);
                var jsonOnOff = JsonConvert.SerializeObject(hueOnOffLightStateUpdate);
                await httpClient.PutAsync(url, new StringContent(jsonOnOff));

                // only proceed if light gets switched on, because otherwise Hue will respond with error:
                if (hueOnOffLightStateUpdate.On)
                {
                    var hueLightStateUpdate = lightStateUpdateFactory.CreateFromLightState(light.Type, stateUpdate);
                    var json     = JsonConvert.SerializeObject(hueLightStateUpdate);
                    var response = await httpClient.PutAsync(url, new StringContent(json));

                    var responseJson = await response.Content.ReadAsStringAsync();

                    var updateResults = JsonConvert.DeserializeObject <HueLightStateUpdateResult[]>(responseJson);

                    success = (updateResults.Length > 0) ? (updateResults[0].Success != null) : false;
                }
                else
                {
                    success = true;
                }
            }
            catch (Exception)
            {
            }

            return(success);
        }
        public void CreateFromLightState_ExtendedColor()
        {
            IHueLightStateUpdateFactory lightStateUpdateFactory = new HueLightStateUpdateFactory();

            var lightStateUpdate = new LightStateUpdate()
            {
                On               = true,
                Brightness       = 90,
                ColorTemperature = 55,
                Saturation       = 27,
                Hue              = 8000
            };

            var result = lightStateUpdateFactory.CreateFromLightState(LightType.HueExtendedColorLight, lightStateUpdate) as HueLightStateUpdateExtendedColor;

            Assert.True(result.On);
            Assert.Equal(90, result.Brightness);
            Assert.Equal(55, result.ColorTemperature);
            Assert.Equal(27, result.Saturation);
            Assert.Equal(8000, result.Hue);
        }
        public async Task SetLightState_NoSuccess()
        {
            var hueLightProvider = Substitute.For <IHueLightProvider>();
            var lightStateUpdate = new LightStateUpdate()
            {
                On               = true,
                Brightness       = 28,
                Saturation       = 190,
                Hue              = 8000,
                ColorTemperature = 103
            };
            bool success = false;

            hueLightProvider.SetLightStateAsync(Arg.Any <int>(), Arg.Any <LightStateUpdate>()).Returns(Task.FromResult(success));
            var controller = new LightingController(hueLightProvider, null);

            var result = await controller.SetLightState(5, lightStateUpdate);

            var notFoundResult = result as NotFoundResult;

            Assert.NotNull(notFoundResult);
        }