将Feature/XP.Common和Feature/XP.Hardware分支合并至Develop/XP.forHardwareAndCommon,完善XPapp注册和相关硬件类库通用类库功能。
This commit is contained in:
@@ -0,0 +1,237 @@
|
||||
using System;
|
||||
using System.Configuration;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using XP.Common.Logging.Interfaces;
|
||||
|
||||
namespace XP.Hardware.RaySource.Config
|
||||
{
|
||||
/// <summary>
|
||||
/// 配置加载器 | Configuration Loader
|
||||
/// 从App.config读取配置,适配.NET 8 ConfigurationManager | Load configuration from App.config, adapted for .NET 8 ConfigurationManager
|
||||
/// </summary>
|
||||
public class ConfigLoader
|
||||
{
|
||||
private const string CONFIG_SECTION = "RaySource";
|
||||
|
||||
/// <summary>
|
||||
/// 加载射线源配置 | Load X-ray source configuration
|
||||
/// </summary>
|
||||
/// <param name="configFilePath">配置文件路径(可选,默认使用App.config)| Configuration file path (optional, defaults to App.config)</param>
|
||||
/// <param name="logger">日志服务(可选)| Logger service (optional)</param>
|
||||
/// <returns>配置对象 | Configuration object</returns>
|
||||
public static RaySourceConfig LoadConfig(string configFilePath = null, ILoggerService logger = null)
|
||||
{
|
||||
var config = new RaySourceConfig();
|
||||
|
||||
try
|
||||
{
|
||||
// 如果指定了配置文件路径,使用自定义配置文件
|
||||
// If a configuration file path is specified, use custom configuration file
|
||||
if (!string.IsNullOrEmpty(configFilePath) && File.Exists(configFilePath))
|
||||
{
|
||||
var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };
|
||||
var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
|
||||
LoadFromConfiguration(config, configuration.AppSettings.Settings, logger);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 使用默认App.config | Use default App.config
|
||||
LoadFromAppSettings(config, logger);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[配置加载警告 | Config Load Warning] 使用默认配置: {ex.Message}");
|
||||
// 加载失败时返回默认配置 | Return default configuration on load failure
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从AppSettings加载配置 | Load configuration from AppSettings
|
||||
/// </summary>
|
||||
private static void LoadFromAppSettings(RaySourceConfig config, ILoggerService logger = null)
|
||||
{
|
||||
// PVI通讯参数 | PVI Communication Parameters
|
||||
config.SourceType = GetAppSetting(CONFIG_SECTION + ":" + "SourceType", config.SourceType);
|
||||
config.PlcIpAddress = GetAppSetting(CONFIG_SECTION + ":" + "PlcIpAddress", config.PlcIpAddress);
|
||||
config.PlcPort = GetAppSetting(CONFIG_SECTION + ":" + "PlcPort", config.PlcPort);
|
||||
config.StationNumber = GetAppSetting(CONFIG_SECTION + ":" + "StationNumber", config.StationNumber);
|
||||
config.PortNumber = GetAppSetting(CONFIG_SECTION + ":" + "PortNumber", config.PortNumber);
|
||||
config.CpuName = GetAppSetting(CONFIG_SECTION + ":" + "CpuName", config.CpuName);
|
||||
config.ConnectionTimeout = GetAppSetting(CONFIG_SECTION + ":" + "ConnectionTimeout", config.ConnectionTimeout);
|
||||
|
||||
// 硬件参数范围 | Hardware Parameter Ranges
|
||||
config.MinVoltage = GetAppSetting(CONFIG_SECTION + ":" + "MinVoltage", config.MinVoltage);
|
||||
config.MaxVoltage = GetAppSetting(CONFIG_SECTION + ":" + "MaxVoltage", config.MaxVoltage);
|
||||
config.MinCurrent = GetAppSetting(CONFIG_SECTION + ":" + "MinCurrent", config.MinCurrent);
|
||||
config.MaxCurrent = GetAppSetting(CONFIG_SECTION + ":" + "MaxCurrent", config.MaxCurrent);
|
||||
|
||||
// 外部程序配置 | External Program Configuration
|
||||
config.AdvanceExePath = GetAppSetting(CONFIG_SECTION + ":" + "AdvanceExePath", config.AdvanceExePath);
|
||||
|
||||
// 操作超时配置 | Operation Timeout Configuration
|
||||
config.InitializationTimeout = GetAppSetting(CONFIG_SECTION + ":" + "InitializationTimeout", config.InitializationTimeout);
|
||||
config.WarmUpTimeout = GetAppSetting(CONFIG_SECTION + ":" + "WarmUpTimeout", config.WarmUpTimeout);
|
||||
config.StartUpTimeout = GetAppSetting(CONFIG_SECTION + ":" + "StartUpTimeout", config.StartUpTimeout);
|
||||
config.AutoCenterTimeout = GetAppSetting(CONFIG_SECTION + ":" + "AutoCenterTimeout", config.AutoCenterTimeout);
|
||||
config.FilamentAdjustTimeout = GetAppSetting(CONFIG_SECTION + ":" + "FilamentAdjustTimeout", config.FilamentAdjustTimeout);
|
||||
config.GeneralOperationTimeout = GetAppSetting(CONFIG_SECTION + ":" + "GeneralOperationTimeout", config.GeneralOperationTimeout);
|
||||
|
||||
// 灯丝寿命配置 | Filament Lifetime Configuration
|
||||
config.SerialNumber = GetAppSetting(CONFIG_SECTION + ":" + "SerialNumber", config.SerialNumber);
|
||||
config.TotalLifeThreshold = GetAppSetting(CONFIG_SECTION + ":" + "TotalLifeThreshold", config.TotalLifeThreshold);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从Configuration对象加载配置 | Load configuration from Configuration object
|
||||
/// </summary>
|
||||
private static void LoadFromConfiguration(RaySourceConfig config, KeyValueConfigurationCollection settings, ILoggerService logger = null)
|
||||
{
|
||||
config.SourceType = GetSetting(settings, CONFIG_SECTION + ":" + "SourceType", config.SourceType);
|
||||
config.PlcIpAddress = GetSetting(settings, CONFIG_SECTION + ":" + "PlcIpAddress", config.PlcIpAddress);
|
||||
config.PlcPort = GetSetting(settings, CONFIG_SECTION + ":" + "PlcPort", config.PlcPort);
|
||||
config.StationNumber = GetSetting(settings, CONFIG_SECTION + ":" + "StationNumber", config.StationNumber);
|
||||
config.PortNumber = GetSetting(settings, CONFIG_SECTION + ":" + "PortNumber", config.PortNumber);
|
||||
config.CpuName = GetSetting(settings, CONFIG_SECTION + ":" + "CpuName", config.CpuName);
|
||||
config.ConnectionTimeout = GetSetting(settings, CONFIG_SECTION + ":" + "ConnectionTimeout", config.ConnectionTimeout);
|
||||
|
||||
config.MinVoltage = GetSetting(settings, CONFIG_SECTION + ":" + "MinVoltage", config.MinVoltage);
|
||||
config.MaxVoltage = GetSetting(settings, CONFIG_SECTION + ":" + "MaxVoltage", config.MaxVoltage);
|
||||
config.MinCurrent = GetSetting(settings, CONFIG_SECTION + ":" + "MinCurrent", config.MinCurrent);
|
||||
config.MaxCurrent = GetSetting(settings, CONFIG_SECTION + ":" + "MaxCurrent", config.MaxCurrent);
|
||||
|
||||
// 外部程序配置 | External Program Configuration
|
||||
config.AdvanceExePath = GetSetting(settings, CONFIG_SECTION + ":" + "AdvanceExePath", config.AdvanceExePath);
|
||||
|
||||
config.InitializationTimeout = GetSetting(settings, CONFIG_SECTION + ":" + "InitializationTimeout", config.InitializationTimeout);
|
||||
config.WarmUpTimeout = GetSetting(settings, CONFIG_SECTION + ":" + "WarmUpTimeout", config.WarmUpTimeout);
|
||||
config.StartUpTimeout = GetSetting(settings, CONFIG_SECTION + ":" + "StartUpTimeout", config.StartUpTimeout);
|
||||
config.AutoCenterTimeout = GetSetting(settings, CONFIG_SECTION + ":" + "AutoCenterTimeout", config.AutoCenterTimeout);
|
||||
config.FilamentAdjustTimeout = GetSetting(settings, CONFIG_SECTION + ":" + "FilamentAdjustTimeout", config.FilamentAdjustTimeout);
|
||||
config.GeneralOperationTimeout = GetSetting(settings, CONFIG_SECTION + ":" + "GeneralOperationTimeout", config.GeneralOperationTimeout);
|
||||
|
||||
// 灯丝寿命配置 | Filament Lifetime Configuration
|
||||
config.SerialNumber = GetSetting(settings, CONFIG_SECTION + ":" + "SerialNumber", config.SerialNumber);
|
||||
config.TotalLifeThreshold = GetSetting(settings, CONFIG_SECTION + ":" + "TotalLifeThreshold", config.TotalLifeThreshold);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存配置到AppSettings | Save configuration to AppSettings
|
||||
/// </summary>
|
||||
public static void SaveConfigToAppSettings(RaySourceConfig config)
|
||||
{
|
||||
// 获取程序默认配置文件路径(对应 ConfigurationManager.AppSettings 的文件)
|
||||
Assembly entryAssembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
|
||||
string assemblyPath = entryAssembly.Location;
|
||||
string defaultConfigPath = Path.ChangeExtension(assemblyPath, ".config");
|
||||
// 复用原有保存逻辑
|
||||
SaveConfig(config, defaultConfigPath);
|
||||
// 刷新 ConfigurationManager,让 AppSettings 立即读取最新值
|
||||
ConfigurationManager.RefreshSection("appSettings");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存配置到文件 | Save configuration to file
|
||||
/// </summary>
|
||||
public static void SaveConfig(RaySourceConfig config, string configFilePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };
|
||||
var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
|
||||
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "SourceType", config.SourceType);
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "PlcIpAddress", config.PlcIpAddress);
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "PlcPort", config.PlcPort.ToString());
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "StationNumber", config.StationNumber.ToString());
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "PortNumber", config.PortNumber.ToString());
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "CpuName", config.CpuName);
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "ConnectionTimeout", config.ConnectionTimeout.ToString());
|
||||
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "MinVoltage", config.MinVoltage.ToString());
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "MaxVoltage", config.MaxVoltage.ToString());
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "MinCurrent", config.MinCurrent.ToString());
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "MaxCurrent", config.MaxCurrent.ToString());
|
||||
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "AdvanceExePath", config.AdvanceExePath);
|
||||
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "InitializationTimeout", config.InitializationTimeout.ToString());
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "WarmUpTimeout", config.WarmUpTimeout.ToString());
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "StartUpTimeout", config.StartUpTimeout.ToString());
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "AutoCenterTimeout", config.AutoCenterTimeout.ToString());
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "FilamentAdjustTimeout", config.FilamentAdjustTimeout.ToString());
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "GeneralOperationTimeout", config.GeneralOperationTimeout.ToString());
|
||||
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "SerialNumber", config.SerialNumber);
|
||||
SetSetting(configuration.AppSettings.Settings, CONFIG_SECTION + ":" + "TotalLifeThreshold", config.TotalLifeThreshold.ToString());
|
||||
|
||||
configuration.Save(ConfigurationSaveMode.Modified);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new InvalidOperationException($"保存配置失败: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
#region 辅助方法 | Helper Methods
|
||||
|
||||
private static string GetAppSetting(string key, string defaultValue)
|
||||
{
|
||||
return ConfigurationManager.AppSettings[key] ?? defaultValue;
|
||||
}
|
||||
|
||||
private static int GetAppSetting(string key, int defaultValue)
|
||||
{
|
||||
var value = ConfigurationManager.AppSettings[key];
|
||||
return int.TryParse(value, out var result) ? result : defaultValue;
|
||||
}
|
||||
|
||||
private static float GetAppSetting(string key, float defaultValue)
|
||||
{
|
||||
var value = ConfigurationManager.AppSettings[key];
|
||||
return float.TryParse(value, out var result) ? result : defaultValue;
|
||||
}
|
||||
|
||||
private static bool GetAppSetting(string key, bool defaultValue)
|
||||
{
|
||||
var value = ConfigurationManager.AppSettings[key];
|
||||
return bool.TryParse(value, out var result) ? result : defaultValue;
|
||||
}
|
||||
|
||||
private static string GetSetting(KeyValueConfigurationCollection settings, string key, string defaultValue)
|
||||
{
|
||||
return settings[key]?.Value ?? defaultValue;
|
||||
}
|
||||
|
||||
private static int GetSetting(KeyValueConfigurationCollection settings, string key, int defaultValue)
|
||||
{
|
||||
var value = settings[key]?.Value;
|
||||
return int.TryParse(value, out var result) ? result : defaultValue;
|
||||
}
|
||||
|
||||
private static float GetSetting(KeyValueConfigurationCollection settings, string key, float defaultValue)
|
||||
{
|
||||
var value = settings[key]?.Value;
|
||||
return float.TryParse(value, out var result) ? result : defaultValue;
|
||||
}
|
||||
|
||||
private static bool GetSetting(KeyValueConfigurationCollection settings, string key, bool defaultValue)
|
||||
{
|
||||
var value = settings[key]?.Value;
|
||||
return bool.TryParse(value, out var result) ? result : defaultValue;
|
||||
}
|
||||
|
||||
private static void SetSetting(KeyValueConfigurationCollection settings, string key, string value)
|
||||
{
|
||||
if (settings[key] == null)
|
||||
settings.Add(key, value);
|
||||
else
|
||||
settings[key].Value = value;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
using System;
|
||||
|
||||
namespace XP.Hardware.RaySource.Config
|
||||
{
|
||||
/// <summary>
|
||||
/// 射线源配置实体类 | X-Ray Source Configuration Entity
|
||||
/// 包含PVI通讯参数、硬件参数和操作超时配置 | Contains PVI communication parameters, hardware parameters, and operation timeout configuration
|
||||
/// </summary>
|
||||
public class RaySourceConfig
|
||||
{
|
||||
#region PVI通讯参数 | PVI Communication Parameters
|
||||
|
||||
/// <summary>
|
||||
/// 射线源类型 | Ray source type
|
||||
/// 可选值: Comet225 | Available values: Comet225
|
||||
/// </summary>
|
||||
public string SourceType { get; set; } = "Comet225";
|
||||
|
||||
/// <summary>
|
||||
/// PLC IP地址 | PLC IP Address
|
||||
/// </summary>
|
||||
public string PlcIpAddress { get; set; } = "192.168.12.10";
|
||||
|
||||
/// <summary>
|
||||
/// PLC端口号 | PLC Port Number
|
||||
/// </summary>
|
||||
public int PlcPort { get; set; } = 11159;
|
||||
|
||||
/// <summary>
|
||||
/// 源站号 | Source Station Number
|
||||
/// </summary>
|
||||
public int StationNumber { get; set; } = 1;
|
||||
|
||||
/// <summary>
|
||||
/// 源端口号 | Source Port Number
|
||||
/// </summary>
|
||||
public int PortNumber { get; set; } = 11;
|
||||
|
||||
/// <summary>
|
||||
/// CPU名称 | CPU Name
|
||||
/// </summary>
|
||||
public string CpuName { get; set; } = "cpu";
|
||||
|
||||
/// <summary>
|
||||
/// 连接超时时间(毫秒)| Connection timeout (milliseconds)
|
||||
/// </summary>
|
||||
public int ConnectionTimeout { get; set; } = 5000;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 硬件参数范围 | Hardware Parameter Ranges
|
||||
|
||||
/// <summary>
|
||||
/// 最小电压(kV)| Minimum voltage (kV)
|
||||
/// </summary>
|
||||
public float MinVoltage { get; set; } = 20f;
|
||||
|
||||
/// <summary>
|
||||
/// 最大电压(kV)| Maximum voltage (kV)
|
||||
/// </summary>
|
||||
public float MaxVoltage { get; set; } = 225f;
|
||||
|
||||
/// <summary>
|
||||
/// 最小电流(μA)| Minimum current (μA)
|
||||
/// </summary>
|
||||
public float MinCurrent { get; set; } = 10f;
|
||||
|
||||
/// <summary>
|
||||
/// 最大电流(μA)| Maximum current (μA)
|
||||
/// </summary>
|
||||
public float MaxCurrent { get; set; } = 1000f;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 外部程序配置 | External Program Configuration
|
||||
|
||||
/// <summary>
|
||||
/// 高级设置外部程序路径 | Advance settings external program path
|
||||
/// </summary>
|
||||
public string AdvanceExePath { get; set; } = @"C:\Program Files (x86)\Feinfocus\FXEControl_3.1.1.65\FXEControl.exe";
|
||||
|
||||
/// <summary>
|
||||
/// Host 进程可执行文件路径(可选)
|
||||
/// 为空时默认在主程序输出目录下查找 XP.Hardware.RaySource.Comet.Host.exe
|
||||
/// </summary>
|
||||
public string HostExePath { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region 操作超时配置 | Operation Timeout Configuration
|
||||
|
||||
/// <summary>
|
||||
/// 初始化超时时间(毫秒)| Initialization timeout (milliseconds)
|
||||
/// </summary>
|
||||
public int InitializationTimeout { get; set; } = 30000;
|
||||
|
||||
/// <summary>
|
||||
/// 暖机超时时间(毫秒)| Warm-up timeout (milliseconds)
|
||||
/// </summary>
|
||||
public int WarmUpTimeout { get; set; } = 300000; // 5分钟
|
||||
|
||||
/// <summary>
|
||||
/// 启动超时时间(毫秒)| Startup timeout (milliseconds)
|
||||
/// </summary>
|
||||
public int StartUpTimeout { get; set; } = 180000; // 3分钟
|
||||
|
||||
/// <summary>
|
||||
/// 自动定心超时时间(毫秒)| Auto-center timeout (milliseconds)
|
||||
/// </summary>
|
||||
public int AutoCenterTimeout { get; set; } = 120000; // 2分钟
|
||||
|
||||
/// <summary>
|
||||
/// 灯丝调整超时时间(毫秒)| Filament adjust timeout (milliseconds)
|
||||
/// </summary>
|
||||
public int FilamentAdjustTimeout { get; set; } = 120000; // 2分钟
|
||||
|
||||
/// <summary>
|
||||
/// 一般操作超时时间(毫秒)| General operation timeout (milliseconds)
|
||||
/// </summary>
|
||||
public int GeneralOperationTimeout { get; set; } = 10000;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 灯丝寿命配置 | Filament Lifetime Configuration
|
||||
|
||||
/// <summary>
|
||||
/// 射线源序列号 | Ray source serial number
|
||||
/// 用于唯一标识设备,灯丝寿命管理依赖此字段匹配设备记录
|
||||
/// Used to uniquely identify the device, filament lifetime management relies on this field to match device records
|
||||
/// </summary>
|
||||
public string SerialNumber { get; set; } = "";
|
||||
|
||||
/// <summary>
|
||||
/// 灯丝总寿命阈值(小时)| Filament total lifetime threshold (hours)
|
||||
/// 默认值为 1000 小时,超过阈值时系统将发出预警
|
||||
/// Default value is 1000 hours, system will issue a warning when threshold is exceeded
|
||||
/// </summary>
|
||||
public int TotalLifeThreshold { get; set; } = 1000;
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 验证配置参数有效性 | Validate configuration parameters
|
||||
/// </summary>
|
||||
/// <returns>验证结果 | Validation result</returns>
|
||||
public (bool IsValid, string ErrorMessage) Validate()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(PlcIpAddress))
|
||||
return (false, "PLC IP地址不能为空");
|
||||
|
||||
if (PlcPort <= 0 || PlcPort > 65535)
|
||||
return (false, "PLC端口号必须在1-65535之间");
|
||||
|
||||
if (MinVoltage >= MaxVoltage)
|
||||
return (false, "最小电压必须小于最大电压");
|
||||
|
||||
if (MinCurrent >= MaxCurrent)
|
||||
return (false, "最小电流必须小于最大电流");
|
||||
|
||||
if (ConnectionTimeout <= 0)
|
||||
return (false, "连接超时时间必须大于0");
|
||||
|
||||
return (true, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user