| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- 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
- {
- /// <summary>
- /// Config 文件夹路径
- /// </summary>
- public static readonly string ConfigFolder = Path.Combine(
- AppDomain.CurrentDomain.BaseDirectory, "Config");
-
- private static readonly string AppSettingsPath = Path.Combine(
- ConfigFolder, "appsettings.json");
-
- /// <summary>
- /// 默认的 Modbus 配置文件路径
- /// </summary>
- public static readonly string DefaultModbusConfigPath = Path.Combine(
- ConfigFolder, "ModbusConfig.xlsx");
-
- /// <summary>
- /// 静态构造函数,确保 Config 文件夹存在
- /// </summary>
- static ConfigService()
- {
- if (!Directory.Exists(ConfigFolder))
- {
- Directory.CreateDirectory(ConfigFolder);
- }
- }
- #region Modbus Config
- public List<ModbusRegisterMapping> LoadModbusConfig(string filePath)
- {
- try
- {
- var rows = MiniExcel.Query<ModbusRegisterMapping>(filePath).ToList();
- return rows;
- }
- catch (Exception ex)
- {
- throw new Exception($"加载配置文件失败: {ex.Message}", ex);
- }
- }
- /// <summary>
- /// 保存 Modbus 配置到文件
- /// </summary>
- public void SaveModbusConfig(string filePath, List<ModbusRegisterMapping> 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
- /// <summary>
- /// 加载应用程序设置
- /// </summary>
- public AppSettings LoadAppSettings()
- {
- try
- {
- if (File.Exists(AppSettingsPath))
- {
- string json = File.ReadAllText(AppSettingsPath);
- var settings = JsonSerializer.Deserialize<AppSettings>(json);
- return settings ?? new AppSettings();
- }
- }
- catch
- {
- // 如果加载失败,返回默认设置
- }
- return new AppSettings();
- }
- /// <summary>
- /// 保存应用程序设置
- /// </summary>
- 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
- }
- }
|