//Attempt to store bridge ID async public static Task StoreBridgeID() { if (Platform.ReadSetting("bridgeIdentity") == "") { try { HttpClient client = new HttpClient(); Task <HttpResponseMessage> getBridgeData = client.GetAsync("https://www.meethue.com/api/nupnp"); HttpResponseMessage bridgeData = await getBridgeData; string bridgeString = await bridgeData.Content.ReadAsStringAsync(); dynamic bridgeJson = JsonParser.Deserialize(bridgeString); foreach (dynamic bridgeInfo in bridgeJson) { string bridgeIdentity = bridgeInfo.id; string bridgeAddress = bridgeInfo.internalipaddress; bridgeIdentity = bridgeIdentity.ToLowerInvariant(); if (bridgeAddress == Platform.ReadSetting("bridgeIP")) { Platform.WriteSetting("bridgeIdentity", bridgeIdentity); Platform.SaveSettings(); } return; } } catch { } } }
public static int InitializeData() { Storage.IntializeDefaultScenes(); AddressBuild.InitializeVar(Platform.ReadSetting("bridgeIP"), Platform.ReadSetting("bridgeUserName"), Platform.ReadSetting("bridgeGroup")); activeTab = Platform.ReadSetting("lastActiveTab"); sceneData = JsonParser.Deserialize(Platform.ReadSetting("customSceneJSON")); latestData = JsonParser.Deserialize(Platform.ReadSetting("backupMainJSON")); groupData = JsonParser.Deserialize(Platform.ReadSetting("backupGroupJSON")); backupData = latestData; groupBackupData = groupData; SunriseEquation.SetCoordinates(); SunriseEquation.SunriseSunset(); System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); System.Diagnostics.FileVersionInfo versionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location); fileVersion[0] = versionInfo.ProductMajorPart; fileVersion[1] = versionInfo.ProductMinorPart; fileVersion[2] = versionInfo.ProductBuildPart; fileVersion[3] = versionInfo.ProductPrivatePart; var result = RefreshData(); var bridgeResult = FindNewBridgeIP(); Effects.AutoEffect(); var bridgeResult2 = StoreBridgeID(); return(0); }
public static void SetCoordinates() { int[] c = { 0, 0 }; //lat, long //Get latitude by country centrics var assembly = System.Reflection.Assembly.GetExecutingAssembly(); Stream stream = assembly.GetManifestResourceStream("Albedo.Resources.latitude.txt"); StreamReader reader = new StreamReader(stream); string geoString = reader.ReadToEnd(); dynamic geoData = JsonParser.Deserialize(geoString); int i = 0; foreach (string country in geoData.country) { if (region == country) { lat = geoData.latitude[i]; break; } i++; } //Get longitude by time zone int offset = TimeZoneInfo.Local.BaseUtcOffset.Hours; lon = offset * -15; //UTC +12 == longitude -180 //Set startup time if (CurrentJulianDate() - 0.83 > Platform.ReadSetting("startupTime") || CurrentJulianDate() < Platform.ReadSetting("startupTime")) { Platform.WriteSetting("startupTime", CurrentJulianDate()); } startup = Platform.ReadSetting("startupTime"); }
async public static Task PairAttempt(string bridgeIP, string bridgeUser) { HttpClient client = new HttpClient(); string address = AddressBuild.APIRootSetup(bridgeIP); try { dynamic dynContent = new ExpandoObject(); string devicetype = String.Format("Albedo#{0}.{1}", Environment.MachineName, DateTime.Now.ToString()); if (devicetype.Length > 40) { devicetype = devicetype.Substring(0, 40); } dynContent.devicetype = devicetype; //dynContent.username = bridgeUser; //OBSOLETE. Username is now assigned by the bridge. StringContent content = new StringContent(JsonParser.Serialize(dynContent)); Task <HttpResponseMessage> postData = client.PostAsync(address, content); HttpResponseMessage response = await postData; string responseString = await response.Content.ReadAsStringAsync(); //Ugly hack; to be removed once parser handles arrays properly responseString = responseString.Substring(1, responseString.Length - 2); Storage.tempData = JsonParser.Deserialize(responseString); return; } catch (Exception) { dynamic failData = new ExpandoObject(); Storage.tempData = failData; return; } }
public static void IntializeDefaultScenes() { var assembly = System.Reflection.Assembly.GetExecutingAssembly(); Stream stream = assembly.GetManifestResourceStream("Albedo.Resources.defaultScenesJSON.txt"); StreamReader reader = new StreamReader(stream); string scenesString = reader.ReadToEnd(); defaultScenes = JsonParser.Deserialize(scenesString); }
//Downloads latest bridge data, and stores it in latestData. async public static Task RefreshData(bool doSceneBackup = false, int retry = 0) { bool finalize = false; try { HttpClient client = new HttpClient(); Task <HttpResponseMessage> getLightData = client.GetAsync(AddressBuild.LightsRoot()); Task <HttpResponseMessage> getGroupsData = client.GetAsync(AddressBuild.GroupsRoot()); Task <HttpResponseMessage> getGroupData = client.GetAsync(AddressBuild.GroupUri()); HttpResponseMessage lightString = await getLightData; HttpResponseMessage groupsString = await getGroupsData; HttpResponseMessage groupString = await getGroupData; dynamic latestTemp = new ExpandoObject(); dynamic groupTemp = new ExpandoObject(); latestTemp.lights = JsonParser.Deserialize(await lightString.Content.ReadAsStringAsync()); latestTemp.groups = JsonParser.Deserialize(await groupsString.Content.ReadAsStringAsync()); groupTemp = JsonParser.Deserialize(await groupString.Content.ReadAsStringAsync()); if (latestTemp.lights == null || groupTemp.lights == null) { throw new Exception(); } latestData = latestTemp; groupData = groupTemp; backupData = latestData; groupBackupData = groupData; finalize = true; return; } catch (Exception) { if (retry < 2) { var result = RefreshData(doSceneBackup, retry + 1); } else { latestData = backupData; groupData = groupBackupData; finalize = true; return; } } finally { if (finalize) { if (doSceneBackup) { sceneBackup = latestData.lights; } Platform.FinalizeRefresh(); } } }
//Find the bridge again if the local IP changes async public static Task FindNewBridgeIP() { bool syncFailed = false; try { HttpClient client = new HttpClient(); Task <HttpResponseMessage> getTestData = client.GetAsync(AddressBuild.LightsRoot()); HttpResponseMessage testData = await getTestData; string testString = await testData.Content.ReadAsStringAsync(); dynamic testJson = JsonParser.Deserialize(testString); } catch { syncFailed = true; } if (!syncFailed) { return; } try { HttpClient client = new HttpClient(); Task <HttpResponseMessage> getBridgeData = client.GetAsync("https://www.meethue.com/api/nupnp"); HttpResponseMessage bridgeData = await getBridgeData; string bridgeString = await bridgeData.Content.ReadAsStringAsync(); dynamic bridgeJson = JsonParser.Deserialize(bridgeString); foreach (dynamic bridgeInfo in bridgeJson) { string bridgeIdentity = bridgeInfo.id; string bridgeAddress = bridgeInfo.internalipaddress; bridgeIdentity = bridgeIdentity.ToLowerInvariant(); if (bridgeIdentity == Platform.ReadSetting("bridgeIdentity")) { if (bridgeAddress != Platform.ReadSetting("bridgeIP")) { Platform.WriteSetting("bridgeIP", bridgeAddress); Platform.SaveSettings(); } } } } catch { } }