Exemplo n.º 1
0
        public IHttpActionResult QueryCabinet(CabinetSearchModel search)
        {
            try
            {
                if (search == null)
                {
                    return(BadRequest());
                }
                if (search.PageIndex == 0)
                {
                    search.PageIndex = 1;
                }
                if (search.PageSize == 0)
                {
                    search.PageSize = 20;
                }
                _logger.Info(JsonConvert.SerializeObject(UserController.LoginDictionary));
                if (!UserController.LoginDictionary.ContainsKey(GetCookie("token")))
                {
                    return(Logout());
                }
                UserInfo userCookie = UserController.LoginDictionary[GetCookie("token")];
                if (userCookie == null)
                {
                    return(Logout());
                }

                //更新保险柜状态
                //var cabinetList = Cabinet.GetAll().FindAll(m => (m.Status == 1 || m.Status == 4));
                //foreach (var m in cabinetList)
                //{
                //    var log = CabinetLog.GetOpenLog(m.ID);
                //    if (log == null || log.CreateTime.AddSeconds(60) < DateTime.Now)
                //    {
                //        m.Status = 3;
                //        Cabinet.Update(m);
                //        _logger.Info("自动重置关闭");
                //    }
                //}

                List <Department> departList = Department.GetAllChildren(userCookie.DepartmentID);
                if (!string.IsNullOrEmpty(search.DepartmentName))
                {
                    departList = departList.FindAll(m => m.Name.Contains(search.DepartmentName));
                }
                var result = Cabinet.GetCabinets(search, departList.Select(m => m.ID).ToList());
                if (result.Items.Count > 0)
                {
                    var depart = Department.GetAll(result.Items.Select(m => m.DepartmentID).ToList());
                    result.Items.ForEach(m =>
                    {
                        m.DepartmentName        = depart.Find(n => n.ID == m.DepartmentID)?.Name;
                        m.FirstContactPassword  = string.IsNullOrEmpty(m.FirstContactPassword) ? "" : AESAlgorithm.Decrypto(m.FirstContactPassword);
                        m.SecondContactPassword = string.IsNullOrEmpty(m.SecondContactPassword) ? "" : AESAlgorithm.Decrypto(m.SecondContactPassword);
                    });
                }
                return(Success(result));
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                return(Failure("查询失败"));
            }
        }
