From f615d01b821d26c7784b7584ed718645bacfa87c Mon Sep 17 00:00:00 2001 From: Yorick Rommers Date: Mon, 30 Nov 2015 13:20:33 +0100 Subject: [PATCH] added slider for testing --- HueUWP/APIHandler.cs | 17 +++++++++++++---- HueUWP/Light.cs | 26 +++++++++++--------------- HueUWP/MainPage.xaml | 6 ++++-- HueUWP/MainPage.xaml.cs | 9 +++++++-- HueUWP/NetworkHandler.cs | 2 +- 5 files changed, 36 insertions(+), 24 deletions(-) diff --git a/HueUWP/APIHandler.cs b/HueUWP/APIHandler.cs index 48362f9..9ece594 100755 --- a/HueUWP/APIHandler.cs +++ b/HueUWP/APIHandler.cs @@ -34,13 +34,22 @@ namespace HueUWP } } - public async void SetLightData(Light l) + public async void SetLightState(Light l) { - - var json = await nwh.ToggleLight(l.ID, $"{{\"on\": {((l.On) ? "true" : "false")},\"bri\": {l.Brightness},\"hue\": { l.Hue},\"sat\": {l.Saturation}}}"); + var json = await nwh.SetLightInfo(l.ID, $"{{\"on\": {((l.IsOn) ? "true" : "false")}}}"); Debug.WriteLine(json); } + public async void SetLightValues(Light l) + { + if(l.IsOn) + { + var json = await nwh.SetLightInfo(l.ID, $"{{\"bri\": {l.Brightness},\"hue\": {l.Hue},\"sat\": {l.Saturation}}}"); + Debug.WriteLine(json); + } + + } + public async void GetAllLights(ObservableCollection alllights) { List lightlist = new List(); @@ -54,7 +63,7 @@ namespace HueUWP { var light = o["" + i]; var state = light["state"]; - alllights.Add(new Light() { api = this,ID = i, Brightness = (int)state["bri"], On = (bool)state["on"], Hue = (int)state["hue"], Saturation = (int)state["sat"], Name = (string)light["name"], Type = (string)light["type"] }); + alllights.Add(new Light() { api = this,ID = i, Brightness = (int)state["bri"], IsOn = (string)state["on"] == "true" ? true : false, Hue = (int)state["hue"], Saturation = (int)state["sat"], Name = (string)light["name"], Type = (string)light["type"] }); Debug.WriteLine("Added light number " + i); } } catch(Exception e) diff --git a/HueUWP/Light.cs b/HueUWP/Light.cs index 93caedc..1000795 100644 --- a/HueUWP/Light.cs +++ b/HueUWP/Light.cs @@ -16,36 +16,32 @@ namespace HueUWP public string Name { get; set; } public string Type { get; set; } - public bool On { get; set; } - public int Brightness{ get; set; } - public int Hue { get; set; } - public int Saturation { get; set; } + public bool IsOn { get; set; } + public double Brightness{ get; set; } + public double Hue { get; set; } + public double Saturation { get; set; } public APIHandler api { get; set; } public Light() - { - //temp - ID = 1; - Name = "Lamp 1"; - Type = "Extended color light"; - On = true; - Brightness = 10; - Hue = 14215; - Saturation = 425; + { } public void UpdateState(bool on) { NotifyPropertyChanged(nameof(UpdateState)); - this.On = on; - api.SetLightData(this); + this.IsOn = on; + api.SetLightState(this); } public void UpdateColor(int r, int g, int b) { NotifyPropertyChanged(nameof(UpdateColor)); + double h; double s; double v; + ColorUtil.RGBtoHSV(r,g,b, out h,out s,out v); + this.Hue = h; this.Saturation = s; this.Brightness = v; + api.SetLightValues(this); Debug.WriteLine(r + "-" + g + "-" + b); } diff --git a/HueUWP/MainPage.xaml b/HueUWP/MainPage.xaml index 6a44e7e..0d9560a 100644 --- a/HueUWP/MainPage.xaml +++ b/HueUWP/MainPage.xaml @@ -22,17 +22,19 @@ - + Off On + - + + diff --git a/HueUWP/MainPage.xaml.cs b/HueUWP/MainPage.xaml.cs index d1df858..8eba8cd 100644 --- a/HueUWP/MainPage.xaml.cs +++ b/HueUWP/MainPage.xaml.cs @@ -70,11 +70,16 @@ namespace HueUWP private void ToggleSwitch_Toggled(object sender, RoutedEventArgs e) { - Debug.WriteLine("Hello again"); ToggleSwitch button = ((ToggleSwitch)sender); Light light = (Light)button.DataContext; - Debug.WriteLine("Hello"); light.UpdateState(button.IsOn); } + + private void slider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) + { + Slider slider = ((Slider)sender); + Light l = (Light)slider.DataContext; + l.UpdateColor((int)slider.Value, 255, 255); + } } } diff --git a/HueUWP/NetworkHandler.cs b/HueUWP/NetworkHandler.cs index 567d2e7..c0a45bf 100755 --- a/HueUWP/NetworkHandler.cs +++ b/HueUWP/NetworkHandler.cs @@ -115,7 +115,7 @@ namespace HueUWP } } - public async Task ToggleLight(int lightid, string json) + public async Task SetLightInfo(int lightid, string json) { var response = await Put($"{(String)MainPage.LOCAL_SETTINGS.Values["id"]}/lights/{lightid}/state", json); return response;