This commit is contained in:
wangkaiyi 2024-08-20 09:53:11 +08:00
parent a303714a63
commit 64cf339683
15 changed files with 162 additions and 228 deletions

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("MasstransferCommon")] [assembly: System.Reflection.AssemblyCompanyAttribute("MasstransferCommon")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+c140544af7e52a0669ceafcb39aa5c4e97c5ccae")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+a303714a635d3146132582a2408fc8cf76a69947")]
[assembly: System.Reflection.AssemblyProductAttribute("MasstransferCommon")] [assembly: System.Reflection.AssemblyProductAttribute("MasstransferCommon")]
[assembly: System.Reflection.AssemblyTitleAttribute("MasstransferCommon")] [assembly: System.Reflection.AssemblyTitleAttribute("MasstransferCommon")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
bea321349982bc0eb41c769bdac56f18be90a91e28cbb7240ece837e7958e892 04161de5d5b7e53625665da3c9aa9d1cc0c406c72bef7aeb16057254b572fd39

View File

@ -1 +1 @@
17234348283768389 17234348310820990

View File

@ -6,8 +6,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MasstransferInfrastructure"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MasstransferCommon", "MasstransferCommon\MasstransferCommon.csproj", "{66C6D73C-BADB-4E28-9C83-E701B019626D}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MasstransferCommon", "MasstransferCommon\MasstransferCommon.csproj", "{66C6D73C-BADB-4E28-9C83-E701B019626D}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTest", "UnitTest\UnitTest.csproj", "{3E23D7B1-7223-45A1-B0C3-7758372986E2}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -26,9 +24,5 @@ Global
{66C6D73C-BADB-4E28-9C83-E701B019626D}.Debug|Any CPU.Build.0 = Debug|Any CPU {66C6D73C-BADB-4E28-9C83-E701B019626D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{66C6D73C-BADB-4E28-9C83-E701B019626D}.Release|Any CPU.ActiveCfg = Release|Any CPU {66C6D73C-BADB-4E28-9C83-E701B019626D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{66C6D73C-BADB-4E28-9C83-E701B019626D}.Release|Any CPU.Build.0 = Release|Any CPU {66C6D73C-BADB-4E28-9C83-E701B019626D}.Release|Any CPU.Build.0 = Release|Any CPU
{3E23D7B1-7223-45A1-B0C3-7758372986E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3E23D7B1-7223-45A1-B0C3-7758372986E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E23D7B1-7223-45A1-B0C3-7758372986E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E23D7B1-7223-45A1-B0C3-7758372986E2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View File

@ -18,11 +18,10 @@ public class LicenseService
await MessageQueueHelper.Subscribe(Topics.UpdateLicenseEvent, HandleUpdateLicenseEvent); await MessageQueueHelper.Subscribe(Topics.UpdateLicenseEvent, HandleUpdateLicenseEvent);
} }
/// <summary> /// <summary>
/// 处理接收到的证书更新事件 /// 处理接收到的证书更新事件
/// </summary> /// </summary>
private static async Task HandleUpdateLicenseEvent(string topic, string license) public static async Task HandleUpdateLicenseEvent(string topic, string license)
{ {
ProcessCommunicator.Subscribe(ProcessTopics.LicenseUpdateEventFeedback, HandleUpdateLicenseEventFeedback); ProcessCommunicator.Subscribe(ProcessTopics.LicenseUpdateEventFeedback, HandleUpdateLicenseEventFeedback);
@ -34,7 +33,7 @@ public class LicenseService
/// </summary> /// </summary>
/// <param name="topic"></param> /// <param name="topic"></param>
/// <param name="result"></param> /// <param name="result"></param>
private static async Task HandleUpdateLicenseEventFeedback(string topic, string result) public static async Task HandleUpdateLicenseEventFeedback(string topic, string result)
{ {
await MessageQueueHelper.Publish(Topics.UpdateLicenseEventFeedback, result); await MessageQueueHelper.Publish(Topics.UpdateLicenseEventFeedback, result);
} }

View File

@ -1,29 +1,43 @@
using System.IO.Compression; using System.IO.Compression;
using MasstransferCommon.Model.Constant;
using MasstransferCommunicate.Mqtt;
using MasstransferCommon.Utils; using MasstransferCommon.Utils;
using MasstransferInfrastructure.OTA.Model; using MasstransferCommunicate.Mqtt.Client;
using MasstransferExporter.OTA.Model;
namespace MasstransferInfrastructure.OTA.Client; namespace MasstransferExporter.OTA.Client;
public class OTAClient public class OTAClient
{ {
/// <summary> /// <summary>
/// 检查更新 /// 订阅OTA更新
/// </summary> /// </summary>
/// <param name="updateUrl"></param> public static async Task ListenOTAUpdateEvent()
/// <returns></returns>
public async Task<UpdateInfo?> CheckForUpdate(string updateUrl)
{ {
return await ApiClient.GetAsync<UpdateInfo>(updateUrl); await MessageQueueHelper.Subscribe(Topics.IssuedOTAPackage, HandleUpdateOTAEvent);
} }
/// <summary> /// <summary>
/// 下载更新 /// 查询OTA更新
/// </summary> /// </summary>
/// <param name="downloadUrl"></param> /// <param name="result"></param>
/// <param name="filePath"></param> public static async Task QueryOTAUpdate(string result)
public async Task DownloadUpdateAsync(string downloadUrl, string filePath)
{ {
await ApiClient.DownloadFileAsync(downloadUrl, filePath); await MessageQueueHelper.Publish(Topics.QueryOTA, result);
}
/// <summary>
/// OTA更新反馈
/// </summary>
/// <param name="resutl"></param>
public static async Task OTALicenseUpdateEventFeedback(string resutl)
{
await MessageQueueHelper.Publish(Topics.OTAUpgradeFeedback, resutl);
}
private static async Task HandleUpdateOTAEvent(string topic, string license)
{
} }
/// <summary> /// <summary>

View File

@ -1,4 +1,4 @@
namespace MasstransferInfrastructure.OTA.Model; namespace MasstransferExporter.OTA.Model;
public class UpdateInfo public class UpdateInfo
{ {

View File

@ -0,0 +1,126 @@
using MasstransferExporter.OTA.Client;
namespace MasstransferExporter.OTA.Service;
public class OTAService
{
private readonly OTAClient _otaClient;
private readonly string _appDir;
private readonly string _criticalBackupDir;
private readonly string _criticalSourceLogPath;
private readonly string[] _criticalFileExtension;
private readonly string _previousBackupPath;
private readonly string _updatePackagePath;
public OTAService(string appDir, string criticalBackupDir, string criticalSourceLogPath, string[] criticalFileExtension, string previousBackupPath, string updatePackagePath)
{
_otaClient = new OTAClient();
_appDir = appDir;
_criticalBackupDir = criticalBackupDir;
_criticalSourceLogPath = criticalSourceLogPath;
_criticalFileExtension = criticalFileExtension;
_previousBackupPath = previousBackupPath;
_updatePackagePath = updatePackagePath;
}
/// <summary>
/// 更新压缩包
/// </summary>
public void Update()
{
try
{
//初始化
initDir();
//备份重要文件
_otaClient.BackupEssentialFiles(_appDir, _criticalBackupDir, _criticalFileExtension, _criticalSourceLogPath);
//备份旧版本文件
_otaClient.CompressDirectory(_appDir, _previousBackupPath);
try
{
//删除旧版本原文件
_otaClient.DeleteDirectory(_appDir);
//解压更新包
_otaClient.ExtractDirectory(_updatePackagePath, _appDir);
//恢复重要文件
_otaClient.RecoverCriticalFiles(_criticalSourceLogPath);
//删除旧版本备份文件
_otaClient.DeleteFile(_previousBackupPath);
//删除更新包
_otaClient.DeleteFile(_updatePackagePath);
}
catch (Exception ex)
{
RollBack();
Console.WriteLine($"更新异常,已回滚: {ex.Message}");
}
}
catch (Exception ex)
{
Console.WriteLine($"更新异常e: {ex.Message}");
//如果重要文件备份已经产生,则删除
if(Directory.Exists(_criticalBackupDir))
{
_otaClient.DeleteDirectory(_criticalBackupDir);
}
}
}
/// <summary>
/// 初始化重要文件备份目录
/// </summary>
private void initDir()
{
if (Directory.Exists(_criticalBackupDir))
{
Directory.Delete(_criticalBackupDir, true);
}
Directory.CreateDirectory(_criticalBackupDir);
}
/// <summary>
/// 回滚
/// </summary>
private void RollBack()
{
//删除安装过程中创建的文件、目录
var directoryList = new string[] { _appDir, _criticalBackupDir};
var filePath = new String[] { _updatePackagePath };
foreach (var directory in directoryList)
{
if (Directory.Exists(directory))
{
_otaClient.DeleteDirectory(directory);
}
}
foreach (var file in filePath)
{
if (File.Exists(file))
{
_otaClient.DeleteFile(file);
}
}
//恢复旧版本文件
_otaClient.ExtractDirectory(_previousBackupPath, _appDir);
//删除旧版本备份
_otaClient.DeleteFile(_previousBackupPath);
}
}

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("MasstransferExporter")] [assembly: System.Reflection.AssemblyCompanyAttribute("MasstransferExporter")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+c140544af7e52a0669ceafcb39aa5c4e97c5ccae")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+a303714a635d3146132582a2408fc8cf76a69947")]
[assembly: System.Reflection.AssemblyProductAttribute("MasstransferExporter")] [assembly: System.Reflection.AssemblyProductAttribute("MasstransferExporter")]
[assembly: System.Reflection.AssemblyTitleAttribute("MasstransferExporter")] [assembly: System.Reflection.AssemblyTitleAttribute("MasstransferExporter")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
b22264d67cf0b32e10f0e75ab1606fde9dde71a2b835269fbcfcdbfc81d14377 78895e0730c7e01dfba29760f86535377f37711dd2d831a492fe0a2d3f652707

View File

@ -1 +1 @@
17234348283873748 17234348310820990

View File

@ -68,20 +68,6 @@ public class ApiClient : IDisposable
return await response.Content.ReadFromJsonAsync<T>(); return await response.Content.ReadFromJsonAsync<T>();
} }
/// <summary>
/// 异步下载文件
/// </summary>
/// <param name="url"></param>
/// <param name="filePath"></param>
public static async Task DownloadFileAsync(string url, string filePath)
{
using var response = await Client.GetAsync(url);
response.EnsureSuccessStatusCode();
using var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
await response.Content.CopyToAsync(fs);
}
public void Dispose() public void Dispose()
{ {
Client.Dispose(); Client.Dispose();

View File

@ -1,126 +0,0 @@
using MasstransferInfrastructure.OTA.Client;
using MasstransferInfrastructure.OTA.Model;
namespace MasstransferInfrastructure.OTA.Service;
public class OTAService
{
private readonly OTAClient _otaClient;
private readonly string appDir;
private readonly string criticalBackupDir;
private readonly string criticalSourceLogPath;
private readonly string[] criticalFileExtension;
private readonly string previousBackupPath;
private readonly string updatePackagePath;
public OTAService(string AppDir, string CriticalBackupDir, string CriticalSourceLogPath, string[] CriticalFileExtension, string PreviousBackupPath, string UpdatePackagePath)
{
_otaClient = new OTAClient();
appDir = AppDir;
criticalBackupDir = CriticalBackupDir;
criticalSourceLogPath = CriticalSourceLogPath;
criticalFileExtension = CriticalFileExtension;
previousBackupPath = PreviousBackupPath;
updatePackagePath = UpdatePackagePath;
}
/// <summary>
/// 更新压缩包
/// </summary>
public void Update()
{
try
{
//初始化
initDir();
//备份重要文件
_otaClient.BackupEssentialFiles(appDir, criticalBackupDir, criticalFileExtension, criticalSourceLogPath);
//备份旧版本文件
_otaClient.CompressDirectory(appDir, previousBackupPath);
try
{
//删除旧版本原文件
_otaClient.DeleteDirectory(appDir);
//解压更新包
_otaClient.ExtractDirectory(updatePackagePath, appDir);
//恢复重要文件
_otaClient.RecoverCriticalFiles(criticalSourceLogPath);
//删除旧版本备份文件
_otaClient.DeleteFile(previousBackupPath);
//删除更新包
_otaClient.DeleteFile(updatePackagePath);
}
catch (Exception ex)
{
RollBack();
Console.WriteLine($"更新异常,已回滚: {ex.Message}");
}
}
catch (Exception ex)
{
Console.WriteLine($"更新异常e: {ex.Message}");
//如果重要文件备份已经产生,则删除
if(Directory.Exists(criticalBackupDir))
{
_otaClient.DeleteDirectory(criticalBackupDir);
}
}
}
/// <summary>
/// 初始化重要文件备份目录
/// </summary>
private void initDir()
{
if (Directory.Exists(criticalBackupDir))
{
Directory.Delete(criticalBackupDir, true);
}
Directory.CreateDirectory(criticalBackupDir);
}
/// <summary>
/// 回滚
/// </summary>
private void RollBack()
{
//删除安装过程中创建的文件、目录
var directoryList = new string[] { appDir, criticalBackupDir};
var filePath = new String[] { updatePackagePath };
foreach (var directory in directoryList)
{
if (Directory.Exists(directory))
{
_otaClient.DeleteDirectory(directory);
}
}
foreach (var file in filePath)
{
if (File.Exists(file))
{
_otaClient.DeleteFile(file);
}
}
//恢复旧版本文件
_otaClient.ExtractDirectory(previousBackupPath, appDir);
//删除旧版本备份
_otaClient.DeleteFile(previousBackupPath);
}
}

View File

@ -1,31 +0,0 @@
using MasstransferInfrastructure.OTA.Service;
namespace UnitTest;
[TestFixture]
public class OTATest
{
private string appDir = @"C:\Users\wangkaiyi\Desktop\DVT\OTA-TEST\Old";
private string criticalBackupDir = @"C:\Users\wangkaiyi\Desktop\DVT\OTA-TEST\CriticalBackup";
private string criticalSourceLogPath = @"C:\Users\wangkaiyi\Desktop\DVT\OTA-TEST\CriticalBackup\log.txt";
private string[] criticalFileExtension = new string[] { ".db", ".reg" };
private string previousBackupPath =
@"C:\Users\wangkaiyi\Desktop\DVT\OTA-TEST\previousBackup.zip";
private string UpdatePackagePath = @"C:\Users\wangkaiyi\Desktop\DVT\OTA-TEST\New.zip";
[SetUp]
public void Setup()
{
}
[Test]
public void Test1()
{
OTAService _otaService = new OTAService(appDir, criticalBackupDir, criticalSourceLogPath, criticalFileExtension,
previousBackupPath, UpdatePackagePath);
_otaService.Update();
}
}

View File

@ -1,28 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="NUnit" Version="3.14.0"/>
<PackageReference Include="NUnit.Analyzers" Version="3.9.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MasstransferInfrastructure\MasstransferInfrastructure.csproj" />
</ItemGroup>
</Project>