HDL Home App 第二版本 旧平台金堂用 正在使用
黄学彪
2020-12-14 e90209beae6a4e822cecb18e6889f8bda23f630e
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlResidenceLogic.cs
File was renamed from ZigbeeApp/Shared/Phone/Common/Logic/HdlResidenceLogic.cs
@@ -3,7 +3,7 @@
using System.Collections.Generic;
using System.Text;
namespace Shared.Phone
namespace Shared.Phone.UserCenter
{
    /// <summary>
    /// 住宅对象的逻辑
@@ -41,20 +41,20 @@
        /// <param name="residenceName">住宅名称</param>
        public string CreatNewResidence(string residenceName)
        {
            var pra = new { homeId = 0, homeName = residenceName, homeType = "ZIGBEE" };
            var Pra = new AddResidencePra();
            Pra.Name = residenceName;
            //添加住宅
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/home/add", RestSharp.Method.POST, pra);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, ShowNetCodeMode.YES) == false)
            string resultData = UserCenterLogic.GetResponseDataByRequestHttps("App/AddHome", false, Pra);
            if (string.IsNullOrEmpty(resultData) == true)
            {
                return null;
            }
            var newInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<NewResidenceInfo>(result.Data.ToString());
            var newInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NewResidenceInfo>>(resultData);
            //添加住宅到缓存
            this.AddHouseToMemmory(newInfo.Id, residenceName);
            this.AddHouseToMemmory(newInfo[0].Id, residenceName);
            return newInfo.Id;
            return newInfo[0].Id;
        }
        /// <summary>
@@ -70,7 +70,7 @@
            var home = new Common.House();
            home.Id = houseId;
            home.Name = houseName;
            home.IsOtherShare = isOthreShare;
            home.IsOthreShare = isOthreShare;
            home.AccountType = accountType;
            //创建文件夹
@@ -106,7 +106,7 @@
            Config.Instance.Home = this.GetHouseByHouseId(residenceId);
            Config.Instance.Save();
            //刷新个人中心的内存及线程
            bool result = HdlUserCenterLogic.Current.InitUserCenterMenmoryAndThread(false);
            bool result = UserCenterLogic.InitUserCenterMenmoryAndThread(false);
            //关闭进度条
            ProgressBar.Close();
@@ -126,18 +126,20 @@
        /// <returns></returns>
        public bool EditorResidenceName(string residenceId, string residenceName)
        {
            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账号权限);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, ShowNetCodeMode.YES) == false)
            var Pra = new EditorResidencePra();
            Pra.HomeId = residenceId;
            Pra.Name = residenceName;
            Pra.IsOtherAccountCtrl = false;
            Pra.LoginAccessToken = Config.Instance.Token;
            //编辑住宅
            bool flage = UserCenterLogic.GetResultStatuByRequestHttps("App/EditHome", false, Pra);
            if (flage == true)
            {
                return false;
                //刷新内存的住宅名
                this.EditorHouseByHouseId(residenceId, residenceName);
            }
            //刷新内存的住宅名
            this.EditorHouseByHouseId(residenceId, residenceName);
            return true;
            return flage;
        }
        /// <summary>
@@ -163,55 +165,6 @@
            UserView.UserPage.Instance.RefreshAllForm = true;
        }
        #endregion
        #region ■ 编辑住宅经纬度_____________________
        /// <summary>
        /// 编辑住宅经纬度
        /// </summary>
        /// <param name="i_latitude">纬度</param>
        /// <param name="i_longitude">经度</param>
        /// <param name="i_addresName">位置名称</param>
        /// <param name="mode">失败时是否显示tip消息</param>
        /// <returns></returns>
        public bool EditorResidenceLongitudeAndLatitude(double i_latitude, double i_longitude, string i_addresName, ShowNetCodeMode mode = ShowNetCodeMode.YES)
        {
            //如果不是虚拟住宅,才更新云端
            if (Config.Instance.Home.IsVirtually == true)
            {
                //保存缓存
                Config.Instance.Home.Longitude = i_longitude;
                Config.Instance.Home.Latitude = i_latitude;
                Config.Instance.Home.ResidenceAddressName = i_addresName;
                Config.Instance.Home.Save();
                return true;
            }
            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账号权限);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false)
            {
                return false;
            }
            //保存缓存
            Config.Instance.Home.Longitude = i_longitude;
            Config.Instance.Home.Latitude = i_latitude;
            Config.Instance.Home.ResidenceAddressName = i_addresName;
            Config.Instance.Home.Save();
            //设置全部网关的住宅地址
            var listGateway = HdlGatewayLogic.Current.GetAllLocalGateway();
            foreach (var gateway in listGateway)
            {
                //不判断是否失败
                HdlGatewayLogic.Current.SetGatewaySite(gateway.GwId, i_longitude, i_latitude, ShowErrorMode.NO);
            }
            return true;
        }
        #endregion
