private async void LoadBridges() { try { var bridges = await HueHelpers.FindHueBridges(); foreach (var bridgeInfo in bridges) { if (this.devices.OfType <HueBridgeDevice>().Any(b => b.SerialNumber == bridgeInfo.SerialNumber)) { continue; //Already known bridge } LocalHueClient2 client = new LocalHueClient2(bridgeInfo.Ip, bridgeInfo.SerialNumber); var bridgeDevice = new HueBridgeDevice(client, bridgeInfo, this); this.devices.Add(bridgeDevice); bridgeDevice.NotifyDeviceArrival += BridgeDevice_NotifyDeviceArrival; bridgeDevice.NotifyDeviceRemoval += BridgeDevice_NotifyDeviceRemoval; this.NotifyDeviceArrival(bridgeDevice); } //Check if any bridges were lost foreach (var bridgeDevice in this.devices.OfType <HueBridgeDevice>().ToArray()) { if (bridges.Any(b => b.SerialNumber == bridgeDevice.SerialNumber)) { continue; } //Remove all bulbs on the bridge var bulbsOnBridge = this.devices.OfType <HueBulbDevice>().Where(b => b.BridgeSerialNumber == bridgeDevice.SerialNumber).ToArray(); foreach (var bulb in bulbsOnBridge) { this.devices.Remove(bulb); this.NotifyDeviceRemoval(bulb); } //Remove the bridge bridgeDevice.NotifyDeviceArrival -= BridgeDevice_NotifyDeviceArrival; bridgeDevice.NotifyDeviceRemoval -= BridgeDevice_NotifyDeviceRemoval; this.devices.Remove(bridgeDevice); this.NotifyDeviceRemoval(bridgeDevice); } } catch (System.Exception ex) { System.Diagnostics.Debug.WriteLine($"Load bridges failed: {ex.Message}\n{ex.StackTrace}\n----"); } }
private async void LoadBridges() { try { var bridges = await HueHelpers.FindHueBridges(); foreach (var bridgeInfo in bridges) { if (this.devices.OfType<HueBridgeDevice>().Any(b => b.SerialNumber == bridgeInfo.SerialNumber)) continue; //Already known bridge LocalHueClient2 client = new LocalHueClient2(bridgeInfo.Ip, bridgeInfo.SerialNumber); var bridgeDevice = new HueBridgeDevice(client, bridgeInfo, this); this.devices.Add(bridgeDevice); bridgeDevice.NotifyDeviceArrival += BridgeDevice_NotifyDeviceArrival; bridgeDevice.NotifyDeviceRemoval += BridgeDevice_NotifyDeviceRemoval; this.NotifyDeviceArrival(bridgeDevice); } //Check if any bridges were lost foreach(var bridgeDevice in this.devices.OfType<HueBridgeDevice>().ToArray()) { if (bridges.Any(b => b.SerialNumber == bridgeDevice.SerialNumber)) continue; //Remove all bulbs on the bridge var bulbsOnBridge = this.devices.OfType<HueBulbDevice>().Where(b => b.BridgeSerialNumber == bridgeDevice.SerialNumber).ToArray(); foreach(var bulb in bulbsOnBridge) { this.devices.Remove(bulb); this.NotifyDeviceRemoval(bulb); } //Remove the bridge bridgeDevice.NotifyDeviceArrival -= BridgeDevice_NotifyDeviceArrival; bridgeDevice.NotifyDeviceRemoval -= BridgeDevice_NotifyDeviceRemoval; this.devices.Remove(bridgeDevice); this.NotifyDeviceRemoval(bridgeDevice); } } catch(System.Exception ex) { System.Diagnostics.Debug.WriteLine($"Load bridges failed: {ex.Message}\n{ex.StackTrace}\n----"); } }