黄学彪
2020-11-19 9ef48d7b2da7c408b53f73be0f6eef3cbac1c84a
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlGatewayLogic.cs
@@ -200,16 +200,21 @@
        /// </summary>
        /// <param name="zbGateway">网关</param>
        /// <param name="mode">是否显示错误</param>
        public bool AddNewGateway(ZbGateway zbGateway, ShowErrorMode mode)
        /// <param name="logMode">是否写Log</param>
        public bool AddNewGateway(ZbGateway zbGateway, ShowErrorMode mode, WriteLogMode logMode = WriteLogMode.NO)
        {
            //设置网关的经纬度
            bool falge = this.SetGatewaySite(zbGateway, Common.Config.Instance.Home.Longitude, Common.Config.Instance.Home.Latitude, ShowErrorMode.NO);
            if (falge == false)
            {
                if (logMode == WriteLogMode.YES)
                {
                    HdlLogLogic.Current.WriteLog(-1, zbGateway.GwIP + " 设置经纬度失败");
                }
                return falge;
            }
            //执行添加网关到内存
            var result = this.DoAddGatewayToMemory(zbGateway, mode);
            var result = this.DoAddGatewayToMemory(zbGateway, mode, logMode);
            //前的网关绑定在了当前账号下的不同住宅里面
            if (result == 0)
            {
@@ -222,6 +227,10 @@
                        msg = string.Format(msg, "\r\n");
                    }
                    this.ShowTipMsg(msg);
                }
                if (logMode == WriteLogMode.YES)
                {
                    HdlLogLogic.Current.WriteLog(-1, zbGateway.GwIP + " 网关绑定在了当前账号下的不同住宅里面,云端不给绑定");
                }
                return false;
            }
@@ -267,8 +276,9 @@
        /// </summary>
        /// <param name="zbGateway">网关对象</param>
        /// <param name="mode">是否显示错误</param>
        /// <param name="logMode">是否写Log</param>
        /// <returns></returns>
        private int DoAddGatewayToMemory(ZbGateway zbGateway, ShowErrorMode mode)
        private int DoAddGatewayToMemory(ZbGateway zbGateway, ShowErrorMode mode, WriteLogMode logMode = WriteLogMode.NO)
        {
            if (zbGateway == null)
            {
@@ -290,6 +300,10 @@
                    string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayInfoFail);
                    this.ShowTipMsg(msg);
                }
                if (logMode == WriteLogMode.YES)
                {
                    HdlLogLogic.Current.WriteLog(-1, zbGateway.GwIP + " 获取网关信息失败");
                }
                return -1;
            }
@@ -304,6 +318,10 @@
                        //向网关设置住宅ID失败
                        string msg = Language.StringByID(R.MyInternationalizationString.uSetHomeIdToGatewayFail);
                        this.ShowTipMsg(msg);
                    }
                    if (logMode == WriteLogMode.YES)
                    {
                        HdlLogLogic.Current.WriteLog(-1, zbGateway.GwIP + " 向网关设置住宅ID失败");
                    }
                    return -1;
                }
@@ -748,7 +766,7 @@
            //断开mqtt连接
            if (realWay != null)
            {
                realWay.DisConnectLocalMqttClient("GD");
                realWay.CloseLocalConnectionOnForce();
            }
            return true;