@@ -252,120 +205,105 @@
        #endregion
        #region ■ 获取云端住宅列表___________________
        /// <summary>
        /// 登陆的时候,获取云端住宅列表(0:当前没有网络的情况下加载本地住宅  1:通过联网加载及校验本地住宅)
        /// </summary>
        /// <param name="mode">失败时是否显示tip消息</param>
        /// <returns></returns>
        public int GetHomeListsFromDbOnLogin(ShowNetCodeMode mode = ShowNetCodeMode.YES)
        {
            //获取云端住宅数据
            var listHouseData = this.GetHomeListsFromDb(mode);
            if (listHouseData == null)
            {
                return 0;
            }
            //清空当前住宅列表
            Config.Instance.HomeFilePathList.Clear();
            var listHouse = new List<House>();
            foreach (var residence in listHouseData)
            {
                //刷新住宅对象的缓存
                var house = this.RefreshResidenceMemory(residence);
                listHouse.Add(house);
            }
            //检测本地的住宅文件是否合法
            var listLocal = this.GetAllLocalResidenceListByDirectory();
            foreach (var myHouse in listLocal)
            {
                //如果本地存在不属于他的住宅文件,则删除
                if (myHouse.HouseDataDiv == 1 && Config.Instance.HomeFilePathList.Contains(myHouse.FileName) == false)
                {
                    string housePath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, myHouse.Id);
                    HdlFileLogic.Current.DeleteDirectory(housePath);
                }
            }
            //如果切换了账号,或者原来的id不存在,则重置住宅ID
            if (Config.Instance.TheSameLoginAccount == false ||
                Config.Instance.HomeFilePathList.Find((obj) => obj == $"House_{Config.Instance.HomeId}.json") == null)
            {
                Config.Instance.HomeId = listHouse.Count > 0 ? listHouse[0].Id : string.Empty;
                foreach (var house in listHouse)
                {
                    //初始选择它自己的住宅
                    if (house.IsOtherShare == false)
                    {
                        Config.Instance.HomeId = house.Id;
                        Config.Instance.Home = GetHouseByHouseId(house.Id);
                        break;
                    }
                }
            }
            Config.Instance.Save();
            return 1;
        }
        /// <summary>
        /// 获取云端住宅列表
        /// </summary>
        /// <param name="mode">失败时是否显示tip消息</param>
        /// <param name="checkNetwork">是否检测网络,如果设置检测的话,当不能联网时,直接返回本地住宅</param>
        /// <returns></returns>
        public List<ResidenceInformation> GetHomeListsFromDb(ShowNetCodeMode mode = ShowNetCodeMode.YES)
        public List<string> GetHomeListsFromDb(bool checkNetwork)
        {
            var pra = new { homeType = "ZIGBEE" };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/home/list", RestSharp.Method.POST, pra);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false)
            if (checkNetwork == true && HdlWifiLogic.Current.CanAccessHttp == false)
            {
                return null;
                //当前无法联网
                return Config.Instance.HomeFilePathList;
            }
            var listTemp = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ResidenceInformation>>(result.Data.ToString());
            var listData = new List<ResidenceInformation>();
            foreach (var data in listTemp)
            var requestObj = new SendDataToServer.ResidenceListReqDto()
            {
                if (data.HomeType == "ZIGBEE")
                RequestVersion = CommonPage.RequestVersion,
                ReqDto = new SendDataToServer.ResidenceListObj()
                {
                    listData.Add(data);
                    LoginAccessToken = Config.Instance.Token,
                    PageSetting = new SendDataToServer.ResidenceListPageSettingObj { PageSize = 999 }
                }
            };
            try
            {
                var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(requestObj);
                var revertObj = CommonPage.Instance.RequestHttpsZigbeeAsync("App/GetHomePager", System.Text.Encoding.UTF8.GetBytes(requestJson), 5);
                if (revertObj == null)
                {
                    return null;
                }
                if (revertObj.StateCode.ToUpper() == "SUCCESS")
                {
                    var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<Shared.Common.ResponseEntity.ResidenceRes>(revertObj.ResponseData.ToString());
                    if (responseDataObj.TotalCount > 0)
                    {
                        //清空当前住宅列表
                        Config.Instance.HomeFilePathList.Clear();
                        var listHouse = new List<House>();
                        foreach (var residence in responseDataObj.PageData)
                        {
                            Config.Instance.HomeFilePathList.Add($"House_{residence.Id}.json");
                            var house = GetHouseByHouseId(residence.Id);
                            if (house == null)
                            {
                                house = new House
                                {
                                    Id = residence.Id,
                                    Name = residence.Name,
                                    IsOthreShare = residence.IsOthreShare,
                                    AccountType = residence.AccountType,
                                    MainUserDistributedMark = residence.MainUserDistributedMark,
                                    Longitude = residence.Longitude,
                                    Latitude = residence.Latitude
                                };
                            }
                            else
                            {
                                house.Id = residence.Id;
                                house.Name = residence.Name;
                                house.IsOthreShare = residence.IsOthreShare;
                                house.AccountType = residence.AccountType;
                                house.MainUserDistributedMark = residence.MainUserDistributedMark;
                                house.Longitude = residence.Longitude;
                                house.Latitude = residence.Latitude;
                            }
                            Global.CreateHomeDirectory(residence.Id);
                            house.Save(false);
                            listHouse.Add(house);
                        }
                        //检测本地的住宅文件是否合法
                        var listLocal = this.GetAllLocalResidenceListByDirectory();
                        foreach (var myHouse in listLocal)
                        {
                            //如果本地存在不属于他的住宅文件,则删除
                            if (myHouse.HouseDataDiv == 1 && Config.Instance.HomeFilePathList.Contains(myHouse.FileName) == false)
                            {
                                string housePath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, myHouse.Id);
                                HdlFileLogic.Current.DeleteDirectory(housePath);
                            }
                        }
                        //如果切换了账号,或者原来的id不存在,则重置住宅ID
                        if (Config.Instance.TheSameLoginAccount == false ||
                            Config.Instance.HomeFilePathList.Contains($"House_{Config.Instance.HomeId}.json") == false)
                        {
                            Config.Instance.HomeId = listHouse[0].Id;
                            if (listHouse.Count > 0)
                            {
                                Config.Instance.HomeId = listHouse[0].Id;
                                Config.Instance.Home = GetHouseByHouseId(listHouse[0].Id);
                            }
                        }
                        Config.Instance.Save();
                    }
                }
            }
            return this.SortHouse(listData);
        }
        /// <summary>
        /// 刷新住宅对象的缓存
        /// </summary>
        /// <param name="i_residence">云端返回的数据转换成的数据</param>
        public House RefreshResidenceMemory(ResidenceInformation i_residence)
        {
            string fileName = $"House_{i_residence.Id}.json";
            if (Config.Instance.HomeFilePathList.Contains(fileName) == false)
            catch (Exception ex)
            {
                Config.Instance.HomeFilePathList.Add(fileName);
                HdlLogLogic.Current.WriteLog(ex);
            }
            var house = this.GetHouseByHouseId(i_residence.Id);
            if (house == null)
            {
                house = new House();
            }
            house.Id = i_residence.Id;
            house.Name = i_residence.HomeName;
            house.IsOtherShare = i_residence.IsOtherShare;
            house.AccountType = i_residence.AccountType == "ADMIN" ? 1 : 0;
            house.Longitude = i_residence.Longitude;
            house.Latitude = i_residence.Latitude;
            house.IsRemoteControl = i_residence.IsRemoteControl;
            Global.CreateHomeDirectory(i_residence.Id);
            house.Save(false);
            return house;
            return Config.Instance.HomeFilePathList;
        }
        #endregion
