gxc
2020-02-28 66a9965c44ecc32a6696abca876ab9d1cd091584
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlGatewayLogic.cs
@@ -117,10 +117,20 @@
            {
                return;
            }
            List<string> listBackupGwId = new List<string>();
            var fileData = Global.ReadFileByDirectory(DirNameResourse.LocalMemoryDirectory, DirNameResourse.BackupGatewayIdFile);
            if (fileData != null)
            {
                //新增:虽然概率低,但是确实发生了。如果有网络时,App重新绑定记录的网关失败的话
                //不应该删除它
                listBackupGwId = JsonConvert.DeserializeObject<List<string>>(System.Text.Encoding.UTF8.GetString(fileData));
            }
            List<string> listDelete = new List<string>();
            foreach (var gatewayId in this.dicGateway.Keys)
            {
                if (result.ContainsKey(gatewayId) == false)
                if (result.ContainsKey(gatewayId) == false && listBackupGwId.Contains(gatewayId) == false)
                {
                    //本地存在云端不存在的网关,则删除
                    listDelete.Add(gatewayId);
@@ -132,8 +142,10 @@
                List<CommonDevice> list = Common.LocalDevice.Current.GetDeviceByGatewayID(gatewayId);
                foreach (var device in list)
                {
                    //删除设备,不删除房间信息
                    Common.LocalDevice.Current.DeleteMemmoryDevice(device, false);
                    //删除一般设备
                    Common.LocalDevice.Current.DeleteMemmoryDevice(device, true);
                    //删除Ota设备
                    Common.LocalDevice.Current.DeleteMemmoryOtaDevice(device.DeviceAddr);
                }
                //删除网关文件
                this.DeleteGatewayFile(gatewayId);
@@ -168,10 +180,18 @@
        /// <returns></returns>
        private ZbGateway GetGatewayFromFile(string file)
        {
            byte[] filebyte = Global.ReadFileByHomeId(file);
            string strvalue = System.Text.Encoding.UTF8.GetString(filebyte);
            var gateway = JsonConvert.DeserializeObject<ZbGateway>(strvalue);
            return gateway;
            try
            {
                byte[] filebyte = Global.ReadFileByHomeId(file);
                string strvalue = System.Text.Encoding.UTF8.GetString(filebyte);
                var gateway = JsonConvert.DeserializeObject<ZbGateway>(strvalue);
                return gateway;
            }
            catch (Exception ex)
            {
                HdlLogLogic.Current.WriteLog(ex);
                return null;
            }
        }
        #endregion
@@ -202,11 +222,13 @@
                }
                return false;
            }
            if (result != 1)
            if (result == -1)
            {
                return false;
            }
            return true;
            //设置网关的经纬度
            bool falge = this.SetGatewaySite(zbGateway, Common.Config.Instance.Home.Longitude, Common.Config.Instance.Home.Latitude);
            return falge;
        }
        /// <summary>
@@ -405,6 +427,7 @@
        /// 重新绑定网关(1:正常  -1:异常  0:当前的网关绑定在了当前账号下的不同住宅里面)
        /// </summary>
        /// <param name="zbGateway">网关</param>
        /// <param name="btnMsg">消息控件</param>
        public async Task<int> ReBindNewGateway(ZbGateway zbGateway, NormalViewControl btnMsg = null)
        {
            if (zbGateway == null)
@@ -412,6 +435,12 @@
                //错误:网关对象丢失
                string msg = Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg);
                this.ShowTipMsg(msg);
                return -1;
            }
            //设置网关的经纬度
            bool falge = this.SetGatewaySite(zbGateway, Common.Config.Instance.Home.Longitude, Common.Config.Instance.Home.Latitude);
            if (falge == false)
            {
                return -1;
            }
@@ -451,7 +480,7 @@
                    btnMsg.TextID = R.MyInternationalizationString.uGatewayDataIsChangingPleaseWhait;
                });
            }
            await System.Threading.Tasks.Task.Delay(8000);
            await Task.Delay(8000);
            //获取网关的信息
            var result = await zbGateway.GetZbGwInfoAsync();
@@ -463,15 +492,7 @@
                return -1;
            }
            if (result == null)
            {
                //获取网关信息失败
                string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayInfoFail);
                this.ShowTipMsg(msg);
                return -1;
            }
            if (result.getGwData == null)
            if (result == null || result.getGwData == null)
            {
                //获取网关信息失败
                string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayInfoFail);
@@ -639,10 +660,10 @@
            }
            //获取全部设备
            bool result = LocalDevice.Current.SetDeviceToMemmoryByGateway(realWay);
            int result = LocalDevice.Current.SetDeviceToMemmoryByGateway(realWay);
            //关闭进度条
            ProgressBar.Close();
            if (result == false)
            if (result == -1)
            {
                return false;
            }
@@ -691,15 +712,15 @@
                bool result = await this.SetHomeIdToGateway(realWay, string.Empty);
                if (result == false)
                {
                    //网关解绑失败
                    //网关解绑失败  不理它,因为网关可以按按键强制搜索得到
                    string msg = Language.StringByID(R.MyInternationalizationString.uGatewayUnBindFail);
                    this.ShowErrorMsg(msg);
                    return false;
                    //this.ShowErrorMsg(msg);
                    //return false;
                }
            }
            //删除云端的网关
            await Phone.UserCenter.HdlGatewayLogic.Current.DeleteDataBaseGateway(zbGatewayID);
            await this.DeleteDataBaseGateway(zbGatewayID);
            //删除网关文件
            this.DeleteGatewayFile(zbGatewayID);
