HDL Home App 第二版本 旧平台金堂用 正在使用
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlGatewayLogic.cs
old mode 100755 new mode 100644
@@ -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,13 +300,17 @@
                    string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayInfoFail);
                    this.ShowTipMsg(msg);
                }
                if (logMode == WriteLogMode.YES)
                {
                    HdlLogLogic.Current.WriteLog(-1, zbGateway.GwIP + " 获取网关信息失败");
                }
                return -1;
            }
            //设置住宅ID到网关
            if (result.HomeId != Common.Config.Instance.HomeId)
            {
                bool flage2 = this.SetHomeIdToGateway(zbGateway, Common.Config.Instance.HomeId, mode);
                bool flage2 = this.SetHomeIdToGateway(zbGateway.GwId, Common.Config.Instance.HomeId, mode);
                if (flage2 == false)
                {
                    if (mode == ShowErrorMode.YES)
@@ -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;
                }
@@ -352,10 +370,10 @@
        /// <summary>
        /// 设置住宅ID到网关(失败时,不弹出任何错误信息,网关断网除外)
        /// </summary>
        /// <param name="zbGateway"></param>
        /// <param name="i_gwId"></param>
        /// <param name="HomeId"></param>
        /// <returns></returns>
        public bool SetHomeIdToGateway(ZbGateway zbGateway, string HomeId, ShowErrorMode mode)
        public bool SetHomeIdToGateway(string i_gwId, string HomeId, ShowErrorMode mode)
        {
            //账号ID
            string accountId = string.Empty;
@@ -375,7 +393,7 @@
            var data = new Newtonsoft.Json.Linq.JObject { { "HomeId", HomeId }, { "AccountId", accountId } };
            jObject.Add("Data", data);
            //住宅ID的设置,固定使用局域网,不存在远程的说法
            var result = this.SendJobjectDataToGateway(zbGateway.GwId, "GwSetHomeId", jObject.ToString(), "GwSetHomeId_Respon", 5, true);
            var result = this.SendJobjectDataToGateway(i_gwId, "GwSetHomeId", jObject.ToString(), "GwSetHomeId_Respon", 5, true);
            if (result.ErrorMsgDiv == -1)
            {
                if (mode == ShowErrorMode.YES)
@@ -469,7 +487,7 @@
            }
            //设置住宅ID到网关
            bool flage2 = this.SetHomeIdToGateway(zbGateway, Common.Config.Instance.HomeId, ShowErrorMode.YES);
            bool flage2 = this.SetHomeIdToGateway(zbGateway.GwId, Common.Config.Instance.HomeId, ShowErrorMode.YES);
            if (flage2 == false)
            {
                //向网关设置住宅ID失败
@@ -734,7 +752,7 @@
            //清空网关的住宅ID 网关解绑失败  不理它,因为网关可以按按键强制搜索得到
            if (realWay != null)
            {
                this.SetHomeIdToGateway(realWay, string.Empty, ShowErrorMode.NO);
                this.SetHomeIdToGateway(realWay.GwId, string.Empty, ShowErrorMode.NO);
            }
            //删除云端的网关
@@ -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,6 +921,22 @@
            }
            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>
@@ -1463,6 +1497,8 @@
        /// <param name="saveFile">是否保存文件</param>
        private void SetGatewayDataToLocalMemmory(ZbGateway localWay, ZbGatewayData.GetGwData data, bool saveFile = true)
        {
            if (data == null) { return; }
            localWay.GwId = data.GwId;
            localWay.GwName = data.GwName;
            localWay.GwSerialNum = data.GWSN;
@@ -1487,52 +1523,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 +1534,7 @@
            //断开mqtt连接(即使保存在内存当中也没问题,因为如果广播不到,则它不会建立链接)
            for (int i = 0; i < ZbGateway.GateWayList.Count; i++)
            {
                ZbGateway.GateWayList[i].DisConnectLocalMqttClient("G");
                ZbGateway.GateWayList[i].CloseLocalConnectionOnForce();
            }
            //加载本地网关对象
            if (roadGateway == true)
@@ -1629,7 +1619,7 @@
            list.Add(coordinatorFirmware);
            //这个网关需要有虚拟驱动这个东西才行
            if (localWay.LinuxImageType != 6)
            if (this.CheckGatewayHadDriveCode(localWay) == true)
            {
                //虚拟驱动号
                foreach (var data in localWay.DriveCodeList)
@@ -1729,7 +1719,7 @@
        #region ■ 设置网关图片_______________________
        /// <summary>
        /// 设置真实网关的图片
        /// 设置真实网关的图片(已废弃)
        /// </summary>
        /// <param name="button"></param>
        /// <param name="zbGateway"></param>
@@ -1778,7 +1768,7 @@
            {
                if (zbGateway.LinuxImageType != -1)
                {
                    button.UnSelectedImagePath = "Gateway/GatewayIcon" + zbGateway.LinuxImageType + ".png";
                    this.SetgatewayIcon(button, "Gateway/GatewayIcon" + zbGateway.LinuxImageType + ".png");
                }
                else
                {
@@ -1792,7 +1782,7 @@
                            this.SetGatewayDataToLocalMemmory(zbGateway, result, false);
                            HdlThreadLogic.Current.RunMain(() =>
                            {
                                button.UnSelectedImagePath = "Gateway/GatewayIcon" + result.LinuxImageType + ".png";
                                this.SetgatewayIcon(button, "Gateway/GatewayIcon" + result.LinuxImageType + ".png");
                            });
                        }
                    });
@@ -1800,8 +1790,24 @@
            }
            else
            {
                button.UnSelectedImagePath = "Gateway/GatewayIcon" + localWay.LinuxImageType + ".png";
                this.SetgatewayIcon(button, "Gateway/GatewayIcon" + localWay.LinuxImageType + ".png");
            }
        }
        /// <summary>
        /// 设置网关图标
        /// </summary>
        /// <param name="button"></param>
        /// <param name="i_IconName"></param>
        public void SetgatewayIcon(Button button, string i_IconName)
        {
            //这个图片本地是否存在?
            if (string.IsNullOrEmpty(IO.FileUtils.GetImageFilePath(i_IconName)) == true)
            {
                //则使用共通图片
                i_IconName = "Gateway/GatewayIcon.png";
            }
            button.UnSelectedImagePath = i_IconName;
        }
        #endregion
@@ -2056,14 +2062,12 @@
        /// <summary>
        /// 重新发送命令去绑定断网情况下备份的网关
        /// </summary>
        public void ResetComandToBindBackupGateway()
        {
            HdlThreadLogic.Current.RunThread(() =>
        public bool ResetComandToBindBackupGateway()
            {
                var fileData = HdlFileLogic.Current.ReadFileByteContent(DirNameResourse.BackupGatewayIdFile);
                if (fileData == null)
                {
                    return;
                return true;
                }
                this.listBackupGwId = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(System.Text.Encoding.UTF8.GetString(fileData));
                var listTempId = new List<string>();
@@ -2097,13 +2101,14 @@
                    {
                        System.IO.File.Delete(file);
                    }
                return true;
                }
                else
                {
                    //备份
                    HdlFileLogic.Current.SaveFileContent(DirNameResourse.BackupGatewayIdFile, listBackupGwId);
                return false;
                }
            });
        }
        #endregion
