xm
2020-04-16 6fa9d69da922c8049f5acfcbb9ce9fd26811024c
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
@@ -184,7 +204,13 @@
        /// <param name="zbGateway">网关</param>
        /// <param name="mode">是否显示错误</param>
        public async Task<bool> AddNewGateway(ZbGateway zbGateway, ShowErrorMode mode)
        {
        {
            //设置网关的经纬度
            bool falge = this.SetGatewaySite(zbGateway, Common.Config.Instance.Home.Longitude, Common.Config.Instance.Home.Latitude, ShowErrorMode.NO);
            if (falge == false)
            {
                return falge;
            }
            //执行添加网关到内存
            var result = await this.DoAddGatewayToMemory(zbGateway, mode);
            //前的网关绑定在了当前账号下的不同住宅里面
@@ -202,7 +228,7 @@
                }
                return false;
            }
            if (result != 1)
            if (result == -1)
            {
                return false;
            }
@@ -405,6 +431,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 +439,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, ShowErrorMode.YES);
            if (falge == false)
            {
                return -1;
            }
@@ -451,7 +484,7 @@
                    btnMsg.TextID = R.MyInternationalizationString.uGatewayDataIsChangingPleaseWhait;
                });
            }
            await System.Threading.Tasks.Task.Delay(8000);
            await Task.Delay(8000);
            //获取网关的信息
            var result = await zbGateway.GetZbGwInfoAsync();
@@ -463,15 +496,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 +664,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 +716,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,18 +765,21 @@
            {
                //删除设备文件
                Common.LocalDevice.Current.DeleteMemmoryDevice(device, true);
                //删除Ota设备
                Common.LocalDevice.Current.DeleteMemmoryOtaDevice(device.DeviceAddr);
            }
            //如果是主网关
            if (this.IsMainGateway(zbGatewayID) == 1)
            {
                foreach (var room in Shared.Common.Room.Lists)
                var listAllRoom = UserCenter.HdlRoomLogic.Current.GetAllListRooms();
                foreach (var room in listAllRoom)
                {
                    //删除场景文件
                    foreach (var scene in room.SceneUIFilePathList)
                    foreach (var sceneId in room.ListSceneId)
                    {
                        if (Global.IsExistsByHomeId(scene) == true)
                        if (Global.IsExistsByHomeId($"Scene_{sceneId}.json") == true)
                        {
                            Global.DeleteFilebyHomeId(scene);
                            Global.DeleteFilebyHomeId($"Scene_{sceneId}.json");
                        }
                    }
                }
@@ -1372,10 +1400,10 @@
            if (mode == GetNameMode.SpecialGateway && localWay.getGwInfo != null)
            {
                string keyName = Common.LocalDevice.deviceModelIdName + this.GetGwInfoAttribute(localWay, "LinuxImageType").ToString();
                if (Common.LocalDevice.Current.dicDeviceDefultNameID.ContainsKey(keyName) == true)
                if (LocalDevice.Current.dicDeviceAllNameID.ContainsKey(keyName) == true)
                {
                    //没有名称时,则使用R文件里面设置的东西
                    return Language.StringByID(Common.LocalDevice.Current.dicDeviceDefultNameID[keyName]);
                    //没有名称时,则使用R文件里面设置的默认设备名称
                    return Language.StringByID(LocalDevice.Current.dicDeviceAllNameID[keyName] + 20000);
                }
            }
@@ -1414,10 +1442,10 @@
                if (zbGateway.getGwInfo != null && zbGateway.getGwInfo.LinuxImageType != 0)
                {
                    string keyName = Common.LocalDevice.deviceModelIdName + zbGateway.getGwInfo.LinuxImageType;
                    if (Common.LocalDevice.Current.dicDeviceDefultNameID.ContainsKey(keyName) == true)
                    if (Common.LocalDevice.Current.dicDeviceAllNameID.ContainsKey(keyName) == true)
                    {
                        //使用R文件里面设置的东西
                        button.TextID = LocalDevice.Current.dicDeviceDefultNameID[keyName];
                        button.TextID = LocalDevice.Current.dicDeviceAllNameID[keyName];
                    }
                }
                else
@@ -1432,10 +1460,10 @@
                            HdlThreadLogic.Current.RunMain(() =>
                            {
                                string keyName = Common.LocalDevice.deviceModelIdName + zbGateway.getGwInfo.LinuxImageType;
                                if (Common.LocalDevice.Current.dicDeviceDefultNameID.ContainsKey(keyName) == true)
                                if (Common.LocalDevice.Current.dicDeviceAllNameID.ContainsKey(keyName) == true)
                                {
                                    //使用R文件里面设置的东西
                                    button.TextID = LocalDevice.Current.dicDeviceDefultNameID[keyName];
                                    button.TextID = LocalDevice.Current.dicDeviceAllNameID[keyName];
                                }
                            });
                        }
@@ -1445,10 +1473,10 @@
            else
            {
                string keyName = Common.LocalDevice.deviceModelIdName + this.dicGateway[gwId].getGwInfo.LinuxImageType;
                if (Common.LocalDevice.Current.dicDeviceDefultNameID.ContainsKey(keyName) == true)
                if (Common.LocalDevice.Current.dicDeviceAllNameID.ContainsKey(keyName) == true)
                {
                    //使用R文件里面设置的东西
                    button.TextID = Common.LocalDevice.Current.dicDeviceDefultNameID[keyName];
                    button.TextID = Common.LocalDevice.Current.dicDeviceAllNameID[keyName];
                }
            }
        }
