using System.Globalization; using HardwareControl.Core.AxisPlatform; using HardwareControl.Core.HardwareServiceImpl.HardwareService; using HardwareControl.Core.Spectrometer; using HardwareControl.Core.SystemSetting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace HardwareControl.Core.Scanning { /// 一次扫描使用的运行参数(从系统参数读取)。 public sealed record ScanSettings( double CameraScale, double CameraOffsetX, double CameraOffsetY, int MoveSpeed, int MoveAccel, int ExposureUs, int Averaging, int DelayUs, int WaitTimeoutMs, TimeSpan FocusTimeout); /// /// 读取扫描所需的系统参数。参数在每次扫描开始时重新读取, /// 管理界面修改后无需重启即可生效。 /// public sealed class ScanSettingsProvider { public const string CameraScaleName = "camera_scale"; public const string CameraOffsetXName = "camera_x_offset"; public const string CameraOffsetYName = "camera_y_offset"; public const string MoveSpeedName = "move_speed"; public const string MoveAccelName = "move_accel"; public const string ExposureName = "ARIS_ExposureUs"; public const string AveragingName = "ARIS_Averaging"; public const string DelayName = "ARIS_DelayUs"; public const string TimeoutName = "ARIS_TimeoutMs"; public const string FocusTimeoutName = "focus_outTime"; private readonly ISystemConfigService _systemConfig; private readonly AxisStageOptions _axisOptions; private readonly ArisSpectrometerOptions _spectrometerOptions; private readonly ILogger _logger; public ScanSettingsProvider( ISystemConfigService systemConfig, IOptions axisOptions, IOptions spectrometerOptions, ILogger logger) { _systemConfig = systemConfig; _axisOptions = axisOptions.Value; _spectrometerOptions = spectrometerOptions.Value; _logger = logger; } public ScanSettings Load() { var values = new Dictionary(StringComparer.OrdinalIgnoreCase); foreach (var config in _systemConfig.GetAll()) { var name = config.ConfigName?.Trim(); if (string.IsNullOrWhiteSpace(name)) { continue; } values[name] = config.ConfigValue?.Trim() ?? string.Empty; } var cameraScale = GetDouble(values, CameraScaleName, 0); if (cameraScale <= 0) { throw new HardwareException( HardwareErrorKind.InvalidState, "未配置有效的标尺比例 camera_scale,请先在标尺设定中完成相机标定"); } var moveSpeed = GetInt( values, MoveSpeedName, _axisOptions.DefaultSpeed); var moveAccel = GetInt( values, MoveAccelName, _axisOptions.DefaultAccel); // ARIS_ExposureUs 在系统参数中的单位是毫秒,接口需要微秒。 var exposureMs = GetDouble( values, ExposureName, _spectrometerOptions.SynchronizedExposureUs / 1000.0); var exposureUs = (int)Math.Round(exposureMs * 1000.0, MidpointRounding.AwayFromZero); if (exposureUs <= 0) { exposureUs = _spectrometerOptions.SynchronizedExposureUs; } var averaging = GetInt( values, AveragingName, _spectrometerOptions.Averaging); if (averaging < 1) { averaging = 1; } var delayUs = GetInt( values, DelayName, _spectrometerOptions.DelayUs); if (delayUs < 0) { delayUs = 0; } var waitTimeoutMs = GetInt( values, TimeoutName, _spectrometerOptions.SynchronizedWaitTimeoutMs); if (waitTimeoutMs <= 0) { waitTimeoutMs = _spectrometerOptions.SynchronizedWaitTimeoutMs; } var focusSeconds = GetDouble(values, FocusTimeoutName, 10); if (focusSeconds <= 0) { focusSeconds = 10; } var settings = new ScanSettings( cameraScale, GetDouble(values, CameraOffsetXName, 0), GetDouble(values, CameraOffsetYName, 0), Math.Clamp(moveSpeed, 1, ushort.MaxValue), Math.Clamp(moveAccel, 1, ushort.MaxValue), exposureUs, averaging, delayUs, waitTimeoutMs, TimeSpan.FromSeconds(focusSeconds)); _logger.LogInformation( "扫描参数:scale={Scale}px/mm,offset=({OffsetX},{OffsetY})mm," + "speed={Speed},accel={Accel},exposure={Exposure}us," + "averaging={Averaging},delay={Delay}us,waitTimeout={WaitTimeout}ms," + "focusTimeout={FocusTimeout}s", settings.CameraScale, settings.CameraOffsetX, settings.CameraOffsetY, settings.MoveSpeed, settings.MoveAccel, settings.ExposureUs, settings.Averaging, settings.DelayUs, settings.WaitTimeoutMs, settings.FocusTimeout.TotalSeconds); return settings; } private static double GetDouble( IReadOnlyDictionary values, string name, double fallback) => values.TryGetValue(name, out var raw) && double.TryParse(raw, NumberStyles.Float, CultureInfo.InvariantCulture, out var value) ? value : fallback; private static int GetInt( IReadOnlyDictionary values, string name, int fallback) => values.TryGetValue(name, out var raw) && int.TryParse(raw, NumberStyles.Integer, CultureInfo.InvariantCulture, out var value) ? value : fallback; } }