Exemplo n.º 2
0
        public IHttpActionResult Command(CommandRequest request)
        {
            if (request == null)
            {
                return(Failure("请求命令无效"));
            }
            if (string.IsNullOrEmpty(request.Mac))
            {
                return(Failure("必须指定mac地址"));
            }
            try
            {
                _logger.Info(JsonConvert.SerializeObject(request));
                var cabinet = Cabinet.GetByMac(request.Mac);
                if (cabinet == null)
                {
                    return(Failure("未找到指定保险柜"));
                }

                #region 心跳
                if (request.OperatorType == (int)OperatorTypeEnum.心跳)
                {
                    if (HeartDictionary.ContainsKey(cabinet.ID))
                    {
                        HeartDictionary[cabinet.ID] = DateTime.Now;
                    }
                    else
                    {
                        HeartDictionary.TryAdd(cabinet.ID, DateTime.Now);
                    }
                    CommandResponse res  = null;
                    int             type = -1;
                    lock (CommandDictionaryLock)
                    {
                        var item = CommandDictionary.Find(m => m.ID == cabinet.ID);
                        if (item != null)
                        {
                            res = item;
                            CommandDictionary.Remove(item);
                        }
                    }

                    if (res != null)
                    {
                        type = res.OperationType;
                        var msg = "AuthCode-" + res.AuthCode;

                        if (type == (int)OperatorTypeEnum.允许开门)
                        {
                            //cabinet.Status = (int)OperatorTypeEnum.允许开门;
                            //cabinet.Alarm = "";
                            //cabinet.IsOpen = true;
                            //Cabinet.Update(cabinet);
                            return(Success1("允许开门", msg));
                        }
                        else if (type == (int)OperatorTypeEnum.拒绝开门)
                        {
                            return(Success1("拒绝开门", msg));
                        }
                        else if (type == (int)OperatorTypeEnum.拒绝语音)
                        {
                            return(Success1("拒绝语音", msg));
                        }
                        else if (type == (int)OperatorTypeEnum.接受语音)
                        {
                            return(Success1("接受语音", msg));
                        }
                        else if (type == (int)OperatorTypeEnum.允许终端修改信息)
                        {
                            return(Success1("允许终端修改信息", msg));
                        }
                        else if (type == (int)OperatorTypeEnum.拒绝终端修改信息)
                        {
                            return(Success1("拒绝终端修改信息", msg));
                        }
                        else if (type == (int)OperatorTypeEnum.允许人员注册)
                        {
                            return(Success1("允许人员注册", msg));
                        }
                        else if (type == (int)OperatorTypeEnum.拒绝人员注册)
                        {
                            return(Success1("拒绝人员注册", msg));
                        }
                    }
                    return(Success());
                }
                #endregion

                #region 增加请求日志
                var log = new CabinetLog
                {
                    CabinetID     = cabinet.ID,
                    DepartmentID  = cabinet.DepartmentID,
                    OperatorName  = request.UserName,
                    OperateTime   = DateTime.Now,
                    OperationType = request.OperatorType,
                    CreateTime    = DateTime.Now,
                    CabinetIP     = GetIP(),
                    EventContent  = JsonConvert.SerializeObject(request),
                    Remark        = request.AuthCode
                };
                CabinetLog.Add(log);

                #endregion
                #region 开门申请
                if (request.OperatorType == (int)OperatorTypeEnum.申请开门)
                {
                    if (!(cabinet.NeedConfirm ?? true))
                    {
                        lock (CommandDictionaryLock)
                        {
                            CommandDictionary.Add(new CommandResponse {
                                OperationType = (int)OperatorTypeEnum.允许开门, AuthCode = request.AuthCode, ID = cabinet.ID, CreateTime = DateTime.Now
                            });
                        }
                        request.OperatorType = (int)OperatorTypeEnum.允许开门;
                        request.EventContent = "不需要审核自动开门";
                        AddLog(cabinet, request);
                        return(Success());
                    }
                    #region 密码开门
                    else if (request.Method == 0)
                    {
                        if (string.IsNullOrEmpty(request.Password))
                        {
                            CabinetLog.Add(new CabinetLog
                            {
                                CabinetID     = cabinet.ID,
                                DepartmentID  = cabinet.DepartmentID,
                                OperatorName  = request.UserName,
                                OperateTime   = DateTime.Now,
                                OperationType = (int)OperatorTypeEnum.密码错误,
                                CreateTime    = DateTime.Now,
                                CabinetIP     = GetIP(),
                                EventContent  = JsonConvert.SerializeObject(request)
                            });
                            _loggerError.Error("密码不得为空");
                            return(Failure("密码不得为空"));
                        }
                        var first  = string.IsNullOrEmpty(cabinet.FirstContactPassword) ? "" : AESAlgorithm.Decrypto(cabinet.FirstContactPassword);
                        var second = string.IsNullOrEmpty(cabinet.SecondContactPassword) ? "" : AESAlgorithm.Decrypto(cabinet.SecondContactPassword);
                        if (first != request.Password && second != request.Password)
                        {
                            CabinetLog.Add(new CabinetLog
                            {
                                CabinetID     = cabinet.ID,
                                DepartmentID  = cabinet.DepartmentID,
                                OperatorName  = request.UserName,
                                OperateTime   = DateTime.Now,
                                OperationType = (int)OperatorTypeEnum.密码错误,
                                CreateTime    = DateTime.Now,
                                CabinetIP     = GetIP(),
                                EventContent  = JsonConvert.SerializeObject(request)
                            });
                            _loggerError.Error("密码错误 " + request.Password + "  [" + cabinet.FirstContactPassword + ":" + first + "]  [" + cabinet.SecondContactPassword + ":" + second + "]");
                            return(Failure("密码错误"));
                        }
                    }
                    #endregion

                    #region 加入前台提醒
                    lock (logLock)
                    {
                        msgID++;
                        log.ID = msgID;
                        CabinetLogQueue.Add(log);
                    }
                    return(Success("等待审核"));

                    #endregion
                }

                #endregion

                #region 语音申请||终端修改信息||申请人员注册
                else if (request.OperatorType == (int)OperatorTypeEnum.请求语音 || request.OperatorType == (int)OperatorTypeEnum.终端修改信息 || request.OperatorType == (int)OperatorTypeEnum.申请人员注册)
                {
                    lock (logLock)
                    {
                        msgID++;
                        log.ID = msgID;
                        CabinetLogQueue.Add(log);
                    }
                    return(Success("等待审核"));
                }
                #endregion


                #region 关门确认
                else if (request.OperatorType == (int)OperatorTypeEnum.正常关门)
                {
                    if (DateTime.Now.Hour < 9 || DateTime.Now.Hour > 17 || DateTime.Now.DayOfWeek == DayOfWeek.Sunday || DateTime.Now.DayOfWeek == DayOfWeek.Saturday)
                    {
                        cabinet.Status = (int)OperatorTypeEnum.非工作时间关门;
                        cabinet.Alarm  = OperatorTypeEnum.非工作时间关门.ToString();
                    }
                    else
                    {
                        cabinet.Status = (int)OperatorTypeEnum.正常关门;
                        cabinet.Alarm  = "";
                    }
                    cabinet.IsOpen = false;
                    Cabinet.Update(cabinet);
                }
                #endregion

                #region 开门确认
                else if (request.OperatorType == (int)OperatorTypeEnum.正常开门)
                {
                    if (DateTime.Now.Hour < 9 || DateTime.Now.Hour > 17 || DateTime.Now.DayOfWeek == DayOfWeek.Sunday || DateTime.Now.DayOfWeek == DayOfWeek.Saturday)
                    {
                        cabinet.Status = (int)OperatorTypeEnum.非工作时间开门;
                        cabinet.Alarm  = OperatorTypeEnum.非工作时间开门.ToString();
                    }
                    else
                    {
                        cabinet.Status = (int)OperatorTypeEnum.正常开门;
                        cabinet.Alarm  = "";
                    }
                    cabinet.IsOpen = true;
                    Cabinet.Update(cabinet);
                }
                #endregion

                #region 应急钥匙开门
                else if (request.OperatorType == (int)OperatorTypeEnum.应急钥匙开门)
                {
                    cabinet.Status = (int)OperatorTypeEnum.应急钥匙开门;
                    cabinet.Alarm  = OperatorTypeEnum.应急钥匙开门.ToString();
                    cabinet.IsOpen = true;
                    Cabinet.Update(cabinet);
                }
                #endregion

                #region 其他请求
                else
                {
                    cabinet.Status = request.OperatorType;
                    cabinet.Alarm  = Enum.GetName(typeof(OperatorTypeEnum), cabinet.Status);
                    Cabinet.Update(cabinet);
                }
                #endregion

                lock (logLock)
                {
                    msgID++;
                    log.ID = msgID;
                    CabinetLogQueue.Add(log);
                }
                return(Success());
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return(Failure("上报失败"));
            }
        }