Exemplo n.º 1
0
 public static void Add(CabinetLog log)
 {
     if (log.OperationType == (int)Principal.OperatorTypeEnum.心跳)
     {
         return;
     }
     using (var cn = Database.GetDbConnection())
     {
         cn.Insert <CabinetLog>(log);
     }
 }
Exemplo n.º 2
0
 public void Update(CabinetLog model)
 {
     Execute(SqlUpdateCommand, model);
 }
Exemplo n.º 3
0
 public void Insert(CabinetLog model)
 {
     Execute(SqlInsertCommand, model);
 }
Exemplo n.º 4
0
        private void SyncCabinet()
        {
            int interval = 30;

            if (!string.IsNullOrEmpty(pingInterval))
            {
                int.TryParse(pingInterval, out interval);
            }
            Thread.Sleep(interval * 1000);
            while (true)
            {
                try
                {
                    //删除5分钟之前的命令
                    lock (AndroidController.logLock)
                        AndroidController.CabinetLogQueue.RemoveAll(m => m.CreateTime.AddMinutes(5) < DateTime.Now);
                    //List<string> keys = UserController.LoginDictionary.Keys.ToList();
                    //foreach (var key in keys)
                    //{
                    //    if (UserController.LoginDictionary.ContainsKey(key))
                    //    {
                    //        if(  UserController.LoginDictionary[key].LastLoginTime!=null&&(DateTime.Now- UserController.LoginDictionary[key].LastLoginTime.Value).TotalHours > 24)
                    //        {
                    //            logger.Error("自动退出 " + key + " " + UserController.LoginDictionary[key].LastLoginTime.Value.ToString("yyyy.MM.dd HH:mm:ss"));
                    //            UserController.LoginDictionary.Remove(key);
                    //        }
                    //    }
                    //}
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                }
                try
                {
                    //List<int> onLine = new List<int>();
                    //List<int> offLine = new List<int>();
                    List <CabinetData.Entities.CabinetLog> logs = new List <CabinetData.Entities.CabinetLog>();
                    var cabinetList = CabinetData.Entities.Cabinet.GetAll();
                    foreach (var cabinet in cabinetList)
                    {
                        bool isOnline = false;
                        if (cabinet.IsOnline ?? false)//在线
                        {
                            //如果保险柜在线,检测是否下线
                            if (!AndroidController.HeartDictionary.ContainsKey(cabinet.ID) || (AndroidController.HeartDictionary.ContainsKey(cabinet.ID) && AndroidController.HeartDictionary[cabinet.ID].AddSeconds(interval) < DateTime.Now))
                            {
                                //string host = cabinet.IP;
                                //Ping p1 = new Ping();
                                //PingReply reply = p1.Send(host); //发送主机名或Ip地址
                                //if (reply.Status != IPStatus.Success)
                                //{
                                logger.Warn("03保险柜在线,心跳不在且ping不通下线" + cabinet.ID + ":" + cabinet.IP);
                                //offLine.Add(cabinet.ID);
                                var log = new CabinetData.Entities.CabinetLog
                                {
                                    CabinetID     = cabinet.ID,
                                    DepartmentID  = cabinet.DepartmentID,
                                    OperatorName  = "",
                                    OperateTime   = DateTime.Now,
                                    OperationType = (int)OperatorTypeEnum.线,
                                    CreateTime    = DateTime.Now,
                                    CabinetIP     = "",
                                    EventContent  = "超时下线"
                                };
                                logs.Add(log);
                                CabinetData.Entities.Cabinet.UpdateOffLine(new List <int> {
                                    cabinet.ID
                                });
                                lock (AndroidController.logLock)
                                {
                                    AndroidController.msgID++;
                                    log.ID = AndroidController.msgID;
                                    AndroidController.CabinetLogQueue.Add(log);
                                }
                                DateTime dt;
                                AndroidController.HeartDictionary.TryRemove(cabinet.ID, out dt);
                                //}
                            }
                        }
                        else
                        {
                            if (AndroidController.HeartDictionary.ContainsKey(cabinet.ID) && AndroidController.HeartDictionary[cabinet.ID].AddSeconds(interval) > DateTime.Now)//如果存在
                            {
                                logger.Warn("01保险柜不在线,心跳上线" + cabinet.ID + ":" + cabinet.IP);
                                isOnline = true;
                            }
                            else
                            {
                                //string host = cabinet.IP;
                                //Ping p1 = new Ping();
                                //PingReply reply = p1.Send(host); //发送主机名或Ip地址
                                //if (reply.Status == IPStatus.Success)
                                //{
                                //    logger.Warn("02保险柜不在线,ping上线" + cabinet.ID + ":" + cabinet.IP);
                                //    isOnline = true;
                                //}
                            }
                            if (isOnline)
                            {
                                //onLine.Add(cabinet.ID);
                                var log = new CabinetData.Entities.CabinetLog
                                {
                                    CabinetID     = cabinet.ID,
                                    DepartmentID  = cabinet.DepartmentID,
                                    OperatorName  = "",
                                    OperateTime   = DateTime.Now,
                                    OperationType = (int)OperatorTypeEnum.线,
                                    CreateTime    = DateTime.Now,
                                    CabinetIP     = "",
                                    EventContent  = ""
                                };
                                logs.Add(log);
                                CabinetData.Entities.Cabinet.UpdateOnLine(new List <int> {
                                    cabinet.ID
                                });
                                lock (AndroidController.logLock)
                                {
                                    AndroidController.msgID++;
                                    log.ID = AndroidController.msgID;
                                    AndroidController.CabinetLogQueue.Add(log);
                                }
                            }
                        }
                    }
                    if (logs.Count > 0)
                    {
                        CabinetLog.Add(logs);
                    }
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                }
                finally
                {
                    Thread.Sleep(3000);
                }
            }
        }