using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using ZigBee.Device; using Shared.Phone.UserCenter; using System.Security.Cryptography; using System.IO; namespace Shared.Common { /// /// 安防本地缓存(代码使用) /// 防区ID(ZoneId): /// 1:24小时防区 2:24小时静音防区 3:出入防区 4:内部防区 5:周界防区 /// 防区模式ID(ActionType -> 防区ID换算:ZoneId=3,4,5都当做3处理): /// 1:24小时防区 2:静音防区 3:其他防区(出入防区、内部防区、周界防区) /// 布防模式ID(GarrisonMode): /// 1:在家布防 或者 布防(内部防区没有设置的时候) 2:离家布防 /// public class LocalSafeguard { #region ■ 变量声明___________________________ /// /// 本地安防数据 /// private static LocalSafeguard m_Current = null; /// /// 本地安防数据 /// public static LocalSafeguard Current { get { if (m_Current == null) { m_Current = new LocalSafeguard(); } return m_Current; } set { m_Current = value; } } /// /// 本次登陆不再询问 /// public bool NotAskAgain = false; /// /// 管理员密码的缓存 /// private string adminPassword = string.Empty; /// /// 用户密码的缓存 /// private string UserPassword = null; /// /// 安防数据缓存 /// private LocalSafeguardData LocalData = new LocalSafeguardData(); /// /// 安防上报信息 /// public List listAlarmInfo = new List(); /// /// 上一次安防上报信息保存的文件名字(考虑到用户有可能24点的时候,不退出APP) /// private string oldDeviceAlarmFile = string.Empty; /// /// 锁 /// private object objLock = new object(); #endregion #region ■ 刷新安防___________________________ /// /// 刷新本地安防信息 /// public void ReFreshByLocal() { if (Global.IsExistsByHomeId(LocalSafeguardData.LocalSecurityFile) == false) { return; } byte[] filebyte = Global.ReadFileByHomeId(LocalSafeguardData.LocalSecurityFile); string strvalue = System.Text.Encoding.UTF8.GetString(filebyte); this.LocalData = JsonConvert.DeserializeObject(strvalue); } /// /// 从新从网关那里获取数据(失败时会弹出信息框) /// /// public async Task ReFreshByGateway(ShowErrorMode showError = ShowErrorMode.YES) { //先清空 this.LocalData.dicAllZoneInfo.Clear(); List listId = this.GetAllZoneId(); bool canGetData = false; foreach (int zoneId in listId) { //通过防区ID查看防区传感器设备列表 var result = await Safeguard.getZoneDeviceListByIdAsync(zoneId); if (result == null || result.getZoneDeviceListByIdResponData == null) { canGetData = false; break; } if (result.getZoneDeviceListByIdResponData.Result == 1) { //失败,布防模式不存在 continue; } canGetData = true; //将防区传感器设备列表加入缓存 this.SetZoneSensorDeviceToMemory(result.getZoneDeviceListByIdResponData); //将报警目标列表加入缓存 bool flage = await this.SetAlarmTargetDeviceToMemory(zoneId); if (flage == false) { canGetData = false; break; } } //全部错误,都获取不到信息 if (canGetData == false) { if (showError == ShowErrorMode.YES) { //获取防区信息失败 string msg = Language.StringByID(R.MyInternationalizationString.uGetSafetyInfoFail); this.ShowErrorMsg(msg); } return false; } return true; } /// /// 将防区设备(传感器)列表加入缓存 /// /// 防区数据 private void SetZoneSensorDeviceToMemory(Safeguard.GetZoneDeviceListByIdResponData allData) { if (this.LocalData.dicAllZoneInfo.ContainsKey(allData.ZoneId) == false) { this.LocalData.dicAllZoneInfo[allData.ZoneId] = new SafeguardZoneInfo(); } //设置基本信息 SafeguardZoneInfo zoneInfo = this.LocalData.dicAllZoneInfo[allData.ZoneId]; zoneInfo.ZoneId = allData.ZoneId; zoneInfo.ZoneName = allData.ZoneName; //处理设备 foreach (var data2 in allData.DeviceList) { string mainKey = data2.MacAddr + data2.Epoint; //传感器设备信息 var Deviceinfo = new Safeguard.ZoneDeviceListData(); Deviceinfo.IsBypass = data2.IsBypass; Deviceinfo.MomentStatus = data2.MomentStatus; Deviceinfo.TriggerZoneStatus = data2.TriggerZoneStatus; Deviceinfo.MacAddr = data2.MacAddr; Deviceinfo.Epoint = data2.Epoint; zoneInfo.dicSensor[mainKey] = Deviceinfo; //本地是否有这个设备 CommonDevice device = LocalDevice.Current.GetDevice(mainKey); if (device != null) { //全部设备 zoneInfo.dicAllDevice[mainKey] = device.FilePath; } else { //全部设备(缺失的未知设备) var tempDevice = new CommonDevice(); tempDevice.DeviceAddr = data2.MacAddr; tempDevice.DeviceEpoint = data2.Epoint; zoneInfo.dicAllDevice[mainKey] = tempDevice.FilePath; } } } /// /// 将报警目标列表加入缓存 /// /// /// 1:24小时防区触发动作 /// 2:24小时静音防区触发动作 /// 3:其他防区(出入防区、内部防区、周界防区)触发动作 /// /// 是否显示错误 /// private async Task SetAlarmTargetDeviceToMemory(int ActionType, ShowErrorMode mode = ShowErrorMode.NO) { if (ActionType > 3) { return true; } if (this.LocalData.dicAllZoneInfo.ContainsKey(ActionType) == false) { this.LocalData.dicAllZoneInfo[ActionType] = new SafeguardZoneInfo(); //设置基本信息 this.LocalData.dicAllZoneInfo[ActionType].ZoneId = ActionType; } SafeguardZoneInfo zoneInfo = this.LocalData.dicAllZoneInfo[ActionType]; //查看防区报警目标 var resultData = await Safeguard.CatZoneActionAsync(ActionType); if (resultData == null || resultData.catZoneActionResponseData == null || resultData.catZoneActionResponseData.Result == 1) { if (mode == ShowErrorMode.YES) { //获取报警目标列表失败 string msg = Language.StringByID(R.MyInternationalizationString.uGetAlarmTargetListFail); this.ShowErrorMsg(msg); } return false; } zoneInfo.dicAlarmDevice.Clear(); zoneInfo.dicScenes.Clear(); //信息推送 zoneInfo.InformationPush = resultData.catZoneActionResponseData.IsDisablePushMessage; foreach (var data in resultData.catZoneActionResponseData.Actions) { //设备 if (data.Type == 0) { //本地是否有这个设备 string mainKey = data.DeviceAddr + data.Epoint; //报警信息 zoneInfo.dicAlarmDevice[mainKey] = data; CommonDevice device = LocalDevice.Current.GetDevice(mainKey); if (device != null) { //全部设备 zoneInfo.dicAllDevice[mainKey] = device.FilePath; } else { //全部设备(缺失的未知设备) var tempDevice = new CommonDevice(); tempDevice.DeviceAddr = data.DeviceAddr; tempDevice.DeviceEpoint = data.Epoint; zoneInfo.dicAllDevice[mainKey] = tempDevice.FilePath; } } //场景 else if (data.Type == 1) { //场景 zoneInfo.dicScenes[data.ScenesId] = data.ESName; } } return true; } #endregion #region ■ 获取设备___________________________ /// /// 获取指定防区的所有传感器设备的信息 /// /// 防区ID(它似乎是唯一主键) /// public List GetSensorDevicesInfoByZoonID(int zoonId) { var list = new List(); if (this.LocalData.dicAllZoneInfo.ContainsKey(zoonId) == false) { return list; } SafeguardZoneInfo info = this.LocalData.dicAllZoneInfo[zoonId]; foreach (var data in info.dicSensor.Values) { list.Add(data); } return list; } /// /// 获取全部的传感器设备的信息(按防区分组) /// /// public Dictionary> GetAllSensorDeviceInfo() { var dic = new Dictionary>(); foreach (int ZoonId in this.LocalData.dicAllZoneInfo.Keys) { List list = this.GetSensorDevicesInfoByZoonID(ZoonId); if (list.Count > 0) { dic[ZoonId] = list; } } return dic; } /// /// 根据防区ID获取本地报警目标 /// /// 防区ID /// public List GetLocalAlarmTargetInfoByZoneId(int ZoneId) { if (ZoneId > 3) { ZoneId = 3; } if (this.LocalData.dicAllZoneInfo.ContainsKey(ZoneId) == false) { return new List(); } SafeguardZoneInfo zoneInfo = this.LocalData.dicAllZoneInfo[ZoneId]; //优先场景吧 var list = new List(); foreach (int sceneId in zoneInfo.dicScenes.Keys) { var objData = new Safeguard.CatActionResponseObj(); objData.Type = 1; objData.ScenesId = sceneId; objData.ESName = zoneInfo.dicScenes[sceneId]; list.Add(objData); } foreach (var data in zoneInfo.dicAlarmDevice.Values) { list.Add(data); } return list; } /// /// 获取指定传感器所处的防区ID(不存在则返回-1) /// /// 指定传感器设备 /// public int GetZoneIdByIASZone(CommonDevice device) { string mainkey = this.GetDeviceMainKeys(device); foreach (SafeguardZoneInfo info in this.LocalData.dicAllZoneInfo.Values) { if (info.dicSensor.ContainsKey(mainkey) == true) { return info.ZoneId; } } return -1; } #endregion #region ■ 添加传感器_________________________ /// /// 添加传感器设备到防区(失败时会弹出信息框) /// /// 防区ID(它似乎是唯一主键) /// 设备列表 public async Task AddSensorDevice(int zoonId, List listdevice) { //现获取当前的布防模式 var safetyMode = await this.GetSafetyMode(); if (safetyMode != null) { //当前正处于布防模式,无法添加设备 string msg = Language.StringByID(R.MyInternationalizationString.uCanNotAddDeviceInGarrisonMode); this.ShowErrorMsg(msg); return false; } //校验密码 bool result = await this.ShowSafetyAdminValidatedDialog(); if (result == false) { return false; } if (listdevice.Count == 0) { return true; } List listMomentStatus = new List(); List listTriggerStatus = new List(); foreach (var device in listdevice) { int MomentStatus = 0; int TriggerZoneStatus = 0; //获取安防传感器的瞬间状态设定值 this.GetSafeguardSensorMomentStatus(device, ref MomentStatus, ref TriggerZoneStatus); listMomentStatus.Add(MomentStatus); listTriggerStatus.Add(TriggerZoneStatus); } //添加设备到网关 List listSuccess = await this.AddSensorDeviceToGateway(zoonId, listdevice, listMomentStatus, listTriggerStatus); if (listSuccess == null) { return false; } //修改缓存 if (this.LocalData.dicAllZoneInfo.ContainsKey(zoonId) == false) { this.LocalData.dicAllZoneInfo[zoonId] = new SafeguardZoneInfo(); } SafeguardZoneInfo info = this.LocalData.dicAllZoneInfo[zoonId]; for (int i = 0; i < listdevice.Count; i++) { CommonDevice device = listdevice[i]; string mainkey = this.GetDeviceMainKeys(device); if (listSuccess.Contains(mainkey) == false) { //没有添加成功 continue; } Safeguard.ZoneDeviceListData sensorInfo = null; if (info.dicSensor.ContainsKey(mainkey) == true) { sensorInfo = info.dicSensor[mainkey]; } else { sensorInfo = new Safeguard.ZoneDeviceListData(); info.dicSensor[mainkey] = sensorInfo; sensorInfo.Epoint = device.DeviceEpoint; sensorInfo.MacAddr = device.DeviceAddr; sensorInfo.IsBypass = 0; } info.dicAllDevice[mainkey] = device.FilePath; //设备信息 sensorInfo.MomentStatus = listMomentStatus[0]; sensorInfo.TriggerZoneStatus = listTriggerStatus[0]; } //保存到本地 this.LocalData.SaveToLocaltion(); return true; } /// /// 添加传感器设备到网关(失败时会弹出信息框) /// /// 防区ID(它似乎是唯一主键) /// 设备列表 /// 设备上报的状态是否为瞬间状态 /// 我也不知道这个是什么东西 /// private async Task> AddSensorDeviceToGateway(int zoonId, List listdevice, List listMomentStatus, List listTriggerStatus) { var addData = new Safeguard.AddDeviceToZoneData(); addData.ZoneId = zoonId; addData.LoginToken = this.GetLoginToken(); for (int i = 0; i < listdevice.Count; i++) { CommonDevice device = listdevice[i]; //数据组装 var deviceData = new Safeguard.DeviceListObj(); deviceData.Epoint = device.DeviceEpoint; deviceData.MacAddr = device.DeviceAddr; deviceData.MomentStatus = listMomentStatus[i]; deviceData.TriggerZoneStatus = listTriggerStatus[i]; addData.DeviceList.Add(deviceData); } //没有数据 if (addData.DeviceList.Count == 0) { return new List(); } //添加到网关 var returnData = await Safeguard.AddDeviceToZoneAsync(addData); if (returnData == null || returnData.addDeviceToPartResponseData == null || returnData.addDeviceToPartResponseData.Result == 1) { //向防区添加设备失败 string msg = Language.StringByID(R.MyInternationalizationString.uAddDeviceToZoneFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, returnData.errorMessageBase); this.ShowErrorMsg(msg); return null; } List listSuccess = new List(); foreach (var data in returnData.addDeviceToPartResponseData.DeviceList) { //一批设备里面,成功添加的 if (data.Status == 0) { listSuccess.Add(data.MacAddr + data.Epoint); } else if (data.Status == 1) { var device = Common.LocalDevice.Current.GetDevice(data.MacAddr, data.Epoint); string msg = Common.LocalDevice.Current.GetDeviceEpointName(device) + "\r\n"; //目标设备不存在 msg += Language.StringByID(R.MyInternationalizationString.uTargetDeviceIsNotExsit); this.ShowTipMsg(msg); } else if (data.Status == 2) { var device = Common.LocalDevice.Current.GetDevice(data.MacAddr, data.Epoint); string msg = Common.LocalDevice.Current.GetDeviceEpointName(device) + "\r\n"; //设备已加入其它防区 msg += Language.StringByID(R.MyInternationalizationString.uDeviceHadAddToTheOtherGarrison); this.ShowTipMsg(msg); } } return listSuccess; } /// /// 获取安防传感器的瞬间状态设定值(添加传感器到安防时,需要调用此方法来初始化参数) /// /// 设备对象 /// 设备上报的状态是否为瞬间状态 /// 我也不知道这个是什么东西 public void GetSafeguardSensorMomentStatus(CommonDevice device, ref int MomentStatus, ref int TriggerZoneStatus) { //运动传感器 if (device.IasDeviceType == 13) { MomentStatus = 1; TriggerZoneStatus = 1; } //烟雾传感器 else if (device.IasDeviceType == 40) { MomentStatus = 1; TriggerZoneStatus = 1; } //水侵传感器 else if (device.IasDeviceType == 42) { MomentStatus = 1; TriggerZoneStatus = 1; } //燃气传感器 else if (device.IasDeviceType == 43) { MomentStatus = 1; TriggerZoneStatus = 1; } //紧急按钮 else if (device.IasDeviceType == 44) { MomentStatus = 1; TriggerZoneStatus = 1; } //钥匙扣 else if (device.IasDeviceType == 277) { MomentStatus = 1; TriggerZoneStatus = 1; } //门窗传感器 else if (device.IasDeviceType == 21 || device.IasDeviceType == 22) { MomentStatus = 0; TriggerZoneStatus = 1; } //如果是虚拟设备,则这个东西永恒为0 if (device.DriveCode > 0) { MomentStatus = 0; } } #endregion #region ■ 删除设备___________________________ /// /// 删除传感器设备 /// /// 防区ID(它似乎是唯一主键) /// 传感器设备对象 public async Task DeleteSensorDevice(int zoonId, List listdevice) { //现获取当前的布防模式 var safetyMode = await this.GetSafetyMode(); if (safetyMode != null) { //当前正处于布防模式,无法删除设备 string msg = Language.StringByID(R.MyInternationalizationString.uCanNotDeleteDeviceInGarrisonMode); this.ShowErrorMsg(msg); return false; } //校验密码 bool result = await this.ShowSafetyAdminValidatedDialog(); if (result == false) { return false; } if (listdevice.Count == 0) { return true; } //从网关那里删除设备 List listKeys = await this.DeleteSensorDeviceFromGateway(zoonId, listdevice); if (listKeys == null) { return false; } //修改缓存 if (this.LocalData.dicAllZoneInfo.ContainsKey(zoonId) == false) { return true; } SafeguardZoneInfo info = this.LocalData.dicAllZoneInfo[zoonId]; foreach (CommonDevice device in listdevice) { string mainkey = this.GetDeviceMainKeys(device); if (info.dicSensor.ContainsKey(mainkey) == true && listKeys.Contains(mainkey) == true) { info.dicSensor.Remove(mainkey); info.dicAllDevice.Remove(mainkey); } } //保存到本地 this.LocalData.SaveToLocaltion(); return true; } /// /// 从网关那里删除传感器设备 /// /// 防区ID(它似乎是唯一主键) /// 设备对象 /// private async Task> DeleteSensorDeviceFromGateway(int zoonId, List listdevice) { var deteleData = new Safeguard.RemoveEqToZoneData(); deteleData.ZoneId = zoonId; deteleData.LoginToken = this.GetLoginToken(); foreach (CommonDevice device in listdevice) { //组装数据 var removeData = new Safeguard.RemoveDeviceListObj(); removeData.MacAddr = device.DeviceAddr; removeData.Epoint = device.DeviceEpoint; deteleData.RemoveDeviceList.Add(removeData); } //没有数据 if (deteleData.RemoveDeviceList.Count == 0) { return new List(); } //从网关中移除 var returnData = await Safeguard.RemoveDeviceToZoneAsync(deteleData); if (returnData == null || returnData.removeDeviceToZoneResponseData == null || returnData.removeDeviceToZoneResponseData.Result == 1) { //设备删除失败 string msg = Language.StringByID(R.MyInternationalizationString.uDeviceDeleteFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, returnData.errorMessageBase); this.ShowErrorMsg(msg); return null; } List listKeys = new List(); foreach (var data in returnData.removeDeviceToZoneResponseData.RemoveDeviceList) { if (data.Status == 0) { listKeys.Add(data.MacAddr + data.Epoint); } } return listKeys; } #endregion #region ■ 存在检测___________________________ /// /// 指定传感器设备是否存在于安防网关 /// /// /// public bool IsSensorDeviceExist(CommonDevice device) { string mainkey = this.GetDeviceMainKeys(device); foreach (SafeguardZoneInfo info in this.LocalData.dicAllZoneInfo.Values) { if (info.dicSensor.ContainsKey(mainkey) == true) { return true; } } return false; } /// /// 指定传感器设备是否存在于安防网关 /// /// 防区ID /// /// public bool IsSensorDeviceExist(int ZoneId, CommonDevice device) { if (this.LocalData.dicAllZoneInfo.ContainsKey(ZoneId) == false) { return false; } string mainkey = this.GetDeviceMainKeys(device); SafeguardZoneInfo info = this.LocalData.dicAllZoneInfo[ZoneId]; if (info.dicSensor.ContainsKey(mainkey) == true) { return true; } return false; } /// /// 指定报警设备是否存在于安防网关 /// /// 防区ID /// /// public bool IsAlarmDeviceExist(int ZoneId, CommonDevice device) { if (ZoneId < 3) { if (this.LocalData.dicAllZoneInfo.ContainsKey(ZoneId) == false) { return false; } string mainkey = this.GetDeviceMainKeys(device); SafeguardZoneInfo info = this.LocalData.dicAllZoneInfo[ZoneId]; if (info.dicAlarmDevice.ContainsKey(mainkey) == true) { return true; } } else { string mainkey = this.GetDeviceMainKeys(device); for (int i = 3; i <= 5; i++) { if (this.LocalData.dicAllZoneInfo.ContainsKey(i) == false) { continue; } SafeguardZoneInfo info = this.LocalData.dicAllZoneInfo[i]; if (info.dicAlarmDevice.ContainsKey(mainkey) == true) { return true; } } } return false; } /// /// 指定布防是否存在于安防网关 /// /// 防区ID(它似乎是唯一主键) /// public bool IsZoonExist(int zoonId) { return this.LocalData.dicAllZoneInfo.ContainsKey(zoonId); } /// /// 是否设置有内部防区 /// /// public bool IsHadInternalDefenseArea() { foreach (SafeguardZoneInfo info in this.LocalData.dicAllZoneInfo.Values) { //存在第四防区 if (info.ZoneId == 4) { //里面有没有设备 if (info.dicSensor.Count > 0) { return true; } //不再往下循环 return false; } } return false; } #endregion #region ■ 安防登陆___________________________ /// /// 主用户登陆(失败时会弹出信息框) /// /// /// public async Task AdminLogin(string password, ShowErrorMode showMode = ShowErrorMode.YES) { //尝试登陆 var resultData = await ZigBee.Device.Safeguard.AdminLoginResponAsync(password, this.GetLoginToken()); if (resultData != null && resultData.Result != 0) { if (showMode == ShowErrorMode.NO) { return false; } //管理员密码错误 string msg = Language.StringByID(R.MyInternationalizationString.uAdministratorPasswordIsError); this.ShowErrorMsg(msg); return false; } return true; } /// /// 获取指定用户的密码,如果用户不存在,则返回null(错误不会弹出信息) /// /// /// public async Task GetUserPassword(int userId) { //先获取密码 var nowPw = await Safeguard.CatUserPasswordAsync(this.GetLoginToken()); if (nowPw == null || nowPw.catUserPasswordResponseData == null) { return null; } foreach (var pw in nowPw.catUserPasswordResponseData.UserPasswordList) { if (pw.UserId == userId) { return pw.Password; } } return null; } /// /// 获取全部用户的密码(错误会弹出信息) /// /// 是否显示错误 /// public async Task> GetAllUserPassword(ShowErrorMode mode = ShowErrorMode.YES) { //先获取密码 var nowPw = await Safeguard.CatUserPasswordAsync(this.GetLoginToken()); if (nowPw == null || nowPw.catUserPasswordResponseData == null) { //获取安防密码失败 string msg = Language.StringByID(R.MyInternationalizationString.uGetSafetyPasswordFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, nowPw.errorMessageBase); if (mode == ShowErrorMode.YES) { this.ShowErrorMsg(msg); } return null; } return nowPw.catUserPasswordResponseData.UserPasswordList; } /// /// 修改用户密码(不存在时,则新建) /// /// /// /// public async Task ChangedUserPassword(int userId, string password) { //创建新用户 var result = await Safeguard.SetUserPasswordAsync(userId, password, this.GetLoginToken()); if (result == null || result.setUserPasswordResponseData == null) { if (userId != 5) { //修改用户密码失败 string msg = Language.StringByID(R.MyInternationalizationString.uChangedUserPasswordFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); return false; } else { //修改胁迫密码失败 string msg = Language.StringByID(R.MyInternationalizationString.uChangedCoercePasswordFail); this.ShowErrorMsg(msg); return false; } } if (result.setUserPasswordResponseData.Result == 1) { //用户密码数已满(最大4个) string msg = Language.StringByID(R.MyInternationalizationString.uUserPasswordCountIsMax); this.ShowErrorMsg(msg); return false; } if (result.setUserPasswordResponseData.Result == 2) { //密码长度不正确 string msg = Language.StringByID(R.MyInternationalizationString.uPasswordLengthIsError); this.ShowErrorMsg(msg); return false; } if (result.setUserPasswordResponseData.Result == 3) { //用户密码重复 string msg = Language.StringByID(R.MyInternationalizationString.uUserPasswordIsRepeat); this.ShowErrorMsg(msg); return false; } return true; } /// /// 显示校验管理员密码的窗口 /// /// public async Task ShowSafetyAdminValidatedDialog() { //自动登录 if (this.NotAskAgain == true) { //解码 string psw = this.DecryptPassword(this.adminPassword); bool flage = await this.AdminLogin(psw, ShowErrorMode.NO); if (flage == true) { return true; } this.NotAskAgain = false; //如果登录不成功的话,则弹出再次输入的窗口 } Phone.UserCenter.SafetyAdminValidatedControl DialogForm = null; bool isShowingProgressBar = false; string ProgressBarText = string.Empty; Application.RunOnMainThread(() => { isShowingProgressBar = CommonPage.Loading.Visible; ProgressBarText = CommonPage.Loading.Text; if (isShowingProgressBar == true) { //如果在弹出校验密码框的时候,显示着进度条的话,先暂时关闭进度条 CommonPage.Loading.Hide(); } DialogForm = new Phone.UserCenter.SafetyAdminValidatedControl(); }); while (DialogForm.IsSuccess == false) { //没有父控件,即:按下了取消键 if (DialogForm.Parent == null) { //恢复进度条 if (isShowingProgressBar == true) { Application.RunOnMainThread(() => { CommonPage.Loading.Start(ProgressBarText); }); } return false; } await Task.Delay(500); } //恢复进度条 if (isShowingProgressBar == true) { Application.RunOnMainThread(() => { CommonPage.Loading.Start(ProgressBarText); }); } return true; } /// /// 修改管理员密码 /// /// /// /// public async Task ChangedAdminPassword(string oldPsw, string newPsw) { //执行修改 var result = await Safeguard.ChangeAdminPasswordAsync(oldPsw, newPsw); if (result == null || result.changeAdminPasswordResponseData == null) { //修改管理员密码失败 string msg = Language.StringByID(R.MyInternationalizationString.uChangedAdministratorPasswordFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); return false; } if (result.changeAdminPasswordResponseData.Result == 1) { //原密码不一致 string msg = Language.StringByID(R.MyInternationalizationString.uOldPsswordIsError); this.ShowErrorMsg(msg); return false; } if (result.changeAdminPasswordResponseData.Result == 2) { //密码长度不正确 string msg = Language.StringByID(R.MyInternationalizationString.uPasswordLengthIsError); this.ShowErrorMsg(msg); return false; } //更改缓存 this.adminPassword = this.EncryptPassword(result.changeAdminPasswordResponseData.NewPassword); return true; } /// /// 检测是否已经配置有用户密码(内部会弹出错误信息,-1:异常 0:没有配置 1:已经配置) /// /// public async Task CheckHadConfigureUserPsw() { var pswInfo = await this.GetAllUserPassword(ShowErrorMode.NO); if (pswInfo == null) { //出现未知错误,请稍后再试 string msg = Language.StringByID(R.MyInternationalizationString.uUnKnowErrorAndResetAgain); this.ShowTipMsg(msg); return -1; } foreach (var data in pswInfo) { //4个用户密码 if (data.UserId >= 1 && data.UserId <= 4) { return 1; } } return 0; } /// /// 保存管理员密码到本地 /// /// public void SetAdminPswInMenmory(string psw) { this.adminPassword = this.EncryptPassword(psw); } #endregion #region ■ 报警目标___________________________ /// /// 添加报警目标到安防 /// /// 防区ID /// 添加的目标 /// public async Task AddAlarmTagetToSafety(int ZoneId, List listAction) { //现获取当前的布防模式 var safetyMode = await this.GetSafetyMode(); if (safetyMode != null) { //当前正处于布防模式,无法添加设备 string msg = Language.StringByID(R.MyInternationalizationString.uCanNotAddDeviceInGarrisonMode); this.ShowErrorMsg(msg); return false; } //验证身份 bool login = await this.ShowSafetyAdminValidatedDialog(); if (login == false) { return false; } if (ZoneId > 3) { ZoneId = 3; } var saveData = new Safeguard.AddZoneActionData(); saveData.ActionType = ZoneId; saveData.Actions = listAction; saveData.LoginToken = this.GetLoginToken(); //添加报警目标到安防 var result = await Safeguard.AddZoneActionAsync(saveData); if (result == null || result.addZoneActionResponseData == null || result.addZoneActionResponseData.Result == 1) { //添加报警目标失败 string msg = Language.StringByID(R.MyInternationalizationString.uAddAlarmTargetFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); return false; } foreach (var data in result.addZoneActionResponseData.Actions) { if (data.Status == "1") { if (data.Type == "0") { var device = Common.LocalDevice.Current.GetDevice(data.DeviceAddr, data.Epoint); string msg = Common.LocalDevice.Current.GetDeviceEpointName(device) + "\r\n"; //目标设备不存在 msg += Language.StringByID(R.MyInternationalizationString.uTargetDeviceIsNotExsit); this.ShowTipMsg(msg); } else if (data.Type == "1") { var scene = Common.SceneRoomUI.AllSceneRoomUIList.Find((obj) => obj.sceneUI.Id == data.ScenesId); if (scene != null) { string msg = scene.sceneUI.Name + "\r\n"; //目标场景不存在 msg += Language.StringByID(R.MyInternationalizationString.uTargetSceneIsNotExsit); this.ShowTipMsg(msg); } } } } //将报警目标列表加入缓存(从新从网关那里获取新追加的设备) await this.SetAlarmTargetDeviceToMemory(ZoneId, ShowErrorMode.YES); return true; } /// /// 删除报警目标 /// /// 防区ID /// 删除的目标 /// public async Task DeleteAlarmTaget(int ZoneId, List listActions) { //现获取当前的布防模式 var safetyMode = await this.GetSafetyMode(); if (safetyMode != null) { //当前正处于布防模式,无法删除设备 string msg = Language.StringByID(R.MyInternationalizationString.uCanNotDeleteDeviceInGarrisonMode); this.ShowErrorMsg(msg); return false; } //验证身份 bool login = await this.ShowSafetyAdminValidatedDialog(); if (login == false) { return false; } if (ZoneId > 3) { ZoneId = 3; } //参数 var Pra = new Safeguard.DelZoneActionData(); Pra.ActionType = ZoneId; Pra.Actions = listActions; Pra.LoginToken = this.GetLoginToken(); //执行删除 var result = await Safeguard.DelZoneActionAsync(Pra); if (result == null || result.delZoneActionResponseData == null || result.delZoneActionResponseData.Result == 1) { //删除报警目标失败 string msg = Language.StringByID(R.MyInternationalizationString.uDeleteAlarmTargetFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); return false; } //获取本地缓存 if (this.LocalData.dicAllZoneInfo.ContainsKey(ZoneId) == false) { return true; } SafeguardZoneInfo zoneInfo = this.LocalData.dicAllZoneInfo[ZoneId]; //修改缓存 foreach (var device in result.delZoneActionResponseData.Actions) { //删除失败 if (device.Status == "1") { continue; } //设备 if (device.Type == "0") { //删除报警设备 string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(device.DeviceAddr, device.Epoint); if (zoneInfo.dicAlarmDevice.ContainsKey(mainkeys) == false) { continue; } zoneInfo.dicAlarmDevice.Remove(mainkeys); zoneInfo.dicAllDevice.Remove(mainkeys); } //场景 else if (device.Type == "1") { //删除场景 if (zoneInfo.dicScenes.ContainsKey(device.ScenesId) == true) { zoneInfo.dicScenes.Remove(device.ScenesId); } } } return true; } #endregion #region ■ 获取场景___________________________ /// /// 根据防区ID获取本地的场景 /// /// 防区ID /// public Dictionary GetLocalSceneByZoneID(int ZoneId) { if (ZoneId > 3) { ZoneId = 3; } if (this.LocalData.dicAllZoneInfo.ContainsKey(ZoneId) == false) { return new Dictionary(); } var infoData = this.LocalData.dicAllZoneInfo[ZoneId]; return infoData.dicScenes; } #endregion #region ■ 旁路设置___________________________ /// /// 旁路状态设置 /// /// 防区ID /// 传感器设备 /// 旁路状态 -> 0:不旁路 1:旁路 /// public async Task SetByPassStatuToSafety(int zoneId, CommonDevice device, int statu) { //验证 bool flage = await this.ShowSafetyAdminValidatedDialog(); if (flage == false) { return false; } //参数 var Pra = new Safeguard.EqByPassData(); Pra.ZoneId = zoneId; Pra.MacAddr = device.DeviceAddr; Pra.Epoint = device.DeviceEpoint; Pra.IsByPass = statu; Pra.LoginToken = this.GetLoginToken(); var result = await Safeguard.EqByPassAllDataAsync(Pra); if (result == null || result.eqByPassResponseData != null && result.eqByPassResponseData.Result == 3) { //设置失败,系统当前处于撤防状态 string msg2 = Language.StringByID(R.MyInternationalizationString.uByPassFailAndSystemInWithdrawGarrisonStatu); this.ShowErrorMsg(msg2); return false; } //其他错误 if (result.eqByPassResponseData == null || result.eqByPassResponseData.Result != 0) { //旁路状态设置失败 string msg = Language.StringByID(R.MyInternationalizationString.uSetByPassStatuFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); return false; } return true; } #endregion #region ■ 布防撤防___________________________ /// /// 设置安防的布防模式,成功时,内部不会提示信息(不要调用此方法来实现【撤防】) /// /// 布防模式 /// 网关现在实行的布防模式 public async Task SetSafetyGarrisonByModel(GarrisonMode garrison) { //检测其他防区里面有没有配置有传感器 if (this.CheckHadInteriorSectorsSensor() == false) { //其他防区里没有配置传感器设备 string msg = Language.StringByID(R.MyInternationalizationString.uNotSensorInOtherSectors); this.ShowErrorMsg(msg); return GarrisonMode.None; } if (this.UserPassword == null) { var data = Global.ReadFileByDirectory(DirNameResourse.LocalMemoryDirectory, DirNameResourse.SafeguardUserPassword); if (data != null) { this.UserPassword = System.Text.Encoding.UTF8.GetString(data); } } if (string.IsNullOrEmpty(this.UserPassword) == true) { //输入密码 string password = await this.ShowInputUserPasswordForm(); if (string.IsNullOrEmpty(password) == true) { return GarrisonMode.None; } var result = await this.SetSafetyGarrisonByModel(garrison, false); if (result != GarrisonMode.None && result != GarrisonMode.RemoveGarrison) { //保存加密的密码到本地 Global.WriteFileToDirectoryByBytes(DirNameResourse.LocalMemoryDirectory, DirNameResourse.SafeguardUserPassword, System.Text.Encoding.UTF8.GetBytes(this.UserPassword)); } return result; } else { return await this.SetSafetyGarrisonByModel(garrison, true); } } /// /// 设置安防的布防模式,成功时,内部不会提示信息(不要调用此方法来实现【撤防】) /// /// 布防模式 /// 用户密码错误时,是否允许弹出输入用户密码的窗口 /// 网关现在实行的布防模式 private async Task SetSafetyGarrisonByModel(GarrisonMode garrison, bool showPswForm) { //先把当前的模式给移除掉 bool flage = await this.RemoveSafetyGarrison(garrison, showPswForm); if (flage == false) { return GarrisonMode.None; } //参数 var Pra = new Safeguard.EnableModeData(); //模式ID Pra.ModeId = (int)garrison; //检查防区设备最近上报的安防信息状态进行布防 Pra.CheckIASStatus = 1; //永久布防,直到撤防 Pra.Setting = 1; //用户密码 Pra.UserPassword = this.DecryptPassword(this.UserPassword); //执行布防 var result = await Safeguard.EnableModeAsync(Pra); if (result == null || result.enableModeResponseData == null) { //布防设置失败 string msg = Language.StringByID(R.MyInternationalizationString.uSetGarrisonFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); return GarrisonMode.RemoveGarrison; } //错误检测 if (result.enableModeResponseData.Result == 1) { //布防模式不存在 string msg = Language.StringByID(R.MyInternationalizationString.uGarrisonModeIsNotEsixt); this.ShowErrorMsg(msg); return GarrisonMode.RemoveGarrison; } else if (result.enableModeResponseData.Result == 2) { //清空密码缓存 this.UserPassword = null; if (showPswForm == false) { //用户密码错误或未设置用户密码 string msg = Language.StringByID(R.MyInternationalizationString.uUserPasswordIsError); this.ShowErrorMsg(msg); return GarrisonMode.RemoveGarrison; } else { //重新显示输入用户密码的窗口 string password = await this.ShowInputUserPasswordForm(); if (string.IsNullOrEmpty(password) == true) { return GarrisonMode.RemoveGarrison; } var result2 = await this.SetSafetyGarrisonByModel(garrison, false); if (result2 != GarrisonMode.None && result2 != GarrisonMode.RemoveGarrison) { //保存加密密码到本地 Global.WriteFileToDirectoryByBytes(DirNameResourse.LocalMemoryDirectory, DirNameResourse.SafeguardUserPassword, System.Text.Encoding.UTF8.GetBytes(this.UserPassword)); } return result2; } } else if (result.enableModeResponseData.Result == 3) { //安防设备未就绪 string msg = Language.StringByID(R.MyInternationalizationString.uSafetyDeviceDoNotReady); msg += "(" + result.enableModeResponseData.IASName + ")"; this.ShowErrorMsg(msg); return GarrisonMode.RemoveGarrison; } else if (result.enableModeResponseData.Result == 4) { //其他布防模式正在启用中 string msg = Language.StringByID(R.MyInternationalizationString.uOtherGarrisonIsActtion); this.ShowErrorMsg(msg); return (GarrisonMode)result.enableModeResponseData.ModeIdBeUsing; } else if (result.enableModeResponseData.Result == 5) { //模式属性不允许失能 string msg = Language.StringByID(R.MyInternationalizationString.uGarrisonModeElementCanNotLostFunction); this.ShowErrorMsg(msg); return GarrisonMode.RemoveGarrison; } //保存布防操作信息到本地 this.SaveSafeguardAlarmInfo(garrison); return garrison; } /// /// 移除当前正在运行的布防模式(成功时,内部不会提示信息) /// /// 布防模式(这个变量只是为了变更提示错误信息,当不是撤防时,错误提示信息会改变) /// 用户密码错误时,是否允许弹出输入用户密码的窗口 /// public async Task RemoveSafetyGarrison(GarrisonMode garrison, bool showPswForm) { //现获取当前的布防模式 var safetyMode = await this.GetSafetyMode(); if (safetyMode == null) { //如果是撤防的话 if (garrison == GarrisonMode.RemoveGarrison) { //当前不存在布防 string msg = Language.StringByID(R.MyInternationalizationString.uNowDoNotHadGarrison); this.ShowErrorMsg(msg); return false; } //并没有设置布防,也就不谈什么撤防了 return true; } //撤防的时候,无条件弹出输入密码框 if (garrison == GarrisonMode.RemoveGarrison) { string psw = await this.ShowInputUserPasswordForm(); if (string.IsNullOrEmpty(psw) == true) { return false; } } //撤防 var result = await Safeguard.WithdrawModeAsync(this.DecryptPassword(this.UserPassword)); if (result == null || result.withdrawModeResponseData == null) { //执行撤防操作的时候 if (garrison == GarrisonMode.RemoveGarrison) { //撤防失败 string msg = Language.StringByID(R.MyInternationalizationString.uRemoveGarrisonFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); } else { //布防模式变更失败 string msg = Language.StringByID(R.MyInternationalizationString.uChangedGarrisonModeFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); } return false; } if (result.withdrawModeResponseData.Result == 2) { //清空密码缓存 this.UserPassword = null; if (showPswForm == false || garrison == GarrisonMode.RemoveGarrison) { //用户密码错误或未设置用户密码 string msg = Language.StringByID(R.MyInternationalizationString.uUserPasswordIsError); this.ShowErrorMsg(msg); return false; } else { //重新显示输入用户密码的窗口 string password = await this.ShowInputUserPasswordForm(); if (string.IsNullOrEmpty(password) == true) { return false; } var result2 = await this.RemoveSafetyGarrison(garrison, false); if (result2 == true) { //保存到加密密码本地 Global.WriteFileToDirectoryByBytes(DirNameResourse.LocalMemoryDirectory, DirNameResourse.SafeguardUserPassword, System.Text.Encoding.UTF8.GetBytes(this.UserPassword)); } return result2; } } else if (result.withdrawModeResponseData.Result == 3 && garrison == GarrisonMode.RemoveGarrison) { //当前模式不可撤防 string msg = Language.StringByID(R.MyInternationalizationString.uNowGarrisonCanNotRemove); this.ShowErrorMsg(msg); return false; } if (garrison == GarrisonMode.RemoveGarrison) { //保存布防操作信息到本地 this.SaveSafeguardAlarmInfo(garrison); } return true; } /// /// 获取当前安防的模式(在家,离家等等),如果没有布防,则返回null /// public async Task GetSafetyMode() { var result = await Safeguard.GetModeUsingAsync(); if (result == null || result.getModeUsingResponseData == null) { return null; } if (result.getModeUsingResponseData.Result == 1) { return null; } return result.getModeUsingResponseData; } /// /// 显示输入用户密码的界面(返回null时代表出错或者取消) /// public async Task ShowInputUserPasswordForm() { //获取当前正在激活的画面 UserCenterCommonForm form = UserCenterLogic.GetNowActionForm(); if (form == null) { //这种情况应该不存在 this.ShowErrorMsg("ERROR:Not Found The ActionForm!"); return null; } DialogInputFrameControl Dialogform = null; bool isShowingProgressBar = false; string ProgressBarText = string.Empty; string pasword = null; Application.RunOnMainThread(() => { isShowingProgressBar = CommonPage.Loading.Visible; ProgressBarText = CommonPage.Loading.Text; if (isShowingProgressBar == true) { //如果在弹出校验密码框的时候,显示着进度条的话,先暂时关闭进度条 CommonPage.Loading.Hide(); } Dialogform = new DialogInputFrameControl(form, DialogFrameMode.OnlyPassword); //用户密码 Dialogform.SetTitleText(Language.StringByID(R.MyInternationalizationString.uUserPassword)); //设置提示信息:请输入用户密码 Dialogform.SetTipText(Language.StringByID(R.MyInternationalizationString.uPleaseInputUserPassword)); //确认按钮 Dialogform.ComfirmClickEvent += (() => { if (Dialogform.InputText == string.Empty) { this.ShowErrorMsg(Language.StringByID(R.MyInternationalizationString.uPleaseInputUserPassword)); return; } Dialogform.CloseDialog(); //用户密码 pasword = Dialogform.InputText; //加密密码 this.UserPassword = this.EncryptPassword(pasword); }); }); while (pasword == null) { if (Dialogform != null && Dialogform.Parent == null) { break; } await Task.Delay(500); } //恢复进度条 if (isShowingProgressBar == true) { Application.RunOnMainThread(() => { CommonPage.Loading.Start(ProgressBarText); }); } return pasword; } /// /// 检测其他防区里面有没有配置有传感器(布防使用) /// /// private bool CheckHadInteriorSectorsSensor() { for (int i = 3; i <= 5; i++) { if (LocalData.dicAllZoneInfo.ContainsKey(i) == true && LocalData.dicAllZoneInfo[i].dicSensor.Count > 0) { return true; } } return false; } #endregion #region ■ 信息推送___________________________ /// /// 设置指定防区的信息推送状态 /// /// 防区ID /// 状态 0:推送 1:不推送 /// public async Task SetGarrisonInformationPushStatu(int zoneId, int statu) { //校验密码 bool flage = await this.ShowSafetyAdminValidatedDialog(); if (flage == false) { return false; } if (zoneId > 3) { zoneId = 3; } //状态变更 var result = await Safeguard.DisablePushMessageAsync(zoneId, statu); if (result == null || result.disablePushMessageResponseData == null || result.disablePushMessageResponseData.Result == 1) { //设置信息推送失败 string msg = Language.StringByID(R.MyInternationalizationString.uSetInformationPushFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); return false; } if (this.LocalData.dicAllZoneInfo.ContainsKey(zoneId) == false) { return true; } SafeguardZoneInfo zoneInfo = this.LocalData.dicAllZoneInfo[zoneId]; zoneInfo.InformationPush = statu; return true; } /// /// 获取当前防区的信息推送状态 /// /// 防区ID /// 0:推送 1:不推送 public int GetGarrisonInformationPushStatu(int zoneId) { if (zoneId > 3) { zoneId = 3; } if (this.LocalData.dicAllZoneInfo.ContainsKey(zoneId) == false) { return 1; } SafeguardZoneInfo zoneInfo = this.LocalData.dicAllZoneInfo[zoneId]; return zoneInfo.InformationPush; } #endregion #region ■ 延迟设置___________________________ /// /// 获取防区的延迟时间(仅限出入防区),出错时返回null /// /// public async Task GetGarrisonDelayTime() { //获取 var result = await Safeguard.CatDelayTimeAsync(); if (result == null || result.catDelayTimeResponseData == null) { //获取延时时间失败 string msg = Language.StringByID(R.MyInternationalizationString.uGetDelayTimeFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); return null; } return result.catDelayTimeResponseData; } /// /// 设置防区的进出延迟时间 (仅限出入防区),现接口不支持单个修改 /// /// 进入延时(秒) /// 外出延时(秒) /// public async Task SetGarrisonDelayTime(int enterDelayTime, int goOutDelayTime) { //校验密码 bool flage = await this.ShowSafetyAdminValidatedDialog(); if (flage == false) { return false; } //更改时间 var result = await Safeguard.SetDelayTimeAsync(enterDelayTime, goOutDelayTime, this.GetLoginToken()); if (result == null || result.setDelayTimeResponseData == null) { //设置延时时间失败 string msg = Language.StringByID(R.MyInternationalizationString.uSetDelayTimeFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); return false; } return true; } #endregion #region ■ 胁迫设置___________________________ /// /// 获取胁迫的电话联系方式(获取失败时会返回null) /// /// public async Task GetCoercePhoneNumber() { //获取 var result = await Safeguard.CheckCoercePhoneNumberAsync(); if (result == null || result.checkCoercePhoneNumberResponseData == null) { //获取胁迫联系方式信息失败 string msg = Language.StringByID(R.MyInternationalizationString.uGetCoercePhoneNumberFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); return null; } //没有数据 if (result.checkCoercePhoneNumberResponseData.Result == 1) { return null; } return result.checkCoercePhoneNumberResponseData; } /// /// 设置胁迫的联系人方式 /// /// keys:联系方式 values:地区码 /// public async Task SetCoercePhoneNumber(Dictionary dicPhone) { var Pra = new Safeguard.SetCoercePhoneNumberData(); var Actonobj = new Safeguard.PushTargetActionObj(); Actonobj.Type = 2; Pra.Actions.Add(Actonobj); Pra.LoginToken = this.GetLoginToken(); foreach (string phoneNum in dicPhone.Keys) { var phoneInfo = new Safeguard.PushTargetInfo(); Actonobj.PushTarget.Add(phoneInfo); //电话号码 phoneInfo.PushNumber = dicPhone[phoneNum] + "-" + phoneNum; } var result = await Safeguard.SetCoercePhoneNumberAsync(Pra); if (result == null || result.setCoercePhoneNumberResponseData == null || result.setCoercePhoneNumberResponseData.Result != 0) { //修改胁迫联系方式失败 string msg = Language.StringByID(R.MyInternationalizationString.uChangedCoercePhoneNumberFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); return false; } return true; } /// /// 删除胁迫的联系人方式 /// /// /// /// public async Task DeleteCoercePhoneNumber(string areaCode, string phoneNum) { var Pra = new Safeguard.DelCoercePhoneNumberData(); var Actonobj = new Safeguard.PushTargetActionObj(); Actonobj.Type = 2; Pra.Actions.Add(Actonobj); Pra.LoginToken = this.GetLoginToken(); var phoneInfo = new Safeguard.PushTargetInfo(); Actonobj.PushTarget.Add(phoneInfo); //电话号码 phoneInfo.PushNumber = areaCode + "-" + phoneNum; var result = await Safeguard.DelCoercePhoneNumberAsync(Pra); if (result == null || result.delCoercePhoneNumberResponseData == null || result.delCoercePhoneNumberResponseData.Result != 0) { //删除胁迫联系方式失败 string msg = Language.StringByID(R.MyInternationalizationString.uDeleteCoercePhoneNumberFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); return false; } return true; } #endregion #region ■ 传感器报警_________________________ /// /// 获取传感器报警的信息翻译文本(返回null则表示没有达成报警条件,停止报警则返回空字符串) /// /// /// public IASZoneReportInfo GetSensorAlarmInfo(CommonDevice i_device) { //IAS安防信息上报 if ((i_device is IASZone) == false) { return null; } string mainkey = Common.LocalDevice.Current.GetDeviceMainKeys(i_device); //因为推送的是虚拟的设备,所以去获取真实物理设备 CommonDevice device = Common.LocalDevice.Current.GetDevice(mainkey); if (device == null) { return null; } IASZone iASZone = (IASZone)i_device; var alarmInfo = new IASZoneReportInfo(); if (iASZone.iASInfo.Battery == 1) { //电池电量低 alarmInfo.BatteryMsg = Language.StringByID(R.MyInternationalizationString.uLowElectricityConsumption); } if (iASZone.iASInfo.Tamper == 1) { //设备被拆除 alarmInfo.DemolishmenMsg = Language.StringByID(R.MyInternationalizationString.uDeviceWasDismantled); } //运动传感器 if (device.IasDeviceType == 13) { if (iASZone.iASInfo.Alarm1 == 1) { //有不明物体经过 alarmInfo.AlarmMsg = Language.StringByID(R.MyInternationalizationString.uHadUnKnowObjectPassed); } } //烟雾传感器 else if (device.IasDeviceType == 40) { if (iASZone.iASInfo.Alarm1 == 1) { //烟雾报警 alarmInfo.AlarmMsg = Language.StringByID(R.MyInternationalizationString.uSmogAlarm); } } //水侵传感器 else if (device.IasDeviceType == 42) { if (iASZone.iASInfo.Alarm1 == 1) { //检测到漏水 alarmInfo.AlarmMsg = Language.StringByID(R.MyInternationalizationString.uWaterLeakageDetection); } } //燃气传感器 else if (device.IasDeviceType == 43) { if (iASZone.iASInfo.Alarm1 == 1) { //燃气泄漏 alarmInfo.AlarmMsg = Language.StringByID(R.MyInternationalizationString.uCarbonMonoxideRevealed); } } //紧急按钮 else if (device.IasDeviceType == 44) { //紧急按钮 if (iASZone.iASInfo.Alarm1 == 1) { //紧急按钮被按下 alarmInfo.AlarmMsg = Language.StringByID(R.MyInternationalizationString.uEmergencyButtonPressdown); } } //钥匙扣 else if (device.IasDeviceType == 277) { if (iASZone.iASInfo.Alarm1 == 1) { //钥匙扣触发报警 alarmInfo.AlarmMsg = Language.StringByID(R.MyInternationalizationString.uKeyFobAlarm); } } //门窗传感器 else if (device.IasDeviceType == 21 || device.IasDeviceType == 22) { if (iASZone.iASInfo.Alarm1 == 1) { //门窗被打开 alarmInfo.AlarmMsg = Language.StringByID(R.MyInternationalizationString.uDoorOrWindowHadBeenOpen); } else { //门窗已关闭 alarmInfo.AlarmMsg = Language.StringByID(R.MyInternationalizationString.uDoorOrWindowHadClosed); } } else { //传感器 if (iASZone.iASInfo.Alarm1 == 1) { //触发传感器报警 alarmInfo.AlarmMsg = Language.StringByID(R.MyInternationalizationString.uSensorAlarmHadTriggered); } } return alarmInfo; } #endregion #region ■ 安防报警保存_______________________ /// /// 从新刷新缓存中的安防上报信息 /// public void RefreshAlarmInfo() { this.listAlarmInfo.Clear(); string fileName = DateTime.Now.ToString("yyyyMMdd"); //判断有没有这个文件 string fullName = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.SafeguardAlarmDirectory, fileName); if (System.IO.File.Exists(fullName) == true) { lock (objLock) { string dir = System.IO.Path.Combine(DirNameResourse.LocalMemoryDirectory, DirNameResourse.SafeguardAlarmDirectory); var data = Global.ReadFileByDirectory(dir, fileName); this.listAlarmInfo = JsonConvert.DeserializeObject>(System.Text.Encoding.UTF8.GetString(data)); if (this.listAlarmInfo == null) { this.listAlarmInfo = new List(); } } } } /// /// 保存安防报警信息到本地 /// /// public bool SaveSafeguardAlarmInfo(CommonDevice device) { int ZoneId = this.GetZoneIdByIASZone(device); if (ZoneId == -1) { //这个传感器不存在于任何防区 return false; } var msgInfo = this.GetSensorAlarmInfo(device); if (msgInfo == null) { //异常?? return false; } //获取保存安防信息的文件名字 string fileName = this.GetSafeguardAlarmFileName(); var data = new SafeguardAlarmInfo(); data.AlarmType = SafeguardAlarmType.Sensor; data.ZoneId = ZoneId; data.DeviceAddr = device.DeviceAddr; data.DeviceEpoint = device.DeviceEpoint; data.DeviceName = device.DeviceName; data.Time = DateTime.Now.ToString("HH:mm:ss"); data.RoomName = Common.Room.CurrentRoom.GetRoomNameByDevice(device); //报警结束 data.AlarmMsg = msgInfo.AlarmMsg != string.Empty ? msgInfo.AlarmMsg : Language.StringByID(R.MyInternationalizationString.uAlarmFinish); //电池报警 data.BatteryMsg = msgInfo.BatteryMsg; //被拆报警 data.DemolishmentMsg = msgInfo.DemolishmenMsg; //限制它的长度 this.listAlarmInfo.Insert(0, data); if (this.listAlarmInfo.Count > 10) { this.listAlarmInfo.RemoveAt(this.listAlarmInfo.Count - 1); } lock (objLock) { //保存到本地 var saveData = JsonConvert.SerializeObject(this.listAlarmInfo); var byteData = System.Text.Encoding.UTF8.GetBytes(saveData); string dir = System.IO.Path.Combine(DirNameResourse.LocalMemoryDirectory, DirNameResourse.SafeguardAlarmDirectory); Global.WriteFileToDirectoryByBytes(dir, fileName, byteData); return true; } } /// /// 保存布防操作信息到本地 /// /// public void SaveSafeguardAlarmInfo(GarrisonMode garrison) { if (garrison == GarrisonMode.None) { return; } //获取保存安防信息的文件名字 string fileName = this.GetSafeguardAlarmFileName(); var data = new SafeguardAlarmInfo(); data.Time = DateTime.Now.ToString("HH:mm:ss"); data.AlarmType = SafeguardAlarmType.Safeguard; if (garrison == GarrisonMode.AtHome) { //没有内部防区 if (this.IsHadInternalDefenseArea() == false) { //布防已执行 data.AlarmMsg = Language.StringByID(R.MyInternationalizationString.uExecuteGarrison); } else { //在家布防已执行 data.AlarmMsg = Language.StringByID(R.MyInternationalizationString.uExecuteAtHomeGarrison); } } else if (garrison == GarrisonMode.RemoveHome) { //离家布防已执行 data.AlarmMsg = Language.StringByID(R.MyInternationalizationString.uExecuteRemoveHomeGarrison); } else if (garrison == GarrisonMode.RemoveGarrison) { //撤防已执行 data.AlarmMsg = Language.StringByID(R.MyInternationalizationString.uExecuteWithdrawGarrison); } else { return; } //限制它的长度 this.listAlarmInfo.Insert(0, data); if (this.listAlarmInfo.Count > 10) { this.listAlarmInfo.RemoveAt(this.listAlarmInfo.Count - 1); } lock (objLock) { //保存到本地 var saveData = JsonConvert.SerializeObject(this.listAlarmInfo); var byteData = System.Text.Encoding.UTF8.GetBytes(saveData); string dir = System.IO.Path.Combine(DirNameResourse.LocalMemoryDirectory, DirNameResourse.SafeguardAlarmDirectory); Global.WriteFileToDirectoryByBytes(dir, fileName, byteData); } } /// /// 获取保存安防信息的文件名字 /// /// private string GetSafeguardAlarmFileName() { string fileName = DateTime.Now.ToString("yyyyMMdd"); if (fileName != oldDeviceAlarmFile) { //考虑到用户有可能24点的时候不退出APP,这个时候直接刷新 this.RefreshAlarmInfo(); } oldDeviceAlarmFile = fileName; return fileName; } /// /// 获取 /// /// public Dictionary> GetSafeguardAlarmInfo() { lock (objLock) { string fullPath = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.SafeguardAlarmDirectory); //借用一下自动备份获取指定目录下面的文件的函数 List listFile = HdlAutoBackupLogic.GetFileFromDirectory(fullPath); //升序 listFile.Sort(); var dic = new Dictionary>(); string dir = System.IO.Path.Combine(DirNameResourse.LocalMemoryDirectory, DirNameResourse.SafeguardAlarmDirectory); for (int i = listFile.Count - 1; i >= 0; i--) { var data = Global.ReadFileByDirectory(dir, listFile[i]); var info = JsonConvert.DeserializeObject>(System.Text.Encoding.UTF8.GetString(data)); if (info == null) { continue; } var listInfo = new List(); for (int j = 0; j < info.Count; j++) { if (j == 10) { //每天最多十条数据 break; } listInfo.Add(info[j]); } dic[listFile[i]] = listInfo; if (dic.Count == 3) { //只要三天的量 break; } } return dic; } } #endregion #region ■ 重置管理员密码_____________________ /// /// 重置管理员密码(内部会弹出错误信息) /// /// 新密码 /// public async Task DoResetAdministratorPsw(string password) { var realWay = ZigBee.Device.ZbGateway.MainGateWay; if (realWay == null) { //错误:网关对象丢失 string msg = Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg); this.ShowTipMsg(msg); return false; } //是否达成中断的时机 bool canBreak = false; bool success = false; //超时时间 int TimeOut = 0; string checkTopic = Common.LocalGateway.Current.GetGatewayId(realWay) + "/Security/AdminSetNewPassword_Respon"; Action getResultAction = (topic, message) => { try { if (topic == checkTopic) { TimeOut = 0; var jobject = Newtonsoft.Json.Linq.JObject.Parse(message); var result = JsonConvert.DeserializeObject(jobject["Data"]["Result"].ToString()); success = result == 0; canBreak = true; } } catch { } }; realWay.Actions += getResultAction; try { var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 4033 } }; var data = new Newtonsoft.Json.Linq.JObject { { "HomeId", Common.Config.Instance.HomeId }, { "Password", password } }; jObject.Add("Data", data); await realWay.Send("Security/AdminSetNewPassword", jObject.ToString()); } catch { canBreak = true; //出现未知错误,请稍后再试 string msg = Language.StringByID(R.MyInternationalizationString.uUnKnowErrorAndResetAgain); this.ShowTipMsg(msg); } while (canBreak == false && TimeOut < 60) { await Task.Delay(100); TimeOut++; } realWay.Actions -= getResultAction; if (TimeOut >= 60) { //重置管理员密码失败(网关回复超时) string msg = Language.StringByID(R.MyInternationalizationString.uResetAdministratorPswFail); msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, "回复超时"); this.ShowTipMsg(msg); return false; } if (success == false) { //重置管理员密码失败 string msg = Language.StringByID(R.MyInternationalizationString.uResetAdministratorPswFail); this.ShowTipMsg(msg); } return success; } #endregion #region ■ 一般方法___________________________ /// /// 获取所有防区的ID(默认是1~5) /// /// private List GetAllZoneId() { //防区id //1:24小时防区 //2:24小时静音防区 //3:出入防区 //4:内部防区 //5:周界防区 List list = new List() { 1, 2, 3, 4, 5 }; return list; } /// /// 获取防区的翻译名字 /// /// /// public string GetGarrisonText(int ZoneID) { if (ZoneID == 1) { //24小时防区 return Language.StringByID(R.MyInternationalizationString.u24HourSectors); } else if (ZoneID == 2) { //静音防区 return Language.StringByID(R.MyInternationalizationString.uMuteSectors); } else if (ZoneID == 3) { //出入防区 return Language.StringByID(R.MyInternationalizationString.uInAndOutSectors); } else if (ZoneID == 4) { //内部防区 return Language.StringByID(R.MyInternationalizationString.uInteriorSectors); } else if (ZoneID == 5) { //周界防区 return Language.StringByID(R.MyInternationalizationString.uPerimeterSectors); } return string.Empty; } /// /// 显示错误信息窗口 /// /// private void ShowErrorMsg(string msg) { Application.RunOnMainThread(() => { var contr = new ErrorMsgControl(msg); contr.Show(); }); } /// /// 显示正常信息窗口 /// /// private void ShowNormalMsg(string msg) { Application.RunOnMainThread(() => { var contr = new NormalMsgControl(msg); contr.Show(); }); } /// /// 显示Tip窗口 /// /// private void ShowTipMsg(string msg) { Application.RunOnMainThread(() => { var contr = new TipViewControl(msg); contr.ShowView(); }); } /// /// 加密密码 /// /// /// private string EncryptPassword(string strPsw) { string sKey = "hdljkdrt"; DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(strPsw); des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } return ret.ToString(); } /// /// 解密密码 /// /// /// private string DecryptPassword(string strPsw) { string sKey = "hdljkdrt"; DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = new byte[strPsw.Length / 2]; for (int x = 0; x < strPsw.Length / 2; x++) { int i = (Convert.ToInt32(strPsw.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray()); } /// /// 获取设备的唯一主键 /// /// /// private string GetDeviceMainKeys(CommonDevice device) { return Common.LocalDevice.Current.GetDeviceMainKeys(device); } /// /// 获取登陆者的Token(好像管理员登陆的时候,需要变更Token,所以暂且定义一个函数出来) /// /// private string GetLoginToken() { return Shared.Common.Config.Instance.Token; } #endregion } /// /// 安防本地缓存(生成本地保存文件使用) /// public class LocalSafeguardData { #region ■ 变量声明___________________________ /// /// 安防文件 /// public const string LocalSecurityFile = "LocalSecurityFile.json"; /// /// 防区的信息(Keys:防区ID, value:防区的信息) /// public Dictionary dicAllZoneInfo = new Dictionary(); #endregion #region ■ 保存方法___________________________ /// /// 保存现阶段的网关信息到本地文件 /// public void SaveToLocaltion() { var file = JsonConvert.SerializeObject(this); var bytes = System.Text.Encoding.UTF8.GetBytes(file); Global.WriteFileByBytesByHomeId(LocalSecurityFile, bytes); } #endregion } /// /// 内部使用,安防防区的一些信息 /// public class SafeguardZoneInfo { #region ■ 变量声明___________________________ /// /// 防区id。 /// public int ZoneId; /// /// 布防防区名称 ,最大32个字符 /// public string ZoneName; /// /// 信息推送 0:推送 1:不推送 (只有防区ID:1,2,3才会有。4,5的都归为3,因为它是以防区模式ID(ActionType)为单位的) /// public int InformationPush = 1; /// /// 传感器设备的信息(keys:设备主键) /// public Dictionary dicSensor = new Dictionary(); /// /// 该防区下全部设备文件路径(keys:设备主键,value:设备文件路径) /// public Dictionary dicAllDevice = new Dictionary(); /// /// 该防区下面的报警设备(keys:设备主键),<只有防区ID:1,2,3才会有。4,5的都归为3,因为它是以防区模式ID(ActionType)为单位的> /// public Dictionary dicAlarmDevice = new Dictionary(); /// /// 该防区下面的场景(keys:场景No),<只有防区ID:1,2,3才会有。4,5的都归为3,因为它是以防区模式ID(ActionType)为单位的> /// public Dictionary dicScenes = new Dictionary(); #endregion } }