public static async Task UpdateSource(Bitbar bitbar) { if (!userConnections.ContainsKey(bitbar.User.UserId)) { return; } JObject source = new JObject(); source["source_id"] = bitbar.Id; source["value"] = bitbar.Value; source["max_value"] = bitbar.MaxValue; source["image"] = Base64.Encode(bitbar.Image); source["target_color"] = "#" + bitbar.TargetColor; source["fill_color"] = "#" + bitbar.FillColor; List <BitbarHub> remList = new List <BitbarHub>(); foreach (BitbarHub hub in userConnections[bitbar.User.UserId]) { try { // TODO: Some connection hubs are disposed, is there a better way to tell? await hub.Clients.Caller.SendAsync("update_source", source.ToString()); } catch { remList.Add(hub); } } foreach (BitbarHub hub in remList) { userConnections[bitbar.User.UserId].Remove(hub); } }
public static IActionResult Index(RequestHandler <IActionResult> req) { if (req.User == null) { throw new UnauthorizedException(); } Bitbar bitbar = BitbarManager.GetBitbar(req.User); req.View.Bitbar = bitbar; return(req.Controller.View()); }
public static JToken Bitbar(RequestHandler <JToken> req) { string id = req.Request.Query["id"]; Bitbar bitbar = BitbarManager.GetBitbar(id); JObject source = new JObject(); source["source_id"] = bitbar.Id; source["value"] = bitbar.Value; source["max_value"] = bitbar.MaxValue; source["target_color"] = "#" + bitbar.TargetColor; source["fill_color"] = "#" + bitbar.FillColor; return(source); }
// TODO: Move this logic up one level when we create a new hub! public async Task ReceiveSource(string source) { JObject json = JObject.Parse(source); Bitbar bitbar = BitbarManager.GetBitbar((string)json["source_id"]); string userid = bitbar.User.UserId; connectionUsers[this] = userid; if (!userConnections.ContainsKey(userid)) { userConnections[userid] = new HashSet <BitbarHub>(); } userConnections[userid].Add(this); await UpdateSource(bitbar); }
public static IActionResult BitbarSubmit(RequestHandler <IActionResult> req) { IFormCollection form = req.Request.Form; if (req.User == null) { throw new UnauthorizedException(); } try { int value = int.Parse(form["value"]); int maxValue = int.Parse(form["max_value"]); string targetColor = form["target_color"].ToString(); string fillColor = form["fill_color"].ToString(); byte[] image = null; if (form.Files.Count > 0) { image = new byte[form.Files["image"].Length]; form.Files["image"].OpenReadStream().Read(image, 0, image.Length); } Bitbar bitbar = BitbarManager.GetBitbar(req.User); bitbar.Value = value; bitbar.MaxValue = maxValue; if (image != null) { bitbar.Image = image; } bitbar.TargetColor = targetColor; bitbar.FillColor = fillColor; BitbarManager.UpdateBitbar(bitbar); } catch (Exception ex) { throw new BadRequestException(); } return(req.Controller.RedirectToAction("Index", "Streamkit")); }