黄学彪
2020-12-17 9f326f4000847e6167d8166fa2f6a66f53cb3734
ZigbeeApp/Shared/Phone/Common/Logic/HdlGatewayLogic.cs
@@ -111,7 +111,7 @@
        public void SynchronizeDbGateway()
        {
            //从云端获取网列表ID
            Dictionary<string, GatewayResult> result = this.GetGateWayListFromDataBase();
            Dictionary<string, GatewayResult> result = this.GetGateWayListFromDataBase(Config.Instance.Home.Id);
            if (result == null)
            {
                return;
@@ -244,12 +244,12 @@
            if (Config.Instance.Home.TemplateMode == 2)
            {
                //如果这个类型的网关只有一个模板,则自动匹配
                var dicName = TemplateData.TemplateCommonLogic.Current.GetGatewayCanSelectTemplateName(zbGateway);
                var dicName = HdlTemplateCommonLogic.Current.GetGatewayCanSelectTemplateName(zbGateway);
                if (dicName.Count == 1)
                {
                    foreach (var tempId in dicName.Keys)
                    {
                        TemplateData.TemplateCommonLogic.Current.AddGatewayTemplateSelect(zbGateway.GwId, tempId);
                        HdlTemplateCommonLogic.Current.AddGatewayTemplateSelect(zbGateway.GwId, tempId);
                        break;
                    }
                }
@@ -291,7 +291,7 @@
                return -1;
            }
            //获取网关的信息
            var result = this.GetGatewayInfo(zbGateway, true, mode);
            var result = this.RefreshGatewayInfo(zbGateway, true, mode);
            if (result == null)
            {
                if (mode == ShowErrorMode.YES)
@@ -361,7 +361,7 @@
            if (isEsist == false)
            {
                //添加备份
                HdlAutoBackupLogic.AddOrEditorFile(this.dicGateway[gwID].FilePath);
                HdlBackupLogic.Current.AddOrEditorAutoBackFileStatu(this.dicGateway[gwID].FilePath);
            }
            return 1;
@@ -495,7 +495,7 @@
            int count = 5;
            while (count >= 0)
            {
                result = this.GetGatewayInfo(zbGateway, true, ShowErrorMode.NO);
                result = this.RefreshGatewayInfo(zbGateway, true, ShowErrorMode.NO);
                if (result != null)
                {
                    break;
@@ -533,7 +533,7 @@
            if (isEsist == false)
            {
                //添加备份
                HdlAutoBackupLogic.AddOrEditorFile(this.dicGateway[gwID].FilePath);
                HdlBackupLogic.Current.AddOrEditorAutoBackFileStatu(this.dicGateway[gwID].FilePath);
            }
            //添加网关的话,强制主页刷新
@@ -542,12 +542,12 @@
            if (Config.Instance.Home.TemplateMode == 2)
            {
                //如果这个类型的网关只有一个模板,则自动匹配
                var dicName = TemplateData.TemplateCommonLogic.Current.GetGatewayCanSelectTemplateName(zbGateway);
                var dicName = HdlTemplateCommonLogic.Current.GetGatewayCanSelectTemplateName(zbGateway);
                if (dicName.Count == 1)
                {
                    foreach (var tempId in dicName.Keys)
                    {
                        TemplateData.TemplateCommonLogic.Current.AddGatewayTemplateSelect(zbGateway.GwId, tempId);
                        HdlTemplateCommonLogic.Current.AddGatewayTemplateSelect(zbGateway.GwId, tempId);
                        break;
                    }
                }
@@ -598,7 +598,7 @@
            this.dicGateway[gwID].ReSave();
            //添加自动备份
            HdlAutoBackupLogic.AddOrEditorFile(zbGateway.FilePath);
            HdlBackupLogic.Current.AddOrEditorAutoBackFileStatu(zbGateway.FilePath);
            return true;
        }
@@ -668,7 +668,7 @@
            if (getGatewayInfo == true)
            {
                //获取网关信息
                var info = this.GetGatewayInfo(realWay, false);
                var info = this.RefreshGatewayInfo(realWay, false);
                if (info == null)
                {
                    //关闭进度条
@@ -758,7 +758,7 @@
            //移除缓存
            dicGateway.Remove(zbGatewayID);
            //删除自动备份
            HdlAutoBackupLogic.DeleteFile(file);
            HdlBackupLogic.Current.DeleteAutoBackFileStatu(file);
            //删除设备文件
            List<CommonDevice> list = HdlDeviceCommonLogic.Current.GetDeviceByGatewayID(zbGatewayID);
@@ -848,7 +848,7 @@
        private void RefreshGatewayOnlineOnRemode(List<ZbGateway> listGateway)
        {
            //获取云端上面的网关
            Dictionary<string, GatewayResult> dicDbGateway = this.GetGateWayListFromDataBase();
            Dictionary<string, GatewayResult> dicDbGateway = this.GetGateWayListFromDataBase(Config.Instance.Home.Id);
            if (dicDbGateway == null)
            {
                return;
@@ -1383,7 +1383,7 @@
                    //给一个线程去获取它的镜像类型
                    HdlThreadLogic.Current.RunThread(() =>
                    {
                        var result = this.GetGatewayInfo(zbGateway, false, ShowErrorMode.NO);
                        var result = this.RefreshGatewayInfo(zbGateway, false, ShowErrorMode.NO);
                        if (result != null)
                        {
                            //将网关的数据设置到本地缓存中
@@ -1416,17 +1416,34 @@
        #region ■ 获取网关信息_______________________
        /// <summary>
        /// 获取网关信息(版本信息,镜像类型,基本信息等。只刷新本地网关的缓存)
        /// 刷新网关信息(版本信息,镜像类型,基本信息等。只刷新本地网关的缓存)
        /// </summary>
        /// <param name="zbGateway"></param>
        /// <param name="useLocalConnect">是否强制使用本地连接发送</param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public ZbGatewayData.GetGwData GetGatewayInfo(ZbGateway zbGateway, bool useLocalConnect, ShowErrorMode mode = ShowErrorMode.YES)
        public ZbGatewayData.GetGwData RefreshGatewayInfo(ZbGateway zbGateway, bool useLocalConnect, ShowErrorMode mode = ShowErrorMode.YES)
        {
            //从网关那里刷新网关信息
            var getGwInfo = this.RefreshGatewayInfo(zbGateway.GwId, useLocalConnect, mode);
            //顺便这个变量也设置一下
            this.SetGatewayDataToLocalMemmory(zbGateway, getGwInfo, false);
            return getGwInfo;
        }
        /// <summary>
        /// 从网关那里刷新网关信息
        /// </summary>
        /// <param name="gatewateId">网关ID</param>
        /// <param name="useLocalConnect">是否强制使用本地连接发送</param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public ZbGatewayData.GetGwData RefreshGatewayInfo(string gatewateId, bool useLocalConnect, ShowErrorMode mode = ShowErrorMode.YES)
        {
            //获取网关版本信息
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 95 } };
            var result = this.SendJobjectDataToGateway(zbGateway.GwId, "GetZbGwInfo", jObject.ToString(), "GetZbGwInfo_Respon", 5, useLocalConnect);
            var result = this.SendJobjectDataToGateway(gatewateId, "GetZbGwInfo", jObject.ToString(), "GetZbGwInfo_Respon", 5, useLocalConnect);
            if (result.ErrorMsgDiv != 1)
            {
@@ -1440,18 +1457,15 @@
                }
                return null;
            }
            var getGwInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<ZbGatewayData.GetGwData>(result.ReceiptData);
            string gwID = zbGateway.GwId;
            if (this.dicGateway.ContainsKey(gwID) == true)
            if (this.dicGateway.ContainsKey(gatewateId) == true)
            {
                //刷新缓存
                ZbGateway localWay = this.dicGateway[gwID];
                ZbGateway localWay = this.dicGateway[gatewateId];
                //将网关的数据设置到本地缓存中
                this.SetGatewayDataToLocalMemmory(localWay, getGwInfo);
            }
            //顺便这个变量也设置一下
            this.SetGatewayDataToLocalMemmory(zbGateway, getGwInfo, false);
            return getGwInfo;
        }
@@ -1540,7 +1554,7 @@
                return new List<FirmwareVersionInfo> { null, null, null };
            }
            //获取网关版本信息
            var result = this.GetGatewayInfo(zbGateway, false, mode);
            var result = this.RefreshGatewayInfo(zbGateway, false, mode);
            if (result == null)
            {
                return null;
@@ -1583,7 +1597,7 @@
            list.Add(coordinatorFirmware);
            //这个网关需要有虚拟驱动这个东西才行
            if (localWay.LinuxImageType != 6)
            if (HdlGatewayLogic.Current.CheckGatewayHadDriveCode(localWay) == true)
            {
                //虚拟驱动号
                foreach (var data in localWay.DriveCodeList)
@@ -1683,7 +1697,7 @@
        #region ■ 设置网关图片_______________________
        /// <summary>
        /// 设置真实网关的图片
        /// 设置真实网关的图片(已废弃)
        /// </summary>
        /// <param name="button"></param>
        /// <param name="zbGateway"></param>
@@ -1701,7 +1715,7 @@
                    //给一个线程去获取它的镜像类型
                    HdlThreadLogic.Current.RunThread(() =>
                    {
                        var result = this.GetGatewayInfo(zbGateway, true, ShowErrorMode.NO);
                        var result = this.RefreshGatewayInfo(zbGateway, true, ShowErrorMode.NO);
                        if (result != null)
                        {
                            //将网关的数据设置到本地缓存中
@@ -1732,21 +1746,21 @@
            {
                if (zbGateway.LinuxImageType != -1)
                {
                    button.UnSelectedImagePath = "Gateway/GatewayIcon" + zbGateway.LinuxImageType + ".png";
                    this.SetgatewayIcon(button, "Gateway/GatewayIcon" + zbGateway.LinuxImageType + ".png");
                }
                else
                {
                    //给一个线程去获取它的镜像类型
                    HdlThreadLogic.Current.RunThread(() =>
                    {
                        var result = this.GetGatewayInfo(zbGateway, true, ShowErrorMode.NO);
                        var result = this.RefreshGatewayInfo(zbGateway, true, ShowErrorMode.NO);
                        if (result != null)
                        {
                            //将网关的数据设置到本地缓存中
                            this.SetGatewayDataToLocalMemmory(zbGateway, result, false);
                            HdlThreadLogic.Current.RunMain(() =>
                            {
                                button.UnSelectedImagePath = "Gateway/GatewayIcon" + result.LinuxImageType + ".png";
                                this.SetgatewayIcon(button, "Gateway/GatewayIcon" + result.LinuxImageType + ".png");
                            });
                        }
                    });
@@ -1754,8 +1768,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
@@ -1906,12 +1936,12 @@
        /// <summary>
        /// 重新发送命令去绑定断网情况下备份的网关
        /// </summary>
        public void ResetComandToBindBackupGateway()
        public bool ResetComandToBindBackupGateway()
        {
            var fileData = HdlFileLogic.Current.ReadFileByteContent(HdlFileNameResourse.BackupGatewayIdFile);
            if (fileData == null)
            {
                return;
                return true;
            }
            this.listBackupGwId = JsonConvert.DeserializeObject<List<string>>(System.Text.Encoding.UTF8.GetString(fileData));
            var listTempId = new List<string>();
@@ -1939,11 +1969,13 @@
                {
                    System.IO.File.Delete(file);
                }
                return true;
            }
            else
            {
                //备份
                HdlFileLogic.Current.SaveFileContent(HdlFileNameResourse.BackupGatewayIdFile, listBackupGwId);
                return false;
            }
        }
@@ -2169,7 +2201,7 @@
        private void CheckGatewayStatuByRemote(Dictionary<string, bool> dicOldOnline)
        {
            //获取云端上面的网关
            Dictionary<string, GatewayResult> dicDbGateway = this.GetGateWayListFromDataBase();
            Dictionary<string, GatewayResult> dicDbGateway = this.GetGateWayListFromDataBase(Config.Instance.Home.Id);
            if (dicDbGateway == null)
            {
                //如果网络不通,则也往下走
@@ -2283,7 +2315,7 @@
        {
            //调用接口,绑定网关(即使失败,也返回true往下走)
            var pra = new { homeId = Config.Instance.Home.Id, mac = i_gatewayId, gatewayType = "ZIGBEEGATEWAY" };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/gateway/bindGateway", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/app/gateway/bindGateway", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
            if (result == null || result.Code != HttpMessageEnum.A成功)
            {
                return -1;
@@ -2295,11 +2327,11 @@
        /// 从云端获取全部网关列表ID
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, GatewayResult> GetGateWayListFromDataBase()
        public Dictionary<string, GatewayResult> GetGateWayListFromDataBase(string i_homeId)
        {
            //这个接口特殊,不需要检测权限
            var pra = new { homeId = Config.Instance.Home.Id };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/gateway/getGatewayList", RestSharp.Method.POST, pra);
            var pra = new { homeId = i_homeId };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/app/gateway/getGatewayList", RestSharp.Method.POST, pra);
            if (result == null || result.Code != HttpMessageEnum.A成功)
            {
                return null;
@@ -2324,7 +2356,7 @@
        public bool DeleteDataBaseGateway(string strWayId)
        {
            var pra = new { homeId = Config.Instance.Home.Id, mac = strWayId };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/gateway/untieGateway", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/app/gateway/untieGateway", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
            if (result == null || result.Code != HttpMessageEnum.A成功)
            {
                return false;
@@ -2337,6 +2369,58 @@
        #region ■ 一般方法___________________________
        /// <summary>
        /// 检测本地全部网关,是否都支持发送启动模板导入的Json命令(返回的是不支持启动命令的网关ID)
        /// </summary>
        /// <returns></returns>
        public List<string> CheckAllGatewayCanSendSetupComand()
        {
            var list = new List<string>();
            foreach (var gateway in this.dicGateway.Values)
            {
                if (gateway.LinuxFirmwareVersion < 10205)
                {
                    //网关小于这个版本,都需要升级
                    list.Add(gateway.GwId);
                }
            }
            return list;
        }
        /// <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>