Ejemplo n.º 1
0
        //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();
                }
            }
        }
Ejemplo n.º 2
0
        //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 { }
        }