@@ -740,6 +761,8 @@
            {
                //删除设备文件
                Common.LocalDevice.Current.DeleteMemmoryDevice(device, true);
                //删除Ota设备
                Common.LocalDevice.Current.DeleteMemmoryOtaDevice(device.DeviceAddr);
            }
            //如果是主网关
            if (this.IsMainGateway(zbGatewayID) == 1)
@@ -1830,7 +1853,15 @@
                return;
            }
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 85 } };
            realWay.Send("GwLinuxLocate_Respon", jObject.ToString());
            if (this.IsGatewayExist(zbGateway) == true)
            {
                realWay.Send("GwLinuxLocate_Respon", jObject.ToString());
            }
            else
            {
                //如果这个网关还没有绑定的话,则强制使用本地连接
                realWay.SendLocation("GwLinuxLocate_Respon", System.Text.Encoding.UTF8.GetBytes(jObject.ToString()));
            }
        }
        #endregion
@@ -2119,7 +2150,7 @@
            bool canBreak = false;
            HdlThreadLogic.Current.RunThread(async () =>
            {
                List<string> list = new List<string>() { "NotSetAgain" };
                List<string> list = new List<string>() { "NotSetAgain", "NotCheck" };
                //设置访问接口的参数
                var pra = new GetGatewayPra();
@@ -2203,7 +2234,15 @@
            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);
            realWay.Send("Logic/SetSite", jObject.ToString());
            if (this.IsGatewayExist(gateway) == true)
            {
                realWay.Send("Logic/SetSite", jObject.ToString());
            }
            else
            {
                //如果这个网关还没有绑定的话,则强制使用本地连接
                realWay.SendLocation("Logic/SetSite", System.Text.Encoding.UTF8.GetBytes(jObject.ToString()));
            }
            int TimeOut = 0;
            while (result == -1 && TimeOut < 30)
@@ -2243,7 +2282,7 @@
            //获取控制主人账号的Token
            Pra.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
            List<string> listNotShowError = new List<string>() { "NoExist", "NoBind", "NoRecord" };
            List<string> listNotShowError = new List<string>() { "NoExist", "NoBind", "NoRecord", "NotSetAgain" };
            bool result = await UserCenterLogic.GetResultStatuByRequestHttps("App/ReleaseGatewayToHome", true, Pra, listNotShowError);
            if (result == false)
@@ -2345,27 +2384,27 @@
        /// <returns></returns>
        public async Task<bool> UpLoadDeviceBackupDataToGateway(CommonDevice device, GatewayBackupEnum backupEnum, object upLaodData)
        {
            //ZbGateway realWay = null;
            //if (HdlGatewayLogic.Current.GetRealGateway(ref realWay, device.CurrentGateWayId) == false)
            //{
            //    //没有找到真实物理网关
            //    return false;
            //}
            //string fileName = device.FilePath + ((int)backupEnum).ToString().PadLeft(5, '0');
            ////创建文件对象
            //var result = await realWay.CreateFileAsync(fileName);
            //if (result == null || result.Result != 0)
            //{
            //    return false;
            //}
            ////发送数据流
            //var data = Newtonsoft.Json.JsonConvert.SerializeObject(upLaodData);
            //var byteData = System.Text.Encoding.UTF8.GetBytes(data);
            //var result2 = await realWay.SendFileAsync(byteData);
            //if (result2 == null || result2.Result != 0)
            //{
            //    return false;
            //}
            ZbGateway realWay = null;
            if (HdlGatewayLogic.Current.GetRealGateway(ref realWay, device.CurrentGateWayId) == false)
            {
                //没有找到真实物理网关
                return false;
            }
            string fileName = device.FilePath + ((int)backupEnum).ToString().PadLeft(5, '0');
            //创建文件对象
            var result = await realWay.CreateFileAsync(fileName);
            if (result == null || result.Result != 0)
            {
                return false;
            }
            //发送数据流
            var data = Newtonsoft.Json.JsonConvert.SerializeObject(upLaodData);
            var byteData = System.Text.Encoding.UTF8.GetBytes(data);
            var result2 = await realWay.SendFileAsync(byteData);
            if (result2 == null || result2.Result != 0)
            {
                return false;
            }
            return true;
        }
@@ -2512,7 +2551,7 @@
            else if (backType == GatewayBackupEnum.APir灯光配置)
            {
                var recoverData = Newtonsoft.Json.JsonConvert.DeserializeObject<IASZone.ConfigureParamates>(System.Text.Encoding.UTF8.GetString(byteData));
                result = await HdlDevicePirSensorLogic.Current.SetPirSensorLightSettion((IASZone)device, recoverData);
                result = await HdlDevicePirSensorLogic.Current.SetPirSensorSettion((IASZone)device, recoverData);
            }
            else if (backType == GatewayBackupEnum.A干接点颜色调节)
            {
@@ -2534,10 +2573,10 @@
                int level = Convert.ToInt32(recoverData["level"]);
                result = await HdlDevicePanelLogic.Current.SetDeviceEnergyConservationMode((Panel)device, modeEnable, modeTime, level);
            }
            else if (backType == GatewayBackupEnum.A干接点私有属性)
            else if (backType == GatewayBackupEnum.A干接点第三级别私有属性)
            {
                var recoverData = Newtonsoft.Json.JsonConvert.DeserializeObject<int>(System.Text.Encoding.UTF8.GetString(byteData));
                result = await HdlDevicePanelLogic.Current.EditorDryContactFunction((Panel)device, recoverData);
                result = await HdlDevicePanelLogic.Current.EditorDryContactThirdFunction((Panel)device, recoverData);
            }
            else if (backType == GatewayBackupEnum.A窗帘方向)
            {