@@ -451,7 +389,7 @@
            {
                listName.Add(house.Name);
            }
            int numberLength = HdlCommonLogic.Current.GetNumberMaxLength(listName);
            int numberLength = this.GetNumberMaxLength(listName);
            var listSort = new List<string[]>();
            var dicHouse = new Dictionary<string, House>();
@@ -510,18 +448,18 @@
        /// </summary>
        /// <param name="i_listData"></param>
        /// <returns></returns>
        public List<ResidenceInformation> SortHouse(List<ResidenceInformation> i_listData)
        public List<Common.ResponseEntity.ResidenceObj> SortHouse(List<Common.ResponseEntity.ResidenceObj> i_listData)
        {
            //从一堆文字中,获取这一堆文字里面数字字符串的最长长度
            var listName = new List<string>();
            foreach (var house in i_listData)
            {
                listName.Add(house.HomeName);
                listName.Add(house.Name);
            }
            int numberLength = HdlCommonLogic.Current.GetNumberMaxLength(listName);
            int numberLength = this.GetNumberMaxLength(listName);
            var listSort = new List<string[]>();
            var dicHouse = new Dictionary<string, ResidenceInformation>();
            var dicHouse = new Dictionary<string, Common.ResponseEntity.ResidenceObj>();
            foreach (var house in i_listData)
            {
                //临时缓存
@@ -532,7 +470,7 @@
                strArry[1] = string.Empty;
                string value = string.Empty;
                foreach (var c in house.HomeName)
                foreach (var c in house.Name)
                {
                    if (char.IsNumber(c) == true)
                    {
@@ -564,7 +502,7 @@
                }
                return -1;
            });
            var listSortHouse = new List<ResidenceInformation>();
            var listSortHouse = new List<Common.ResponseEntity.ResidenceObj>();
            foreach (var strArry in listSort)
            {
                listSortHouse.Add(dicHouse[strArry[0]]);
@@ -572,26 +510,47 @@
            return listSortHouse;
        }
        /// <summary>
        /// 从一堆文字中,获取这一堆文字里面数字字符串的最长长度
        /// </summary>
        /// <param name="listText"></param>
        /// <returns></returns>
        private int GetNumberMaxLength(List<string> listText)
        {
            int maxLength = 0;
            foreach (var text in listText)
            {
                string value = string.Empty;
                foreach (var c in text)
                {
                    if (char.IsNumber(c) == true)
                    {
                        //数字
                        value += c.ToString();
                        continue;
                    }
                    else if (value != string.Empty)
                    {
                        //判断数字长度
                        if (maxLength <= value.Length)
                        {
                            maxLength = value.Length;
                        }
                        value = string.Empty;
                    }
                }
                //判断数字长度
                if (maxLength <= value.Length)
                {
                    maxLength = value.Length;
                }
            }
            return maxLength;
        }
        #endregion
        #region ■ 删除住宅___________________________
        /// <summary>
        /// 删除云端住宅
        /// </summary>
        /// <param name="houseId">删除指定住宅</param>
        /// <returns></returns>
        public bool DeleteCloundHouse(string houseId)
        {
            var pra = new { homeId = houseId };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/home/delete", RestSharp.Method.POST, pra);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, ShowNetCodeMode.YES) == false)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 删除住宅
@@ -694,6 +653,25 @@
        #region ■ 结构体_____________________________
        /// <summary>
        ///  添加住宅的启动参数
        /// </summary>
        private class AddResidencePra : IfacePraCommon
        {
            /// <summary>
            /// RequestVersion
            /// </summary>
            public string RequestVersion = Common.CommonPage.RequestVersion;
            /// <summary>
            /// LoginAccessToken
            /// </summary>
            public string LoginAccessToken = Common.Config.Instance.Token;
            /// <summary>
            /// Name
            /// </summary>
            public string Name = string.Empty;
        }
        /// <summary>
        /// 新住宅的信息
        /// </summary>
        private class NewResidenceInfo