This commit is contained in:
2025-03-21 08:51:20 +08:00
parent 0dedff36fd
commit 9a5d3be528
70 changed files with 4542 additions and 2207 deletions

View File

@ -0,0 +1,227 @@
using System;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using DH.Commons.Base;
using DH.Commons.Models;
namespace DH.Commons.Helper
{
public static class ConfigHelper
{
private static readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
IgnoreNullValues = true
};
/// <summary>
/// 配置存储根目录
/// </summary>
private static string ConfigsRoot => Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"configs");
/// <summary>
/// 获取当前方案的主配置文件路径
/// </summary>
private static string CurrentConfigPath
{
get
{
ValidateCurrentScheme();
return Path.Combine(
ConfigsRoot,
$"config_{SystemModel.CurrentScheme}.json");
}
}
/// <summary>
/// 获取当前方案的备份目录路径
/// </summary>
private static string CurrentBackupDir
{
get
{
ValidateCurrentScheme();
return Path.Combine(
ConfigsRoot,
$"bak_{SystemModel.CurrentScheme}");
}
}
/// <summary>
/// 初始化新方案(创建空文件)
/// </summary>
public static void InitializeScheme(string scheme)
{
SystemModel.CurrentScheme = scheme;
// 创建空配置文件
if (!File.Exists(CurrentConfigPath))
{
Directory.CreateDirectory(ConfigsRoot);
File.WriteAllText(CurrentConfigPath, "{}");
}
// 创建备份目录
Directory.CreateDirectory(CurrentBackupDir);
}
/// <summary>
/// 保存当前配置(自动备份)
/// </summary>
public static void SaveConfig()
{
ValidateCurrentScheme();
// 确保配置目录存在
Directory.CreateDirectory(ConfigsRoot);
// 备份现有配置
if (File.Exists(CurrentConfigPath))
{
var backupName = $"config_{SystemModel.CurrentScheme}_{DateTime.Now:yyyyMMddHHmmss}.json";
var backupPath = Path.Combine(CurrentBackupDir, backupName);
Directory.CreateDirectory(CurrentBackupDir);
File.Copy(CurrentConfigPath, backupPath);
}
// 序列化当前配置
var json = JsonSerializer.Serialize(new
{
ConfigModel.CameraBaseList,
ConfigModel.PLCBaseList,
ConfigModel.DetectionList
}, _jsonOptions);
// 写入新配置
File.WriteAllText(CurrentConfigPath, json);
}
/// <summary>
/// 加载当前方案配置
/// </summary>
public static void LoadConfig()
{
ValidateCurrentScheme();
if (!File.Exists(CurrentConfigPath))
{
InitializeScheme(SystemModel.CurrentScheme);
return;
}
var json = File.ReadAllText(CurrentConfigPath);
var data = JsonSerializer.Deserialize<ConfigData>(json, _jsonOptions);
ConfigModel.CameraBaseList = data?.Cameras ?? new List<CameraBase>();
ConfigModel.PLCBaseList = data?.PLCs ?? new List<PLCBase>();
ConfigModel.DetectionList = data?.Detections ?? new List<DetectionConfig>();
}
/// <summary>
/// 验证当前方案有效性
/// </summary>
private static void ValidateCurrentScheme()
{
if (string.IsNullOrWhiteSpace(SystemModel.CurrentScheme))
throw new InvalidOperationException("当前方案未设置");
}
/// <summary>
/// 派生新方案(基于当前方案创建副本)
/// </summary>
/// <param name="newSchemeName">新方案名称</param>
public static void DeriveScheme(string newSchemeName)
{
// 验证输入
if (string.IsNullOrWhiteSpace(newSchemeName))
{
throw new ArgumentException("新方案名称不能为空");
}
// 验证当前方案是否有效
ValidateCurrentScheme();
// 检查新方案是否已存在
var newConfigPath = Path.Combine(ConfigsRoot, $"config_{newSchemeName}.json");
if (File.Exists(newConfigPath))
{
throw new InvalidOperationException($"方案 {newSchemeName} 已存在");
}
// 保存当前配置确保最新
SaveConfig();
try
{
// 复制配置文件
File.Copy(CurrentConfigPath, newConfigPath);
// 创建备份目录
var newBackupDir = Path.Combine(ConfigsRoot, $"bak_{newSchemeName}");
Directory.CreateDirectory(newBackupDir);
// 可选:自动切换新方案
// SystemModel.CurrentScheme = newSchemeName;
}
catch (IOException ex)
{
throw new InvalidOperationException($"方案派生失败: {ex.Message}", ex);
}
}
/// <summary>
/// 删除指定方案的配置文件及备份目录
/// </summary>
/// <param name="schemeName">要删除的方案名称</param>
/// <exception cref="ArgumentException">当方案名称为空时抛出</exception>
/// <exception cref="IOException">文件操作失败时抛出</exception>
public static void DeleteSchemeConfig(string schemeName)
{
if (string.IsNullOrWhiteSpace(schemeName))
throw new ArgumentException("方案名称无效");
// 构造路径
var configPath = Path.Combine(ConfigsRoot, $"config_{schemeName}.json");
var backupDir = Path.Combine(ConfigsRoot, $"bak_{schemeName}");
try
{
// 删除配置文件
if (File.Exists(configPath))
{
File.Delete(configPath);
}
// 删除备份目录(递归删除)
if (Directory.Exists(backupDir))
{
Directory.Delete(backupDir, true);
}
}
catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException)
{
throw new IOException($"删除方案 {schemeName} 的配置文件失败: {ex.Message}", ex);
}
}
/// <summary>
/// 配置数据模型(内部类)
/// </summary>
private class ConfigData
{
[JsonPropertyName("cameraBaseList")]
public List<CameraBase> Cameras { get; set; } = new List<CameraBase>();
[JsonPropertyName("plcBaseList")]
public List<PLCBase> PLCs { get; set; } = new List<PLCBase>();
[JsonPropertyName("detectionList")]
public List<DetectionConfig> Detections { get; set; } = new List<DetectionConfig>();
}
}
}

