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."); }