黄学彪
2020-12-16 0d9f64668fd7350d6a21fd157e32009a96d98134
ZigbeeApp/Shared/Phone/Common/Logic/HdlGatewayLogic.cs
File was renamed from ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlGatewayLogic.cs
@@ -5,7 +5,7 @@
using System.Threading.Tasks;
using ZigBee.Device;
namespace Shared.Phone.UserCenter
namespace Shared.Phone
{
    /// <summary>
    /// 网关业务的逻辑类
@@ -79,25 +79,25 @@
        /// </summary>
        public void RefreshAppOldSelectGatewayId()
        {
            GatewayResourse.AppOldSelectGatewayId = string.Empty;
            HdlGatewayResourse.AppOldSelectGatewayId = string.Empty;
            //从文件中获取上一次选择的网关id
            byte[] data = HdlFileLogic.Current.ReadFileByteContent(DirNameResourse.AppOldSelectGatewayFile);
            byte[] data = HdlFileLogic.Current.ReadFileByteContent(HdlFileNameResourse.AppOldSelectGatewayFile);
            if (data != null)
            {
                string strvalue = System.Text.Encoding.UTF8.GetString(data);
                GatewayResourse.AppOldSelectGatewayId = JsonConvert.DeserializeObject<string>(strvalue);
                HdlGatewayResourse.AppOldSelectGatewayId = JsonConvert.DeserializeObject<string>(strvalue);
            }
            //如果本地没有这个网关的话
            if (this.IsGatewayExist(GatewayResourse.AppOldSelectGatewayId) == false)
            if (this.IsGatewayExist(HdlGatewayResourse.AppOldSelectGatewayId) == false)
            {
                GatewayResourse.AppOldSelectGatewayId = string.Empty;
                HdlGatewayResourse.AppOldSelectGatewayId = string.Empty;
                lock (dicGateway)
                {
                    //随便选一个网关
                    foreach (string wayId in this.dicGateway.Keys)
                    {
                        GatewayResourse.AppOldSelectGatewayId = wayId;
                        HdlGatewayResourse.AppOldSelectGatewayId = wayId;
                        break;
                    }
                }
@@ -111,14 +111,14 @@
        public void SynchronizeDbGateway()
        {
            //从云端获取网列表ID
            Dictionary<string, GatewayResult> result = HdlGatewayLogic.Current.GetGateWayListFromDataBase();
            Dictionary<string, GatewayResult> result = this.GetGateWayListFromDataBase(Config.Instance.Home.Id);
            if (result == null)
            {
                return;
            }
            List<string> listBackupGwId = new List<string>();
            var fileData = HdlFileLogic.Current.ReadFileByteContent(DirNameResourse.BackupGatewayIdFile);
            var fileData = HdlFileLogic.Current.ReadFileByteContent(HdlFileNameResourse.BackupGatewayIdFile);
            if (fileData != null)
            {
                //新增:虽然概率低,但是确实发生了。如果有网络时,App重新绑定记录的网关失败的话
@@ -138,13 +138,13 @@
            foreach (var gatewayId in listDelete)
            {
                //删除本地这个网关所有的设备
                List<CommonDevice> list = Common.LocalDevice.Current.GetDeviceByGatewayID(gatewayId);
                List<CommonDevice> list = HdlDeviceCommonLogic.Current.GetDeviceByGatewayID(gatewayId);
                foreach (var device in list)
                {
                    //删除一般设备
                    Common.LocalDevice.Current.DeleteMemmoryDevice(device, true);
                    HdlDeviceCommonLogic.Current.DeleteMemmoryDevice(device, true);
                    //删除Ota设备
                    Common.LocalDevice.Current.DeleteMemmoryOtaDevice(device.DeviceAddr);
                    HdlDeviceCommonLogic.Current.DeleteMemmoryOtaDevice(device.DeviceAddr);
                }
                //删除网关文件
                this.DeleteGatewayFile(gatewayId);
@@ -204,7 +204,7 @@
        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);
            bool falge = this.SetGatewaySite(zbGateway.GwId, Common.Config.Instance.Home.Longitude, Common.Config.Instance.Home.Latitude, ShowErrorMode.NO);
            if (falge == false)
            {
                if (logMode == WriteLogMode.YES)
@@ -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)
@@ -310,7 +310,7 @@
            //设置住宅ID到网关
            if (result.HomeId != Common.Config.Instance.HomeId)
            {
                bool flage2 = this.SetHomeIdToGateway(zbGateway.GwId, Common.Config.Instance.HomeId, mode);
                bool flage2 = this.SetHomeIdToGateway(zbGateway, Common.Config.Instance.HomeId, mode);
                if (flage2 == false)
                {
                    if (mode == ShowErrorMode.YES)
@@ -328,7 +328,7 @@
            }
            //更新云端数据库
            int flage1 = this.SetGatewayIdToDataBase(zbGateway);
            int flage1 = this.SetGatewayIdToDataBase(zbGateway.GwId);
            //异常也不鸟它,0是特殊含义
            if (flage1 == 0)
            {
@@ -361,7 +361,7 @@
            if (isEsist == false)
            {
                //添加备份
                HdlAutoBackupLogic.AddOrEditorFile(this.dicGateway[gwID].FilePath);
                HdlBackupLogic.Current.AddOrEditorAutoBackFileStatu(this.dicGateway[gwID].FilePath);
            }
            return 1;
@@ -370,30 +370,22 @@
        /// <summary>
        /// 设置住宅ID到网关(失败时,不弹出任何错误信息,网关断网除外)
        /// </summary>
        /// <param name="i_gwId"></param>
        /// <param name="zbGateway"></param>
        /// <param name="HomeId"></param>
        /// <returns></returns>
        public bool SetHomeIdToGateway(string i_gwId, string HomeId, ShowErrorMode mode)
        public bool SetHomeIdToGateway(ZbGateway zbGateway, string HomeId, ShowErrorMode mode)
        {
            //账号ID
            string accountId = string.Empty;
            if (HomeId != string.Empty)
            {
                if (UserCenterResourse.UserInfo.AuthorityNo == 1)
                {
                    //主账号
                    accountId = Config.Instance.Guid;
                }
                else
                {
                    accountId = Config.Instance.Home.MainUserDistributedMark;
                }
                accountId = Config.Instance.Guid;
            }
            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(i_gwId, "GwSetHomeId", jObject.ToString(), "GwSetHomeId_Respon", 5, true);
            var result = this.SendJobjectDataToGateway(zbGateway.GwId, "GwSetHomeId", jObject.ToString(), "GwSetHomeId_Respon", 5, true);
            if (result.ErrorMsgDiv == -1)
            {
                if (mode == ShowErrorMode.YES)
@@ -409,32 +401,6 @@
                return false;
            }
            return true;
        }
        /// <summary>
        /// 更新网关ID到云端数据库(1:正常  -1:异常  0:当前的网关绑定在了当前账号下的不同住宅里面)
        /// </summary>
        /// <param name="zbGateway">网关对象</param>
        /// <returns></returns>
        private int SetGatewayIdToDataBase(ZbGateway zbGateway)
        {
            //调用接口,绑定网关(即使失败,也返回true往下走)
            var bindGateway = new BindGatewayPra();
            bindGateway.BindGateways.Add(zbGateway.GwId);
            //获取控制主人账号的Token
            bindGateway.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
            var result = UserCenterLogic.GetResultCodeByRequestHttps("App/BindGatewayToHome", true, bindGateway, new List<string> { "NotCheck" }, false);
            if (result == "Error")
            {
                return -1;
            }
            if (result == "BindGatewaysExists")
            {
                return 0;
            }
            return result == "Success" ? 1 : -1;
        }
        /// <summary>
@@ -480,14 +446,14 @@
                return -1;
            }
            //设置网关的经纬度
            bool falge = this.SetGatewaySite(zbGateway, Common.Config.Instance.Home.Longitude, Common.Config.Instance.Home.Latitude, ShowErrorMode.YES);
            bool falge = this.SetGatewaySite(zbGateway.GwId, Common.Config.Instance.Home.Longitude, Common.Config.Instance.Home.Latitude, ShowErrorMode.YES);
            if (falge == false)
            {
                return -1;
            }
            //设置住宅ID到网关
            bool flage2 = this.SetHomeIdToGateway(zbGateway.GwId, Common.Config.Instance.HomeId, ShowErrorMode.YES);
            bool flage2 = this.SetHomeIdToGateway(zbGateway, Common.Config.Instance.HomeId, ShowErrorMode.YES);
            if (flage2 == false)
            {
                //向网关设置住宅ID失败
@@ -497,7 +463,7 @@
            }
            //更新云端数据库
            int flage1 = this.SetGatewayIdToDataBase(zbGateway);
            int flage1 = this.SetGatewayIdToDataBase(zbGateway.GwId);
            //异常也不鸟它,0是特殊含义
            if (flage1 == 0)
            {
@@ -506,7 +472,7 @@
            if (flage1 == -1)
            {
                //备份失败的网关ID
                HdlGatewayLogic.Current.BackupGatewayIdOnNotNetwork(zbGateway);
                this.BackupGatewayIdOnNotNetwork(zbGateway);
            }
            if (btnMsg == null)
@@ -529,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;
@@ -567,7 +533,7 @@
            if (isEsist == false)
            {
                //添加备份
                HdlAutoBackupLogic.AddOrEditorFile(this.dicGateway[gwID].FilePath);
                HdlBackupLogic.Current.AddOrEditorAutoBackFileStatu(this.dicGateway[gwID].FilePath);
            }
            //添加网关的话,强制主页刷新
@@ -576,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;
                    }
                }
@@ -621,7 +587,7 @@
                //网关名称修改失败
                string msg = Language.StringByID(R.MyInternationalizationString.uGatewayReNameFail);
                //网关回复超时
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                msg = HdlCommonLogic.Current.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                this.ShowTipMsg(msg);
                return false;
            }
@@ -632,7 +598,7 @@
            this.dicGateway[gwID].ReSave();
            //添加自动备份
            HdlAutoBackupLogic.AddOrEditorFile(zbGateway.FilePath);
            HdlBackupLogic.Current.AddOrEditorAutoBackFileStatu(zbGateway.FilePath);
            return true;
        }
@@ -702,7 +668,7 @@
            if (getGatewayInfo == true)
            {
                //获取网关信息
                var info = this.GetGatewayInfo(realWay, false);
                var info = this.RefreshGatewayInfo(realWay, false);
                if (info == null)
                {
                    //关闭进度条
@@ -717,7 +683,7 @@
            }
            //获取全部设备
            int result = LocalDevice.Current.SetDeviceToMemmoryByGateway(realWay.GwId, false);
            int result = HdlDeviceCommonLogic.Current.SetDeviceToMemmoryByGateway(realWay.GwId, false);
            //关闭进度条
            ProgressBar.Close();
            if (result == -1)
@@ -733,8 +699,8 @@
        /// <param name="gatewayId"></param>
        public void SaveGatewayIdToLocation(string gatewayId)
        {
            GatewayResourse.AppOldSelectGatewayId = gatewayId;
            HdlFileLogic.Current.SaveFileContent(DirNameResourse.AppOldSelectGatewayFile, GatewayResourse.AppOldSelectGatewayId);
            HdlGatewayResourse.AppOldSelectGatewayId = gatewayId;
            HdlFileLogic.Current.SaveFileContent(HdlFileNameResourse.AppOldSelectGatewayFile, HdlGatewayResourse.AppOldSelectGatewayId);
        }
        #endregion
@@ -752,7 +718,7 @@
            //清空网关的住宅ID 网关解绑失败  不理它,因为网关可以按按键强制搜索得到
            if (realWay != null)
            {
                this.SetHomeIdToGateway(realWay.GwId, string.Empty, ShowErrorMode.NO);
                this.SetHomeIdToGateway(realWay, string.Empty, ShowErrorMode.NO);
            }
            //删除云端的网关
@@ -792,21 +758,21 @@
            //移除缓存
            dicGateway.Remove(zbGatewayID);
            //删除自动备份
            HdlAutoBackupLogic.DeleteFile(file);
            HdlBackupLogic.Current.DeleteAutoBackFileStatu(file);
            //删除设备文件
            List<CommonDevice> list = Common.LocalDevice.Current.GetDeviceByGatewayID(zbGatewayID);
            List<CommonDevice> list = HdlDeviceCommonLogic.Current.GetDeviceByGatewayID(zbGatewayID);
            foreach (var device in list)
            {
                //删除设备文件
                Common.LocalDevice.Current.DeleteMemmoryDevice(device, true);
                HdlDeviceCommonLogic.Current.DeleteMemmoryDevice(device, true);
                //删除Ota设备
                Common.LocalDevice.Current.DeleteMemmoryOtaDevice(device.DeviceAddr);
                HdlDeviceCommonLogic.Current.DeleteMemmoryOtaDevice(device.DeviceAddr);
            }
            //如果是主网关
            if (this.IsMainGateway(zbGatewayID) == 1)
            {
                var listAllRoom = UserCenter.HdlRoomLogic.Current.GetAllListRooms();
                var listAllRoom = HdlRoomLogic.Current.GetAllListRooms();
                foreach (var room in listAllRoom)
                {
                    //删除场景文件
@@ -882,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;
@@ -1123,7 +1089,7 @@
            {
                //获取协调器信道失败
                string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayChannelIdFail);
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                msg = HdlCommonLogic.Current.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                this.ShowTipMsg(msg);
            }
@@ -1175,7 +1141,7 @@
            {
                //获取协调器Mac失败
                string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayCoordinatorMacFail);
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                msg = HdlCommonLogic.Current.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                this.ShowTipMsg(msg);
            }
@@ -1227,7 +1193,7 @@
            {
                //获取协调器PanID失败
                string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayPanIDFail);
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                msg = HdlCommonLogic.Current.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                this.ShowTipMsg(msg);
            }
@@ -1281,7 +1247,7 @@
            {
                //获取网关自动备份设置失败
                string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayAutoBackupStatuFail);
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                msg = HdlCommonLogic.Current.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                this.ShowTipMsg(msg);
            }
@@ -1332,7 +1298,7 @@
            {
                //设置网关自动备份失败
                string msg = Language.StringByID(R.MyInternationalizationString.uSetGatewayAutoBackupStatuFail);
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                msg = HdlCommonLogic.Current.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                this.ShowTipMsg(msg);
                return false;
            }
@@ -1366,7 +1332,7 @@
                return name;
            }
            var nameContent = LocalDevice.Current.GetDeviceModelIdNameInfo(localWay.LinuxImageType.ToString());
            var nameContent = HdlDeviceCommonLogic.Current.GetDeviceModelIdNameInfo(localWay.LinuxImageType.ToString());
            if (nameContent != null)
            {
                return nameContent.A备注名字;
@@ -1406,7 +1372,7 @@
                //如果这个网关没有信息,则从新获取
                if (zbGateway.LinuxImageType != -1)
                {
                    var nameContent = LocalDevice.Current.GetDeviceModelIdNameInfo(zbGateway.LinuxImageType.ToString());
                    var nameContent = HdlDeviceCommonLogic.Current.GetDeviceModelIdNameInfo(zbGateway.LinuxImageType.ToString());
                    if (nameContent != null)
                    {
                        button.Text = nameContent.A官方名字;
@@ -1417,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)
                        {
                            //将网关的数据设置到本地缓存中
@@ -1425,7 +1391,7 @@
                            HdlThreadLogic.Current.RunMain(() =>
                            {
                                var nameContent = LocalDevice.Current.GetDeviceModelIdNameInfo(zbGateway.LinuxImageType.ToString());
                                var nameContent = HdlDeviceCommonLogic.Current.GetDeviceModelIdNameInfo(zbGateway.LinuxImageType.ToString());
                                if (nameContent != null)
                                {
                                    button.Text = nameContent.A官方名字;
@@ -1437,7 +1403,7 @@
            }
            else
            {
                var nameContent = LocalDevice.Current.GetDeviceModelIdNameInfo(zbGateway.LinuxImageType.ToString());
                var nameContent = HdlDeviceCommonLogic.Current.GetDeviceModelIdNameInfo(zbGateway.LinuxImageType.ToString());
                if (nameContent != null)
                {
                    button.Text = nameContent.A官方名字;
@@ -1450,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)
            {
@@ -1469,23 +1452,20 @@
                    //获取网关信息失败
                    string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayInfoFail);
                    //拼接上【网关回复超时】的Msg
                    msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                    msg = HdlCommonLogic.Current.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                    this.ShowErrorMsg(msg);
                }
                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;
        }
@@ -1497,8 +1477,6 @@
        /// <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;
@@ -1576,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;
@@ -1589,7 +1567,7 @@
            }
            //添加网关的升级固件(成不成功都无所谓)
            var flage = HdlFirmwareUpdateLogic.AddFirmwareVersionInfo(FirmwareLevelType.Linux,
            var flage = HdlFirmwareUpdateLogic.Current.AddFirmwareVersionInfo(FirmwareLevelType.Linux,
                localWay.LinuxHardVersion.ToString(),
                localWay.LinuxImageType.ToString());
@@ -1597,19 +1575,19 @@
            if (flage == 1)
            {
                //没网的时候不再处理
                HdlFirmwareUpdateLogic.AddFirmwareVersionInfo(FirmwareLevelType.A协调器,
                HdlFirmwareUpdateLogic.Current.AddFirmwareVersionInfo(FirmwareLevelType.A协调器,
                    localWay.CoordinatorHardVersion.ToString(),
                    localWay.CoordinatorImageId.ToString());
            }
            //网关的版本
            var gatewayFirmware = HdlFirmwareUpdateLogic.GetFirmwareMostVersionInfo(FirmwareLevelType.Linux,
            var gatewayFirmware = HdlFirmwareUpdateLogic.Current.GetFirmwareMostVersionInfo(FirmwareLevelType.Linux,
                localWay.LinuxHardVersion.ToString(),
                localWay.LinuxImageType.ToString(),
                localWay.LinuxFirmwareVersion);
            //协调器版本
            var coordinatorFirmware = HdlFirmwareUpdateLogic.GetFirmwareMostVersionInfo(FirmwareLevelType.A协调器,
            var coordinatorFirmware = HdlFirmwareUpdateLogic.Current.GetFirmwareMostVersionInfo(FirmwareLevelType.A协调器,
                localWay.CoordinatorHardVersion.ToString(),
                localWay.CoordinatorImageId.ToString(),
                localWay.CoordinatorFirmwareVersion);
@@ -1619,7 +1597,7 @@
            list.Add(coordinatorFirmware);
            //这个网关需要有虚拟驱动这个东西才行
            if (this.CheckGatewayHadDriveCode(localWay) == true)
            if (HdlGatewayLogic.Current.CheckGatewayHadDriveCode(localWay) == true)
            {
                //虚拟驱动号
                foreach (var data in localWay.DriveCodeList)
@@ -1627,13 +1605,13 @@
                    //添加虚拟驱动的升级固件(成不成功都无所谓) 必须能够联网才行
                    if (flage == 1)
                    {
                        HdlFirmwareUpdateLogic.AddFirmwareVersionInfo(FirmwareLevelType.A虚拟驱动,
                        HdlFirmwareUpdateLogic.Current.AddFirmwareVersionInfo(FirmwareLevelType.A虚拟驱动,
                           data.DriveHwVersion.ToString(),
                           data.DriveImageType.ToString());
                    }
                    //虚拟驱动
                    var virtualFirmware = HdlFirmwareUpdateLogic.GetFirmwareMostVersionInfo(FirmwareLevelType.A虚拟驱动,
                    var virtualFirmware = HdlFirmwareUpdateLogic.Current.GetFirmwareMostVersionInfo(FirmwareLevelType.A虚拟驱动,
                        data.DriveHwVersion.ToString(),
                        data.DriveImageType.ToString(),
                        data.DriveFwVersion);
@@ -1737,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)
                        {
                            //将网关的数据设置到本地缓存中
@@ -1775,7 +1753,7 @@
                    //给一个线程去获取它的镜像类型
                    HdlThreadLogic.Current.RunThread(() =>
                    {
                        var result = this.GetGatewayInfo(zbGateway, true, ShowErrorMode.NO);
                        var result = this.RefreshGatewayInfo(zbGateway, true, ShowErrorMode.NO);
                        if (result != null)
                        {
                            //将网关的数据设置到本地缓存中
@@ -1867,90 +1845,18 @@
        #endregion
        #region ■ 从云端获取全部网关列表ID___________
        /// <summary>
        /// 从云端获取全部网关列表ID
        /// </summary>
        /// <returns>从云端获取全部网关列表ID</returns>
        public Dictionary<string, GatewayResult> GetGateWayListFromDataBase()
        {
            Dictionary<string, GatewayResult> dicDbGateway = null;
            if (UserCenterResourse.UserInfo.AuthorityNo == 3)
            {
                //成员
                return dicDbGateway;
            }
            bool canBreak = false;
            HdlThreadLogic.Current.RunThread(() =>
            {
                List<string> list = new List<string>() { "NotCheck" };
                //设置访问接口的参数
                var pra = new GetGatewayPra();
                pra.ReqDto.PageSetting.Page = 1;
                pra.ReqDto.PageSetting.PageSize = 999;
                //获取控制主人账号的Token
                pra.ReqDto.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
                var result = UserCenterLogic.GetResponseDataByRequestHttps("App/GetSingleHomeGatewayPagger", true, pra, list, false);
                if (string.IsNullOrEmpty(result) == true)
                {
                    canBreak = true;
                    return;
                }
                var infoResult = Newtonsoft.Json.JsonConvert.DeserializeObject<GetGatewayResult>(result);
                Dictionary<string, GatewayResult> dic = new Dictionary<string, GatewayResult>();
                foreach (var data in infoResult.PageData)
                {
                    dic[data.GatewayUniqueId] = data;
                }
                dicDbGateway = dic;
                canBreak = true;
            });
            int count = 0;
            while (canBreak == false)
            {
                System.Threading.Thread.Sleep(200);
                count++;
                if (count == 25)
                {
                    //如果5秒还不能获取得到数据,则中断此次操作
                    break;
                }
            }
            return dicDbGateway;
        }
        #endregion
        #region ■ 设置网关经纬度_____________________
        /// <summary>
        /// 设置网关经纬度
        /// </summary>
        /// <param name="gateway">网关对象</param>
        /// <param name="gatewayId">网关Id</param>
        /// <param name="Longitude">经度</param>
        /// <param name="Latitude">维度</param>
        /// <param name="mode">显示错误</param>
        /// <returns></returns>
        public bool SetGatewaySite(ZbGateway gateway, double Longitude, double Latitude, ShowErrorMode mode)
        public bool SetGatewaySite(string gatewayId, double Longitude, double Latitude, ShowErrorMode mode)
        {
            ZbGateway realWay = null;
            if (this.GetRealGateway(ref realWay, gateway) == false)
            {
                if (mode == ShowErrorMode.YES)
                {
                    //错误:网关对象丢失
                    string msg = Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg);
                    this.ShowTipMsg(msg);
                }
                return false;
            }
            if (((decimal)Longitude) == 0 && ((decimal)Latitude) == 0)
            {
                //没有配置过经纬度
@@ -1958,16 +1864,14 @@
            }
            int result = -1;
            Action<string, string> action = (topic, message) =>
            HdlGatewayReceiveLogic.Current.AddGatewayReceiveEvent(gatewayId, (topic, message) =>
            {
                var gatewayID = topic.Split('/')[0];
                if (topic == gatewayID + "/" + "Logic/SetSite_Respon")
                if (topic == gatewayId + "/" + "Logic/SetSite_Respon")
                {
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    result = Convert.ToInt32(jobject["Data"]["Result"].ToString());
                }
            };
            realWay.Actions += action;
            });
            //两位小数
            Longitude = Math.Round(Longitude, 2);
            Latitude = Math.Round(Latitude, 2);
@@ -1978,15 +1882,9 @@
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 2013 } };
            var data = new Newtonsoft.Json.Linq.JObject { { "Longitude", intLongitude }, { "Latitude", intLatitude } };
            jObject.Add("Data", data);
            if (this.IsGatewayExist(gateway) == true)
            {
                realWay.Send("Logic/SetSite", jObject.ToString());
            }
            else
            {
                //如果这个网关还没有绑定的话,则强制使用本地连接
                realWay.SendLocation("Logic/SetSite", System.Text.Encoding.UTF8.GetBytes(jObject.ToString()));
            }
            //如果这个网关还没有绑定的话,则强制使用本地连接
            this.SendJobjectData(gatewayId, "Logic/SetSite", jObject.ToString(), this.IsGatewayExist(gatewayId) == false);
            int TimeOut = 0;
            while (result == -1 && TimeOut < 30)
@@ -1995,7 +1893,8 @@
                TimeOut++;
            }
            realWay.Actions -= action;
            //移除监听事件
            HdlGatewayReceiveLogic.Current.RemoveGatewayReceiveEvent();
            if (result != 0)
            {
                if (mode == ShowErrorMode.YES)
@@ -2004,38 +1903,13 @@
                    string msg = Language.StringByID(R.MyInternationalizationString.uSetGatewaySiteFail);
                    if (result == -1)
                    {
                        msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                        msg = HdlCommonLogic.Current.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                    }
                    this.ShowTipMsg(msg);
                }
                return false;
            }
            return true;
        }
        #endregion
        #region ■ 解绑云端网关_______________________
        /// <summary>
        /// 解绑云端绑定的网关
        /// </summary>
        /// <param name="strWayId"></param>
        public bool DeleteDataBaseGateway(string strWayId)
        {
            var Pra = new DeleteGatewayPra();
            Pra.BindGateways.Add(strWayId);
            //获取控制主人账号的Token
            Pra.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
            List<string> listNotShowError = new List<string>() { "NoExist", "NoBind", "NoRecord", "NotCheck" };
            bool result = UserCenterLogic.GetResultStatuByRequestHttps("App/ReleaseGatewayToHome", true, Pra, listNotShowError, false);
            if (result == false)
            {
                return false;
            }
            return true;
        }
@@ -2055,7 +1929,7 @@
                listBackupGwId.Add(strId);
                //备份
                HdlFileLogic.Current.SaveFileContent(DirNameResourse.BackupGatewayIdFile, listBackupGwId);
                HdlFileLogic.Current.SaveFileContent(HdlFileNameResourse.BackupGatewayIdFile, listBackupGwId);
            }
        }
@@ -2064,39 +1938,33 @@
        /// </summary>
        public bool ResetComandToBindBackupGateway()
        {
            var fileData = HdlFileLogic.Current.ReadFileByteContent(DirNameResourse.BackupGatewayIdFile);
            var fileData = HdlFileLogic.Current.ReadFileByteContent(HdlFileNameResourse.BackupGatewayIdFile);
            if (fileData == null)
            {
                return true;
            }
            this.listBackupGwId = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(System.Text.Encoding.UTF8.GetString(fileData));
            this.listBackupGwId = 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();
            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")
                if (this.IsGatewayExist(gwId) == true)
                {
                    this.listBackupGwId.Remove(gwId);
                    //调用接口,绑定网关(即使失败,也返回true往下走)
                    var result = this.SetGatewayIdToDataBase(gwId);
                    if (result != 1)
                    {
                        break;
                    }
                }
                if (result == "Error")
                {
                    break;
                }
                this.listBackupGwId.Remove(gwId);
            }
            if (this.listBackupGwId.Count == 0)
            {
                //如果没有了内容,则删除文件
                string file = DirNameResourse.BackupGatewayIdFile;
                string file = HdlFileNameResourse.BackupGatewayIdFile;
                if (System.IO.File.Exists(file) == true)
                {
                    System.IO.File.Delete(file);
@@ -2106,7 +1974,7 @@
            else
            {
                //备份
                HdlFileLogic.Current.SaveFileContent(DirNameResourse.BackupGatewayIdFile, listBackupGwId);
                HdlFileLogic.Current.SaveFileContent(HdlFileNameResourse.BackupGatewayIdFile, listBackupGwId);
                return false;
            }
        }
@@ -2142,7 +2010,7 @@
                if (topic == errorTopic)
                {
                    var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.ErrorResponData>(jobject["Data"].ToString());
                    reResult.ErrorMsg = HdlCheckLogic.Current.CheckCommonErrorCode(temp.Error);
                    reResult.ErrorMsg = HdlCheckLogic.Current.CheckGatewayErrorCode(temp.Error);
                }
                //如果是指定的主题
                if (topic == checkTopic)
@@ -2210,98 +2078,6 @@
                //强制使用本地连接
                realWay.SendLocation(topic, System.Text.Encoding.UTF8.GetBytes(sendData));
            }
        }
        #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
@@ -2425,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)
            {
                //如果网络不通,则也往下走
@@ -2498,13 +2274,9 @@
        {
            try
            {
                for (int i = 0; i < UserCenterResourse.listActionFormId.Count; i++)
                var listForm = HdlFormLogic.Current.GetAllOpenForm();
                for (int i = 0; i < listForm.Count; i++)
                {
                    string formId = UserCenterResourse.listActionFormId[i];
                    if (UserCenterResourse.DicActionForm.ContainsKey(formId) == false)
                    {
                        continue;
                    }
                    //网关在线推送
                    var zbway = this.GetLocalGateway(gwId);
                    if (zbway != null)
@@ -2514,7 +2286,7 @@
                        {
                            zbway.OnlineTime = DateTime.Now;
                        }
                        UserCenterResourse.DicActionForm[formId]?.GatewayOnlinePush(zbway, online, hadGwOnline);
                        listForm[i]?.GatewayOnlinePush(zbway, online, hadGwOnline);
                    }
                }
            }
@@ -2532,9 +2304,89 @@
        #endregion
        #region ■ 调用云端接口的方法_________________
        /// <summary>
        /// 更新网关ID到云端数据库(1:正常  -1:异常  0:当前的网关绑定在了当前账号下的不同住宅里面)
        /// </summary>
        /// <param name="i_gatewayIds">网关ID</param>
        /// <returns></returns>
        private int SetGatewayIdToDataBase(string i_gatewayId)
        {
            //调用接口,绑定网关(即使失败,也返回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账号权限);
            if (result == null || result.Code != HttpMessageEnum.A成功)
            {
                return -1;
            }
            return 1;
        }
        /// <summary>
        /// 从云端获取全部网关列表ID
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, GatewayResult> GetGateWayListFromDataBase(string i_homeId)
        {
            //这个接口特殊,不需要检测权限
            var pra = new { homeId = i_homeId };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/gateway/getGatewayList", RestSharp.Method.POST, pra);
            if (result == null || result.Code != HttpMessageEnum.A成功)
            {
                return null;
            }
            var listData = JsonConvert.DeserializeObject<List<GatewayResult>>(result.Data.ToString());
            var dicDbGateway = new Dictionary<string, GatewayResult>();
            foreach (var data in listData)
            {
                if (data.GatewayType == "ZIGBEEGATEWAY")
                {
                    dicDbGateway[data.Mac] = data;
                }
            }
            return dicDbGateway;
        }
        /// <summary>
        /// 解绑云端绑定的网关
        /// </summary>
        /// <param name="strWayId"></param>
        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账号权限);
            if (result == null || result.Code != HttpMessageEnum.A成功)
            {
                return false;
            }
            return true;
        }
        #endregion
        #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>