using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using ZigBee.Device; namespace Shared.Phone.UserCenter { /// /// 固件升级控件 /// public class GatewayFirmwareUpdateControl : RowLayout { #region ■ 变量声明___________________________ /// /// 虚拟设备的固件信息 /// public FirmwareVersionInfo virtualFirmware = null; /// /// 协调器新版本的固件信息 /// public FirmwareVersionInfo coordinatorFirmware = null; /// /// 网关新版本的固件信息 /// public FirmwareVersionInfo gatewayFirmware = null; /// /// 升级按钮(由外部进行赋值) /// public BottomClickButton btnUpdateButton = null; /// /// 当前执行状态 /// public UpdateStatuMode UpdateStatu = UpdateStatuMode.None; /// /// 升级是否完成 /// public bool IsFinishUpdate = true; /// /// 前一次的最终状态 /// private UpdateStatuMode oldUpdateStatu = UpdateStatuMode.None; /// /// 进度控件 /// private FrameLayout frameBar = null; /// /// 百分比进度 /// private NormalViewControl btnProgress = null; /// /// 要升级的网关 /// private ZbGateway upDatezbGateway = null; /// /// 错误信息 /// private string errorMsg = string.Empty; #endregion #region ■ 初始化_____________________________ /// /// 网关升级控件 /// /// 列表控件 /// 网关 /// 虚拟设备的固件信息 /// 协调器新版本的固件信息 /// 网关新版本的固件信息 public GatewayFirmwareUpdateControl(VerticalScrolViewLayout listView, ZbGateway zbGateway, FirmwareVersionInfo i_virtualFirmware, FirmwareVersionInfo i_coordinatorFirmware, FirmwareVersionInfo i_gatewayFirmware) { this.upDatezbGateway = zbGateway; this.virtualFirmware = i_virtualFirmware; this.coordinatorFirmware = i_coordinatorFirmware; this.gatewayFirmware = i_gatewayFirmware; this.Height = ControlCommonResourse.ListViewRowHeight; if (listView != null) { listView.AddChidren(this); //初始化控件 this.InitChidrenControl(); } } /// /// 初始化控件 /// public void InitChidrenControl() { this.RemoveAll(); //进度控件(背景色为绿色,会动的那个) this.frameBar = new FrameLayout(); this.frameBar.Width = 0; this.frameBar.BackgroundColor = UserCenterColor.Current.Green; this.AddChidren(frameBar); //桌布 var frameLayout = new RowLayoutControl(); frameLayout.BackgroundColor = UserCenterColor.Current.Transparent; this.AddChidren(frameLayout); //图标 var btnIcon = frameLayout.frameTable.AddLeftIcon(); HdlGatewayLogic.Current.SetGatewayIcon(btnIcon, this.upDatezbGateway); //指定的设备 var btnDevice = frameLayout.frameTable.AddLeftCaption("", 830); //btnDevice.Y = UserCenterLogic.GetControlChidrenYaxis(ControlCommonResourse.ListViewRowHeight, ControlCommonResourse.ListViewRowHalfHeight, UViewAlignment.Top); btnDevice.X = Application.GetRealWidth(200); btnDevice.BackgroundColor = UserCenterColor.Current.Transparent; btnDevice.Text = HdlGatewayLogic.Current.GetGatewayName(upDatezbGateway); //固件信息 this.btnProgress = frameLayout.frameTable.AddLeftCaption("", 830); //btnDevice.Y = UserCenterLogic.GetControlChidrenYaxis(ControlCommonResourse.ListViewRowHeight, ControlCommonResourse.ListViewRowHalfHeight, UViewAlignment.Bottom); btnDevice.X = Application.GetRealWidth(200); btnProgress.BackgroundColor = UserCenterColor.Current.Transparent; if (this.gatewayFirmware != null) { //版本 btnProgress.Text = Common.LocalDevice.Current.AppendVersion(this.gatewayFirmware.FirmwareVersion); } else if (this.coordinatorFirmware != null) { //版本 btnProgress.Text = Common.LocalDevice.Current.AppendVersion(this.coordinatorFirmware.FirmwareVersion); } else if (this.virtualFirmware != null) { //版本 btnProgress.Text = Common.LocalDevice.Current.AppendVersion(this.virtualFirmware.FirmwareVersion); } } #endregion #region ■ 开始更新___________________________ /// /// 进入执行更新操作准备阶段 /// public void StartUpdateReady() { if (btnUpdateButton.CanClick == false) { return; } if (this.UpdateStatu == UpdateStatuMode.Wait) { //如果是等待模式,再次点击时,移除列表 string gwId = HdlGatewayLogic.Current.GetGatewayId(this.upDatezbGateway); if (FirmwareUpdateResourse.dicGatewayUpdateList.ContainsKey(gwId) == true) { FirmwareUpdateResourse.dicGatewayUpdateList.Remove(gwId); } //升级 btnUpdateButton.TextID = R.MyInternationalizationString.uLevelUp; btnUpdateButton.SetNotClickStatu(); this.UpdateStatu = UpdateStatuMode.None; return; } //如果它有状态,则表示之前它被什么错误中断了 if (this.UpdateStatu != UpdateStatuMode.None) { //保存起来,后面有用处 this.oldUpdateStatu = this.UpdateStatu; } //进入等待模式 this.UpdateStatu = UpdateStatuMode.Wait; this.btnUpdateButton.TextID = R.MyInternationalizationString.uWaitting; this.btnUpdateButton.SetClickStatu(); string gwId2 = HdlGatewayLogic.Current.GetGatewayId(this.upDatezbGateway); FirmwareUpdateResourse.dicGatewayUpdateList[gwId2] = this; //执行下一个可更新的固件的更新操作 HdlFirmwareUpdateLogic.DoUpdateNextFirmware(); } /// /// 开始执行更新操作(FirmwareUpdateLogic调用) /// public void DoStartUpdate() { //清空错误信息 this.errorMsg = string.Empty; //状态变更 this.IsFinishUpdate = false; this.UpdateStatu = UpdateStatuMode.Action; //开始执行操作的时候,按键不能再点击 this.btnUpdateButton.CanClick = false; //设置拥有网关正在升级的标识 HdlGatewayLogic.Current.SetHadGatewayUpdateFlage(true); //根据状态执行操作 this.DoAdjustByStatuMode(); } /// /// 根据状态执行操作 /// private void DoAdjustByStatuMode() { //首发时,从开始执行 if (this.oldUpdateStatu == UpdateStatuMode.None) { //下载虚拟驱动文件 this.DownLoadVirtualFile(); } //虚拟驱动下载失败 else if (this.oldUpdateStatu == UpdateStatuMode.VirtualDownLoadFail) { //下载虚拟驱动文件 this.DownLoadVirtualFile(); } //虚拟驱动更新失败 else if (this.oldUpdateStatu == UpdateStatuMode.VirtualUpdateFail) { //执行虚拟驱动更新 this.DoUpdateVirtual(); } //协调器下载失败 else if (this.oldUpdateStatu == UpdateStatuMode.CoordinatorDownLoadFail) { //下载虚拟驱动文件(从头再来) this.DownLoadVirtualFile(); } //协调器升级失败 else if (this.oldUpdateStatu == UpdateStatuMode.CoordinatorUpdateFail) { //执行虚拟驱动更新(从头再来) this.DoUpdateVirtual(); } //网关下载失败 else if (this.oldUpdateStatu == UpdateStatuMode.GatewayDownLoadFail) { //下载虚拟驱动文件(从头再来) this.DownLoadVirtualFile(); } //网关升级失败 else if (this.oldUpdateStatu == UpdateStatuMode.GatewayUpdateFail) { //执行虚拟驱动更新(从头再来) this.DoUpdateVirtual(); } else { //重新再来 this.DownLoadVirtualFile(); } } #endregion #region ■ 虚拟驱动下载_______________________ /// /// 下载虚拟驱动文件 /// private async void DownLoadVirtualFile() { //开启网关下载超时线程 this.StartDownLoadTimeOutThread(); //如果虚拟驱动不需要升级,则直接跳转下一步 if (this.virtualFirmware == null) { //下载协调器文件 this.DownLoadCoordinatorFile(); return; } this.UpdateStatu = UpdateStatuMode.VirtualDownLoad; Application.RunOnMainThread(() => { this.btnUpdateButton.TextID = R.MyInternationalizationString.uDownLoading; }); //虚拟驱动固件下载中 string msg = Language.StringByID(R.MyInternationalizationString.uVirtualFirmwareDownLoading); this.SetProgressValue(0, 100, msg); //下载虚拟驱动文件的进度 this.upDatezbGateway.ReportAction += this.DownLoadVirtualFileProgress; //下载虚拟驱动文件 var result = await this.upDatezbGateway.DownloadFileAsync(this.virtualFirmware.DistributedMark, this.virtualFirmware.Name); if (string.IsNullOrEmpty(result.errorMessageBase) == false) { System.Console.WriteLine("✩✩下载虚拟驱动文件✩" + result.errorResponData?.Error + " " + result.errorMessageBase); if (result.errorResponData != null && result.errorResponData.Error == 2) { //因为tcp底层在网络不好的时候,有可能会重发多次,所以这里忽略掉【协调器正在升级或备份/恢复数据】的错误 return; } //虚拟驱动固件资源下载失败 this.ShowErrorMsg(Language.StringByID(R.MyInternationalizationString.uVirtualFirmwareDownLoadFail)); //显示重新下载模式 this.ShowReDownLoadMode(); this.upDatezbGateway.ReportAction -= this.DownLoadVirtualFileProgress; this.UpdateStatu = UpdateStatuMode.VirtualDownLoadFail; return; } } /// /// 下载虚拟驱动文件的进度 /// /// /// private void DownLoadVirtualFileProgress(string CommadDiv, object objValue) { if (CommadDiv != "DownloadFileProgress" || objValue == null) { return; } var tempZb = (ZbGateway)objValue; System.Console.WriteLine("★★★下载驱动" + tempZb.CurrentGateWayId + " " + tempZb.downloadFileProgressResponData.Status + " " + tempZb.downloadFileProgressResponData.DownloadPercent); if (tempZb.CurrentGateWayId != HdlGatewayLogic.Current.GetGatewayId(this.upDatezbGateway)) { //不是自己的网关推送,则不处理 return; } //刷新超时时间 this.DownLoadTimeOutRefresh(); //设置进度 var responData = tempZb.downloadFileProgressResponData; //虚拟驱动固件下载中 string msg = Language.StringByID(R.MyInternationalizationString.uVirtualFirmwareDownLoading); this.SetProgressValue(responData.DownloadPercent, 100, msg); if (responData.Status == 2) { //虚拟驱动固件资源下载失败 this.ShowErrorMsg(Language.StringByID(R.MyInternationalizationString.uVirtualFirmwareDownLoadFail)); //显示重新下载模式 this.ShowReDownLoadMode(); this.upDatezbGateway.ReportAction -= this.DownLoadVirtualFileProgress; this.UpdateStatu = UpdateStatuMode.VirtualDownLoadFail; } else if (responData.Status == 0) { this.upDatezbGateway.ReportAction -= this.DownLoadVirtualFileProgress; //虚拟驱动下载完了之后,下载协调器文件 this.DownLoadCoordinatorFile(); } } #endregion #region ■ 虚拟驱动升级_______________________ /// /// 执行虚拟驱动升级程序 /// private async void DoUpdateVirtual() { //刷新超时时间 this.DownLoadTimeOutRefresh(); //开启网关升级虚拟进度线程 this.StartUpdateVirtualThread(); //如果虚拟驱动不需要升级,则直接跳转下一步 if (this.virtualFirmware == null) { //执行协调器升级程序 this.DoUpdateCoordinator(); return; } this.UpdateStatu = UpdateStatuMode.VirtualUpdateReady; Application.RunOnMainThread(() => { this.btnUpdateButton.TextID = R.MyInternationalizationString.uLevelUping; }); //升级虚拟驱动的进度 this.upDatezbGateway.ReportAction += this.UpdateVirtualProgress; //虚拟驱动升级 var result = await this.upDatezbGateway.VirtualDriveUpgradeAsync(this.virtualFirmware.Name, this.virtualFirmware.VirtualCode); if (string.IsNullOrEmpty(result.errorMessageBase) == false) { System.Console.WriteLine("✩✩虚拟驱动升级✩" + result.errorResponData?.Error + " " + result.errorMessageBase); if (result.errorResponData != null && result.errorResponData.Error == 2) { //因为tcp底层在网络不好的时候,有可能会重发多次,所以这里忽略掉【协调器正在升级或备份/恢复数据】的错误 return; } if ((int)this.UpdateStatu > (int)UpdateStatuMode.VirtualUpdateReady) { //虽然你说报错了,但是之后拥有正常操作的话,直接无视这个错误 //状态的数值即为操作顺序,状态值大于当前值,即表示可以往下执行 return; } //虚拟驱动升级失败 this.errorMsg = Language.StringByID(R.MyInternationalizationString.uVirtualUpdatingFail); //显示重新安装 this.ShowReSetupMsg(); this.upDatezbGateway.ReportAction -= this.UpdateVirtualProgress; this.UpdateStatu = UpdateStatuMode.VirtualUpdateFail; } } /// /// 升级虚拟驱动的进度 /// /// /// private void UpdateVirtualProgress(string CommadDiv, object objValue) { if (CommadDiv != "VirtualDriveUpgrade" || objValue == null) { return; } var tempZb = (ZbGateway)objValue; System.Console.WriteLine("★★★升级驱动" + tempZb.CurrentGateWayId + " " + tempZb.virtualDriveUpgradeResponData.Flag + " " + tempZb.virtualDriveUpgradeResponData.Percent); if (tempZb.CurrentGateWayId != HdlGatewayLogic.Current.GetGatewayId(this.upDatezbGateway)) { //不是自己的网关推送,则不处理 return; } //网关有回应,则超时时间刷新 this.UpdateTimeOutRefresh(); //设置进度 var responData = tempZb.virtualDriveUpgradeResponData; if (responData.Flag != 0 && responData.Flag != 2) { //虚拟驱动升级失败 this.errorMsg = Language.StringByID(R.MyInternationalizationString.uVirtualUpdatingFail); //显示重新安装 this.ShowReSetupMsg(); this.upDatezbGateway.ReportAction -= this.UpdateVirtualProgress; this.UpdateStatu = UpdateStatuMode.VirtualUpdateFail; } else if (responData.Flag == 0) { this.upDatezbGateway.ReportAction -= this.UpdateVirtualProgress; //虚拟驱动升级完了之后,升级协调器 this.DoUpdateCoordinator(); } else { //虚拟驱动升级中 this.UpdateStatu = UpdateStatuMode.VirtualUpdating; } } #endregion #region ■ 协调器下载_________________________ /// /// 下载协调器文件 /// private async void DownLoadCoordinatorFile() { //如果协调器不需要升级,则直接跳转下一步 if (this.coordinatorFirmware == null) { //下载网关文件 this.DownLoadGatewayFile(); return; } //刷新超时时间 this.DownLoadTimeOutRefresh(); this.UpdateStatu = UpdateStatuMode.CoordinatorDownLoad; Application.RunOnMainThread(() => { this.btnUpdateButton.TextID = R.MyInternationalizationString.uDownLoading; }); //等个1秒 await Task.Delay(1000); //协调器固件下载中 string msg = Language.StringByID(R.MyInternationalizationString.uCoordinatorFirmwareDownLoading); this.SetProgressValue(0, 100, msg); //下载协调器文件的进度 this.upDatezbGateway.ReportAction += this.DownLoadCoordinatorFileProgress; //下载协调器文件 var result = await this.upDatezbGateway.DownloadFileAsync(this.coordinatorFirmware.DistributedMark, this.coordinatorFirmware.Name); if (string.IsNullOrEmpty(result.errorMessageBase) == false) { System.Console.WriteLine("✩✩下载协调器文件✩" + result.errorResponData?.Error + " " + result.errorMessageBase); if (result.errorResponData != null && result.errorResponData.Error == 2) { //因为tcp底层在网络不好的时候,有可能会重发多次,所以这里忽略掉【协调器正在升级或备份/恢复数据】的错误 return; } //协调器固件资源下载失败 this.ShowErrorMsg(Language.StringByID(R.MyInternationalizationString.uCoordinatorFirmwareDownLoadFail)); //显示重新下载模式 this.ShowReDownLoadMode(); this.upDatezbGateway.ReportAction -= this.DownLoadCoordinatorFileProgress; this.UpdateStatu = UpdateStatuMode.CoordinatorDownLoadFail; return; } } /// /// 下载协调器文件的进度 /// /// /// private void DownLoadCoordinatorFileProgress(string CommadDiv, object objValue) { if (CommadDiv != "DownloadFileProgress" || objValue == null) { return; } var tempZb = (ZbGateway)objValue; System.Console.WriteLine("★★★下载协调器" + tempZb.CurrentGateWayId + " " + tempZb.downloadFileProgressResponData.Status + " " + tempZb.downloadFileProgressResponData.DownloadPercent); if (tempZb.CurrentGateWayId != HdlGatewayLogic.Current.GetGatewayId(this.upDatezbGateway)) { //不是自己的网关推送,则不处理 return; } //刷新超时时间 this.DownLoadTimeOutRefresh(); //设置进度 var responData = tempZb.downloadFileProgressResponData; //协调器固件下载中 string msg = Language.StringByID(R.MyInternationalizationString.uCoordinatorFirmwareDownLoading); this.SetProgressValue(responData.DownloadPercent, 100, msg); if (responData.Status == 2) { //协调器固件资源下载失败 this.ShowErrorMsg(Language.StringByID(R.MyInternationalizationString.uCoordinatorFirmwareDownLoadFail)); //显示重新下载模式 this.ShowReDownLoadMode(); this.upDatezbGateway.ReportAction -= this.DownLoadCoordinatorFileProgress; this.UpdateStatu = UpdateStatuMode.CoordinatorDownLoadFail; } else if (responData.Status == 0) { this.upDatezbGateway.ReportAction -= this.DownLoadCoordinatorFileProgress; //协调器下载完了之后,下载网关文件 this.DownLoadGatewayFile(); } } #endregion #region ■ 协调器升级_________________________ /// /// 执行协调器升级程序 /// private async void DoUpdateCoordinator() { //如果协调器不需要升级,则直接跳转下一步 if (this.coordinatorFirmware == null) { //执行网关升级程序 this.DoUpdateGateway(); return; } this.UpdateStatu = UpdateStatuMode.CoordinatorUpdateReady; Application.RunOnMainThread(() => { this.btnUpdateButton.TextID = R.MyInternationalizationString.uLevelUping; }); //升级协调器的进度 this.upDatezbGateway.ReportAction += this.UpdateCoordinatorProgress; //协调器升级 var result = await this.upDatezbGateway.UpgradeNVAsync(this.coordinatorFirmware.Name); if (string.IsNullOrEmpty(result.errorMessageBase) == false) { System.Console.WriteLine("✩✩协调器升级✩" + result.errorResponData?.Error + " " + result.errorMessageBase); if (result.errorResponData != null && result.errorResponData.Error == 2) { //因为tcp底层在网络不好的时候,有可能会重发多次,所以这里忽略掉【协调器正在升级或备份/恢复数据】的错误 return; } if ((int)this.UpdateStatu > (int)UpdateStatuMode.CoordinatorUpdateReady) { //虽然你说报错了,但是之后拥有正常操作的话,直接无视这个错误 //状态的数值即为操作顺序,状态值大于当前值,即表示可以往下执行 return; } //协调器升级失败 this.errorMsg = Language.StringByID(R.MyInternationalizationString.uCoordinatorUpdatingFail); //显示重新安装 this.ShowReSetupMsg(); this.upDatezbGateway.ReportAction -= this.UpdateCoordinatorProgress; this.UpdateStatu = UpdateStatuMode.CoordinatorUpdateFail; } } /// /// 升级协调器的进度 /// /// /// private void UpdateCoordinatorProgress(string CommadDiv, object objValue) { if (CommadDiv != "CordinatorUpgradePercent" || objValue == null) { return; } var tempZb = (ZbGateway)objValue; System.Console.WriteLine("★★★升级协调器" + tempZb.CurrentGateWayId + " " + tempZb.zbGwOperationUpgradeData.Flag + " " + tempZb.zbGwOperationUpgradeData.Percent); if (tempZb.CurrentGateWayId != HdlGatewayLogic.Current.GetGatewayId(this.upDatezbGateway)) { //不是自己的网关推送,则不处理 return; } //网关有回应,则超时时间刷新 this.UpdateTimeOutRefresh(); //设置进度 var responData = tempZb.zbGwOperationUpgradeData; if (responData.Flag != 0 && responData.Flag != 2) { //协调器升级失败 this.errorMsg = Language.StringByID(R.MyInternationalizationString.uCoordinatorUpdatingFail); //显示重新安装 this.ShowReSetupMsg(); this.upDatezbGateway.ReportAction -= this.UpdateCoordinatorProgress; this.UpdateStatu = UpdateStatuMode.CoordinatorUpdateFail; } else if (responData.Flag == 0) { this.upDatezbGateway.ReportAction -= this.UpdateCoordinatorProgress; //协调器升级完了之后,升级网关 this.DoUpdateGateway(); } else { //协调器升级中 this.UpdateStatu = UpdateStatuMode.CoordinatorUpdating; } } #endregion #region ■ 网关下载___________________________ /// /// 下载网关文件 /// private async void DownLoadGatewayFile() { //如果网关不用升级,则直接跳转下一步 if (this.gatewayFirmware == null) { //执行虚拟驱动升级程序 this.DoUpdateVirtual(); return; } this.UpdateStatu = UpdateStatuMode.GatewayDownLoad; Application.RunOnMainThread(() => { this.btnUpdateButton.TextID = R.MyInternationalizationString.uDownLoading; }); //刷新超时时间 this.DownLoadTimeOutRefresh(); //等个1秒 await Task.Delay(1000); //网关固件下载中 string msg = Language.StringByID(R.MyInternationalizationString.uGatewayFirmwareDownLoading); this.SetProgressValue(0, 100, msg); //下载网关文件的进度 this.upDatezbGateway.ReportAction += this.DownLoadGatewayFileProgress; //下载网关文件 var result = await this.upDatezbGateway.DownloadFileAsync(this.gatewayFirmware.DistributedMark, this.gatewayFirmware.Name); if (string.IsNullOrEmpty(result.errorMessageBase) == false) { System.Console.WriteLine("✩✩下载网关文件✩" + result.errorResponData?.Error + " " + result.errorMessageBase); if (result.errorResponData != null && result.errorResponData.Error == 2) { //因为tcp底层在网络不好的时候,有可能会重发多次,所以这里忽略掉【协调器正在升级或备份/恢复数据】的错误 return; } //网关固件资源下载失败 this.ShowErrorMsg(Language.StringByID(R.MyInternationalizationString.uGatewayFirmwareDownLoadFail)); //显示重新下载模式 this.ShowReDownLoadMode(); this.upDatezbGateway.ReportAction -= this.DownLoadGatewayFileProgress; this.UpdateStatu = UpdateStatuMode.GatewayDownLoadFail; } } /// /// 下载网关文件的进度 /// /// /// private void DownLoadGatewayFileProgress(string CommadDiv, object objValue) { if (CommadDiv != "DownloadFileProgress" || objValue == null) { return; } var tempZb = (ZbGateway)objValue; System.Console.WriteLine("★★★下载网关" + tempZb.CurrentGateWayId + " " + tempZb.downloadFileProgressResponData.Status + " " + tempZb.downloadFileProgressResponData.DownloadPercent); if (tempZb.CurrentGateWayId != HdlGatewayLogic.Current.GetGatewayId(this.upDatezbGateway)) { //不是自己的网关推送,则不处理 return; } //刷新超时时间 this.DownLoadTimeOutRefresh(); //设置进度 var responData = tempZb.downloadFileProgressResponData; string msg = Language.StringByID(R.MyInternationalizationString.uGatewayFirmwareDownLoading); this.SetProgressValue(responData.DownloadPercent, 100, msg); if (responData.Status == 2) { //网关固件资源下载失败 this.ShowErrorMsg(Language.StringByID(R.MyInternationalizationString.uGatewayFirmwareDownLoadFail)); //显示重新下载模式 this.ShowReDownLoadMode(); this.upDatezbGateway.ReportAction -= this.DownLoadGatewayFileProgress; this.UpdateStatu = UpdateStatuMode.GatewayDownLoadFail; } else if (responData.Status == 0) { this.upDatezbGateway.ReportAction -= this.DownLoadGatewayFileProgress; if (this.Parent == null) { return; } //执行虚拟驱动升级程序 this.DoUpdateVirtual(); } } #endregion #region ■ 网关升级___________________________ /// /// 执行网关升级程序 /// private async void DoUpdateGateway() { //如果网关不用升级,则直接跳转下一步 if (this.gatewayFirmware == null) { //显示升级完成的信息 this.ShowFinishMsg(); return; } this.UpdateStatu = UpdateStatuMode.GatewayUpdateReady; Application.RunOnMainThread(() => { this.btnUpdateButton.TextID = R.MyInternationalizationString.uLevelUping; }); //网关升级 var result = await this.upDatezbGateway.LinuxUpgradeAsync(this.gatewayFirmware.Name); if (string.IsNullOrEmpty(result.errorMessageBase) == false) { System.Console.WriteLine("✩✩网关升级✩" + result.errorResponData?.Error + " " + result.errorMessageBase); if (result.errorMessageBase.Contains("回复超时") == true) { //网关升级的时候,有可能它不再回什么信息 return; } if (result.errorResponData != null && result.errorResponData.Error == 2) { //因为tcp底层在网络不好的时候,有可能会重发多次,所以这里忽略掉【协调器正在升级或备份/恢复数据】的错误 return; } //网关升级失败 this.ShowErrorMsg(Language.StringByID(R.MyInternationalizationString.uGatewayUpdatingFail)); //显示重新安装 this.ShowReSetupMsg(); this.UpdateStatu = UpdateStatuMode.GatewayUpdateFail; return; } //网关升级中 this.UpdateStatu = UpdateStatuMode.GatewayUpdating; //开启获取网关版本的线程 this.StartGetGatewayVersionThread(); } #endregion #region ■ 升级完成提示_______________________ /// /// 显示升级完成的信息 /// private void ShowFinishMsg() { HdlThreadLogic.Current.RunThread(() => { //状态变更 this.IsFinishUpdate = true; //让别的线程先退出 System.Threading.Thread.Sleep(2000); //设置没有网关正在升级的标识 HdlGatewayLogic.Current.SetHadGatewayUpdateFlage(false); //网关升级成功! this.SetProgressValue(100, 100, Language.StringByID(R.MyInternationalizationString.uGatewayUpdateSuccess)); Application.RunOnMainThread(() => { if (UserCenterResourse.DicActionForm.ContainsKey("GatewayFirmwareInfoForm") == true) { //刷新设备信息界面 ((GatewayManage.GatewayFirmwareInfoForm)UserCenterResourse.DicActionForm["GatewayFirmwareInfoForm"]).ShowForm(upDatezbGateway); } this.UpdateStatu = UpdateStatuMode.UpdateSuccess; //执行下一个升级 HdlFirmwareUpdateLogic.DoUpdateNextFirmware(); //完成 this.btnUpdateButton.TextID = R.MyInternationalizationString.uFinish; this.btnUpdateButton.CanClick = true; this.btnUpdateButton.SetNotClickStatu(); }); }); } #endregion #region ■ 处理结果提示_______________________ /// /// 显示重新安装的信息 /// private void ShowReSetupMsg() { //状态变更 this.IsFinishUpdate = true; //设置没有网关正在升级的标识 HdlGatewayLogic.Current.SetHadGatewayUpdateFlage(false); Application.RunOnMainThread(() => { //重新升级 this.btnUpdateButton.TextID = R.MyInternationalizationString.uReLevelUp; this.btnUpdateButton.CanClick = true; this.btnUpdateButton.SetNotClickStatu(); //清空进度条 this.frameBar.Width = 0; //执行下一个升级 HdlFirmwareUpdateLogic.DoUpdateNextFirmware(); }); } /// /// 显示重新下载模式 /// private void ShowReDownLoadMode() { //状态变更 this.IsFinishUpdate = true; //设置没有网关正在升级的标识 HdlGatewayLogic.Current.SetHadGatewayUpdateFlage(false); Application.RunOnMainThread(() => { this.btnUpdateButton.TextID = R.MyInternationalizationString.uReDownLoad; this.btnUpdateButton.CanClick = true; this.btnUpdateButton.SetNotClickStatu(); //清空进度条 this.frameBar.Width = 0; //执行下一个升级 HdlFirmwareUpdateLogic.DoUpdateNextFirmware(); }); } #endregion #region ■ 设置进度___________________________ /// /// 设定进度值 /// /// 进度值 /// 最大值 /// 追加信息 private void SetProgressValue(decimal value, decimal maxValue, string appendText = "") { if (value > maxValue) { //有时候会瞎发什么鬼过来 return; } decimal result = value / maxValue; Application.RunOnMainThread(() => { //百分比数,取整数部分 int percent = (int)(result * 100); int width = (int)(result * this.Width); if (appendText != string.Empty) { this.btnProgress.Text = percent + "% " + appendText; } else { this.btnProgress.Text = percent + "%"; } this.frameBar.Width = width; }); } /// /// 设定进度值 /// /// 进度值 private void SetProgressValue(string value) { Application.RunOnMainThread(() => { this.btnProgress.Text = value; }); } /// /// 设置错误信息 /// /// private void ShowErrorMsg(string value) { Application.RunOnMainThread(() => { //设置错误信息 this.btnProgress.Text = value; //清空进度条 this.frameBar.Width = 0; }); } #endregion #region ■ 网关下载超时线程___________________ /// /// 超时时间设置 /// private int downLoadTimeOutCount = 30; /// /// 开启网关下载超时线程 /// private void StartDownLoadTimeOutThread() { this.downLoadTimeOutCount = 30; HdlThreadLogic.Current.RunThread(() => { while (true) { var value = this.UpdateStatu.ToString(); if (value.EndsWith("DownLoad") == false) { //执行完成下载操作 break; } System.Threading.Thread.Sleep(1000); this.downLoadTimeOutCount--; if (this.downLoadTimeOutCount < 0) { this.upDatezbGateway.ReportAction -= this.DownLoadVirtualFileProgress; this.upDatezbGateway.ReportAction -= this.DownLoadCoordinatorFileProgress; this.upDatezbGateway.ReportAction -= this.DownLoadGatewayFileProgress; //响应超时,升级失败 string msg = Language.StringByID(R.MyInternationalizationString.uResponseTimeoutsAndUpdateFail); //从头再来 this.UpdateStatu = UpdateStatuMode.GatewayDownLoadFail; //显示重新下载模式 this.ShowReDownLoadMode(); //设置错误信息 this.ShowErrorMsg(msg); break; } } }); } /// /// 超时时间刷新 /// private void DownLoadTimeOutRefresh() { this.downLoadTimeOutCount = 30; } #endregion #region ■ 网关升级虚拟进度线程_______________ /// /// 超时时间设置 /// private int updateTimeOutCount = 60; /// /// 开启网关升级虚拟进度线程 /// private void StartUpdateVirtualThread() { //获取升级的大致总共时间 int timeCount = this.GetUpdateTimeCount(); //网关升级中,剩余时间大约为: string msg = Language.StringByID(R.MyInternationalizationString.uGatewaiUpdatingAndRemainingTimeMsg); //当前用时 int nowTimeCount = 0; //超时时间设置 this.updateTimeOutCount = 60; HdlThreadLogic.Current.RunThread(() => { while (this.IsFinishUpdate == false) { System.Threading.Thread.Sleep(1000); nowTimeCount++; //超时 updateTimeOutCount--; if (updateTimeOutCount == 0) { //响应超时,升级失败 this.errorMsg = Language.StringByID(R.MyInternationalizationString.uResponseTimeoutsAndUpdateFail); //从头再来 this.UpdateStatu = UpdateStatuMode.GatewayDownLoadFail; //显示重新下载模式 this.ShowReDownLoadMode(); break; } //剩余时间 int remainingTime = timeCount - nowTimeCount; if (remainingTime == -1) { //时间用完了,直接显示成功 this.IsFinishUpdate = true; break; } //设置进度值 this.SetProgressValue(nowTimeCount, timeCount, msg + remainingTime + "s"); } //升级失败 if (this.errorMsg != string.Empty) { //设置错误信息 this.ShowErrorMsg(this.errorMsg); } }); } /// /// 获取升级的大致总共时间 /// /// private int GetUpdateTimeCount() { int timeCount = 0; if (this.virtualFirmware != null) { //虚拟驱动升级 timeCount += 20; } if (this.coordinatorFirmware != null) { //协调器升级 timeCount += 120; } if (this.gatewayFirmware != null) { //网关升级 timeCount += 50; //网关重启 timeCount += 180; } return timeCount; } /// /// 超时时间刷新 /// private void UpdateTimeOutRefresh() { this.updateTimeOutCount = 60; } #endregion #region ■ 开启获取网关版本的线程_____________ /// /// 开启获取网关版本的线程 /// /// private void StartGetGatewayVersionThread() { HdlThreadLogic.Current.RunThread(async () => { //当前网关的版本 var oldVersion = HdlGatewayLogic.Current.GetGwInfoAttribute(this.upDatezbGateway, "LinuxFWVersion"); int oldLinuxVersion = Convert.ToInt32(oldVersion); //先等个120秒吧,网关升级和网关完全重启需要很长时间 int count = 12; for (int i = 0; i < count; i++) { if (this.IsFinishUpdate == true) { //升级完了,因为有错误 break; } //这个时候,不应该提示超时 this.UpdateTimeOutRefresh(); await Task.Delay(10000); } while (this.IsFinishUpdate == false) { //获取版本 var result = await HdlGatewayLogic.Current.GetGatewayNewInfoAsync(this.upDatezbGateway, ShowErrorMode.NO); if (result != null) { break; } //这个时候,不应该提示超时 this.UpdateTimeOutRefresh(); await Task.Delay(5000); } //已经能够获取数据 this.ShowFinishMsg(); }); } #endregion } }