View File

@ -76,160 +76,50 @@ namespace DH.Commons.Enums
}
}
//public static System.Windows.Media.Color GetEnumSelectedMediaColor(this Enum enumObj)
//{
// Type t = enumObj.GetType();
// FieldInfo f = t.GetField(enumObj.ToString());
// ColorSelectAttribute attr = f.GetCustomAttribute<ColorSelectAttribute>();
// if (attr != null)
// {
// var prop = typeof(System.Windows.Media.Colors).GetProperties().FirstOrDefault(p => p.Name == attr.SelectedColor);
// if (prop != null)
// {
// return (System.Windows.Media.Color)prop.GetValue(null);
// }
// }
// return System.Windows.Media.Colors.Transparent;
//}
//public static System.Drawing.Color GetEnumSelectedColor(this Enum enumObj)
//{
// Type t = enumObj.GetType();
// FieldInfo f = t.GetField(enumObj.ToString());
// ColorSelectAttribute attr = f.GetCustomAttribute<ColorSelectAttribute>();
// if (attr != null)
// {
// return System.Drawing.Color.FromName(attr.SelectedColor);
// }
// else
// {
// return System.Drawing.Color.Transparent;
// }
//}
//public static System.Drawing.Color GetEnumSelectedFontColor(this Enum enumObj)
//{
// Type t = enumObj.GetType();
// FieldInfo f = t.GetField(enumObj.ToString());
// var attr = f.GetCustomAttribute<FontColorSelectAttribute>();
// if (attr != null)
// {
// return System.Drawing.Color.FromName(attr.SelectedColor);
// }
// else
// {
// return System.Drawing.Color.Transparent;
// }
//}
//public static string GetEnumSelectedColorString(this Enum enumObj)
//{
// Type t = enumObj.GetType();
// FieldInfo f = t.GetField(enumObj.ToString());
// ColorSelectAttribute attr = f.GetCustomAttribute<ColorSelectAttribute>();
// if (attr != null)
// {
// return attr.SelectedColor;
// }
// else
// {
// return "Transparent";
// }
//}
// 根据描述获取枚举值(泛型方法)
public static T GetEnumFromDescription<T>(string description) where T : Enum
{
Type enumType = typeof(T);
foreach (T value in Enum.GetValues(enumType))
{
string desc = GetEnumDescription(value);
if (desc == description)
{
return value;
}
}
throw new ArgumentException($"在枚举 {enumType.Name} 中未找到描述为 '{description}' 的值");
}
/// <summary>
/// 当枚举牵涉到状态变换,检查现状态是否满足待转换的状态的前置状态要求
/// 获取枚举类型的所有描述文本
/// </summary>
/// <param name="stateToBe"></param>
/// <param name="currentState"></param>
/// <returns></returns>
//public static bool CheckPreStateValid(this Enum stateToBe, int currentState)
//{
// Type t = stateToBe.GetType();
// FieldInfo f = t.GetField(stateToBe.ToString());
/// <param name="enumType">枚举类型</param>
/// <returns>描述文本列表</returns>
public static List<string> GetEnumDescriptions(Type enumType)
{
// 验证类型是否为枚举
if (!enumType.IsEnum)
throw new ArgumentException("传入的类型必须是枚举类型", nameof(enumType));
// PreStateAttribute attr = f.GetCustomAttribute<PreStateAttribute>();
// if (attr == null)
// {
// return true;
// }
// else
// {
// return attr.CheckPreStateValid(currentState);
// }
//}
var descriptions = new List<string>();
/// <summary>
/// 设备状态定义
/// 未初始化和异常状态无前置状态要求
/// 初始化操作前置状态必须是未初始化、关闭状态和异常状态
/// 打开前置必须是初始化和暂停
/// 关闭前置必须是打开和暂停和异常
/// 暂停前置必须是打开
/// </summary>
//public enum DeviceState
//{
// TBD = -1,
// [ColorSelect("Gray")]
// [FontColorSelect("Black")]
// [Description("未初始化")]
// DSUninit = 1,
// [ColorSelect("Gold")]
// [FontColorSelect("White")]
// [PreState(1 + 2 + 4 + 8 + 32)]
// [Description("初始化")]
// DSInit = 2,
// [ColorSelect("Lime")]
// [FontColorSelect("Black")]
// [PreState(2 + 4 + 16)]
// [Description("运行中")]
// DSOpen = 4,
// [ColorSelect("Gray")]
// [FontColorSelect("White")]
// [PreState(1 + 4 + 8 + 16 + 32)]
// [Description("关闭")]
// DSClose = 8,
// [ColorSelect("Gold")]
// [FontColorSelect("White")]
// [PreState(4 + 16)]
// [Description("暂停")]
// DSPause = 16,
// [ColorSelect("Red")]
// [FontColorSelect("White")]
// [Description("异常")]
// DSExcept = 32
//}
///// <summary>
///// 工序状态
///// </summary>
//public enum RunState
//{
// [ColorSelect("Gold")]
// [Description("空闲")]
// Idle = 1,
// [ColorSelect("Lime")]
// [Description("运行中")]
// Running = 2,
// [ColorSelect("Gray")]
// [Description("停止")]
// Stop = 3,
// [ColorSelect("Red")]
// [Description("宕机")]
// Down = 99,
//}
foreach (var value in Enum.GetValues(enumType))
{
var fieldInfo = enumType.GetField(value.ToString());
var attribute = fieldInfo.GetCustomAttribute<DescriptionAttribute>();
descriptions.Add(attribute?.Description ?? value.ToString());
}
return descriptions;
}
public static string GetEnumDescription1(Enum value)
{
var field = value.GetType().GetField(value.ToString());
var attribute = field.GetCustomAttributes(typeof(DescriptionAttribute), false)
.FirstOrDefault() as DescriptionAttribute;
return attribute?.Description ?? value.ToString();
}
[Flags]
public enum DeviceAttributeType
{

View File

@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DH.Commons.Helper
{
public static class SchemeHelper
{
private const string SchemesKey = "Schemes";
private const string CurrentSchemeKey = "CurrentScheme";
private const char Separator = '|';
/// <summary>
/// 初始化配置(首次运行时调用)
/// </summary>
public static void Initialize()
{
// 如果Schemes不存在创建空键
if (!SystemConfigHelper.KeyExists(SchemesKey))
{
SystemConfigHelper.SetValue(SchemesKey, "");
}
// 如果CurrentScheme不存在创建空键
if (!SystemConfigHelper.KeyExists(CurrentSchemeKey))
{
SystemConfigHelper.SetValue(CurrentSchemeKey, "");
}
}
/// <summary>
/// 获取所有方案(自动处理空值)
/// </summary>
public static List<string> GetAllSchemes()
{
var schemeString = SystemConfigHelper.GetValue(SchemesKey, "");
return string.IsNullOrEmpty(schemeString)
? new List<string>()
: new List<string>(schemeString.Split(Separator));
}
/// <summary>
/// 添加新方案(自动初始化处理)
/// </summary>
public static void AddScheme(string schemeName)
{
if (string.IsNullOrWhiteSpace(schemeName))
throw new ArgumentException("方案名称无效");
var schemes = GetAllSchemes();
if (schemes.Contains(schemeName))
throw new InvalidOperationException($"方案 {schemeName} 已存在");
schemes.Add(schemeName);
SaveSchemes(schemes);
}
/// <summary>
/// 设置当前方案(空值安全处理)
/// </summary>
public static void SetCurrentScheme(string schemeName)
{
var schemes = GetAllSchemes();
if (!schemes.Contains(schemeName))
throw new KeyNotFoundException($"方案 {schemeName} 不存在");
SystemConfigHelper.SetValue(CurrentSchemeKey, schemeName);
}
/// <summary>
/// 获取当前方案(默认值处理)
/// </summary>
public static string GetCurrentScheme()
{
var current = SystemConfigHelper.GetValue(CurrentSchemeKey, "");
return !string.IsNullOrEmpty(current) ? current : "默认方案";
}
private static void SaveSchemes(List<string> schemes)
{
var schemeString = schemes.Count > 0
? string.Join(Separator.ToString(), schemes)
: "";
SystemConfigHelper.SetValue(SchemesKey, schemeString);
}
/// <summary>
/// 删除指定方案(自动同步当前方案状态)
/// </summary>
/// <param name="schemeName">要删除的方案名称</param>
/// <exception cref="ArgumentException">当方案名称为空时抛出</exception>
/// <exception cref="KeyNotFoundException">当方案不存在时抛出</exception>
public static void DeleteScheme(string schemeName)
{
if (string.IsNullOrWhiteSpace(schemeName))
throw new ArgumentException("方案名称无效");
var schemes = GetAllSchemes();
if (!schemes.Contains(schemeName))
throw new KeyNotFoundException($"方案 {schemeName} 不存在");
// 删除前检查是否是当前方案
bool isCurrent = GetCurrentScheme() == schemeName;
// 执行删除操作
schemes.Remove(schemeName);
SaveSchemes(schemes);
}
}
}

View File

@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace DH.Commons.Helper
{
/// <summary>
/// 配置文件操作工具类(自动定位主程序配置)
/// </summary>
public static class SystemConfigHelper
{
private static Configuration _mainConfig;
private static readonly object _lock = new object();
/// <summary>
/// 获取主程序配置对象
/// </summary>
private static Configuration MainConfiguration
{
get
{
if (_mainConfig == null)
{
lock (_lock)
{
if (_mainConfig == null)
{
// 获取主程序路径
string exePath = Assembly.GetEntryAssembly().Location;
var configFile = exePath + ".config";
// 加载主程序配置
var fileMap = new ExeConfigurationFileMap
{
ExeConfigFilename = configFile
};
_mainConfig = ConfigurationManager.OpenMappedExeConfiguration(
fileMap,
ConfigurationUserLevel.None
);
}
}
}
return _mainConfig;
}
}
/// <summary>
/// 检查配置项是否存在
/// </summary>
public static bool KeyExists(string key)
{
return MainConfiguration.AppSettings.Settings[key] != null;
}
/// <summary>
/// 读取配置项(带类型自动转换)
/// </summary>
public static T GetValue<T>(string key, T defaultValue = default)
{
try
{
var setting = MainConfiguration.AppSettings.Settings[key];
if (setting == null) return defaultValue;
return (T)Convert.ChangeType(setting.Value, typeof(T));
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 写入配置项(自动保存)
/// </summary>
public static void SetValue(string key, object value)
{
var settings = MainConfiguration.AppSettings.Settings;
var stringValue = value?.ToString() ?? string.Empty;
if (settings[key] == null)
{
settings.Add(key, stringValue);
}
else
{
settings[key].Value = stringValue;
}
SaveChanges();
}
/// <summary>
/// 删除指定配置项
/// </summary>
public static void RemoveKey(string key)
{
if (KeyExists(key))
{
MainConfiguration.AppSettings.Settings.Remove(key);
SaveChanges();
}
}
/// <summary>
/// 保存配置修改
/// </summary>
private static void SaveChanges()
{
MainConfiguration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
}
}