public static async Task ScanDevices(LifxClient lc) { if (scanning) { return; } scanning = true; var db = GetDb(); // Get dream devices var ld = new LifxDiscovery(lc); var nanoTask = NanoDiscovery.Discover(); var hueTask = HueDiscovery.Discover(); var dreamTask = DreamDiscovery.Discover(); var wLedTask = WledDiscovery.Discover(); var bulbTask = ld.Discover(5); await Task.WhenAll(nanoTask, hueTask, dreamTask, bulbTask).ConfigureAwait(false); var leaves = await nanoTask.ConfigureAwait(false); var bridges = await hueTask.ConfigureAwait(false); var dreamDevices = await dreamTask.ConfigureAwait(false); var bulbs = await bulbTask.ConfigureAwait(false); var wleds = await wLedTask.ConfigureAwait(false); var bridgeCol = db.GetCollection <BridgeData>("Dev_Hue"); var nanoCol = db.GetCollection <NanoData>("Dev_Nanoleaf"); var devCol = db.GetCollection <BaseDevice>("Dev_DreamScreen"); var lifxCol = db.GetCollection <LifxData>("Dev_Lifx"); var wledCol = db.GetCollection <WLedData>("Dev_Wled"); foreach (var b in bridges) { bridgeCol.Upsert(b); } foreach (var n in leaves) { nanoCol.Upsert(n); } foreach (var dd in dreamDevices) { devCol.Upsert(dd); } foreach (var b in bulbs) { lifxCol.Upsert(b); } foreach (var w in wleds) { wledCol.Upsert(w); } bridgeCol.EnsureIndex(x => x.Id); nanoCol.EnsureIndex(x => x.Id); devCol.EnsureIndex(x => x.Id); lifxCol.EnsureIndex(x => x.Id); wledCol.EnsureIndex(x => x.Id); scanning = false; }
public static async void RefreshDevices(LifxClient c) { var cs = new CancellationTokenSource(); cs.CancelAfter(10000); LogUtil.Write("Starting scan."); scanning = true; // Get dream devices var ld = new LifxDiscovery(c); var nanoTask = NanoDiscovery.Refresh(cs.Token); var bridgeTask = HueDiscovery.Refresh(cs.Token); var dreamTask = DreamDiscovery.Discover(); var wLedTask = WledDiscovery.Discover(); var bulbTask = ld.Refresh(cs.Token); try { await Task.WhenAll(nanoTask, bridgeTask, dreamTask, bulbTask, wLedTask); } catch (TaskCanceledException e) { LogUtil.Write("Discovery task was canceled before completion: " + e.Message, "WARN"); } catch (SocketException f) { LogUtil.Write("Socket Exception during discovery: " + f.Message, "WARN"); } LogUtil.Write("Refresh complete."); try { var leaves = nanoTask.Result; var bridges = bridgeTask.Result; var dreamDevices = dreamTask.Result; var bulbs = bulbTask.Result; var wleds = wLedTask.Result; var db = GetDb(); var bridgeCol = db.GetCollection <BridgeData>("Dev_Hue"); var nanoCol = db.GetCollection <NanoData>("Dev_Nanoleaf"); var devCol = db.GetCollection <BaseDevice>("Dev_DreamScreen"); var lifxCol = db.GetCollection <LifxData>("Dev_Lifx"); var wledCol = db.GetCollection <WLedData>("Dev_Wled"); foreach (var b in bridges) { bridgeCol.Upsert(b); } foreach (var n in leaves) { nanoCol.Upsert(n); } foreach (var dd in dreamDevices) { devCol.Upsert(dd); } foreach (var b in bulbs) { lifxCol.Upsert(b); } foreach (var w in wleds) { wledCol.Upsert(w); } bridgeCol.EnsureIndex(x => x.Id); nanoCol.EnsureIndex(x => x.Id); devCol.EnsureIndex(x => x.Id); lifxCol.EnsureIndex(x => x.Id); wledCol.EnsureIndex(x => x.Id); } catch (TaskCanceledException) { } catch (AggregateException) { } scanning = false; cs.Dispose(); }