Exemple #1
0
        protected async Task<List<Bridge>> SearchBridgesUsingProxyAsync()
        {
            // First, we'll attempt to use the proxy upnp server provided by Hue
            var discoveredBridges = new List<Bridge>();

            try
            {
                HttpClient client = new HttpClient();
                HttpResponseMessage resp = await client.GetAsync(ProxyUPNPUrl);
                resp.EnsureSuccessStatusCode();

                // Try to parse JSON response
                var result = await resp.Content.ReadAsStringAsync();
                JsonArray bridgeArray = JsonArray.Parse(result);
                foreach (var bridgeValue in bridgeArray)
                {
                    var bridgeObject = bridgeValue.GetObject();

                    Bridge bridge = new Bridge();
                    bridge.BridgeId = bridgeObject.GetNamedString("id");
                    bridge.IPAddress = bridgeObject.GetNamedString("internalipaddress");

                    discoveredBridges.Add(bridge);

                    Debug.WriteLine("Found bridge {0} at {1}", bridge.BridgeId, bridge.IPAddress);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            
            return discoveredBridges;
        }
        public static void UpdateBridgeLightsWithJObject(Bridge bridge, JObject json)
        {
            try
            {
                // Get light keys
                IList<string> lightKeys = json.Properties().Select(p => p.Name).ToList();

                bridge.LightList.Clear();
                bridge.LightCache.Clear();

                foreach(var lightId in lightKeys)
                {
                    Light light = new Light();
                    light.LightId = lightId;
                    bridge.LightList.Add(light);
                    bridge.LightCache[lightId] = light;

                    JObject lightJson = (JObject)json[lightId];
                    LightFactory.UpdateLightWithJObject(light, lightJson);
                }
            }
            catch(Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
        public static void UpdateBridgeConfigurationsWithJObject(Bridge bridge, JObject configJson)
        {
            try
            {
                bridge.Name = configJson["name"].ToString();

                // Version update info
                bridge.Version = configJson["swversion"].ToString();
                JToken updateValue;
                if (configJson.TryGetValue("swupdate", out updateValue))
                {
                    JObject updateJson = (JObject)configJson["swupdate"];
                    bridge.HasSoftwareUpdate = (updateJson["updatestate"].ToString() == "1");
                    bridge.SoftwareUpdateText = updateJson["text"].ToString();
                    bridge.SoftwareUpdateUrl = updateValue["url"].ToString();
                }

                // White list
                JToken whitelistValue;
                if (configJson.TryGetValue("whitelist", out whitelistValue))
                {
                    bridge.WhiteList.Clear();
                    JObject whitelistJson = (JObject)configJson["whitelist"];
                    IList<string> appKeys = whitelistJson.Properties().Select(p => p.Name).ToList();
                    bridge.WhiteList.AddRange(appKeys);
                }
            }
            catch(Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
        public static void UpdateBridgeWithJSON(Bridge bridge, string jsonString)
        {
            try
            {
                JObject json = JObject.Parse(jsonString);
                // Config section
                JToken configValue;
                if(json.TryGetValue("config", out configValue))
                {
                    UpdateBridgeConfigurationsWithJObject(bridge, (JObject)json["config"]);
                }

                // Light section
                JToken lightsValue;
                if (json.TryGetValue("lights", out lightsValue))
                {
                    UpdateBridgeLightsWithJObject(bridge, (JObject)json["lights"]);
                }

                // Schedule section
                JToken scheduleValue;
                if (json.TryGetValue("schedules", out scheduleValue))
                {
                    UpdateBridgeSchedulesWithJObject(bridge, (JObject)json["schedules"]);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
        private async void TestConnectionAsync(Bridge bridge)
        {
            BridgeManager.Instance.CurrentBridge = bridge;

            // Verify bridge
            HueAPI.Instance.BridgeIp = bridge.IPAddress;            

            bool valid = await HueAPI.Instance.TestConnectAsync();
            Debug.WriteLine(valid);

            if (!valid)
            {
                this.Frame.Navigate(typeof(BridgeConnectPage));
            }
            else
            {
                ShowMainPage();
            }
        }
Exemple #6
0
        protected async Task<Bridge> BridgeFromDescriptionAsync(string url)
        {
            Bridge bridge = null;

            try
            {
                HttpClient client = new HttpClient();
                client.Timeout = TimeSpan.FromSeconds(3);
                HttpResponseMessage resp = await client.GetAsync(url);
                resp.EnsureSuccessStatusCode();

                // Try to parse JSON response
                var result = await resp.Content.ReadAsStringAsync();
                if (result.Contains("Philips hue bridge"))
                {
                    var ip = url.Replace("http://", "").Replace("/description.xml", "");
                    bridge = new Bridge();
                    bridge.IPAddress = ip;

                    Debug.WriteLine("Found bridge via UPNP: {0}", ip);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                bridge = null;
            }

            return bridge;
        }
        public static void UpdateBridgeSchedulesWithJObject(Bridge bridge, JObject json)
        {
            try
            {
                // Get schedule keys
                IList<string> scheduleKeys = json.Properties().Select(p => p.Name).ToList();
                bridge.ScheduleList.Clear();

                foreach (var scheduleId in scheduleKeys)
                {
                    Schedule schedule = new Schedule();
                    schedule.ScheduleId = scheduleId;
                    bridge.ScheduleList.Add(schedule);

                    JObject scheduleJson = (JObject)json[scheduleId];
                    ScheduleFactory.UpdateScheduleWithJObject(schedule, scheduleJson);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }