黄学彪
2020-12-17 9f326f4000847e6167d8166fa2f6a66f53cb3734
ZigbeeApp/Shared/Phone/Common/Logic/HdlResidenceLogic.cs
@@ -39,20 +39,36 @@
        /// 创建新的住宅(返回住宅id,null代表失败)
        /// </summary>
        /// <param name="residenceName">住宅名称</param>
        public string CreatNewResidence(string residenceName)
        /// <param name="listUintName">自定义单元的标题名称(homeApp设置为null)</param>
        /// <param name="listUintContent">自定义单元的内容(homeApp设置为null)</param>
        /// <param name="TemplateMode">1:无模板有设备  2:有模板有设备  3:无模板无设备</param>
        public string CreatNewResidence(string residenceName, List<string> listUintName,
            List<string> listUintContent, int TemplateMode = 1)
        {
            var pra = new { homeId = 0, homeName = residenceName, homeType = "ZIGBEE" };
            //添加住宅
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/home/add", RestSharp.Method.POST, pra);
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/app/home/add", RestSharp.Method.POST, pra);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, ShowNetCodeMode.YES) == false)
            {
                return null;
            }
            var newInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<NewResidenceInfo>(result.Data.ToString());
            var newInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<IdInfoClass>(result.Data.ToString());
            if (listUintName != null)
            {
                //编辑住宅区域
                var success = this.EditorResidenceArea(newInfo.Id, listUintName, listUintContent);
                if (success == false)
                {
                    //编辑住宅区域失败
                    HdlMessageLogic.Current.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uEditorResidenceAreaFail));
                }
            }
            //添加住宅到缓存
            this.AddHouseToMemmory(newInfo.Id, residenceName);
            this.AddHouseToMemmory(newInfo.Id, residenceName, listUintName,
                listUintContent, TemplateMode);
            return newInfo.Id;
        }
@@ -62,9 +78,13 @@
        /// </summary>
        /// <param name="houseId">住宅id</param>
        /// <param name="houseName">住宅名称.</param>
        /// <param name="listUintName">自定义单元的标题名称(homeApp设置为null)</param>
        /// <param name="listUintContent">自定义单元的内容(homeApp设置为null)</param>
        /// <param name="TemplateMode">1:无模板有设备  2:有模板有设备  3:无模板无设备</param>
        /// <param name="isOthreShare">是否为其他主用户分享过来的住宅</param>
        /// <param name="accountType">仅子账号登陆的时候使用,当【IsOthreShare】为"true",并且【AccountType】为"1"时,该账号拥有管理员权限</param>
        public void AddHouseToMemmory(string houseId, string houseName,
            List<string> listUintName, List<string> listUintContent, int TemplateMode = 1,
            bool isOthreShare = false, int accountType = 0)
        {
            var home = new Common.House();
@@ -72,6 +92,18 @@
            home.Name = houseName;
            home.IsOtherShare = isOthreShare;
            home.AccountType = accountType;
            if (listUintName != null)
            {
                home.ListUintName.AddRange(listUintName);
                home.ListUintContent.AddRange(listUintContent);
            }
            home.TemplateMode = TemplateMode;
            if (TemplateMode == 3)
            {
                //标记为虚拟住宅
                home.IsVirtually = true;
            }
            //创建文件夹
            Common.Global.CreateHomeDirectory(houseId);
@@ -123,19 +155,32 @@
        /// </summary>
        /// <param name="residenceId">住宅id</param>
        /// <param name="residenceName">住宅名称</param>
        /// <param name="listUintName">自定义单元的标题名称(homeApp设置为null)</param>
        /// <param name="listUintContent">自定义单元的内容(homeApp设置为null)</param>
        /// <param name="TemplateMode">1:无模板有设备  2:有模板有设备  3:无模板无设备</param>
        /// <returns></returns>
        public bool EditorResidenceName(string residenceId, string residenceName)
        public bool EditorResidenceName(string residenceId, string residenceName,
            List<string> listUintName, List<string> listUintContent, int TemplateMode = 1)
        {
            var pra = new { homeId = Config.Instance.Home.Id, homeType = "ZIGBEE", homeName = residenceName };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/home/update", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/app/home/update", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, ShowNetCodeMode.YES) == false)
            {
                return false;
            }
            if (listUintName != null)
            {
                //编辑住宅区域
                var success = this.EditorResidenceArea(residenceId, listUintName, listUintContent);
                if (success == false)
                {
                    //编辑住宅区域失败
                    HdlMessageLogic.Current.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uEditorResidenceAreaFail));
                }
            }
            //刷新内存的住宅名
            this.EditorHouseByHouseId(residenceId, residenceName);
            this.EditorHouseByHouseId(residenceId, residenceName, listUintName, listUintContent, TemplateMode);
            return true;
        }
@@ -145,24 +190,54 @@
        /// </summary>
        /// <param name="residenceId">住宅id</param>
        /// <param name="residenceName">住宅名称</param>
        public void EditorHouseByHouseId(string residenceId, string residenceName)
        /// <param name="listUintName">自定义单元的标题名称(homeApp设置为null)</param>
        /// <param name="listUintContent">自定义单元的内容(homeApp设置为null)</param>
        /// <param name="TemplateMode">1:无模板有设备  2:有模板有设备  3:无模板无设备</param>
        public void EditorHouseByHouseId(string residenceId, string residenceName,
            List<string> listUintName, List<string> listUintContent, int TemplateMode = 1)
        {
            if (Config.Instance.Home.Id == residenceId)
            House home = Config.Instance.Home;
            if (Config.Instance.Home.Id != residenceId)
            {
                Config.Instance.Home.Name = residenceName;
                //从缓存中获取
                home = GetHouseByHouseId(residenceId);
            }
            var home = GetHouseByHouseId(residenceId);
            if (home == null)
            {
                return;
            }
            home.Name = residenceName;
            if (listUintName != null)
            {
                home.ListUintName.Clear();
                home.ListUintName.AddRange(listUintName);
                home.ListUintContent.Clear();
                home.ListUintContent.AddRange(listUintContent);
            }
            home.TemplateMode = TemplateMode;
            if (TemplateMode == 3)
            {
                //标记为虚拟住宅
                home.IsVirtually = true;
            }
            home.Save();
            //住宅修改名称的话,主页需要重新刷新
            UserView.UserPage.Instance.RefreshAllForm = true;
        }
        /// <summary>
        /// 编辑住宅区域
        /// </summary>
        /// <param name="residenceId">住宅id</param>
        /// <param name="listUintName">自定义单元的标题名称</param>
        /// <param name="listUintContent">自定义单元的内容</param>
        /// <returns></returns>
        private bool EditorResidenceArea(string residenceId, List<string> listUintName, List<string> listUintContent)
        {
            //☆マーク☆ 住宅区域
            return true;
        }
        #endregion
