更新项目配置,新增设备同步模块,优化WebSocket和Swagger配置,增强SCADA系统的免登录接口,支持数据字典项和登录日志的免登录查询与记录。调整Java编译设置,确保更好的开发体验。
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
using AutoUpdaterDotNET;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Reflection;
|
||||
using System.Xml;
|
||||
|
||||
namespace YY.Admin.Services.Service.AutoUpdate
|
||||
{
|
||||
public class AutoUpdateService : IAutoUpdateService
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
private readonly HttpClient _httpClient;
|
||||
public AutoUpdateService(IConfiguration configuration, HttpClient httpClient)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_httpClient = httpClient;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取当前程序版本
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetCurrentVersion()
|
||||
{
|
||||
try
|
||||
{
|
||||
var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
|
||||
var version = assembly.GetName().Version;
|
||||
return version?.ToString() ?? "1.0.0.0";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"获取当前版本失败: {ex.Message}");
|
||||
return "1.0.0.0";
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取程序版本信息(包含版本号、产品名称等)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public VersionInfo GetApplicationInfo()
|
||||
{
|
||||
try
|
||||
{
|
||||
var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
|
||||
var version = assembly.GetName().Version;
|
||||
var title = assembly.GetCustomAttribute<AssemblyTitleAttribute>()?.Title;
|
||||
var product = assembly.GetCustomAttribute<AssemblyProductAttribute>()?.Product;
|
||||
var company = assembly.GetCustomAttribute<AssemblyCompanyAttribute>()?.Company;
|
||||
|
||||
return new VersionInfo
|
||||
{
|
||||
CurrentVersion = version?.ToString() ?? "1.0.0.0",
|
||||
ApplicationName = title ?? product ?? "应用程序",
|
||||
CompanyName = company ?? "",
|
||||
PublishDate = File.GetLastWriteTime(assembly.Location).ToString("yyyy-MM-dd")
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"获取应用程序信息失败: {ex.Message}");
|
||||
return new VersionInfo
|
||||
{
|
||||
CurrentVersion = "1.0.0.0",
|
||||
ApplicationName = "应用程序",
|
||||
CompanyName = "",
|
||||
PublishDate = DateTime.Now.ToString("yyyy-MM-dd")
|
||||
};
|
||||
}
|
||||
}
|
||||
public async Task<bool> CheckForUpdatesAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
VersionInfo? versionInfo = await ReadRemoteVersionInfoAsync();
|
||||
|
||||
if (versionInfo == null) return false;
|
||||
|
||||
var currentVersion = new Version(GetCurrentVersion());
|
||||
var latestVersion = new Version(versionInfo.LatestVersion);
|
||||
|
||||
return latestVersion > currentVersion;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"自动更新检查失败: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<VersionInfo?> GetVersionInfo()
|
||||
{
|
||||
try
|
||||
{
|
||||
VersionInfo? versionInfo = await ReadRemoteVersionInfoAsync();
|
||||
if (versionInfo == null) return null;
|
||||
|
||||
versionInfo.CurrentVersion = versionInfo.CurrentVersion;
|
||||
versionInfo.ApplicationName = versionInfo.ApplicationName;
|
||||
|
||||
return versionInfo;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"获取版本信息失败: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<VersionInfo?> ReadRemoteVersionInfoAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
// 从配置文件读取远程更新地址
|
||||
string remoteConfigUrl = _configuration.GetSection("AutoUpdate:RemoteConfigUrl").Value!;
|
||||
|
||||
var xmlContent = await _httpClient.GetStringAsync(remoteConfigUrl);
|
||||
return ParseVersionXml(xmlContent);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"读取远程版本信息失败: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private VersionInfo? ParseVersionXml(string xmlContent)
|
||||
{
|
||||
try
|
||||
{
|
||||
var xmlDoc = new XmlDocument();
|
||||
xmlDoc.LoadXml(xmlContent);
|
||||
|
||||
var item = xmlDoc.SelectSingleNode("item");
|
||||
if (item != null)
|
||||
{
|
||||
return new VersionInfo
|
||||
{
|
||||
LatestVersion = item.SelectSingleNode("version")?.InnerText ?? "1.0.0",
|
||||
DownloadUrl = item.SelectSingleNode("url")?.InnerText ?? "",
|
||||
Changelog = item.SelectSingleNode("changelog")?.InnerText ?? "暂无更新内容",
|
||||
PublishDate = item.SelectSingleNode("publishDate")?.InnerText ?? DateTime.Now.ToString("yyyy-MM-dd"),
|
||||
Mandatory = bool.TryParse(item.SelectSingleNode("mandatory")?.InnerText, out var mandatory) && mandatory
|
||||
};
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"解析版本XML失败: {ex.Message}");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public void StartUpdate(string downloadUrl)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 设置更新完成后的回调
|
||||
AutoUpdater.ApplicationExitEvent += () =>
|
||||
{
|
||||
// 更新完成后可以执行一些清理操作
|
||||
System.Diagnostics.Debug.WriteLine("应用程序准备退出以完成更新");
|
||||
};
|
||||
|
||||
AutoUpdater.Start(downloadUrl);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception($"更新启动失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否需要强制更新
|
||||
public bool IsMandatoryUpdate(VersionInfo versionInfo)
|
||||
{
|
||||
if (versionInfo == null) return false;
|
||||
|
||||
var currentVersion = new Version(GetCurrentVersion());
|
||||
var latestVersion = new Version(versionInfo.LatestVersion);
|
||||
|
||||
return versionInfo.Mandatory && latestVersion > currentVersion;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user