@@ -847,8 +865,8 @@
        {
            foreach (var zbway in listGateway)
            {
                //是否存在网关存在于WIFI下
                if (this.CheckGatewayOnlineByFlage(zbway) == true)
                //如果广播得到网关,则会刷新OnlineTime,如果与当前时间相差少于指定秒数,则判断为存在网关存在于WIFI下
                if ((DateTime.Now - zbway.OnlineTime).TotalSeconds <= ZigBee.Common.Application.SendTime * 2)
                {
                    return true;
                }
@@ -903,13 +921,29 @@
            }
            return this.CheckGatewayOnlineByFlage(zbGateway);
        }
        }
        /// <summary>
        /// 刷新网关的在线时间点
        /// </summary>
        /// <param name="i_gatewayId">网关ID</param>
        public void RefreshGatewayOnlineTime(string i_gatewayId)
        {
            try
            {
                if (this.dicGateway.ContainsKey(i_gatewayId) == true)
                {
                    this.dicGateway[i_gatewayId].OnlineTime = DateTime.Now;
                }
            }
            catch { }
        }
        /// <summary>
        /// 根据指定标识,判断网关是否在线
        /// </summary>
        /// <param name="zbGateway"></param>
        /// <returns></returns>
        /// <returns></returns>
        private bool CheckGatewayOnlineByFlage(ZbGateway zbGateway)
        {
            if (this.nowGwConnectMode == GatewayConnectMode.Remote)
@@ -1487,52 +1521,6 @@
        #endregion
        #region ■ 网关房间相关_______________________
        /// <summary>
        /// 获取网关所在的房间
        /// </summary>
        /// <param name="zbGateway">网关对象</param>
        /// <returns></returns>
        public Room GetRoomByGateway(ZbGateway zbGateway)
        {
            return this.GetRoomByGateway(zbGateway.GwId);
        }
        /// <summary>
        /// 获取网关所在的房间
        /// </summary>
        /// <param name="gatewayId">网关ID</param>
        /// <returns></returns>
        public Room GetRoomByGateway(string gatewayId)
        {
            var localGateway = this.GetLocalGateway(gatewayId);
            if (localGateway == null)
            {
                return null;
            }
            return HdlRoomLogic.Current.GetRoomById(localGateway.RoomId);
        }
        /// <summary>
        /// 变更网关房间
        /// </summary>
        /// <param name="gwId">网关Id</param>
        /// <param name="roomId">房间ID</param>
        public void ChangedGatewayRoom(string gwId, string roomId)
        {
            var localGateway = this.GetLocalGateway(gwId);
            if (localGateway != null)
            {
                localGateway.RoomId = roomId;
                localGateway.ReSave();
                //添加备份
                HdlAutoBackupLogic.AddOrEditorFile(localGateway.FilePath);
            }
        }
        #endregion
        #region ■ 清空真实网关链接___________________
        /// <summary>
@@ -1544,7 +1532,7 @@
            //断开mqtt连接(即使保存在内存当中也没问题,因为如果广播不到,则它不会建立链接)
            for (int i = 0; i < ZbGateway.GateWayList.Count; i++)
            {
                ZbGateway.GateWayList[i].DisConnectLocalMqttClient("G");
                ZbGateway.GateWayList[i].CloseLocalConnectionOnForce();
            }
            //加载本地网关对象
            if (roadGateway == true)
@@ -2056,54 +2044,53 @@
        /// <summary>
        /// 重新发送命令去绑定断网情况下备份的网关
        /// </summary>
        public void ResetComandToBindBackupGateway()
        public bool ResetComandToBindBackupGateway()
        {
            HdlThreadLogic.Current.RunThread(() =>
            var fileData = HdlFileLogic.Current.ReadFileByteContent(DirNameResourse.BackupGatewayIdFile);
            if (fileData == null)
            {
                var fileData = HdlFileLogic.Current.ReadFileByteContent(DirNameResourse.BackupGatewayIdFile);
                if (fileData == null)
                {
                    return;
                }
                this.listBackupGwId = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(System.Text.Encoding.UTF8.GetString(fileData));
                var listTempId = new List<string>();
                listTempId.AddRange(this.listBackupGwId);
                return true;
            }
            this.listBackupGwId = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(System.Text.Encoding.UTF8.GetString(fileData));
            var listTempId = new List<string>();
            listTempId.AddRange(this.listBackupGwId);
                //调用接口,绑定网关
                var bindGateway = new BindGatewayPra();
                //获取控制主人账号的Token
                bindGateway.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
            //调用接口,绑定网关
            var bindGateway = new BindGatewayPra();
            //获取控制主人账号的Token
            bindGateway.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
                foreach (var gwId in listTempId)
            foreach (var gwId in listTempId)
            {
                bindGateway.BindGateways.Clear();
                bindGateway.BindGateways.Add(gwId);
                var result = UserCenterLogic.GetResultCodeByRequestHttps("App/BindGatewayToHome", true, bindGateway, new List<string> { "NotCheck" }, false);
                if (result == "Success")
                {
                    bindGateway.BindGateways.Clear();
                    bindGateway.BindGateways.Add(gwId);
                    var result = UserCenterLogic.GetResultCodeByRequestHttps("App/BindGatewayToHome", true, bindGateway, new List<string> { "NotCheck" }, false);
                    if (result == "Success")
                    {
                        this.listBackupGwId.Remove(gwId);
                    }
                    if (result == "Error")
                    {
                        break;
                    }
                    this.listBackupGwId.Remove(gwId);
                }
                if (result == "Error")
                {
                    break;
                }
            }
                if (this.listBackupGwId.Count == 0)
            if (this.listBackupGwId.Count == 0)
            {
                //如果没有了内容,则删除文件
                string file = DirNameResourse.BackupGatewayIdFile;
                if (System.IO.File.Exists(file) == true)
                {
                    //如果没有了内容,则删除文件
                    string file = DirNameResourse.BackupGatewayIdFile;
                    if (System.IO.File.Exists(file) == true)
                    {
                        System.IO.File.Delete(file);
                    }
                    System.IO.File.Delete(file);
                }
                else
                {
                    //备份
                    HdlFileLogic.Current.SaveFileContent(DirNameResourse.BackupGatewayIdFile, listBackupGwId);
                }
            });
                return true;
            }
            else
            {
                //备份
                HdlFileLogic.Current.SaveFileContent(DirNameResourse.BackupGatewayIdFile, listBackupGwId);
                return false;
            }
        }
        #endregion
@@ -2191,9 +2178,9 @@
            if (realWay == null)
            {
                //如果网关对象丢失了,则创建个新的
                realWay = new ZbGateway { IsVirtual = true, };
                realWay.GwId = i_gatewayId;
                realWay.HomeId = Config.Instance.HomeId;
                realWay = new ZbGateway { IsVirtual = true, };
                realWay.GwId = i_gatewayId;
                realWay.HomeId = Config.Instance.HomeId;
                ZbGateway.GateWayList.Add(realWay);
            }
            if (useLocalConnect == false)
@@ -2290,7 +2277,7 @@
        private void CheckGatewayStatuByWIFI(Dictionary<string, bool> dicOldOnline)
        {
            //从网关获取全部的网关
            List<ZbGateway> list = this.GetAllGatewayFromGateway();
            List<ZbGateway> list = this.GetAllLocalGateway();
            //2020.05.25追加:此住宅是否拥有网关在线
            var hadGwOnline = false;