예제 #1
0
        /// <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, "添加设备监测异常");
            }
        }