From ccd5f25f9ebde1beb49a7ccb11102162280b78cb Mon Sep 17 00:00:00 2001 From: Yorick Rommers Date: Fri, 27 Nov 2015 15:03:50 +0100 Subject: [PATCH] added lgihts to gui --- HueUWP/APIHandler.cs | 28 +++++++++++++++++++++------- HueUWP/Light.cs | 18 +++++++----------- HueUWP/MainPage.xaml.cs | 2 ++ HueUWP/NetworkHandler.cs | 12 ++++++++++-- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/HueUWP/APIHandler.cs b/HueUWP/APIHandler.cs index b037081..915a0af 100755 --- a/HueUWP/APIHandler.cs +++ b/HueUWP/APIHandler.cs @@ -1,6 +1,7 @@ using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Diagnostics; using System.Linq; using System.Text; @@ -19,18 +20,31 @@ namespace HueUWP this.nwh = nwh; } - private JObject ObjectifyfJson(string json) - { - json = json.Replace("[", "").Replace("]", ""); - return JObject.Parse(json); - } - public async void Register() { var json = await nwh.RegisterName("Hue", "Kenneth&Yorick"); - JObject o = ObjectifyfJson(json); + json = json.Replace("[", "").Replace("]", ""); + JObject o = JObject.Parse(json); string id= o["success"]["username"].ToString(); MainPage.LOCAL_SETTINGS.Values["id"] = id; } + + public async void GetAllLights(ObservableCollection alllights) + { + List lightlist = new List(); + + var json = await nwh.AllLights(); + Debug.WriteLine(json); + JObject o = JObject.Parse(json); + Debug.WriteLine(o.ToString()); + + for(int i = 1; i <= o.Count; i++) + { + var light = o["" + i]; + var state = light["state"]; + alllights.Add(new Light() { 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"]}); + Debug.WriteLine("Added light number " + i); + } + } } } diff --git a/HueUWP/Light.cs b/HueUWP/Light.cs index ea6a1e8..28b9c26 100644 --- a/HueUWP/Light.cs +++ b/HueUWP/Light.cs @@ -12,14 +12,14 @@ namespace HueUWP { public class Light : INotifyPropertyChanged { - public int ID { get; private set; } - public string Name { get; private set; } - public string Type { get; private set; } + public int ID { get; set; } + public string Name { get; set; } + public string Type { get; set; } - public bool On { get; private set; } - public int Brightness{ get; private set; } - public int Hue { get; private set; } - public int Saturation { get; private set; } + public bool On { get; set; } + public int Brightness{ get; set; } + public int Hue { get; set; } + public int Saturation { get; set; } public Light() { @@ -64,10 +64,6 @@ namespace HueUWP public static ObservableCollection GetLights() { - if (_lights.Count == 0) - { - _lights.Add(new Light()); - } return _lights; } } diff --git a/HueUWP/MainPage.xaml.cs b/HueUWP/MainPage.xaml.cs index 2ec8a73..a6e5ab5 100644 --- a/HueUWP/MainPage.xaml.cs +++ b/HueUWP/MainPage.xaml.cs @@ -45,6 +45,8 @@ namespace HueUWP NetworkHandler nwh = new NetworkHandler(); APIHandler api = new APIHandler(nwh); //api.Register(); + api.GetAllLights(_lightsViewModel); + Debug.WriteLine(LOCAL_SETTINGS.Values["id"]); } diff --git a/HueUWP/NetworkHandler.cs b/HueUWP/NetworkHandler.cs index 0bd041c..c59fb8a 100755 --- a/HueUWP/NetworkHandler.cs +++ b/HueUWP/NetworkHandler.cs @@ -92,7 +92,7 @@ namespace HueUWP HttpClient client = new HttpClient(); //HttpStringContent content = new HttpStringContent($"{{\"devicetype\":\"Test#Test\"}}", Windows.Storage.Streams.UnicodeEncoding.Utf8, "application /json"); - Uri uriLampState = new Uri($"http://{ip}:{port}/api" + path); + Uri uriLampState = new Uri($"http://{ip}:{port}/api/" + path); var response = await client.GetAsync(uriLampState).AsTask(cts.Token); if (!response.IsSuccessStatusCode) @@ -122,9 +122,17 @@ namespace HueUWP return response; } + public async Task AllLights() + { + var response = await Get($"{(String)MainPage.LOCAL_SETTINGS.Values["id"]}/lights"); + if (string.IsNullOrEmpty(response)) + await new MessageDialog("Error while getting all liights. ….").ShowAsync(); + return response; + } + public async Task Test() { - var response = await Get( "/111bb033202ac68b5812245c22f77eb/lights"); + var response = await Get( "111bb033202ac68b5812245c22f77eb/lights"); if (string.IsNullOrEmpty(response)) await new MessageDialog("Error while setting username. ….").ShowAsync(); return response;