using Prism.Events;
using System;
using System.Collections.Generic;
using System.IO;
using XP.Common.Logging.Interfaces;
using XP.Hardware.RaySource.Abstractions;
using XP.Hardware.RaySource.Config;
using XP.Hardware.RaySource.Implementations;
namespace XP.Hardware.RaySource.Factories
{
///
/// 射线源工厂实现类 | X-Ray Source Factory Implementation
/// 负责创建不同类型的射线源实例 | Responsible for creating different types of X-ray source instances
///
public class RaySourceFactory : IRaySourceFactory
{
private readonly RaySourceConfig _config;
private readonly IEventAggregator _eventAggregator;
private readonly ILoggerService _logger;
///
/// 构造函数 | Constructor
///
/// 配置对象 | Configuration object
/// 事件聚合器 | Event aggregator
/// 日志服务 | Logger service
public RaySourceFactory(RaySourceConfig config, IEventAggregator eventAggregator, ILoggerService logger)
{
_config = config ?? throw new ArgumentNullException(nameof(config));
_eventAggregator = eventAggregator ?? throw new ArgumentNullException(nameof(eventAggregator));
_logger = (logger ?? throw new ArgumentNullException(nameof(logger))).ForModule("RaySource.Factory");
_logger.Info("射线源工厂已创建 | X-ray source factory created");
}
///
/// 创建指定类型的射线源实例 | Create X-ray source instance of specified type
///
/// 射线源类型名称 | X-ray source type name
/// 射线源实例 | X-ray source instance
public IXRaySource CreateRaySource(string sourceType)
{
if (string.IsNullOrWhiteSpace(sourceType))
{
_logger.Error(null, "射线源类型不能为空 | X-ray source type cannot be empty");
throw new ArgumentException("射线源类型不能为空", nameof(sourceType));
}
_logger.Info($"创建射线源实例,类型: {sourceType} | Creating X-ray source instance, type: {sourceType}");
try
{
var instance = sourceType.ToUpperInvariant() switch
{
"COMET225" => CreateComet225RaySource(),
_ => throw new NotSupportedException($"不支持的射线源类型: {sourceType}")
};
_logger.Info($"射线源实例创建成功: {sourceType} | X-ray source instance created successfully: {sourceType}");
return instance;
}
catch (NotSupportedException ex)
{
_logger.Error(ex, $"不支持的射线源类型: {sourceType} | Unsupported X-ray source type: {sourceType}");
throw;
}
catch (TypeLoadException ex)
{
_logger.Error(ex, "跨框架类型加载失败,请检查 BR.AN.PviServices.dll 是否正确部署:{Message}", ex.Message);
throw;
}
catch (FileNotFoundException ex)
{
_logger.Error(ex, "跨框架程序集未找到,请检查 XP.Hardware.RaySource.Comet.dll 和 BR.AN.PviServices.dll 是否存在:{Message}", ex.Message);
throw;
}
catch (Exception ex)
{
_logger.Error(ex, $"创建射线源实例失败: {sourceType} | Failed to create X-ray source instance: {sourceType}");
throw;
}
}
///
/// 获取支持的射线源类型列表 | Get list of supported X-ray source types
///
/// 类型名称列表 | List of type names
public IEnumerable GetSupportedSourceTypes()
{
return new List
{
"Comet225"
};
}
///
/// 创建 Comet225 射线源实例
/// 通过 CometHostManager 管理 Host 进程生命周期,CometIpcClient 负责 IPC 通信
///
private IXRaySource CreateComet225RaySource()
{
try
{
var hostManager = new CometHostManager(_logger, _config);
var ipcClient = new CometIpcClient(_config, _eventAggregator, _logger);
return new Comet225RaySource(hostManager, ipcClient, _logger);
}
catch (Exception ex)
{
_logger.Error(ex, "创建 Comet225 射线源实例失败:{Message}", ex.Message);
throw;
}
}
}
}