103 lines
5.0 KiB
C#
103 lines
5.0 KiB
C#
using Prism.Ioc;
|
||
using Prism.Modularity;
|
||
using System.Resources;
|
||
using XP.Common.Localization;
|
||
using XP.Common.Localization.Interfaces;
|
||
using XP.Common.Logging.Implementations;
|
||
using XP.Common.Logging.Interfaces;
|
||
using XP.Hardware.RaySource.Abstractions;
|
||
using XP.Hardware.RaySource.Config;
|
||
using XP.Hardware.RaySource.Factories;
|
||
using XP.Hardware.RaySource.Services;
|
||
|
||
namespace XP.Hardware.RaySource.Module
|
||
{
|
||
/// <summary>
|
||
/// 射线源模块 | X-Ray Source Module
|
||
/// Prism模块入口,注册服务/工厂到Unity容器 | Prism module entry, register services/factories to Unity container
|
||
/// </summary>
|
||
[Module(ModuleName = "RaySourceModule")]
|
||
public class RaySourceModule : IModule
|
||
{
|
||
/// <summary>
|
||
/// 模块初始化 | Module initialization
|
||
/// </summary>
|
||
public void OnInitialized(IContainerProvider containerProvider)
|
||
{
|
||
// 注册模块级多语言资源到 Fallback Chain | Register module-level localization resources to Fallback Chain
|
||
var localizationService = containerProvider.Resolve<ILocalizationService>();
|
||
var resourceManager = new ResourceManager(
|
||
"XP.Hardware.RaySource.Resources.Resources",
|
||
typeof(RaySourceModule).Assembly);
|
||
localizationService.RegisterResourceSource("XP.Hardware.RaySource", resourceManager);
|
||
|
||
// 初始化 LocalizationHelper,使其通过 ILocalizationService 获取字符串(支持 Fallback Chain)
|
||
// Initialize LocalizationHelper to use ILocalizationService for string lookup (supports Fallback Chain)
|
||
LocalizationHelper.Initialize(localizationService);
|
||
|
||
// 初始化灯丝寿命服务(建表、异常恢复)| Initialize filament lifetime service (create tables, recover anomalies)
|
||
var filamentService = containerProvider.Resolve<IFilamentLifetimeService>();
|
||
var initialized = filamentService.Initialize();
|
||
|
||
if (initialized && filamentService.ShouldShowLifetimeWarning())
|
||
{
|
||
// 获取预警数据 | Get warning data
|
||
var totalSeconds = filamentService.GetCurrentTotalLifeSeconds();
|
||
var thresholdSeconds = filamentService.GetThresholdSeconds();
|
||
var percentage = filamentService.GetLifetimePercentage();
|
||
|
||
// 转换为小时,保留一位小数 | Convert to hours, keep one decimal place
|
||
var totalHours = (totalSeconds / 3600.0).ToString("F1");
|
||
var thresholdHours = (thresholdSeconds / 3600.0).ToString("F1");
|
||
var percentageText = percentage.ToString("F1");
|
||
|
||
// 使用 LocalizationHelper 获取本地化文本(OnInitialized 中不方便注入)
|
||
// Use LocalizationHelper for localized text (injection not convenient in OnInitialized)
|
||
var title = LocalizationHelper.Get("RaySource_FilamentLifetimeWarningTitle");
|
||
var message = LocalizationHelper.Get("RaySource_FilamentLifetimeWarningMessage",
|
||
totalHours, thresholdHours, percentageText);
|
||
|
||
// 在 UI 线程弹出模态提醒窗口 | Show modal warning dialog on UI thread
|
||
System.Windows.Application.Current?.Dispatcher?.Invoke(() =>
|
||
{
|
||
System.Windows.MessageBox.Show(
|
||
message,
|
||
title,
|
||
System.Windows.MessageBoxButton.OK,
|
||
System.Windows.MessageBoxImage.Warning);
|
||
});
|
||
}
|
||
|
||
System.Console.WriteLine("[RaySourceModule] 模块已初始化");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 注册类型到DI容器 | Register types to DI container
|
||
/// </summary>
|
||
public void RegisterTypes(IContainerRegistry containerRegistry)
|
||
{
|
||
// 注册日志服务(单例)| Register logger service (singleton)
|
||
if (!containerRegistry.IsRegistered<ILoggerService>())
|
||
{
|
||
containerRegistry.RegisterSingleton<ILoggerService, SerilogLoggerService>();
|
||
System.Console.WriteLine("[RaySourceModule] 日志服务已注册 | Logger service registered");
|
||
}
|
||
|
||
// 注册配置对象(单例)| Register configuration object (singleton)
|
||
var config = ConfigLoader.LoadConfig();
|
||
containerRegistry.RegisterInstance(config);
|
||
|
||
// 注册工厂层(单例)| Register factory layer (singleton)
|
||
containerRegistry.RegisterSingleton<IRaySourceFactory, RaySourceFactory>();
|
||
|
||
// 注册服务层(单例)| Register service layer (singleton)
|
||
containerRegistry.RegisterSingleton<IRaySourceService, RaySourceService>();
|
||
|
||
// 注册灯丝寿命服务(单例)| Register filament lifetime service (singleton)
|
||
containerRegistry.RegisterSingleton<IFilamentLifetimeService, FilamentLifetimeService>();
|
||
|
||
System.Console.WriteLine("[RaySourceModule] 类型注册完成 | Type registration completed");
|
||
}
|
||
}
|
||
}
|