HDL Home App 第二版本 旧平台金堂用 正在使用
黄学彪
2020-07-24 0087dd7734e71dfcfd1bb54db394ad7855021ffd
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlGatewayLogic.cs
@@ -196,7 +196,7 @@
        /// </summary>
        /// <param name="zbGateway">网关</param>
        /// <param name="mode">是否显示错误</param>
        public async Task<bool> AddNewGateway(ZbGateway zbGateway, ShowErrorMode mode)
        public bool AddNewGateway(ZbGateway zbGateway, ShowErrorMode mode)
        {
            //设置网关的经纬度
            bool falge = this.SetGatewaySite(zbGateway, Common.Config.Instance.Home.Longitude, Common.Config.Instance.Home.Latitude, ShowErrorMode.NO);
@@ -205,7 +205,7 @@
                return falge;
            }
            //执行添加网关到内存
            var result = await this.DoAddGatewayToMemory(zbGateway, mode);
            var result = this.DoAddGatewayToMemory(zbGateway, mode);
            //前的网关绑定在了当前账号下的不同住宅里面
            if (result == 0)
            {
@@ -250,7 +250,7 @@
        /// <param name="zbGateway">网关对象</param>
        /// <param name="mode">是否显示错误</param>
        /// <returns></returns>
        private async Task<int> DoAddGatewayToMemory(ZbGateway zbGateway, ShowErrorMode mode)
        private int DoAddGatewayToMemory(ZbGateway zbGateway, ShowErrorMode mode)
        {
            if (zbGateway == null)
            {
@@ -278,7 +278,7 @@
            //设置住宅ID到网关
            if (result.HomeId != Common.Config.Instance.HomeId)
            {
                bool flage2 = await this.SetHomeIdToGateway(zbGateway, Common.Config.Instance.HomeId, mode);
                bool flage2 = this.SetHomeIdToGateway(zbGateway, Common.Config.Instance.HomeId, mode);
                if (flage2 == false)
                {
                    if (mode == ShowErrorMode.YES)
@@ -337,10 +337,28 @@
        /// <param name="zbGateway"></param>
        /// <param name="HomeId"></param>
        /// <returns></returns>
        public async Task<bool> SetHomeIdToGateway(ZbGateway zbGateway, string HomeId, ShowErrorMode mode)
        public bool SetHomeIdToGateway(ZbGateway zbGateway, string HomeId, ShowErrorMode mode)
        {
            ZbGateway realWay = null;
            if (this.GetRealGateway(ref realWay, zbGateway) == false)
            //账号ID
            string accountId = string.Empty;
            if (HomeId != string.Empty)
            {
                if (UserCenterResourse.UserInfo.AuthorityNo == 1)
                {
                    //主账号
                    accountId = Config.Instance.Guid;
                }
                else
                {
                    accountId = Config.Instance.Home.MainUserDistributedMark;
                }
            }
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 82 } };
            var data = new Newtonsoft.Json.Linq.JObject { { "HomeId", HomeId }, { "AccountId", accountId } };
            jObject.Add("Data", data);
            //住宅ID的设置,固定使用局域网,不存在远程的说法
            var result = this.SendJobjectDataToGateway(zbGateway, "GwSetHomeId", jObject.ToString(), "GwSetHomeId_Respon", 5, true);
            if (result.ErrorMsgDiv == -1)
            {
                if (mode == ShowErrorMode.YES)
                {
@@ -350,13 +368,11 @@
                }
                return false;
            }
            var info = await realWay.GwSetHomeIdAsync(HomeId);
            if (info != null && info.gwSetHomeIdData != null)
            if (result.ErrorMsgDiv == 0)
            {
                return true;
                return false;
            }
            return false;
            return true;
        }
        /// <summary>
@@ -372,7 +388,7 @@
            //获取控制主人账号的Token
            bindGateway.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
            var result = UserCenterLogic.GetResultCodeByRequestHttps("App/BindGatewayToHome", true, bindGateway, new List<string> { "NotCheck" });
            var result = UserCenterLogic.GetResultCodeByRequestHttps("App/BindGatewayToHome", true, bindGateway, new List<string> { "NotCheck" }, false);
            if (result == "Error")
            {
                return -1;
@@ -418,7 +434,7 @@
        /// </summary>
        /// <param name="zbGateway">网关</param>
        /// <param name="btnMsg">消息控件</param>
        public async Task<int> ReBindNewGateway(ZbGateway zbGateway, NormalViewControl btnMsg = null)
        public int ReBindNewGateway(ZbGateway zbGateway, NormalViewControl btnMsg = null)
        {
            if (zbGateway == null)
            {
@@ -435,7 +451,7 @@
            }
            //设置住宅ID到网关
            bool flage2 = await this.SetHomeIdToGateway(zbGateway, Common.Config.Instance.HomeId, ShowErrorMode.YES);
            bool flage2 = this.SetHomeIdToGateway(zbGateway, Common.Config.Instance.HomeId, ShowErrorMode.YES);
            if (flage2 == false)
            {
                //向网关设置住宅ID失败
@@ -470,7 +486,7 @@
                    btnMsg.TextID = R.MyInternationalizationString.uGatewayDataIsChangingPleaseWhait;
                });
            }
            await Task.Delay(8000);
            System.Threading.Thread.Sleep(8000);
            //获取网关的信息
            ZbGatewayData.GetGwData result = null;
@@ -484,7 +500,7 @@
                }
                count--;
                //最多再等20秒
                await Task.Delay(4000);
                System.Threading.Thread.Sleep(4000);
            }
            if (result == null)
            {
@@ -579,12 +595,10 @@
        /// 执行切换网关操作
        /// </summary>
        /// <param name="gatewayId"></param>
        public async Task<bool> DoSwitchGateway(string gatewayId)
        public bool DoSwitchGateway(string gatewayId)
        {
            var zbGateway = this.GetLocalGateway(gatewayId);
            //重新获取在线网关的信息
            var result = await this.GetOnlineGatewayInfo(gatewayId);
            var result = this.GetOnlineGatewayInfo(gatewayId);
            if (result == false)
            {
                return false;
@@ -603,7 +617,7 @@
        /// </summary>
        /// <param name="gatewayId"></param>
        /// <returns></returns>
        private async Task<bool> GetOnlineGatewayInfo(string gatewayId)
        private bool GetOnlineGatewayInfo(string gatewayId)
        {
            //显示进度条
            ProgressBar.Show();
@@ -616,7 +630,7 @@
                //重新设置住宅ID(这个应该是不经过APP,直接把网关恢复了出厂设置)
                if (this.HomeIdIsEmpty(realWay.HomeId) == true)
                {
                    int result2 = await this.ReBindNewGateway(realWay);
                    int result2 = this.ReBindNewGateway(realWay);
                    if (result2 == 0)
                    {
                        //出现未知错误,请稍后再试
@@ -675,25 +689,15 @@
        /// <summary>
        /// 删除网关,包括云端和本地(失败时不会显示信息,并且会返回true)
        /// </summary>
        /// <param name="zbGateway"></param>
        public async Task<bool> DeleteGateway(ZbGateway zbGateway)
        {
            //移除本地网关信息
            return await this.DeleteGateway(zbGateway.GwId);
        }
        /// <summary>
        /// 删除网关,包括云端和本地(失败时不会显示信息,并且会返回true)
        /// </summary>
        /// <param name="zbGatewayID"></param>
        public async Task<bool> DeleteGateway(string zbGatewayID)
        public bool DeleteGateway(string zbGatewayID)
        {
            ZbGateway realWay = null;
            this.GetRealGateway(ref realWay, zbGatewayID);
            //清空网关的住宅ID 网关解绑失败  不理它,因为网关可以按按键强制搜索得到
            if (realWay != null)
            {
                await this.SetHomeIdToGateway(realWay, string.Empty, ShowErrorMode.NO);
                this.SetHomeIdToGateway(realWay, string.Empty, ShowErrorMode.NO);
            }
            //删除云端的网关
@@ -1487,24 +1491,39 @@
        #endregion
        #region ■ 清空真实网关列表___________________
        #region ■ 清空真实网关链接___________________
        /// <summary>
        /// 清空全部的真实物理网关对象
        /// 清空全部的真实物理网关的链接
        /// </summary>
        public void ClearAllRealGateway()
        /// <param name="roadGateway">是否加载本地的网关对象到真实列表中</param>
        public void ClearAllRealGatewayConection(bool roadGateway)
        {
            //因为那一瞬间,有可能mqtt会加回来,所以先加缓存
            var list = new List<ZbGateway>();
            list.AddRange(ZbGateway.GateWayList);
            //然后清空掉
            ZbGateway.GateWayList.Clear();
            //最后再断开mqtt连接
            for (int i = 0; i < list.Count; i++)
            //断开mqtt连接(即使保存在内存当中也没问题,因为如果广播不到,则它不会建立链接)
            for (int i = 0; i < ZbGateway.GateWayList.Count; i++)
            {
                list[i].DisConnectLocalMqttClient("G");
                ZbGateway.GateWayList[i].DisConnectLocalMqttClient("G");
            }
            list.Clear();
            //加载本地网关对象
            if (roadGateway == true)
            {
                List<string> listFile = this.GetAllGatewayFile();
                //反序列化添加到缓存
                foreach (string file in listFile)
                {
                    //从文件中反序列化出网关对象
                    var gateway = this.GetGatewayFromFile(file);
                    if (gateway == null)
                    {
                        continue;
                    }
                    var tempWay = ZbGateway.GateWayList.Find(obj => (obj != null) && (obj.GwId == gateway.GwId));
                    if (tempWay == null)
                    {
                        ZbGateway.GateWayList.Add(tempWay);
                    }
                }
            }
        }
        #endregion
@@ -1639,12 +1658,16 @@
        /// <returns></returns>
        public int IsMainGateway(string waiID)
        {
            ZbGateway zbGateway = null;
            if (this.GetRealGateway(ref zbGateway, waiID) == false)
            var realWay = ZbGateway.GateWayList.Find((obj) =>
            {
                return obj.GwId == waiID;
            });
            //虚拟网关也返回 0
            if (realWay == null || realWay.IsVirtual == true)
            {
                return 0;
            }
            return zbGateway.IsMainGateWay == true ? 1 : 2;
            return realWay.IsMainGateWay == true ? 1 : 2;
        }
        #endregion
@@ -1811,7 +1834,7 @@
                //获取控制主人账号的Token
                pra.ReqDto.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
                var result = UserCenterLogic.GetResponseDataByRequestHttps("App/GetSingleHomeGatewayPagger", true, pra, list);
                var result = UserCenterLogic.GetResponseDataByRequestHttps("App/GetSingleHomeGatewayPagger", true, pra, list, false);
                if (string.IsNullOrEmpty(result) == true)
                {
                    canBreak = true;
@@ -1943,7 +1966,7 @@
            List<string> listNotShowError = new List<string>() { "NoExist", "NoBind", "NoRecord", "NotCheck" };
            bool result = UserCenterLogic.GetResultStatuByRequestHttps("App/ReleaseGatewayToHome", true, Pra, listNotShowError);
            bool result = UserCenterLogic.GetResultStatuByRequestHttps("App/ReleaseGatewayToHome", true, Pra, listNotShowError, false);
            if (result == false)
            {
                return false;
@@ -1996,7 +2019,7 @@
                {
                    bindGateway.BindGateways.Clear();
                    bindGateway.BindGateways.Add(gwId);
                    var result = UserCenterLogic.GetResultCodeByRequestHttps("App/BindGatewayToHome", true, bindGateway, new List<string> { "NotCheck" });
                    var result = UserCenterLogic.GetResultCodeByRequestHttps("App/BindGatewayToHome", true, bindGateway, new List<string> { "NotCheck" }, false);
                    if (result == "Success")
                    {
                        this.listBackupGwId.Remove(gwId);