@@ -1541,6 +1569,54 @@
        #endregion
        #region ■ 网关房间相关_______________________
        /// <summary>
        /// 获取网关所在的房间
        /// </summary>
        /// <param name="zbGateway">网关对象</param>
        /// <returns></returns>
        public Room GetRoomByGateway(ZbGateway zbGateway)
        {
            var gatewayId = this.GetGatewayId(zbGateway);
            return this.GetRoomByGateway(gatewayId);
        }
        /// <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="zbGateway">网关对象</param>
        /// <param name="roomId">房间ID</param>
        public void ChangedGatewayRoom(ZbGateway zbGateway, string roomId)
        {
            var gatewayId = this.GetGatewayId(zbGateway);
            var localGateway = this.GetLocalGateway(gatewayId);
            if (localGateway != null)
            {
                localGateway.RoomId = roomId;
                localGateway.ReSave();
                //添加备份
                HdlAutoBackupLogic.AddOrEditorFile(localGateway.FilePath);
            }
        }
        #endregion
        #region ■ 清空真实网关列表___________________
        /// <summary>
@@ -1609,8 +1685,11 @@
                this.GetGwInfoAttribute(localWay, "ZbImageType").ToString(),
                Convert.ToInt32(this.GetGwInfoAttribute(localWay, "ZbFWVersion")));
            var list = new List<FirmwareVersionInfo>();
            list.Add(gatewayFirmware);
            list.Add(coordinatorFirmware);
            //这个网关需要有虚拟驱动这个东西才行
            FirmwareVersionInfo virtualFirmware = null;
            string linImage = this.GetGwInfoAttribute(localWay, "LinuxImageType").ToString();
            if (linImage != "6")
            {
@@ -1618,28 +1697,32 @@
                var listvVode = await HdlGatewayLogic.Current.GetListVDDriveCode(localWay);
                if (listvVode != null && listvVode.Count > 0)
                {
                    //添加虚拟驱动的升级固件(成不成功都无所谓)
                    flage = await HdlFirmwareUpdateLogic.AddFirmwareVersionInfo(FirmwareLevelType.VirtualDevice,
                        listvVode[0].DriveHwVersion.ToString(),
                        listvVode[0].DriveImageType.ToString());
                    //虚拟驱动
                    virtualFirmware = HdlFirmwareUpdateLogic.GetFirmwareMostVersionInfo(FirmwareLevelType.VirtualDevice,
                        listvVode[0].DriveHwVersion.ToString(),
                        listvVode[0].DriveImageType.ToString(),
                        listvVode[0].DriveFwVersion);
                    if (virtualFirmware != null)
                    foreach (var data in listvVode)
                    {
                        virtualFirmware.VirtualCode = listvVode[0].DriveCode;
                        //添加虚拟驱动的升级固件(成不成功都无所谓)
                        flage = await HdlFirmwareUpdateLogic.AddFirmwareVersionInfo(FirmwareLevelType.VirtualDevice,
                            data.DriveHwVersion.ToString(),
                            data.DriveImageType.ToString());
                        //虚拟驱动
                        var virtualFirmware = HdlFirmwareUpdateLogic.GetFirmwareMostVersionInfo(FirmwareLevelType.VirtualDevice,
                            data.DriveHwVersion.ToString(),
                            data.DriveImageType.ToString(),
                            data.DriveFwVersion);
                        if (virtualFirmware != null)
                        {
                            virtualFirmware.VirtualCode = data.DriveCode;
                            list.Add(virtualFirmware);
                        }
                    }
                }
            }
            var list = new List<FirmwareVersionInfo>();
            list.Add(gatewayFirmware);
            list.Add(coordinatorFirmware);
            list.Add(virtualFirmware);
            if (list.Count == 2)
            {
                //虚拟驱动如果没有新版本的话,固定添加一个空的
                list.Add(null);
            }
            return list;
        }
@@ -1830,7 +1913,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 +2210,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();
@@ -2170,15 +2261,19 @@
        /// <param name="gateway">网关对象</param>
        /// <param name="Longitude">经度</param>
        /// <param name="Latitude">维度</param>
        /// <param name="mode">显示错误</param>
        /// <returns></returns>
        public bool SetGatewaySite(ZbGateway gateway, double Longitude, double Latitude)
        public bool SetGatewaySite(ZbGateway gateway, double Longitude, double Latitude, ShowErrorMode mode)
        {
            ZbGateway realWay = null;
            if (this.GetRealGateway(ref realWay, gateway) == false)
            {
                //错误:网关对象丢失
                string msg = Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg);
                this.ShowTipMsg(msg);
                if (mode == ShowErrorMode.YES)
                {
                    //错误:网关对象丢失
                    string msg = Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg);
                    this.ShowTipMsg(msg);
                }
                return false;
            }
@@ -2203,7 +2298,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)
@@ -2215,13 +2318,16 @@
            realWay.Actions -= action;
            if (result != 0)
            {
                //设置网关经纬度失败
                string msg = Language.StringByID(R.MyInternationalizationString.uSetGatewaySiteFail);
                if (result == -1)
                if (mode == ShowErrorMode.YES)
                {
                    msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                    //设置网关经纬度失败
                    string msg = Language.StringByID(R.MyInternationalizationString.uSetGatewaySiteFail);
                    if (result == -1)
                    {
                        msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                    }
                    this.ShowTipMsg(msg);
                }
                this.ShowTipMsg(msg);
                return false;
            }
@@ -2243,7 +2349,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 +2451,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 +2618,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 +2640,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窗帘方向)
            {