Exemple #1
0
        public List <string> Send(NotifierData msg, List <string> userIds)
        {
            List <string> successes = new List <string>();

            foreach (var userId in userIds)
            {
                try
                {
                    var notifier = GlobalHost.ConnectionManager.GetHubContext <MyHub>();

                    var isonline = MyHub.IsOnline(userId);
                    if (!isonline)
                    {
                        return(null);
                    }

                    var cids = MyHub.CurrClients.Where(x => x.Value.ClientName == userId).Select(x => x.Key).ToList();
                    foreach (var item in cids)
                    {
                        notifier.Clients.Client(item).broadcast(msg.Msg);
                    }
                    successes.Add(userId);
                }
                catch (Exception ex)
                {
                    MyConsole.log(ex, "Web消息发送异常");
                }
            }
            return(successes);
        }
Exemple #2
0
        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, "移除设备监测异常");
            }
        }
Exemple #3
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, "添加设备监测异常");
            }
        }