added lgihts to gui

This commit is contained in:
Yorick Rommers
2015-11-27 15:03:50 +01:00
parent 2e381c8073
commit ccd5f25f9e
4 changed files with 40 additions and 20 deletions
+21 -7
View File
@@ -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<Light> alllights)
{
List<Light> lightlist = new List<Light>();
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);
}
}
}
}
+7 -11
View File
@@ -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<Light> GetLights()
{
if (_lights.Count == 0)
{
_lights.Add(new Light());
}
return _lights;
}
}
+2
View File
@@ -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"]);
}
+10 -2
View File
@@ -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<String> 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<String> 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;