Toggle light working

This commit is contained in:
Yorick Rommers
2015-11-30 11:40:17 +01:00
parent ccd5f25f9e
commit 7c3a84d4cf
7 changed files with 260 additions and 27 deletions
+32 -16
View File
@@ -10,7 +10,7 @@ using Windows.Storage;
namespace HueUWP
{
class APIHandler
public class APIHandler
{
@@ -22,28 +22,44 @@ namespace HueUWP
public async void Register()
{
var json = await nwh.RegisterName("Hue", "Kenneth&Yorick");
json = json.Replace("[", "").Replace("]", "");
JObject o = JObject.Parse(json);
string id= o["success"]["username"].ToString();
MainPage.LOCAL_SETTINGS.Values["id"] = id;
try {
var json = await nwh.RegisterName("Hue", "Kenneth&Yorick");
json = json.Replace("[", "").Replace("]", "");
JObject o = JObject.Parse(json);
string id = o["success"]["username"].ToString();
MainPage.LOCAL_SETTINGS.Values["id"] = id; }
catch(Exception e)
{
Debug.WriteLine("Could not register.");
}
}
public async void SetLightData(Light l)
{
var json = await nwh.ToggleLight(l.ID, $"{{\"on\": {((l.On) ? "true" : "false")},\"bri\": {l.Brightness},\"hue\": { l.Hue},\"sat\": {l.Saturation}}}");
Debug.WriteLine(json);
}
public async void GetAllLights(ObservableCollection<Light> alllights)
{
List<Light> lightlist = new List<Light>();
try {
var json = await nwh.AllLights();
Debug.WriteLine(json);
JObject o = JObject.Parse(json);
Debug.WriteLine(o.ToString());
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++)
for (int i = 1; i <= o.Count; i++)
{
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"] });
Debug.WriteLine("Added light number " + i);
} }
catch(Exception e)
{
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);
Debug.WriteLine("Could not get all lights.");
}
}
}