@@ -2209,6 +2214,98 @@
        #endregion
        #region ■ 更换网关___________________________
        /// <summary>
        /// 执行更换网关(里面是子线程执行)
        /// </summary>
        /// <param name="i_realGateway">新网关</param>
        /// <param name="i_targetGwId">被替换的目标网关</param>
        /// <param name="i_info">备份数据</param>
        /// <param name="finishEvent">-1:异常失败 0:没有备份数据 1:成功</param>
        public void DoReplaceGateway(ZbGateway i_realGateway, string i_targetGwId, BackupListNameInfo i_info, Action<int> finishEvent)
        {
            HdlThreadLogic.Current.RunThread(() =>
            {
                //正在获取配置数据
                ProgressFormBar.Current.Start();
                ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uGettingConfigurationData));
                System.Threading.Thread.Sleep(1000);
                //首先的更新云端
                //然后发数据给网关,让网关去下载数据
                var result = HdlGatewayBackupLogic.Current.RecoverGateway(i_realGateway, i_info.Id, i_info.IsAutoBack, false);
                if (result == false)
                {
                    finishEvent?.Invoke(-1);
                    return;
                }
                //正在变更本地配置
                ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uChangingLocalConfiguration));
                ProgressFormBar.Current.ResetProgressBar();
                System.Threading.Thread.Sleep(1500);
                //格式化旧网关
                string strComand = "{\"Cluster_ID\":0,\"Command\":160,\"Data\": {\"DelAllInfo\": 1}}";
                this.SendJobjectData(i_targetGwId, "Formate", strComand, true);
                //删除掉被替换的网关
                this.DeleteGatewayFile(i_targetGwId);
                ProgressFormBar.Current.SetValue(1);
                System.Threading.Thread.Sleep(500);
                ProgressFormBar.Current.Close();
                finishEvent?.Invoke(1);
            });
        }
        /// <summary>
        /// 在网关替换之前,获取网关的一个备份数据
        /// </summary>
        /// <param name="i_gwId">网关ID</param>
        /// <param name="statu">-1:异常失败 0:没有备份数据 1:成功</param>
        /// <returns></returns>
        private BackupListNameInfo GetBackupInfoBeforeReplace(string i_gwId, ref int statu)
        {
            //获取自动备份的数据
            var pageData = HdlBackupLogic.Current.GetBackupListNameFromDB(3, i_gwId);
            if (pageData == null)
            {
                statu = -1;
                return null;
            }
            //如果获取不到自动备份,则去获取手动备份
            if (pageData.Count == 0)
            {
                //从云端获取数据
                pageData = HdlBackupLogic.Current.GetBackupListNameFromDB(2, i_gwId);
                if (pageData == null)
                {
                    statu = -1;
                    return null;
                }
            }
            else
            {
                //自动备份
                pageData[0].IsAutoBack = 1;
            }
            if (pageData.Count == 0)
            {
                //没有备份
                statu = 0;
                return null;
            }
            statu = 1;
            return pageData[0];
        }
        #endregion
        #region ■ 网关监视___________________________
        /// <summary>
