From 835ce1375ba8de618ff99de05ade92ae7fe57d14 Mon Sep 17 00:00:00 2001 From: wangkaiyi Date: Tue, 27 Aug 2024 17:09:48 +0800 Subject: [PATCH] fix OTA --- .../Model/Constant/ProcessTopics.cs | 25 +++ .../net7.0/MasstransferCommon.AssemblyInfo.cs | 2 +- MasstransferExporter.sln.DotSettings.user | 0 .../OTA/Model/OTAUpdateFileManager.cs | 16 ++ .../OTA/Service/OTAService.cs | 161 ++++++++++-------- MasstransferExporter/Program.cs | 8 +- .../MasstransferExporter.AssemblyInfo.cs | 2 +- 7 files changed, 136 insertions(+), 78 deletions(-) create mode 100644 MasstransferExporter.sln.DotSettings.user create mode 100644 MasstransferExporter/OTA/Model/OTAUpdateFileManager.cs diff --git a/MasstransferCommon/Model/Constant/ProcessTopics.cs b/MasstransferCommon/Model/Constant/ProcessTopics.cs index 6f07f4e..e6708f6 100644 --- a/MasstransferCommon/Model/Constant/ProcessTopics.cs +++ b/MasstransferCommon/Model/Constant/ProcessTopics.cs @@ -11,4 +11,29 @@ public class ProcessTopics /// 证书更新事件反馈 /// public const string LicenseUpdateEventFeedback = "LicenseUpdateEventFeedback"; + + /// + /// OTA请求事件 + /// + public const string OTAQueryEvent = "OTAUpdataQueryEvent"; + + /// + /// OTA请求反馈事件 + /// + public const string OTAQueryEventFeedback = "OTAQueryEventFeedback"; + + /// + /// OTA更新事件 + /// + public const string OTAUpdateEvent = "OTAUpdateEvent"; + + /// + /// 下载更新包事件 + /// + public const string DownloadUpdatePackageEvent = "DownloadUpdatePackageEvent"; + + /// + /// 下载更新包事件反馈 + /// + public const string DownloadUpdatePackageEventFeedback = "DownloadUpdatePackageEventFeedback"; } \ No newline at end of file diff --git a/MasstransferCommon/obj/Debug/net7.0/MasstransferCommon.AssemblyInfo.cs b/MasstransferCommon/obj/Debug/net7.0/MasstransferCommon.AssemblyInfo.cs index 07111d0..ce6fc65 100644 --- a/MasstransferCommon/obj/Debug/net7.0/MasstransferCommon.AssemblyInfo.cs +++ b/MasstransferCommon/obj/Debug/net7.0/MasstransferCommon.AssemblyInfo.cs @@ -13,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("MasstransferCommon")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+64cf3396835594f835ea904d6fccf95de4bc3e01")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+877cf4ba7ae74758a89a91f7ed677f7e83789ca8")] [assembly: System.Reflection.AssemblyProductAttribute("MasstransferCommon")] [assembly: System.Reflection.AssemblyTitleAttribute("MasstransferCommon")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/MasstransferExporter.sln.DotSettings.user b/MasstransferExporter.sln.DotSettings.user new file mode 100644 index 0000000..e69de29 diff --git a/MasstransferExporter/OTA/Model/OTAUpdateFileManager.cs b/MasstransferExporter/OTA/Model/OTAUpdateFileManager.cs new file mode 100644 index 0000000..2874d45 --- /dev/null +++ b/MasstransferExporter/OTA/Model/OTAUpdateFileManager.cs @@ -0,0 +1,16 @@ +namespace MasstransferExporter.OTA.Model; + +public class OTAUpdateFileManager +{ + public string UpdatePackagePath; + + public string AppDir; + + public string CriticalBackupDir; + + public string[] CriticalFileExtension; + + public string CriticalSourceLogPath; + + public string PreviousBackupPath; +} \ No newline at end of file diff --git a/MasstransferExporter/OTA/Service/OTAService.cs b/MasstransferExporter/OTA/Service/OTAService.cs index 8a4ddad..893a5ed 100644 --- a/MasstransferExporter/OTA/Service/OTAService.cs +++ b/MasstransferExporter/OTA/Service/OTAService.cs @@ -1,81 +1,92 @@ -using MasstransferCommon.Model.Constant; +using System.Diagnostics; +using MasstransferCommon.Model.Constant; using MasstransferCommon.Utils; using MasstransferCommunicate.Mqtt.Client; +using MasstransferCommunicate.Process.Service; using MasstransferExporter.OTA.Client; using MasstransferExporter.OTA.Model; +using MasstransferInfrastructure.Database.Sqlite; namespace MasstransferExporter.OTA.Service; public class OTAService { - private readonly string _appDir; - private readonly string _criticalBackupDir; - private readonly string _criticalSourceLogPath; - private readonly string[] _criticalFileExtension; - private readonly string _previousBackupPath; - private readonly string _updatePackagePath; + private static readonly SqliteHelper Db = SqliteHelper.GetInstance(); + private static OTAUpdateFileManager _otaUpdateFileManager; - public OTAService(string appDir, string criticalBackupDir, string criticalSourceLogPath, string[] criticalFileExtension, string previousBackupPath, string updatePackagePath) - { - _appDir = appDir; - _criticalBackupDir = criticalBackupDir; - _criticalSourceLogPath = criticalSourceLogPath; - _criticalFileExtension = criticalFileExtension; - _previousBackupPath = previousBackupPath; - _updatePackagePath = updatePackagePath; - } - /// - /// 订阅OTA更新 + /// 启动OTA服务 /// - public async Task ListenOTAUpdateEvent() + public static async Task StartOTAService() { - await MessageQueueHelper.Subscribe(Topics.IssuedOTAPackage, HandleUpdateOTAEvent); + + _otaUpdateFileManager = GetOTAUpdateFileManager(); + + //订阅云端发包 + await MessageQueueHelper.Subscribe(Topics.IssuedOTAPackage, HandleIssuedOTAPackage); + + //订阅Masstransfer进程 请求OTA事件 + ProcessCommunicator.Subscribe(ProcessTopics.OTAQueryEvent, HandleOTAQueryEvent); + + //订阅Masstransfer进程 启动更新事件 + ProcessCommunicator.Subscribe(ProcessTopics.OTAUpdateEvent, HandleOTAUpdateEvent); + + //订阅订阅Masstransfer进程 下载安装包事件 + ProcessCommunicator.Subscribe(ProcessTopics.DownloadUpdatePackageEvent, HandleDownloadUpdatePackageEvent); } /// - /// 查询OTA更新 + /// 从数据库读取文件操作地址、关键文件后缀 /// - /// - public static async Task QueryOTAUpdate() + /// + private static OTAUpdateFileManager? GetOTAUpdateFileManager() { - await MessageQueueHelper.Publish(Topics.QueryOTA, null); + return Db.Query("").FirstOrDefault(); + } + + public static async Task HandleIssuedOTAPackage(string topic, OTAUpdateData otaUpdateData) + { + await ProcessCommunicator.Send(ProcessTopics.OTAQueryEventFeedback, otaUpdateData); } - /// - /// OTA更新反馈 - /// - /// - public async Task OTALicenseUpdateEventFeedback(OTAResultData resutl) - { - await MessageQueueHelper.Publish(Topics.OTAUpgradeFeedback, resutl); - } /// - /// 处理OTA更新 + /// Masstransfer 请求OTA事件响应,向云端发送请求信息 /// /// - /// - private async Task HandleUpdateOTAEvent(string topic, OTAUpdateData otaUpdateData) + public static async Task HandleOTAQueryEvent(string topic) { - var type = otaUpdateData.Type; - var url = otaUpdateData.Url; - var version = otaUpdateData.Version; - var module = otaUpdateData.Module; - var size = otaUpdateData.Size; - var checksum = otaUpdateData.Checksum; - var name = otaUpdateData.Name; - var description = otaUpdateData.Description; + await MessageQueueHelper.Publish(Topics.QueryOTA, new object()); + } + public static async Task HandleDownloadUpdatePackageEvent(string topic, (string Url, string Checksum) data) + { + var updatePackagePath = _otaUpdateFileManager.UpdatePackagePath; + string url = data.Url; + string checksum = data.Checksum; + bool result = false; + try + { + await ApiClient.DownloadFileAsync(url, updatePackagePath); + if(!OTAClient.CheckMD5(checksum, updatePackagePath)) throw new Exception("MD5 check failed."); + result = true; + } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } + + await ProcessCommunicator.Send(ProcessTopics.DownloadUpdatePackageEventFeedback, result); + + } + //Masstransfer 通知-启动更新 + public static async Task HandleOTAUpdateEvent(string topic, OTAUpdateData otaUpdateData) + { + var version = ""; + OTAResultData otaResultData = new OTAResultData(); - //下载更新包 - await ApiClient.DownloadFileAsync(url, _updatePackagePath); - - //校验压缩包 - if(!OTAClient.CheckMD5(checksum, _updatePackagePath)) return; - - //kill masstransfer //启动更新 otaResultData.Result = Install() ? (byte)1 : (byte)0; @@ -86,48 +97,54 @@ public class OTAService otaResultData.OtaSoftwareVersion = version; otaResultData.CurrentSoftwareVersion = version; //**需要修改为当前版本 - await OTALicenseUpdateEventFeedback(otaResultData); + await MessageQueueHelper.Publish(Topics.OTAUpgradeFeedback, otaResultData); } /// /// 安装压缩包 /// - public bool Install() + public static bool Install() { + var appDir = _otaUpdateFileManager.AppDir; + var criticalBackupDir = _otaUpdateFileManager.CriticalBackupDir; + var criticalFileExtension = _otaUpdateFileManager.CriticalFileExtension; + var criticalSourceLogPath = _otaUpdateFileManager.CriticalSourceLogPath; + var previousBackupPath = _otaUpdateFileManager.PreviousBackupPath; + var updatePackagePath = _otaUpdateFileManager.UpdatePackagePath; try { //初始化 - initDir(); + initDir(criticalBackupDir); //备份重要文件 - OTAClient.BackupEssentialFiles(_appDir, _criticalBackupDir, _criticalFileExtension, _criticalSourceLogPath); + OTAClient.BackupEssentialFiles(appDir, criticalBackupDir, criticalFileExtension, criticalSourceLogPath); //备份旧版本文件 - OTAClient.CompressDirectory(_appDir, _previousBackupPath); + OTAClient.CompressDirectory(appDir, previousBackupPath); try { //删除旧版本原文件 - OTAClient.DeleteDirectory(_appDir); + OTAClient.DeleteDirectory(appDir); //解压更新包 - OTAClient.ExtractDirectory(_updatePackagePath, _appDir); + OTAClient.ExtractDirectory(updatePackagePath, appDir); //恢复重要文件 - OTAClient.RecoverCriticalFiles(_criticalSourceLogPath); + OTAClient.RecoverCriticalFiles(criticalSourceLogPath); //删除旧版本备份文件 - OTAClient.DeleteFile(_previousBackupPath); + OTAClient.DeleteFile(previousBackupPath); //删除更新包 - OTAClient.DeleteFile(_updatePackagePath); + OTAClient.DeleteFile(updatePackagePath); return true; } catch (Exception ex) { - RollBack(); + RollBack(appDir, criticalBackupDir, updatePackagePath, previousBackupPath); Console.WriteLine($"更新异常,已回滚: {ex.Message}"); return false; } @@ -137,9 +154,9 @@ public class OTAService Console.WriteLine($"更新异常e: {ex.Message}"); //如果重要文件备份已经产生,则删除 - if(Directory.Exists(_criticalBackupDir)) + if(Directory.Exists(criticalBackupDir)) { - OTAClient.DeleteDirectory(_criticalBackupDir); + OTAClient.DeleteDirectory(criticalBackupDir); } return false; @@ -149,24 +166,24 @@ public class OTAService /// /// 初始化重要文件备份目录 /// - private void initDir() + private static void initDir(string criticalBackupDir) { - if (Directory.Exists(_criticalBackupDir)) + if (Directory.Exists(criticalBackupDir)) { - Directory.Delete(_criticalBackupDir, true); + Directory.Delete(criticalBackupDir, true); } - Directory.CreateDirectory(_criticalBackupDir); + Directory.CreateDirectory(criticalBackupDir); } /// /// 回滚 /// - private void RollBack() + private static void RollBack(string appDir, string criticalBackupDir, string updatePackagePath, string previousBackupPath) { //删除安装过程中创建的文件、目录 - var directoryList = new string[] { _appDir, _criticalBackupDir}; - var filePath = new String[] { _updatePackagePath }; + var directoryList = new string[] { appDir, criticalBackupDir}; + var filePath = new String[] { updatePackagePath }; foreach (var directory in directoryList) { @@ -185,10 +202,10 @@ public class OTAService } //恢复旧版本文件 - OTAClient.ExtractDirectory(_previousBackupPath, _appDir); + OTAClient.ExtractDirectory(previousBackupPath, appDir); //删除旧版本备份 - OTAClient.DeleteFile(_previousBackupPath); + OTAClient.DeleteFile(previousBackupPath); } } \ No newline at end of file diff --git a/MasstransferExporter/Program.cs b/MasstransferExporter/Program.cs index bbd6049..47bdb29 100644 --- a/MasstransferExporter/Program.cs +++ b/MasstransferExporter/Program.cs @@ -1,5 +1,6 @@ using MasstransferCommunicate.Mqtt.Client; using MasstransferExporter.DataExporter; +using MasstransferExporter.OTA.Service; using MasstransferInfrastructure.Mqtt.Model; class Program @@ -13,11 +14,10 @@ class Program EnableTls = false }; - // await MessageQueueHelper.InitConnect(options); - - await FormulaService.FormulaDataExporter(); - + await MessageQueueHelper.InitConnect(options); + CoordinateService.CoordinateExporter(); + await OTAService.StartOTAService(); Console.WriteLine("按任意键退出"); Console.ReadKey(); } diff --git a/MasstransferExporter/obj/Debug/net7.0/MasstransferExporter.AssemblyInfo.cs b/MasstransferExporter/obj/Debug/net7.0/MasstransferExporter.AssemblyInfo.cs index 46876c0..bab0211 100644 --- a/MasstransferExporter/obj/Debug/net7.0/MasstransferExporter.AssemblyInfo.cs +++ b/MasstransferExporter/obj/Debug/net7.0/MasstransferExporter.AssemblyInfo.cs @@ -13,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("MasstransferExporter")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+64cf3396835594f835ea904d6fccf95de4bc3e01")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+877cf4ba7ae74758a89a91f7ed677f7e83789ca8")] [assembly: System.Reflection.AssemblyProductAttribute("MasstransferExporter")] [assembly: System.Reflection.AssemblyTitleAttribute("MasstransferExporter")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]