public string DeleteDevice(string loginIdentifer, string devID)
        {
            JavaScriptObject obj2 = new JavaScriptObject();

            obj2.Add("Result", false);
            obj2.Add("Message", "");
            LoginUser loginUser = GlobalAppModule.GetLoginUser(loginIdentifer);

            if (loginUser == null)
            {
                obj2["Message"] = "未登录";
                return(JavaScriptConvert.SerializeObject(obj2));
            }
            if (loginUser.LoginTimeout)
            {
                obj2["Message"] = "登录超时";
                return(JavaScriptConvert.SerializeObject(obj2));
            }
            loginUser.LastOperateTime = DateTime.Now;
            CommonUtil.WaitMainLibInit();

            Device device = DeviceModule.GetDeviceByID(long.Parse(devID));

            if (device == null)
            {
                obj2["Result"]  = true;
                obj2["Message"] = "noDevice";
                return(JavaScriptConvert.SerializeObject(obj2));
            }

            string FullDeviceNo = DeviceModule.GetFullDeviceNoByID(device.Id);

            ResMsg msg = DeviceModule.DeleteDevice(long.Parse(devID));

            if (msg.Result)
            {
                GlobalAppModule.IsInitMainLib = true;
                try
                {
                    //更新网关设备缓存
                    Thread parameterThread = new Thread(new ParameterizedThreadStart(TcpRunThread.ParameterRun));
                    parameterThread.Start(ProtocolConst.WebToGateUpdateCache + ProtocolConst.UpdateCache_Device + "02" + FullDeviceNo.PadLeft(16, '0'));
                }
                catch (Exception exception)
                {
                    //new Guard().Logger(exception, "GetOperateDevice");
                    myLogger.Error(exception.Message);
                }
                GlobalAppModule.IsInitMainLib = false;
                obj2["Result"]  = true;
                obj2["Message"] = "成功";
            }
            else
            {
                obj2["Message"] = msg.Message;
            }
            try
            {
                //添加日志
                DeviceLog log = new DeviceLog();
                log.DeviceId               = device.Id;
                log.LogUserId              = loginUser.UserId;
                log.LogUserName            = loginUser.LoginName;
                log.LogAddress             = ToolsWeb.GetIP(context.Request);
                log.LogTime                = loginUser.LastOperateTime;
                log.LogType                = "删除";
                log.LogContent             = msg.Message + " | " + JavaScriptConvert.SerializeObject(device);
                log.SimNo                  = device.SimNo;
                log.DeviceName             = device.DeviceName;
                log.Description            = device.Description;
                log.SetupDate              = device.SetupDate;
                log.SetupAddress           = device.SetupAddress;
                log.LON                    = device.LON;
                log.LAT                    = device.LAT;
                log.IsValid                = device.IsValid;
                log.LastUpdate             = device.LastUpdate;
                log.DistrictId             = device.DistrictId;
                log.DeviceNo               = device.DeviceNo;
                log.Online                 = device.Online;
                log.OnlineTime             = device.OnlineTime;
                log.YearExploitation       = device.YearExploitation;
                log.AlertAvailableWater    = device.AlertAvailableWater;
                log.AlertAvailableElectric = device.AlertAvailableElectric;
                log.DeviceTypeCodeId       = device.DeviceTypeCodeId;
                log.MeterPulse             = device.MeterPulse;
                log.AlertWaterLevel        = device.AlertWaterLevel;
                log.TerminalState          = device.TerminalState;
                log.Remark                 = device.Remark;
                log.CropId                 = device.CropId;
                log.Area                   = device.Area;
                log.StationType            = device.StationType;
                log.StationCode            = device.StationCode;
                log.Frequency              = device.Frequency;
                log.MainId                 = device.MainId;
                log.DeviceType             = device.DeviceType;
                DeviceLogModule.Add(log);
            }
            catch
            {
            }
            return(JavaScriptConvert.SerializeObject(obj2));
        }
        public string AddDevice(string loginIdentifer, string deviceJSONString)
        {
            JavaScriptObject obj2 = new JavaScriptObject();

            obj2.Add("Result", false);
            obj2.Add("Message", "");
            LoginUser loginUser = GlobalAppModule.GetLoginUser(loginIdentifer);

            if (loginUser == null)
            {
                obj2["Message"] = "未登录";
                return(JavaScriptConvert.SerializeObject(obj2));
            }
            if (loginUser.LoginTimeout)
            {
                obj2["Message"] = "登录超时";
                return(JavaScriptConvert.SerializeObject(obj2));
            }
            JavaScriptObject obj3 = (JavaScriptObject)JavaScriptConvert.DeserializeObject(deviceJSONString);

            if (obj3 == null)
            {
                obj2["Message"] = "参数deviceJSONString格式不正确";
                return(JavaScriptConvert.SerializeObject(obj2));
            }
            loginUser.LastOperateTime = DateTime.Now;
            CommonUtil.WaitMainLibInit();

            Device device = new Device();

            device.SimNo       = obj3["手机卡号"].ToString();
            device.DeviceName  = obj3["名称"].ToString();
            device.Description = "";
            try
            {
                device.SetupDate = DateTime.Parse(obj3["安装时间"].ToString() + ":00");
            }
            catch
            {
                obj2["Message"] = "安装时间格式错误";
                return(JavaScriptConvert.SerializeObject(obj2));
            }
            device.SetupAddress = obj3["安装位置"].ToString();
            try
            {
                string[] arrMap = obj3["经纬度"].ToString().Trim().Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                device.LON = (long)(double.Parse(arrMap[0]) * 1000000);
                device.LAT = (long)(double.Parse(arrMap[1]) * 1000000);
            }
            catch { device.LON = 0; device.LAT = 0; }
            device.IsValid    = 1;
            device.LastUpdate = DateTime.Parse("2000-1-1");
            try
            {
                device.DistrictId = long.Parse(obj3["管理ID"].ToString());
            }
            catch
            {
                obj2["Message"] = "请选择区域";
                return(JavaScriptConvert.SerializeObject(obj2));
            }
            device.DeviceNo = obj3["编码"].ToString();
            try
            {
                if (device.DeviceNo.Length > 3)
                {
                    obj2["Message"] = "设备序号必须为整数,范围1-255";
                    return(JavaScriptConvert.SerializeObject(obj2));
                }
                int i = Convert.ToInt16(device.DeviceNo);
                if (i < 1 || i > 255)
                {
                    obj2["Message"] = "设备序号必须为整数,范围1-255";
                    return(JavaScriptConvert.SerializeObject(obj2));
                }
            }
            catch
            {
                obj2["Message"] = "设备序号必须为整数,范围1-255";
                return(JavaScriptConvert.SerializeObject(obj2));
            }

            device.Online     = 0;
            device.OnlineTime = DateTime.Parse("2000-1-1");
            try
            {
                device.YearExploitation = decimal.Parse(obj3["年可开采水量"].ToString());
            }
            catch { device.YearExploitation = 0; }
            try
            {
                device.AlertAvailableWater = int.Parse(obj3["可用水量提醒"].ToString());
            }
            catch { device.AlertAvailableWater = 0; }
            try
            {
                device.AlertAvailableElectric = int.Parse(obj3["可用电量提醒"].ToString());
            }
            catch { device.AlertAvailableElectric = 0; }
            try
            {
                device.DeviceTypeCodeId = int.Parse(obj3["流量计类型"].ToString());
            }
            catch { device.DeviceTypeCodeId = 0; }
            try
            {
                device.MeterPulse = int.Parse(obj3["电表脉冲数"].ToString());
            }
            catch { device.MeterPulse = 0; }
            try
            {
                device.AlertWaterLevel = decimal.Parse(obj3["水位报警值"].ToString());
            }
            catch { device.AlertWaterLevel = 0; }
            device.TerminalState = "正常";
            device.Remark        = "";
            try
            {
                //device.CropId = long.Parse(obj3["作物"].ToString());
                device.CropId = 0;
            }
            catch
            {
                device.CropId = 0;
                //obj2["Message"] = "请选择作物";
                //return JavaScriptConvert.SerializeObject(obj2);
            }
            try
            {
                device.Area = decimal.Parse(obj3["面积"].ToString());
            }
            catch
            {
                device.Area = 0;
            }
            try
            {
                device.StationType = int.Parse(obj3["站类型"].ToString());
            }
            catch
            {
                device.StationType = 0;
            }
            try
            {
                device.StationCode = int.Parse(obj3["地址码"].ToString());
                //device.StationCode = 0;
            }
            catch
            {
                device.StationCode = 0;
            }
            if (device.StationCode < 0 || device.StationCode > 65535)
            {
                //obj2["Message"] = "地址码必须为整数,范围0-65535";
                //return JavaScriptConvert.SerializeObject(obj2);
            }
            try
            {
                //device.Frequency = int.Parse(obj3["通信频率"].ToString());
                device.Frequency = 0;
            }
            catch
            {
                device.Frequency = 0;
            }
            try
            {
                string MainDevNo = obj3["主站编码"].ToString().PadLeft(3, '0');
                Device MainD     = DeviceModule.GetDeviceByDistrictId(device.DistrictId, MainDevNo);
                if (MainD != null)
                {
                    device.MainId = MainD.Id;
                }
            }
            catch
            {
                device.MainId = 0;
            }
            device.DeviceType    = obj3["设备类型"].ToString();
            device.RemoteStation = obj3["水位仪编码"].ToString().Trim();

            if (DeviceModule.ExistsSimNo(device.SimNo))
            {
                obj2["Message"] = "已存在手机号码";
                return(JavaScriptConvert.SerializeObject(obj2));
            }
            if (DeviceModule.ExistsDeviceName(device.DeviceName, device.DistrictId))
            {
                obj2["Message"] = "已存在设备名称";
                return(JavaScriptConvert.SerializeObject(obj2));
            }
            if (DeviceModule.ExistsDeviceNo(device.DeviceNo, device.DistrictId))
            {
                obj2["Message"] = "已存在设备编码";
                return(JavaScriptConvert.SerializeObject(obj2));
            }
            /*********update by kqz 2017-3-6***************/

            /*if (device.RemoteStation != null && device.RemoteStation.Length > 0)
             * {
             *  if (DeviceModule.ExistsRemoteStation(device.RemoteStation, device.Id))
             *  {
             *      obj2["Message"] = "已存在水位仪编码";
             *      return JavaScriptConvert.SerializeObject(obj2);
             *  }
             * }*/
            /*********update by kqz 2017-3-6***************/
            ResMsg msg = DeviceModule.AddDevice(device);

            if (msg.Result)
            {
                GlobalAppModule.IsInitMainLib = true;
                try
                {
                    //更新网关设备缓存
                    Thread parameterThread = new Thread(new ParameterizedThreadStart(TcpRunThread.ParameterRun));
                    parameterThread.Start(ProtocolConst.WebToGateUpdateCache + ProtocolConst.UpdateCache_Device + "01" + DeviceModule.GetFullDeviceNoByID(device.Id).PadLeft(16, '0'));
                }
                catch (Exception exception)
                {
                    //new Guard().Logger(exception, "GetOperateDevice");
                    myLogger.Error(exception.Message);
                }
                GlobalAppModule.IsInitMainLib = false;
                obj2["Result"]  = true;
                obj2["Message"] = "成功";
            }
            else
            {
                obj2["Message"] = msg.Message;
            }
            try
            {
                //添加日志
                DeviceLog log = new DeviceLog();
                log.DeviceId               = device.Id;
                log.LogUserId              = loginUser.UserId;
                log.LogUserName            = loginUser.LoginName;
                log.LogAddress             = ToolsWeb.GetIP(context.Request);
                log.LogTime                = loginUser.LastOperateTime;
                log.LogType                = "添加";
                log.LogContent             = msg.Message + " | " + JavaScriptConvert.SerializeObject(device);
                log.SimNo                  = device.SimNo;
                log.DeviceName             = device.DeviceName;
                log.Description            = device.Description;
                log.SetupDate              = device.SetupDate;
                log.SetupAddress           = device.SetupAddress;
                log.LON                    = device.LON;
                log.LAT                    = device.LAT;
                log.IsValid                = device.IsValid;
                log.LastUpdate             = device.LastUpdate;
                log.DistrictId             = device.DistrictId;
                log.DeviceNo               = device.DeviceNo;
                log.Online                 = device.Online;
                log.OnlineTime             = device.OnlineTime;
                log.YearExploitation       = device.YearExploitation;
                log.AlertAvailableWater    = device.AlertAvailableWater;
                log.AlertAvailableElectric = device.AlertAvailableElectric;
                log.DeviceTypeCodeId       = device.DeviceTypeCodeId;
                log.MeterPulse             = device.MeterPulse;
                log.AlertWaterLevel        = device.AlertWaterLevel;
                log.TerminalState          = device.TerminalState;
                log.Remark                 = device.Remark;
                log.CropId                 = device.CropId;
                log.Area                   = device.Area;
                log.StationType            = device.StationType;
                log.StationCode            = device.StationCode;
                log.Frequency              = device.Frequency;
                log.MainId                 = device.MainId;
                log.DeviceType             = device.DeviceType;
                DeviceLogModule.Add(log);
            }
            catch
            {
            }
            return(JavaScriptConvert.SerializeObject(obj2));
        }