@@ -2290,7 +2387,7 @@
        private void CheckGatewayStatuByWIFI(Dictionary<string, bool> dicOldOnline)
        {
            //从网关获取全部的网关
            List<ZbGateway> list = this.GetAllGatewayFromGateway();
            List<ZbGateway> list = this.GetAllLocalGateway();
            //2020.05.25追加:此住宅是否拥有网关在线
            var hadGwOnline = false;
@@ -2438,6 +2535,40 @@
        #region ■ 一般方法___________________________
        /// <summary>
        /// 是否是mini网关
        /// </summary>
        /// <param name="i_zbGateway">网关对象</param>
        /// <returns></returns>
        public bool IsMiniGateway(ZbGateway i_zbGateway)
        {
            if (i_zbGateway == null) { return false; }
            if (i_zbGateway.LinuxImageType == 11 || i_zbGateway.LinuxImageType == 13)
            {
                //这个网关是mini网关
                return true;
            }
            return false;
        }
        /// <summary>
        /// 判断网关是否拥有虚拟驱动
        /// </summary>
        /// <param name="i_zbGateway">网关对象</param>
        /// <returns></returns>
        public bool CheckGatewayHadDriveCode(ZbGateway i_zbGateway)
        {
            if (i_zbGateway == null) { return false; }
            if (i_zbGateway.LinuxImageType != 6 && i_zbGateway.LinuxImageType != 12)
            {
                //这个网关没有虚拟驱动
                return false;
            }
            return false;
        }
        /// <summary>
        /// 显示错误信息窗口
        /// </summary>
        /// <param name="msg"></param>