using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.Json; using APS7100TestTool.Models; using MiniExcelLibs; namespace APS7100TestTool.Services { public class ConfigService { /// /// Config 文件夹路径 /// public static readonly string ConfigFolder = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "Config"); private static readonly string AppSettingsPath = Path.Combine( ConfigFolder, "appsettings.json"); /// /// 默认的 Modbus 配置文件路径 /// public static readonly string DefaultModbusConfigPath = Path.Combine( ConfigFolder, "ModbusConfig.xlsx"); /// /// 静态构造函数,确保 Config 文件夹存在 /// static ConfigService() { if (!Directory.Exists(ConfigFolder)) { Directory.CreateDirectory(ConfigFolder); } } #region Modbus Config public List LoadModbusConfig(string filePath) { try { var rows = MiniExcel.Query(filePath).ToList(); return rows; } catch (Exception ex) { throw new Exception($"加载配置文件失败: {ex.Message}", ex); } } /// /// 保存 Modbus 配置到文件 /// public void SaveModbusConfig(string filePath, List mappings) { try { // 如果文件已存在,先删除(MiniExcel 不支持覆盖) if (File.Exists(filePath)) { File.Delete(filePath); } MiniExcel.SaveAs(filePath, mappings); } catch (Exception ex) { throw new Exception($"保存配置文件失败: {ex.Message}", ex); } } public void CreateTemplate(string filePath) { // 模板示例:区分 APS7100 和 PSW250 的命令格式 var data = new[] { // ========== APS7100 示例 ========== new ModbusRegisterMapping { Address = 1, RegisterType = "Holding", OperationType = "Write", DataType = "Float", DeviceTarget = "APS7100", ScpiCommand = "SOUR:VOLT {0}", ScaleFactor = 1.0, Description = "[APS] 设置电压 (V)" }, new ModbusRegisterMapping { Address = 10, RegisterType = "Holding", OperationType = "Read", DataType = "Float", DeviceTarget = "APS7100", // ⚠ APS7100 必须使用 MEAS:SCAL:VOLT? ScpiCommand = "MEAS:SCAL:VOLT?", ScaleFactor = 1.0, Description = "[APS] 测量电压 (V)" }, new ModbusRegisterMapping { Address = 20, RegisterType = "Holding", OperationType = "Write", DataType = "Int16", DeviceTarget = "APS7100", TriggerValue = 1, // ⚠ APS7100 必须使用 OUTP:STAT ScpiCommand = "OUTP:STAT ON", ScaleFactor = 1.0, Description = "[APS] 开启输出" }, // ========== PSW250 示例 ========== new ModbusRegisterMapping { Address = 100, RegisterType = "Holding", OperationType = "Write", DataType = "Float", DeviceTarget = "PSW250", ScpiCommand = "SOUR:VOLT {0}", ScaleFactor = 1.0, Description = "[PSW] 设置电压 (V)" }, new ModbusRegisterMapping { Address = 110, RegisterType = "Holding", OperationType = "Read", DataType = "Float", DeviceTarget = "PSW250", // PSW250 支持简写命令 ScpiCommand = "MEAS:VOLT?", ScaleFactor = 1.0, Description = "[PSW] 测量电压 (V)" }, }; MiniExcel.SaveAs(filePath, data); } #endregion #region App Settings /// /// 加载应用程序设置 /// public AppSettings LoadAppSettings() { try { if (File.Exists(AppSettingsPath)) { string json = File.ReadAllText(AppSettingsPath); var settings = JsonSerializer.Deserialize(json); return settings ?? new AppSettings(); } } catch { // 如果加载失败,返回默认设置 } return new AppSettings(); } /// /// 保存应用程序设置 /// public void SaveAppSettings(AppSettings settings) { try { settings.LastUpdated = DateTime.Now; var options = new JsonSerializerOptions { WriteIndented = true }; string json = JsonSerializer.Serialize(settings, options); File.WriteAllText(AppSettingsPath, json); } catch (Exception ex) { throw new Exception($"保存设置失败: {ex.Message}", ex); } } #endregion } }