public static void RemoveWatch(string user, int target, CategoryDictionary type) { try { if (string.IsNullOrEmpty(user)) { return; } var notifier = GlobalHost.ConnectionManager.GetHubContext <MyHub>(); //var onlines = MyHub.CurrClients.Keys; var isonline = MyHub.IsOnline(user); if (!isonline) { if (CurrObservers.ContainsKey(user)) { CurrObservers.Remove(user); } return; } else { if (CurrObservers.ContainsKey(user)) { CurrObservers[user] = CurrObservers[user].Where(x => !(x.TargetId == target && x.TargetType == type)).ToList(); if (CurrObservers[user].Count == 0) { CurrObservers.Remove(user); } } //else //{ // CurrObservers.Add(user, new List<string>() { meter }); //} } if (timer != null) { //如果需要监测的用户为启动,则启动监控程序 if (CurrObservers.Keys.Count == 0) { timer.Dispose(); } //timer = new Timer(2000); //timer.Elapsed += new ElapsedEventHandler(DispatchTask); //timer.Enabled = true; //timer.AutoReset = true; } } catch (Exception ex) { MyConsole.log(ex, "移除设备监测异常"); } }
/// <summary> /// 添加要实时监测的设备 /// </summary> /// <param name="user"></param> /// <param name="meter"></param> /// <param name="param"></param> /// <param name="infinite">单次监测或一直监测</param> public static void AddWatch(string user, int target, CategoryDictionary targetType, List <string> param, bool infinite = true) { try { if (string.IsNullOrEmpty(user)) { return; } var notifier = GlobalHost.ConnectionManager.GetHubContext <MyHub>(); //var onlines = MyHub.CurrClients.Keys; var isonline = MyHub.IsOnline(user); if (!isonline) { if (CurrObservers.ContainsKey(user)) { CurrObservers.Remove(user); } return; } else { var dic = new Dictionary <string, decimal>(); param.ForEach(x => dic.Add(x, decimal.Zero)); var node = new MonitorMeta() { TargetId = target, TargetType = targetType, //Category = param.Count > 0 ? (StrangerCategory.State | StrangerCategory.Parameter) : StrangerCategory.State, Params = dic, Infinite = infinite }; if (CurrObservers.ContainsKey(user)) { //CurrObservers[user].Clear(); CurrObservers[user] = CurrObservers[user].Where(x => x.TargetId != target).ToList(); CurrObservers[user].Add(node); } else { CurrObservers.Add(user, new List <MonitorMeta>() { node }); } } if (timer == null) { //如果未启动,则启动监控程序 timer = new Timer(2000); timer.Elapsed += new ElapsedEventHandler(DispatchTask); timer.Enabled = true; timer.AutoReset = true; } } catch (Exception ex) { MyConsole.log(ex, "添加设备监测异常"); } }