From 10d936eef0639c3d30891c3f1db20f0ea512171a Mon Sep 17 00:00:00 2001 From: Yorick Rommers Date: Thu, 3 Dec 2015 12:41:17 +0100 Subject: [PATCH] disco added --- HueUWP/APIHandler.cs | 34 ++++++++++++++++++++++++++++++++-- HueUWP/Light.cs | 5 +++++ HueUWP/MainPage.xaml | 1 + HueUWP/MainPage.xaml.cs | 5 +++++ 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/HueUWP/APIHandler.cs b/HueUWP/APIHandler.cs index b0da5d8..f18f9bf 100755 --- a/HueUWP/APIHandler.cs +++ b/HueUWP/APIHandler.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Threading; using Windows.Storage; namespace HueUWP @@ -49,6 +50,23 @@ namespace HueUWP lights.ForEach(l => SetLightState(l)); } + public async void DiscoMode(ObservableCollection lights) + { + Random rnd = new Random(); + for(int i = 0; i < 100; i++) + { + lights.ToList().ForEach(l => + { + l.Hue = rnd.Next(0, 65535); + l.Brightness = rnd.Next(128, 254); + l.UpdateColorDisco(); + }); + //"transitiontime": 0 + await Task.Delay(TimeSpan.FromMilliseconds(100)); + } + + } + public async Task SetLightValues(Light l) { if(l.IsOn) @@ -61,6 +79,18 @@ namespace HueUWP return "error"; } + public async Task SetInstantLightValues(Light l) + { + if (l.IsOn) + { + Debug.WriteLine(l.Hue); + var json = await nwh.SetLightInfo(l.ID, $"{{\"bri\": {l.Brightness},\"hue\": {(l.Hue)},\"sat\": {l.Saturation}, \"transitiontime\" : 0}}"); + Debug.WriteLine(json); + return "success"; + } + + return "error"; + } public async void SetLightValues(List lights) { @@ -84,8 +114,8 @@ namespace HueUWP } else { - alllights.Add(new Light() { api = this, ID = Int32.Parse(i.Key), Brightness = (int)state["bri"], SaturationEnabled = false,HueEnabled = false,IsOn = ((string)state["on"]).ToLower() == "true" ? true : false, Hue =-1, Saturation = 254, Name = (string)light["name"], Type = (string)light["type"] }); - }//Debug.WriteLine("Added light number " + i + " " + state["on"]); + alllights.Add(new Light() { api = this, ID = Int32.Parse(i.Key), Brightness = (int)state["bri"], SaturationEnabled = false, HueEnabled = false, IsOn = ((string)state["on"]).ToLower() == "true" ? true : false, Hue = -1, Saturation = 254, Name = (string)light["name"], Type = (string)light["type"] }); + } } return "success"; } diff --git a/HueUWP/Light.cs b/HueUWP/Light.cs index ca1ce12..3911676 100644 --- a/HueUWP/Light.cs +++ b/HueUWP/Light.cs @@ -76,6 +76,11 @@ namespace HueUWP api.SetLightValues(this); } + public void UpdateColorDisco() + { + api.SetInstantLightValues(this); + } + public void UpdateColor(int h, int s, int b) { //double h; double s; double v; diff --git a/HueUWP/MainPage.xaml b/HueUWP/MainPage.xaml index 705dbd2..67e4769 100644 --- a/HueUWP/MainPage.xaml +++ b/HueUWP/MainPage.xaml @@ -61,6 +61,7 @@ + diff --git a/HueUWP/MainPage.xaml.cs b/HueUWP/MainPage.xaml.cs index bfa0287..315e90f 100644 --- a/HueUWP/MainPage.xaml.cs +++ b/HueUWP/MainPage.xaml.cs @@ -118,5 +118,10 @@ namespace HueUWP Frame rootframe = Window.Current.Content as Frame; rootframe.Navigate(typeof(AboutView)); } + + private void AppBarButton_Click(object sender, RoutedEventArgs e) + { + api.DiscoMode(_lightsViewModel); + } } }