/// <summary> /// Populates the page with content passed during navigation. Any saved state is also /// provided when recreating a page from a prior session. /// </summary> /// <param name="sender"> /// The source of the event; typically <see cref="NavigationHelper"/>. /// </param> /// <param name="e">Event data that provides both the navigation parameter passed to /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and /// a dictionary of state preserved by this page during an earlier /// session. The state will be null the first time a page is visited.</param> private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e) { this._pageLoaded = false; this._light = e.NavigationParameter as Light; this.DataContext = this._light; if(_light.isGroup) { toggleOn.Header = "Group state: "; } else { toggleOn.Header = "Light state: "; } }
public static async Task<string> GroupColorTask(Light light) { var cts = new CancellationTokenSource(); cts.CancelAfter(5000); try { HttpClient client = new HttpClient(); HttpStringContent content = new HttpStringContent(string.Format("{{ \"hue\": {0}, \"sat\": {1}, \"bri\": {2} }}", light.hue, light.saturation, light.value), Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json"); string ip, username; int port; SettingsService.RetrieveSettings(out ip, out port, out username); var response = await client.PutAsync(new Uri(string.Format("http://{0}:{1}/api/{2}/groups/{3}/action", ip, port, username, light.id)), content).AsTask(cts.Token); if (!response.IsSuccessStatusCode) { return string.Empty; } string jsonResponse = await response.Content.ReadAsStringAsync(); System.Diagnostics.Debug.WriteLine(jsonResponse); return jsonResponse; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); return string.Empty; } }
public static List<Light> ParseGroups(string json) { List<Light> lightGroups = new List<Light>(); JsonObject jsonObject = JsonObject.Parse(json); foreach (string key in jsonObject.Keys) { string lightId = key; if (lightId.Equals("error")) { System.Diagnostics.Debug.WriteLine(jsonObject[key]); continue; } JsonObject groupToAdd; Light g = null; try { groupToAdd = jsonObject.GetNamedObject(key, null); JsonObject lightState = groupToAdd.GetNamedObject("action", null); if (lightState != null) { Light.Effect effect = Light.Effect.EFFECT_NONE; if (lightState.GetNamedString("effect", string.Empty).Equals("colorloop")) effect = Light.Effect.EFFECT_COLORLOOP; g = new Light(Convert.ToInt32(lightId), groupToAdd.GetNamedString("name", string.Empty), lightState.GetNamedBoolean("on", false), Convert.ToInt32(lightState.GetNamedNumber("hue", 0)), Convert.ToInt32(lightState.GetNamedNumber("sat", 255)), Convert.ToInt32(lightState.GetNamedNumber("bri", 255)), effect, lightState.GetNamedBoolean("reachable", false), true); } else g = new Light(Convert.ToInt32(lightId), string.Format("Hue lamp {0}", lightId), true, 20000, 255, 255, Light.Effect.EFFECT_NONE, true, true); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(string.Format("{0} - {1}", e.Message, e.StackTrace)); continue; } if (g != null) lightGroups.Add(g); } return lightGroups; }
public static async Task<string> LightLoopTask(Light light) { var cts = new CancellationTokenSource(); cts.CancelAfter(5000); try { HttpClient client = new HttpClient(); HttpStringContent content = new HttpStringContent(string.Format("{{ \"effect\": \"{0}\" }}", light.effect == Light.Effect.EFFECT_COLORLOOP ? "colorloop" : "none"), Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json"); string ip, username; int port; SettingsService.RetrieveSettings(out ip, out port, out username); var response = await client.PutAsync(new Uri(string.Format("http://{0}:{1}/api/{2}/lights/{3}/state", ip, port, username, light.id)), content).AsTask(cts.Token); if (!response.IsSuccessStatusCode) { return string.Empty; } string jsonResponse = await response.Content.ReadAsStringAsync(); System.Diagnostics.Debug.WriteLine(jsonResponse); return jsonResponse; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); return string.Empty; } }