using Shared.Common; using System; using System.Collections.Generic; namespace Shared.Phone.UserCenter.Residence { /// /// 住宅管理的画面★ /// public class ResidenceListMainForm : EditorCommonForm { #region ■ 变量声明___________________________ /// /// 列表控件 /// private VerticalListControl listView = null; /// /// 住宅数据(有网时使用) /// private Dictionary dicResidenceData = null; /// /// 住宅数据(无网时使用) /// private List listLocalHouse = null; #endregion #region ■ 初始化_____________________________ /// /// 画面显示(底层会固定调用此方法,借以完成画面创建) /// public void ShowForm() { //设置标题信息 base.SetTitleText(Language.StringByID(R.MyInternationalizationString.ResidenceManagement)); //虚拟住宅无法新建住宅 if (Common.Config.Instance.Home.IsVirtually == false) { //右上添加按钮 var btnTopIcon = new MostRightIconControl(69, 69); btnTopIcon.UnSelectedImagePath = "Item/Add.png"; topFrameLayout.AddChidren(btnTopIcon); btnTopIcon.InitControl(); btnTopIcon.ButtonClickEvent += (sender, e) => { //显示添加住宅名称的画面 this.ShowAddNameForm(); }; } //初始化中部控件 this.InitMiddleFrame(); } /// /// 初始化中部控件 /// private void InitMiddleFrame() { //清空bodyFrame this.ClearBodyFrame(); listView = new VerticalListControl(29); listView.Height = bodyFrameLayout.Height + Application.GetRealHeight(6); listView.BackgroundColor = UserCenterColor.Current.White; bodyFrameLayout.AddChidren(listView); //设置住宅信息 this.SetResidenceInfo(1); } /// /// 设置住宅信息(1:先读取云端,无法联网,再读取本地 2:只读云端 3:只读本地) /// /// 1:先读取云端,无法联网,再读取本地 2:只读云端 3:只读本地 private void SetResidenceInfo(int div) { HdlThreadLogic.Current.RunThread(() => { //虚拟住宅的话,只读本地 if (div == 3 || Config.Instance.Home.IsVirtually == true) { //只读本地 this.SetResidenceInfoByOffline(); } else { //开启进度条 this.ShowProgressBar(); //设置住宅信息(有网情况) var result = this.SetResidenceInfoByOnline(); if (result == true) { //云端读取成功 this.CloseProgressBar(); return; } if (div == 2) { //如果只读云端 关闭进度条 this.CloseProgressBar(ShowReLoadMode.YES); return; } //如果在没网的情况下,从文件夹路径中获取全部住宅,然后显示 this.SetResidenceInfoByOffline(); //关闭进度条 this.CloseProgressBar(); } }); } #endregion #region ■ 设置住宅信息(有网情况)_____________ /// /// 设置住宅信息(有网情况) /// private bool SetResidenceInfoByOnline() { //获取住宅信息 var listHouse = HdlResidenceLogic.Current.GetHomeListsFromDb(ShowNetCodeMode.No); if (listHouse == null) { return false; } HdlThreadLogic.Current.RunMain(() => { if (this.Parent == null) { return; } listView.Height = bodyFrameLayout.Height; this.dicResidenceData = new Dictionary(); for (int i = 0; i < listHouse.Count; i++) { var info = listHouse[i]; //收集该账号住宅的数据 this.dicResidenceData[info.Id] = info; //添加住宅的行(有网情况) this.AddRowLayoutByOnline(info, i != listHouse.Count - 1); } //调整列表控件的高度 this.listView.AdjustRealHeight(Application.GetRealHeight(23)); }); return true; } /// /// 添加住宅的行(有网情况) /// /// private void AddRowLayoutByOnline(ResidenceInformation info, bool addLine) { var rowLayout = new RowLayoutControl(listView.rowSpace / 2); this.listView.AddChidren(rowLayout); //图标 var btnIcon = rowLayout.frameTable.AddLeftIcon(81); btnIcon.UnSelectedImagePath = "Item/ResidenceIcon.png"; //当前住宅 NormalViewControl btnHome = null; if (info.Id == Config.Instance.Home.Id) { //住宅名称 btnHome = rowLayout.frameTable.AddLeftCaption(info.HomeName, 800, 60, true); btnHome.TextSize = 15; btnHome.Y = Application.GetRealHeight(12) + rowLayout.chidrenYaxis; rowLayout.frameTable.AddChidren(btnHome, ChidrenBindMode.BindEvent); //当前住宅 var btnNowView = rowLayout.frameTable.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uNowResidence), 800, 49, true); btnNowView.Y = Application.GetRealHeight(72) + rowLayout.chidrenYaxis; btnNowView.TextSize = 12; btnNowView.TextColor = UserCenterColor.Current.TextGrayColor1; rowLayout.frameTable.AddChidren(btnNowView, ChidrenBindMode.BindEvent); } else { //住宅名称 btnHome = rowLayout.frameTable.AddLeftCaption(info.HomeName, 800, true); btnHome.TextSize = 15; } //右箭头 rowLayout.frameTable.AddRightArrow(); if (addLine == true) { //底线 rowLayout.frameTable.AddBottomLine(); } rowLayout.frameTable.ButtonClickEvent += (sender, e) => { if (info.Id != Config.Instance.Home.Id) { //确认切换当前住宅到「」? string msg = Language.StringByID(R.MyInternationalizationString.uSwitchResidenceMsg); if (msg.Contains("{0}") == true) { msg = string.Format(msg, info.HomeName); } this.ShowMassage(ShowMsgType.Confirm, msg, () => { HdlThreadLogic.Current.RunThread(() => { //开启进度条 this.ShowProgressBar(); //检测本地有没有这个住宅 this.CheckIsExsitResidence(info); var result = HdlResidenceLogic.Current.SwitchResidence(info.Id); HdlThreadLogic.Current.RunMain(() => { if (result == true) { //重新刷新列表 this.listView.RemoveAll(); this.SetResidenceInfo(2); } //关闭进度条 this.CloseProgressBar(); }); }); }); } else { //检测能否删除住宅 bool flage = this.CheckIsCanDeleteResidence(info); var form = new ResidenceManagementForm(); form.AddForm(flage); } }; if (info.IsOtherShare == true) { //分享的,不能更改 return; } //只有切换到当前住宅,并且本地拥有网关的时候,才能过户 if (info.Id == Config.Instance.Home.Id && HdlGatewayLogic.Current.GetAllLocalGateway().Count > 0) { //过户 var btnTransfer = rowLayout.AddDeleteControl(); btnTransfer.TextID = R.MyInternationalizationString.uTransfer; btnTransfer.ButtonClickEvent += (sender, e) => { rowLayout.HideMenu(); HdlCheckLogic.Current.CheckSecondarySecurity(() => { //执行过户操作 this.DoTransferResidence(); }, () => { //请前往个人中心{0}设置二次安全验证方式 string msg = Language.StringByID(R.MyInternationalizationString.uGotoCenterAndSetSecondAuthentication).Replace("{0}", "\r\n"); this.ShowMassage(ShowMsgType.Confirm, msg, () => { var form = new UserMain.SecondAuthenticationForm(); form.AddForm(); }); }); }; } //更改 var btnChanged = rowLayout.AddEditorControl(); btnChanged.TextID = R.MyInternationalizationString.uChanged1; btnChanged.ButtonClickEvent += (sender, e) => { //显示编辑名称界面 this.ShowEditorNameForm(info, null, btnHome); }; } #endregion #region ■ 设置住宅信息(无网情况)_____________ /// /// 设置住宅信息(无网情况) /// private void SetResidenceInfoByOffline() { //从文件夹中获取全部的住宅对象 this.listLocalHouse = HdlResidenceLogic.Current.GetAllLocalResidenceListByDirectory(); if (listLocalHouse.Count == 0) { return; } HdlThreadLogic.Current.RunMain(() => { listView.Height = bodyFrameLayout.Height; for (int i = 0; i < listLocalHouse.Count; i++) { //添加住宅的行(无网情况) this.AddRowLayoutByOffline(listLocalHouse[i], i != listLocalHouse.Count - 1); } //调整列表控件的高度 this.listView.AdjustRealHeight(Application.GetRealHeight(23)); }); } /// /// 添加住宅的行(无网情况) /// /// private void AddRowLayoutByOffline(House i_home, bool addLine) { var rowLayout = new RowLayoutControl(listView.rowSpace / 2); this.listView.AddChidren(rowLayout); //图标 var btnIcon = rowLayout.frameTable.AddLeftIcon(81); btnIcon.UnSelectedImagePath = "Item/ResidenceIcon.png"; //当前住宅 NormalViewControl btnHome = null; if (i_home.Id == Config.Instance.Home.Id) { //住宅名称 btnHome = rowLayout.frameTable.AddLeftCaption(i_home.Name, 800, 60, true); btnHome.TextSize = 15; btnHome.Y = Application.GetRealHeight(12) + rowLayout.chidrenYaxis; rowLayout.frameTable.AddChidren(btnHome, ChidrenBindMode.BindEvent); //当前住宅 var btnNowView = rowLayout.frameTable.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uNowResidence), 800, 49, true); btnNowView.Y = Application.GetRealHeight(72) + rowLayout.chidrenYaxis; btnNowView.TextSize = 12; btnNowView.TextColor = UserCenterColor.Current.TextGrayColor1; rowLayout.frameTable.AddChidren(btnNowView, ChidrenBindMode.BindEvent); } else { //住宅名称 btnHome = rowLayout.frameTable.AddLeftCaption(i_home.Name, 800, true); btnHome.TextSize = 15; } //右箭头 rowLayout.frameTable.AddRightArrow(); if (addLine == true) { //底线 rowLayout.frameTable.AddBottomLine(); } rowLayout.frameTable.ButtonClickEvent += (sender, e) => { if (i_home.Id != Config.Instance.Home.Id) { //确认切换当前住宅到「」? string msg = Language.StringByID(R.MyInternationalizationString.uSwitchResidenceMsg); if (msg.Contains("{0}") == true) { msg = string.Format(msg, i_home.Name); } this.ShowMassage(ShowMsgType.Confirm, msg, () => { HdlThreadLogic.Current.RunThread(() => { var result = HdlResidenceLogic.Current.SwitchResidence(i_home.Id); HdlThreadLogic.Current.RunMain(() => { if (result == true) { //重新刷新列表 this.listView.RemoveAll(); this.SetResidenceInfo(3); } //关闭进度条 this.CloseProgressBar(); }); }); }); } else { //检测能否删除住宅 bool flage = this.CheckIsCanDeleteResidence(i_home); var form = new ResidenceManagementForm(); form.AddForm(flage); } }; ////更改 //var btnChanged = rowLayout.AddEditorControl(); //btnChanged.TextID = R.MyInternationalizationString.uChanged1; //btnChanged.ButtonClickEvent += (sender, e) => //{ // //显示编辑名称界面 // this.ShowEditorNameForm(null, i_home, btnHome); //}; } #endregion #region ■ 编辑住宅名称_______________________ /// /// 显示编辑住宅名称界面 /// /// 有网时使用 /// 无网时适用 private void ShowEditorNameForm(ResidenceInformation info, House i_house, NormalViewControl btnHome) { string oldName = info != null ? info.HomeName : i_house.Name; //生成一个弹窗画面 var dialogForm = new DialogInputControl(); //编辑住宅 dialogForm.SetTitleText(Language.StringByID(R.MyInternationalizationString.uChangedName)); //请输入住宅名称 dialogForm.SetTipText(Language.StringByID(R.MyInternationalizationString.uPleaseInputResidenceName)); dialogForm.Text = oldName; //按下确认按钮 dialogForm.ComfirmClickEvent += ((textValue) => { if (textValue == string.Empty) { //请输入住宅名称 string msg = Language.StringByID(R.MyInternationalizationString.uPleaseInputResidenceName); this.ShowMassage(ShowMsgType.Error, msg); return; } //画面关闭 dialogForm.CloseDialog(); if (oldName != textValue) { if (info != null) { //编辑云端的住宅名称 HdlThreadLogic.Current.RunThread(() => { this.EditorResidenceName(textValue, info, btnHome); }); } else { //直接编辑住宅缓存 if (this.CheckIsCanSaveResidence(i_house.Id, textValue, true) == false) { return; } //刷新内存的住宅名 HdlResidenceLogic.Current.EditorHouseByHouseId(i_house.Id, textValue, null, null); //更改显示的名字 btnHome.Text = textValue; } } }); } /// /// 编辑云端的住宅名称 /// /// /// /// private void EditorResidenceName(string ResidenceName, ResidenceInformation info, NormalViewControl btnHome) { //检测能否保存住宅 if (this.CheckIsCanSaveResidence(info.Id, ResidenceName, true) == false) { return; } //开启进度条 this.ShowProgressBar(); var result = HdlResidenceLogic.Current.EditorResidenceName(info.Id, ResidenceName, null, null); //关闭进度条 this.CloseProgressBar(); if (result == true) { HdlThreadLogic.Current.RunMain(() => { //刷新内存的住宅名 HdlResidenceLogic.Current.EditorHouseByHouseId(info.Id, ResidenceName, null, null); //更改显示的名字 btnHome.Text = ResidenceName; info.HomeName = ResidenceName; }); } } #endregion #region ■ 添加住宅名称_______________________ /// /// 显示添加住宅名称的画面 /// private void ShowAddNameForm() { //生成一个弹窗画面 var dialogForm = new DialogInputControl(); //创建住宅 dialogForm.SetTitleText(Language.StringByID(R.MyInternationalizationString.uCreatResidence)); //请输入住宅名称 dialogForm.SetTipText(Language.StringByID(R.MyInternationalizationString.uPleaseInputResidenceName)); //按下确认按钮 dialogForm.ComfirmClickEvent += ((textValue) => { if (textValue == string.Empty) { //请输入住宅名称 string msg = Language.StringByID(R.MyInternationalizationString.uPleaseInputResidenceName); this.ShowMassage(ShowMsgType.Error, msg); return; } //画面关闭 dialogForm.CloseDialog(); //保存住宅 HdlThreadLogic.Current.RunThread(() => { this.AddNewResidence(textValue); }); }); } /// /// 向云端添加新的住宅 /// /// /// private void AddNewResidence(string ResidenceName) { //检测能否保存住宅 if (this.CheckIsCanSaveResidence(string.Empty, ResidenceName, false) == false) { return; } //开启进度条 this.ShowProgressBar(); //创建住宅 var newId = HdlResidenceLogic.Current.CreatNewResidence(ResidenceName, null, null); this.CloseProgressBar(); if (newId != null) { HdlThreadLogic.Current.RunMain(() => { //重新刷新列表 this.listView.RemoveAll(); this.SetResidenceInfo(2); }); } } #endregion #region ■ 过户_______________________________ /// /// 执行过户操作 /// private void DoTransferResidence() { this.ShowProgressBar(); HdlThreadLogic.Current.RunThread(() => { //绑定还没有成功的网关 var result2 = HdlGatewayLogic.Current.ResetComandToBindBackupGateway(); if (result2 == false) { //绑定网关失败,请重新操作 this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uBindGatewayFailPleaseDoAgain)); this.CloseProgressBar(); return; } //检测它有没有备份 var listBack = HdlBackupLogic.Current.GetBackupListNameFromDB(BackUpMode.A手动备份); this.CloseProgressBar(); if (listBack == null) { return; } HdlThreadLogic.Current.RunMain(() => { if (listBack.Count > 0) { var form = new Transfer.TransferResidenceMainForm(); form.AddForm(1); } else { //检测到还没有备份,请先备份 this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uIsNotHadBackupByCheckPleaseBackup)); //生成一个弹窗画面 this.ShowDialogInputForm(Language.StringByID(R.MyInternationalizationString.uAddBackup), Config.Instance.Home.Name, Language.StringByID(R.MyInternationalizationString.uPleaseInpuBackupName), (dialogForm, textValue) => { //画面关闭 dialogForm.CloseDialog(); HdlThreadLogic.Current.RunThread(() => { //创建一个备份名字 string backupClassId = HdlBackupLogic.Current.CreatNewBackupNameToDB(textValue, BackUpMode.A手动备份); if (backupClassId == null) { //创建备份名字失败 string msg = Language.StringByID(R.MyInternationalizationString.uCreatBackupNameFail); this.ShowMassage(ShowMsgType.Error, msg); return; } //上传数据到云端 bool result = HdlBackupLogic.Current.UpLoadBackupFileToDB(backupClassId); if (result == false) { //文件上传失败 string msg = Language.StringByID(R.MyInternationalizationString.uFileUpLoadFail); this.ShowMassage(ShowMsgType.Error, msg); //如果上传失败的话,就把它删除 HdlBackupLogic.Current.DeleteDbBackupData(backupClassId); return; } HdlThreadLogic.Current.RunMain(() => { var form = new Transfer.TransferResidenceMainForm(); form.AddForm(1); }); }); }); } }); }); } #endregion #region ■ 一般方法___________________________ /// /// 检测本地有没有这个住宅 /// /// private void CheckIsExsitResidence(ResidenceInformation info) { var path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, info.Id); bool isExsit = System.IO.Directory.Exists(path); if (isExsit == true) { return; } //创建文件夹 Global.CreateHomeDirectory(info.Id); //创建新的住宅 var house = new House(); house.Id = info.Id; house.Name = info.HomeName; house.IsOtherShare = info.IsOtherShare; house.AccountType = info.AccountType == "ADMIN" ? 1 : 0; house.Longitude = info.Longitude; house.Latitude = info.Latitude; house.Save(false); string fileName = house.FileName; if (Config.Instance.HomeFilePathList.Contains(fileName) == false) { //添加列表 Config.Instance.HomeFilePathList.Add(fileName); } } /// /// 检测能否删除住宅(有网使用) /// /// /// private bool CheckIsCanDeleteResidence(ResidenceInformation info) { if (info.IsOtherShare == false) { int myCount = 0; foreach (var data in this.dicResidenceData.Values) { if (data.IsOtherShare == false) { myCount++; } } //如果此账号下只剩下唯一一个自己的住宅,则不能再删除 if (myCount == 1) { return false; } } return true; } /// /// 检测能否删除住宅(无网使用) /// /// /// private bool CheckIsCanDeleteResidence(House i_house) { if (i_house.IsVirtually == false) { //非虚拟住宅都不允许删除,只有在能联网的时候,才能删除 return false; } int myCount = 0; foreach (var data in this.listLocalHouse) { if (data.IsVirtually == true) { myCount++; } } //如果此账号下只剩下唯一一个虚拟的住宅,则不能再删除 if (myCount == 1) { return false; } return true; } /// /// 检测能否保存住宅 /// /// 住宅ID /// 住宅名 /// isEditor /// private bool CheckIsCanSaveResidence(string residenceId, string residenceName, bool isEditor) { if (this.dicResidenceData != null) { //住宅名字重复检测 foreach (string strId in this.dicResidenceData.Keys) { if (this.dicResidenceData[strId].IsOtherShare == true) { //分享的住宅不考虑 continue; } if (residenceId != strId && residenceName == this.dicResidenceData[strId].HomeName) { //住宅名字已经存在 int msgId = isEditor == true ? R.MyInternationalizationString.EditZigbeeHome_Exist : R.MyInternationalizationString.AddZigbeeHome_Exist; string msg = Language.StringByID(msgId); this.ShowMassage(ShowMsgType.Error, msg); return false; } } } else { foreach (var myHouse in this.listLocalHouse) { if (myHouse.IsOtherShare == true) { //分享的住宅不考虑 continue; } if (residenceId != myHouse.Id && residenceName == myHouse.Name) { //住宅名字已经存在 int msgId = isEditor == true ? R.MyInternationalizationString.EditZigbeeHome_Exist : R.MyInternationalizationString.AddZigbeeHome_Exist; string msg = Language.StringByID(msgId); this.ShowMassage(ShowMsgType.Error, msg); return false; } } } return true; } #endregion #region ■ 界面重新激活事件___________________ /// /// 自身的上层界面关闭后,它自身处于最上层时,触发的事件 /// public override int FormActionAgainEvent() { if (dicResidenceData != null) { foreach (string keys in dicResidenceData.Keys) { if (Config.Instance.HomeFilePathList.Contains($"House_{keys}.json") == false) { this.listView.RemoveAll(); //数据已经变更 重新设置住宅信息 this.SetResidenceInfo(2); break; } } } else { //检测还有这个住宅文件吗 var strPath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Common.Config.Instance.Guid); foreach (var myHouse in this.listLocalHouse) { string fullName = System.IO.Path.Combine(strPath, myHouse.FileName); //如果不存在了的话 if (System.IO.File.Exists(fullName) == false) { //数据已经变更 重新设置住宅信息 this.listView.RemoveAll(); this.SetResidenceInfo(3); return 1; } } } return 1; } #endregion #region ■ 结构体_____________________________ /// /// 访问住宅信息接口的启动参数(查询专用) /// public class GetResidenceInfoPra : IfacePraCommon { /// /// RequestVersion /// public string RequestVersion = Common.CommonPage.RequestVersion; /// /// ReqDto /// public TokenInfo ReqDto = new TokenInfo(); } /// /// 访问住宅信息接口的启动参数(查询专用) /// public class TokenInfo { /// /// LoginAccessToken /// public string LoginAccessToken = Common.Config.Instance.Token; /// /// PageSetting /// public PageSettingInfo PageSetting = new PageSettingInfo() { Page = 1, PageSize = 5000 }; } /// /// 添加住宅的启动参数 /// public class AddResidencePra : IfacePraCommon { /// /// RequestVersion /// public string RequestVersion = Common.CommonPage.RequestVersion; /// /// LoginAccessToken /// public string LoginAccessToken = Common.Config.Instance.Token; /// /// Name /// public string Name = string.Empty; } /// /// 新住宅的信息 /// public class NewResidenceInfo { /// /// ZigbeeHomeGuid /// public string Id = string.Empty; } #endregion } }