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 IActionResult GetJson() { LogUtil.Write(@"GetJson Called."); var bridgeArray = DataUtil.GetCollection <BridgeData>("Dev_Hue"); if (bridgeArray.Count == 0) { var newBridges = HueDiscovery.Discover().Result; foreach (var b in newBridges) { DataUtil.InsertCollection <BridgeData>("Dev_Hue", b); } } if (DataUtil.GetItem("DsIp") == "127.0.0.1") { var newDevices = DreamDiscovery.Discover(); var devices = DataUtil.GetCollection <BaseDevice>("Dev_DreamScreen"); foreach (var d in devices) { DataUtil.InsertCollection <BaseDevice>("Dev_DreamScreen", d); } } return(Content(DataUtil.GetStoreSerialized(), "application/json")); }
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(); }
public IActionResult Action(string action, string value = "") { var message = "Unrecognized action"; LogUtil.Write($@"{action} called from Web API."); switch (action) { case "loadData": NotifyClients(); return(Content(DataUtil.GetStoreSerialized(), "application/json")); case "refreshDevices": // Just trigger dreamclient to refresh devices TriggerRefresh(); return(new JsonResult("OK")); case "authorizeHue": { LogUtil.Write("AuthHue called, for real."); var doAuth = true; BridgeData bd = null; if (!string.IsNullOrEmpty(value)) { _hubContext?.Clients.All.SendAsync("hueAuth", "start"); bd = DataUtil.GetCollectionItem <BridgeData>("Dev_Hue", value); LogUtil.Write("BD: " + JsonConvert.SerializeObject(bd)); if (bd == null) { LogUtil.Write("Null bridge retrieved."); return(new JsonResult(null)); } if (bd.Key != null && bd.User != null) { LogUtil.Write("Bridge is already authorized."); doAuth = false; } } else { LogUtil.Write("Null value.", "WARN"); doAuth = false; } if (!doAuth) { LogUtil.Write("No auth, returning existing data."); return(new JsonResult(bd)); } LogUtil.Write("Trying to retrieve appkey..."); var appKey = HueDiscovery.CheckAuth(bd.IpAddress).Result; if (appKey == null) { LogUtil.Write("Error retrieving app key."); return(new JsonResult(bd)); } bd.Key = appKey.StreamingClientKey; bd.User = appKey.Username; LogUtil.Write("We should be authorized, returning."); DataUtil.InsertCollection <BridgeData>("Dev_Hue", bd); return(new JsonResult(bd)); } case "authorizeNano": { var doAuth = true; var leaves = DataUtil.GetCollection <NanoData>("Dev_NanoLeaf"); NanoData bd = null; var nanoInt = -1; if (!string.IsNullOrEmpty(value)) { var nanoCount = 0; foreach (var n in leaves) { if (n.IpAddress == value) { bd = n; doAuth = n.Token == null; nanoInt = nanoCount; } nanoCount++; } } if (doAuth) { var panel = new NanoGroup(value); var appKey = panel.CheckAuth().Result; if (appKey != null && bd != null) { bd.Token = appKey.Token; bd.RefreshLeaf(); LogUtil.Write("Leaf refreshed and set..."); } panel.Dispose(); } LogUtil.Write("Returning."); return(new JsonResult(bd)); } } LogUtil.Write(message); return(new JsonResult(message)); }
public async void AuthorizeHue(string id) { LogUtil.Write("AuthHue called, for real."); BridgeData bd; if (!string.IsNullOrEmpty(id)) { await Clients.All.SendAsync("hueAuth", "start"); bd = DataUtil.GetCollectionItem <BridgeData>("bridges", id); LogUtil.Write("BD: " + JsonConvert.SerializeObject(bd)); if (bd == null) { LogUtil.Write("Null bridge retrieved."); await Clients.All.SendAsync("hueAuth", "stop"); return; } if (bd.Key != null && bd.User != null) { LogUtil.Write("Bridge is already authorized."); await Clients.All.SendAsync("hueAuth", "authorized"); await Clients.All.SendAsync("olo", DataUtil.GetStoreSerialized()); return; } } else { LogUtil.Write("Null value.", "WARN"); await Clients.All.SendAsync("hueAuth", "stop"); return; } LogUtil.Write("Trying to retrieve appkey..."); var count = 0; while (count < 30) { count++; try { RegisterEntertainmentResult appKey = HueDiscovery.CheckAuth(bd.IpAddress).Result; LogUtil.Write("Appkey retrieved! " + JsonConvert.SerializeObject(appKey)); if (!string.IsNullOrEmpty(appKey.StreamingClientKey)) { bd.Key = appKey.StreamingClientKey; bd.User = appKey.Username; // Need to grab light group stuff here var nhb = new HueBridge(bd); nhb.RefreshData(); bd = nhb.Bd; nhb.Dispose(); DataUtil.InsertCollection <BridgeData>("bridges", bd); await Clients.All.SendAsync("hueAuth", "authorized"); await Clients.All.SendAsync("olo", DataUtil.GetStoreSerialized()); return; } } catch (NullReferenceException e) { LogUtil.Write("NULL EXCEPTION: " + e.Message, "WARN"); } await Clients.All.SendAsync("hueAuth", count); Thread.Sleep(1000); } LogUtil.Write("We should be authorized, returning."); }