@@ -190,7 +265,7 @@
            }
            var pra = new { homeId = Config.Instance.Home.Id, homeType = "ZIGBEE", latitude = i_latitude, longitude = i_longitude };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/home/update", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/app/home/update", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false)
            {
@@ -319,7 +394,7 @@
        {
            var pra = new { homeType = "ZIGBEE" };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/home/list", RestSharp.Method.POST, pra);
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/app/home/list", RestSharp.Method.POST, pra);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false)
            {
@@ -360,12 +435,48 @@
            house.Longitude = i_residence.Longitude;
            house.Latitude = i_residence.Latitude;
            house.IsRemoteControl = i_residence.IsRemoteControl;
            house.LastEditorTime = HdlCommonLogic.Current.ConvertUtcTimeToLocalTime2(i_residence.ModifyTime).ToString("yyyy/MM/dd HH:mm");
            Global.CreateHomeDirectory(i_residence.Id);
            house.Save(false);
            return house;
        }
        /// <summary>
        /// 从云端,刷新住宅的区域(true:会刷新i_house的List变量, false:什么都不处理)
        /// </summary>
        /// <param name="i_homeId">住宅ID</param>
        public bool RefreshHomeAreaFromDb(string i_homeId)
        {
            var house = this.GetHouseByHouseId(i_homeId);
            if (house == null)
            {
                return false;
            }
            //var pra2 = new
            //{
            //    RequestVersion = CommonPage.RequestVersion,
            //    RequestSource = 5,
            //    LoginAccessToken = Common.Config.Instance.Token,
            //    HomeId = i_house.Id
            //};
            //var result = UserCenterLogic.GetResponseDataByRequestHttps("App/GetHomeDirectory", false, pra2, new List<string> { "NotCheck" });
            //if (result != null)
            //{
            //    var listData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ResidenceAreaInfo>>(result);
            //    //初始化
            //    i_house.ListUintContent = new List<string>();
            //    i_house.ListUintName = new List<string>();
            //    foreach (var data in listData)
            //    {
            //        i_house.ListUintName.Add(data.Tag);
            //        i_house.ListUintContent.Add(data.Remark);
            //    }
            //    return true;
            //}
            return false;
        }
        #endregion
@@ -401,8 +512,9 @@
        /// <summary>
        /// 从文件夹中获取全部的住宅对象
        /// </summary>
        /// <param name="getGatewayId">是否获取网关ID(存在listGatewayId里面)</param>
        /// <returns></returns>
        public List<Common.House> GetAllLocalResidenceListByDirectory()
        public List<House> GetAllLocalResidenceListByDirectory(bool getGatewayId = false)
        {
            var strPath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Common.Config.Instance.Guid);
            var listHome = new List<Common.House>();
@@ -429,9 +541,85 @@
                    }
                    var myHouse = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.House>(textValue);
                    listHome.Add(myHouse);
                    //如果读取网关id的话
                    if (getGatewayId == true)
                    {
                        //标识初始化完成
                        myHouse.listGatewayId = new List<string>();
                        //获取网关文件
                        var arryGateway = System.IO.Directory.GetFiles(nowPath, "Gateway_*");
                        foreach (var myFile in arryGateway)
                        {
                            string[] Arry = myFile.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
                            if (Arry.Length >= 3)
                            {
                                //获取其网关id
                                myHouse.listGatewayId.Add(Arry[2]);
                            }
                        }
                    }
                }
            }
            return this.SortHouse(listHome);
        }
        /// <summary>
        /// 从文件夹中获取全部备份住宅对象
        /// </summary>
        /// <returns></returns>
        public List<House> GetAllLocalBackupResidenceListByDirectory()
        {
            var strPath = System.IO.Path.Combine(IO.FileUtils.RootPath, Config.Instance.Guid);
            var listHome = new List<House>();
            //获取全部的文件夹
            var listDirectory = new List<string>();
            var arryDirs = System.IO.Directory.GetDirectories(strPath);
            foreach (var file in arryDirs)
            {
                string[] arry = file.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                listDirectory.Add(arry[arry.Length - 1]);
            }
            foreach (var myDir in listDirectory)
            {
                if (myDir.StartsWith("BackupResidenceData") == false
                    && myDir.StartsWith("DownLoadResidenceData") == false)
                {
                    //只要手动备份和云端下载的
                    continue;
                }
                //获取各个文件夹里面的住宅文件
                string nowPath = System.IO.Path.Combine(strPath, myDir);
                var arryHouse = System.IO.Directory.GetFiles(nowPath, "House_*");
                if (arryHouse.Length > 0)
                {
                    //读取文件内容
                    var textValue = HdlFileLogic.Current.ReadFileTextContent(System.IO.Path.Combine(nowPath, arryHouse[0]));
                    if (textValue == null)
                    {
                        continue;
                    }
                    var myHouse = Newtonsoft.Json.JsonConvert.DeserializeObject<House>(textValue);
                    if (myHouse.Id != Config.Instance.Home.Id)
                    {
                        //不是这个住宅的备份
                        continue;
                    }
                    if (myDir.StartsWith("BackupResidenceData") == true)
                    {
                        //此住宅数据是临时备份数据
                        myHouse.HouseDataDiv = 2;
                        myHouse.SaveDirctoryName = myDir;
                    }
                    else if (myDir.StartsWith("DownLoadResidenceData") == true)
                    {
                        //此住宅数据是从云端下载的备份数据
                        myHouse.HouseDataDiv = 3;
                        myHouse.SaveDirctoryName = myDir;
                    }
                    listHome.Add(myHouse);
                }
            }
            return listHome;
        }
        #endregion
@@ -584,7 +772,7 @@
        public bool DeleteCloundHouse(string houseId)
        {
            var pra = new { homeId = houseId };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/home/delete", RestSharp.Method.POST, pra);
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/app/home/delete", RestSharp.Method.POST, pra);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, ShowNetCodeMode.YES) == false)
            {
@@ -689,20 +877,32 @@
            return $"House_{houseId}.json";
        }
        #endregion
        #region ■ 结构体_____________________________
        /// <summary>
        /// 获取住宅的区域文本
        /// </summary>
        /// <param name="i_house"></param>
        /// <returns></returns>
        public string GetResidenceAreaText(House i_house)
        {
            return this.GetResidenceAreaText(i_house.ListUintName, i_house.ListUintContent);
        }
        /// <summary>
        /// 新住宅的信息
        /// 获取住宅的区域文本
        /// </summary>
        private class NewResidenceInfo
        /// <param name="i_ListUintName">单元标题</param>
        /// <param name="i_ListUintContent">单元内容</param>
        /// <returns></returns>
        public string GetResidenceAreaText(List<string> i_ListUintName, List<string> i_ListUintContent)
        {
            /// <summary>
            /// ZigbeeHomeGuid
            /// </summary>
            public string Id = string.Empty;
            string textArea = string.Empty;
            for (int i = 0; i < i_ListUintName.Count; i++)
            {
                textArea += i_ListUintContent[i] + i_ListUintName[i] + "_";
            }
            return textArea.TrimEnd('_');
        }
        #endregion
    }
}