예제 #1
0
        public ActionResult AddSmokeDetector(SmokeDetector entity)
        {
            long deviceCode = 0;

            long.TryParse(entity.Code, out deviceCode);
            if (deviceCode <= 0)
            {
                return(Json(new AjaxResult()
                {
                    Success = false, Message = "请扫描正确的设备二维码。"
                }));
            }
            //if (curentUser.UserType != UserType.Installer)
            //{
            //    return Json(new AjaxResult() { Success = false, Message = "您不是管理员,请绑定管理员账号。" });
            //}
            //if ((entity.AddressSysNo == null || entity.AddressSysNo <= 0) && (entity.Memo == "" || entity.Memo == null))
            //{
            //    return Json(new AjaxResult() { Success = false, Message = "请选择详细地址或者填写备注。" });
            //}
            if ((!entity.AddressSysNo.HasValue || entity.AddressSysNo <= 0) && string.IsNullOrWhiteSpace(entity.Position))
            {
                return(Json(new AjaxResult()
                {
                    Success = false, Message = "请选择地址或填写设备具体位置。"
                }));
            }
            Address address = AddressDA.LoadAddress(entity.AddressSysNo.GetValueOrDefault());

            if (address == null)
            {
                return(Json(new AjaxResult()
                {
                    Success = false, Message = "位置信息错误,请重新选择后再试。"
                }));
            }

            entity.AddressCode = address.Code;
            entity.AddressName = address.PathName;
            if ((address.ParentSysNo == 0 || address.ParentSysNo == null) && (address.PathName == "" || address.PathName == null))
            {
                entity.AddressName = address.Name;
            }
            entity.Status = SmokeDetectorStatus.Offline;
            if (curentUser.UserType == UserType.Installer)
            {
                entity.InstallerSysNo = curentUser.ManagerSysNo;
            }
            else
            {
                entity.InstallerSysNo = -curentUser.UserSysNo;
            }

            entity.InstallerName = HttpUtility.UrlDecode(curentUser.UserID);
            entity.InUserSysNo   = curentUser.UserSysNo;
            entity.InUserName    = HttpUtility.UrlDecode(curentUser.UserID);
            SmokeDetector     smoke      = SmokeDetectorServices.IsUniquenessCode(entity.Code);
            SystemUserService service    = new SystemUserService();
            SystemUser        systemuser = service.LoadSystemUser(curentUser.ManagerSysNo.GetValueOrDefault(), ConstValue.ApplicationID);

            if (systemuser == null)
            {
                UserMgr.Logout();
                return(Json(new AjaxResult()
                {
                    Success = false, Message = "登录超时,请退出公众号重新进入。"
                }));
            }
            //if (smoke != null && smoke.CompanySysNo != systemuser.MasterSysNo)
            //{
            //    return Json(new AjaxResult() { Success = false, Message = "设备已安装,不能再安装。" });
            //}
            //else
            if (smoke != null)
            {
                if (smoke.Status == SmokeDetectorStatus.Delete)
                {
                    entity.Status = SmokeDetectorStatus.Offline;
                }
                else
                {
                    entity.Status = smoke.Status;
                    if (smoke.InstallerSysNo <= 0)
                    {
                        if (smoke.InstallerSysNo != -curentUser.UserSysNo)
                        {
                            return(Json(new AjaxResult()
                            {
                                Success = false, Message = "设备已安装,不能再安装。"
                            }));
                        }
                    }
                    else
                    {
                        if (!(smoke.CompanySysNo == systemuser.MasterSysNo && curentUser.UserType == UserType.Installer))
                        {
                            return(Json(new AjaxResult()
                            {
                                Success = false, Message = "设备已安装,不能再安装。"
                            }));
                        }
                    }
                };
                CreateDeviceRequest reques = new CreateDeviceRequest()
                {
                    IMei     = entity.Code,
                    IMsi     = entity.Code,
                    Desc     = "测试设备",
                    IsOnLine = 1,
                    Observe  = 1,
                    Protocol = "LWM2M",
                    Title    = entity.Code,
                    Tags     = entity.Code + "," + entity.Code
                };
                CreateDeviceResponse result = ONENETService.CreateDevice(reques);
                if (result.IsSuccess)
                {
                    entity.DeviceId = result.DeviceId;
                }
                else
                {
                    entity.DeviceId = smoke.DeviceId;
                }

                entity.SysNo = smoke.SysNo;
                SmokeDetectorServices.UpdateSmokeDetector(entity);
                if (curentUser.UserType != UserType.Installer)
                {
                    Logger.WriteLog("Code:" + entity.Code);
                    BindingDevicesApi(entity.Code);
                }
                return(Json(new AjaxResult()
                {
                    Success = true, Message = "修改成功!"
                }));
                //}
                //else
                //{
                //    return Json(new AjaxResult() { Success = false, Message = "系统异常!" });
                //}
            }
            else
            {
                CreateDeviceRequest reques = new CreateDeviceRequest()
                {
                    IMei     = entity.Code,
                    IMsi     = entity.Code,
                    Desc     = "测试设备",
                    IsOnLine = 1,
                    Observe  = 1,
                    Protocol = "LWM2M",
                    Title    = entity.Code,
                    Tags     = entity.Code + "," + entity.Code
                };
                CreateDeviceResponse result = ONENETService.CreateDevice(reques);
                if (result.IsSuccess)
                {
                    entity.DeviceId = result.DeviceId;
                }
                int i = SmokeDetectorServices.InsertSmokeDetector(entity);
                SmokeDetectorStatusLog statusLog = new SmokeDetectorStatusLog()
                {
                    Status            = entity.Status,
                    BeginTime         = DateTimeHelper.GetTimeZoneNow(),
                    ReceivedJsonData  = "",
                    SmokeDetectorCode = entity.Code
                };
                if (curentUser.UserType != UserType.Installer)
                {
                    Logger.WriteLog("Code:" + entity.Code);
                    BindingDevicesApi(entity.Code);
                }

                return(Json(new AjaxResult()
                {
                    Success = true, Message = "添加成功!"
                }));
            }
        }