添加项目文件。
This commit is contained in:
157
UIShare/GlobalVariable/CANSignalBroadcaster.cs
Normal file
157
UIShare/GlobalVariable/CANSignalBroadcaster.cs
Normal file
@@ -0,0 +1,157 @@
|
||||
using Model.Models;
|
||||
using Prism.Events;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using UIShare.PubEvent;
|
||||
using ZLGUSBCANFD;
|
||||
|
||||
namespace UIShare.GlobalVariable
|
||||
{
|
||||
/// <summary>
|
||||
/// CAN 信号广播器(全局单例):
|
||||
/// 统一订阅 GlobalInfo.CanPool 中所有已实例化的 ZLGCANFD 设备,
|
||||
/// 将 DBC 解码后的信号值广播给所有引用该 CAN 设备的作用域,
|
||||
/// 并同步检查各作用域 ValueLimitList 的超限报警。
|
||||
/// </summary>
|
||||
public class CANSignalBroadcaster : IDisposable
|
||||
{
|
||||
private readonly IEventAggregator _eventAggregator;
|
||||
private readonly GlobalInfo _globalInfo;
|
||||
private bool _disposed;
|
||||
|
||||
/// <summary>已订阅解码事件的 CANFD 实例 → 委托 映射</summary>
|
||||
private readonly Dictionary<ZLGCANFD, Action<uint, ZDBC.DBCMessage>> _handlers = new();
|
||||
|
||||
public CANSignalBroadcaster(GlobalInfo globalInfo, IEventAggregator eventAggregator)
|
||||
{
|
||||
_globalInfo = globalInfo;
|
||||
_eventAggregator = eventAggregator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 扫描 GlobalInfo.CanPool 中已创建的 CANFD 实例并订阅解码事件。
|
||||
/// 幂等:已订阅的实例不会重复订阅。
|
||||
/// </summary>
|
||||
public void Discover()
|
||||
{
|
||||
if (_disposed) return;
|
||||
|
||||
foreach (var kvp in _globalInfo.CanPool)
|
||||
{
|
||||
string fingerprint = kvp.Key;
|
||||
var lazy = kvp.Value;
|
||||
if (!lazy.IsValueCreated || lazy.Value == null) continue;
|
||||
|
||||
var canfd = lazy.Value;
|
||||
if (_handlers.ContainsKey(canfd)) continue;
|
||||
|
||||
// 使用闭包捕获指纹,回调时即可区分不同 CAN 卡
|
||||
Action<uint, ZDBC.DBCMessage> handler = (channel, msg) => OnDbcMessageDecoded(fingerprint, channel, msg);
|
||||
canfd.OnDbcMessageDecoded += handler;
|
||||
_handlers[canfd] = handler;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 启动广播器(目前 Discover 已完成订阅,此处保留以兼容 HardwareDataBroadcaster 的使用模式)。
|
||||
/// </summary>
|
||||
public void Start()
|
||||
{
|
||||
Discover();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DBC 解码回调:提取所有信号,向引用该 CAN 设备的作用域广播,并检查报警。
|
||||
/// </summary>
|
||||
private void OnDbcMessageDecoded(string canFingerprint, uint channel, ZDBC.DBCMessage msg)
|
||||
{
|
||||
if (_disposed) return;
|
||||
|
||||
string signalFingerprint = BuildFingerprint(canFingerprint, channel);
|
||||
var now = DateTime.Now;
|
||||
|
||||
// 获取引用该 CAN 设备的所有作用域
|
||||
var scopes = GetScopesForFingerprint(canFingerprint);
|
||||
if (scopes.Count == 0) return;
|
||||
|
||||
for (int i = 0; i < msg.nSignalCount; i++)
|
||||
{
|
||||
var signal = msg.vSignals[i];
|
||||
string signalName = Encoding.Default.GetString(signal.strName).TrimEnd('\0');
|
||||
if (string.IsNullOrEmpty(signalName)) continue;
|
||||
|
||||
double physicalValue = signal.nRawvalue * signal.nFactor + signal.nOffset;
|
||||
string methodName = BuildMethodName(msg.nID, signalName);
|
||||
|
||||
foreach (var scope in scopes)
|
||||
{
|
||||
_eventAggregator.GetEvent<HardwareDataReportedEvent>().Publish(new HardwareReportArgs
|
||||
{
|
||||
Scope = scope,
|
||||
HardwareFingerprint = signalFingerprint,
|
||||
MethodName = methodName,
|
||||
Value = physicalValue,
|
||||
Time = now
|
||||
});
|
||||
|
||||
string MonitorStatus = ValueLimitAlarmHelper.CheckAlarm(scope, signalFingerprint, methodName, physicalValue, _globalInfo);
|
||||
if (MonitorStatus != "" && MonitorStatus != "未报警")
|
||||
{
|
||||
_eventAggregator.GetEvent<AlarmEvent>().Publish((scope,canFingerprint, MonitorStatus));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>获取指定硬件指纹当前被哪些作用域引用</summary>
|
||||
private List<string> GetScopesForFingerprint(string fingerprint)
|
||||
{
|
||||
if (_globalInfo.DeviceAndScopeDic.TryGetValue(fingerprint, out var lazy))
|
||||
{
|
||||
var scopeList = lazy.Value;
|
||||
lock (scopeList) return scopeList.ToList();
|
||||
}
|
||||
return new List<string>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成 CAN 信号的 DisplayName 格式:"{MessageName}.{SignalName}"
|
||||
/// </summary>
|
||||
public static string BuildDisplayName(string messageName, string signalName)
|
||||
{
|
||||
return $"{messageName}.{signalName}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成 CAN 信号的 MethodName 格式:"{MessageId:X}.{SignalName}"
|
||||
/// </summary>
|
||||
public static string BuildMethodName(uint messageId, string signalName)
|
||||
{
|
||||
return $"{messageId:X}.{signalName}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成 CAN 信号的 Fingerprint 格式:"{canDeviceFingerprint}:{channel}"
|
||||
/// canDeviceFingerprint 来自 DeviceManager.ExtractHardwareFingerprint,如 "CAN:0"
|
||||
/// </summary>
|
||||
public static string BuildFingerprint(string canDeviceFingerprint, uint channel)
|
||||
{
|
||||
return $"{canDeviceFingerprint}:{channel}";
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
|
||||
foreach (var kvp in _handlers)
|
||||
{
|
||||
if (kvp.Key != null)
|
||||
kvp.Key.OnDbcMessageDecoded -= kvp.Value;
|
||||
}
|
||||
_handlers.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
139
UIShare/GlobalVariable/ConfigService.cs
Normal file
139
UIShare/GlobalVariable/ConfigService.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using Logger;
|
||||
using UIShare.UIViewModel;
|
||||
|
||||
namespace UIShare.GlobalVariable
|
||||
{
|
||||
public static class ConfigService
|
||||
{
|
||||
private static readonly object _fileLock = new();
|
||||
/// <summary>
|
||||
/// 根据标题查询配置文件是否存在
|
||||
/// </summary>
|
||||
public static bool IsExit(string title)
|
||||
{
|
||||
if (string.IsNullOrEmpty(title))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
string configPath = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ACP"), $"{title}.json");
|
||||
|
||||
if (!File.Exists(configPath))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据标题(格子标识)加载独立的配置文件
|
||||
/// </summary>
|
||||
public static SystemConfig Load(string title)
|
||||
{
|
||||
if (string.IsNullOrEmpty(title))
|
||||
{
|
||||
throw new ArgumentException("配置标题不能为空", nameof(title));
|
||||
}
|
||||
|
||||
// 临时实例化一个对象以获取默认的 SystemPath
|
||||
var dummy = new SystemConfig();
|
||||
string configPath = Path.Combine(dummy.SystemPath, $"{title}.json");
|
||||
|
||||
if (!File.Exists(configPath))
|
||||
{
|
||||
// 如果不存在,创建一个带 Title 的默认配置并保存
|
||||
var defaultConfig = new SystemConfig { Title = title };
|
||||
EnsureDefaultCanDevice(defaultConfig);
|
||||
Save(defaultConfig);
|
||||
return defaultConfig;
|
||||
}
|
||||
|
||||
lock (_fileLock)
|
||||
{
|
||||
try
|
||||
{
|
||||
string json = File.ReadAllText(configPath);
|
||||
var config = JsonConvert.DeserializeObject<SystemConfig>(json, new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.All
|
||||
});
|
||||
|
||||
config ??= new SystemConfig { Title = title };
|
||||
EnsureDefaultCanDevice(config);
|
||||
return config;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LoggerHelper.ErrorWithNotify(title, $"格子 [{title}] 配置加载失败: {ex.Message}");
|
||||
var fallback = new SystemConfig { Title = title };
|
||||
EnsureDefaultCanDevice(fallback);
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存指定的配置实例
|
||||
/// </summary>
|
||||
public static void Save(SystemConfig config)
|
||||
{
|
||||
if (config == null || string.IsNullOrEmpty(config.Title)) return;
|
||||
|
||||
lock (_fileLock)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!Directory.Exists(config.SystemPath))
|
||||
Directory.CreateDirectory(config.SystemPath);
|
||||
|
||||
string configPath = Path.Combine(config.SystemPath, $"{config.Title}.json");
|
||||
|
||||
string json = JsonConvert.SerializeObject(config, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.All
|
||||
});
|
||||
|
||||
File.WriteAllText(configPath, json);
|
||||
LoggerHelper.InfoWithNotify(config.Title, $"配置 [{config.Title}] 已保存。");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LoggerHelper.ErrorWithNotify(config.Title, $"配置 [{config.Title}] 保存失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 确保配置中至少包含一个 CAN 设备(对应 SystemConfig.CANFD)。
|
||||
/// 旧配置或空配置会自动升级,使用户在设置界面能看到 CAN 设备。
|
||||
/// </summary>
|
||||
public static void EnsureDefaultCanDevice(SystemConfig config)
|
||||
{
|
||||
if (config.DeviceList == null)
|
||||
{
|
||||
config.DeviceList = new ObservableCollection<DeviceInfoVM>();
|
||||
}
|
||||
|
||||
bool hasCan = config.DeviceList.Any(d =>
|
||||
string.Equals(d?.ConnectionType, "CAN", StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(d?.DeviceType, "ZLGCANFD", StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (!hasCan)
|
||||
{
|
||||
config.DeviceList.Add(new DeviceInfoVM
|
||||
{
|
||||
DeviceName = "CAN",
|
||||
DeviceType = "ZLGCANFD",
|
||||
Remark = "周立功 CANFD 接口卡",
|
||||
ConnectionType = "CAN",
|
||||
IsEnabled = true,
|
||||
IsConnected = false
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
676
UIShare/GlobalVariable/DeviceManager.cs
Normal file
676
UIShare/GlobalVariable/DeviceManager.cs
Normal file
@@ -0,0 +1,676 @@
|
||||
using DeviceCommand.Base;
|
||||
using DeviceCommand.Devices;
|
||||
using Logger;
|
||||
using Model.Models;
|
||||
using Prism.Events;
|
||||
using Prism.Ioc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Ports;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using UIShare.PubEvent;
|
||||
using UIShare.UIViewModel;
|
||||
using ZLGUSBCANFD;
|
||||
|
||||
namespace UIShare.GlobalVariable
|
||||
{
|
||||
/// <summary>
|
||||
/// 设备管理器:根据 <see cref="SystemConfig.DeviceList"/> 反射实例化所有启用的设备,
|
||||
/// 通过 <see cref="IBaseInterface"/> 多态统一管理,避免为每种设备单独硬编码字段。
|
||||
/// </summary>
|
||||
public class DeviceManager:IDisposable
|
||||
{
|
||||
private object _lockObj = new object();
|
||||
public SystemConfig _systemConfig { get; set; }
|
||||
private readonly GlobalInfo _globalInfo;
|
||||
private readonly string _scopeName;
|
||||
private readonly IEventAggregator _eventAggregator;
|
||||
|
||||
/// <summary>按 DeviceName 索引的设备字典,便于业务层按名取实例。</summary>
|
||||
public IDictionary<string, IBaseInterface> DeviceMap { get; private set; }
|
||||
= new Dictionary<string, IBaseInterface>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>类名 → Type 的反射缓存(仅扫描一次)。</summary>
|
||||
private static readonly IReadOnlyDictionary<string, Type> _deviceTypeMap = BuildDeviceTypeMap();
|
||||
public ZLGCANFD CANFD { get; set; }
|
||||
public IOBoardGroup IOGroup { get; set; }
|
||||
|
||||
public DeviceManager(SystemConfig systemConfig, GlobalInfo globalInfo, IEventAggregator eventAggregator)
|
||||
{
|
||||
_systemConfig = systemConfig;
|
||||
_globalInfo = globalInfo;
|
||||
_eventAggregator = eventAggregator;
|
||||
// 用 SystemConfig.Title 作为作用域唯一标识,无需反查 ConfigDic
|
||||
_scopeName = _systemConfig.Title;
|
||||
InitDevices();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据设备配置提取唯一的硬件指纹字符串。
|
||||
/// <para>Tcp → "Tcp:IP:Port";Serial → "Serial:PortName";无法识别则返回空字符串。</para>
|
||||
/// </summary>
|
||||
public static string ExtractHardwareFingerprint(DeviceInfoVM config)
|
||||
{
|
||||
if (string.Equals(config.ConnectionType, "Tcp", StringComparison.OrdinalIgnoreCase)
|
||||
&& config.TcpConfig != null)
|
||||
{
|
||||
return $"Tcp:{config.TcpConfig.IPAddress}:{config.TcpConfig.Port}";
|
||||
}
|
||||
|
||||
if (string.Equals(config.ConnectionType, "Serial", StringComparison.OrdinalIgnoreCase)
|
||||
&& config.SerialPortConfig != null)
|
||||
{
|
||||
return $"Serial:{config.SerialPortConfig.PortName}";
|
||||
}
|
||||
|
||||
if (string.Equals(config.ConnectionType, "CAN", StringComparison.OrdinalIgnoreCase)
|
||||
&& config.CANConfig != null)
|
||||
{
|
||||
return $"CAN:{config.CANConfig.DeviceIndex}";
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前作用域配置的 CAN 设备硬件指纹(与 GlobalInfo.CanPool 的 Key 一致)。
|
||||
/// 如 "CAN:0",其中 0 是 CANConfig.DeviceIndex。
|
||||
/// </summary>
|
||||
public string GetCanDeviceFingerprint()
|
||||
{
|
||||
if (_systemConfig?.DeviceList == null) return string.Empty;
|
||||
var canConfig = _systemConfig.DeviceList.FirstOrDefault(d =>
|
||||
d != null && d.IsEnabled &&
|
||||
string.Equals(d.ConnectionType, "CAN", StringComparison.OrdinalIgnoreCase));
|
||||
return canConfig != null ? ExtractHardwareFingerprint(canConfig) : string.Empty;
|
||||
}
|
||||
|
||||
private void InitDevices()
|
||||
{
|
||||
DeviceMap = new Dictionary<string, IBaseInterface>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
if (_systemConfig?.DeviceList == null) return;
|
||||
|
||||
foreach (var config in _systemConfig.DeviceList)
|
||||
{
|
||||
if (config == null || !config.IsEnabled) continue;
|
||||
|
||||
// CAN 设备:ZLGCANFD 不实现 IBaseInterface,通过 SystemConfig.CANFD 单独管理,
|
||||
// 按指纹从全局 CanPool 创建/复用实例,并注册作用域引用计数。
|
||||
if (string.Equals(config.ConnectionType, "CAN", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var fingerprint = ExtractHardwareFingerprint(config);
|
||||
if (string.IsNullOrEmpty(fingerprint))
|
||||
{
|
||||
LoggerHelper.Warn($"设备 [{config.DeviceName}] 无法提取硬件指纹(连接方式={config.ConnectionType}),已跳过。");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (config.CANConfig == null)
|
||||
{
|
||||
LoggerHelper.Warn($"设备 [{config.DeviceName}] 缺少 CAN 连接参数,已跳过。");
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 按指纹全局唯一创建 ZLGCANFD 实例(maxChannels 默认 4,对应 USBCANFD-400U)
|
||||
// 波特率与终端电阻从 CANConfigVM 传入,初始化并启动通道 时直接使用
|
||||
var canLazy = _globalInfo.CanPool.GetOrAdd(fingerprint, key => new Lazy<ZLGCANFD>(() =>
|
||||
new ZLGCANFD(config.CANConfig.DeviceType, config.CANConfig.DeviceIndex, 4,
|
||||
config.CANConfig.ABitBaud, config.CANConfig.DBitBaud, config.CANConfig.EnableTerminalResistance)));
|
||||
|
||||
_systemConfig.CANFD = canLazy.Value;
|
||||
CANFD = canLazy.Value;
|
||||
|
||||
// 注册作用域引用计数
|
||||
if (!string.IsNullOrEmpty(_scopeName))
|
||||
{
|
||||
var scopeList = _globalInfo.DeviceAndScopeDic.GetOrAdd(fingerprint,
|
||||
_ => new Lazy<List<string>>(() => new List<string>())).Value;
|
||||
lock (scopeList)
|
||||
{
|
||||
if (!scopeList.Contains(_scopeName))
|
||||
scopeList.Add(_scopeName);
|
||||
}
|
||||
}
|
||||
|
||||
LoggerHelper.Info($"已加载 CAN 设备 [{config.DeviceName}] 指纹={fingerprint}(通过 SystemConfig.CANFD 管理)");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LoggerHelper.ErrorWithNotify(_scopeName, $"CAN 设备 [{config.DeviceName}] 实例化失败:{ex.Message}");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(config.DeviceType) ||
|
||||
!_deviceTypeMap.TryGetValue(config.DeviceType, out var deviceType))
|
||||
{
|
||||
LoggerHelper.Warn($"未识别的设备类型 [{config.DeviceType}],已跳过 [{config.DeviceName}]。");
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 第一步:提取硬件指纹,为空则跳过
|
||||
var fingerprint = ExtractHardwareFingerprint(config);
|
||||
if (string.IsNullOrEmpty(fingerprint))
|
||||
{
|
||||
LoggerHelper.Warn($"设备 [{config.DeviceName}] 无法提取硬件指纹(连接方式={config.ConnectionType}),已跳过。");
|
||||
continue;
|
||||
}
|
||||
|
||||
// 第二步:原子化获取或添加 Lazy 包装盒,确保同一指纹全局只创建一个实例
|
||||
var lazy = _globalInfo.HardwarePool.GetOrAdd(fingerprint, key => new Lazy<IBaseInterface>(() =>
|
||||
{
|
||||
return config.ConnectionType switch
|
||||
{
|
||||
"Tcp" => CreateTcpDevice(deviceType, config.TcpConfig)!,
|
||||
"Serial" => CreateSerialDevice(deviceType, config.SerialPortConfig)!,
|
||||
_ => null!
|
||||
};
|
||||
}));
|
||||
|
||||
// 第三步:安全拆盒,Lazy 内部线程锁保证只实例化一次
|
||||
var instance = lazy.Value;
|
||||
|
||||
if (instance == null)
|
||||
{
|
||||
LoggerHelper.Warn($"设备 [{config.DeviceName}] 连接方式 [{config.ConnectionType}] 不支持,已跳过。");
|
||||
continue;
|
||||
}
|
||||
|
||||
// 第四步:绑定逻辑名 → 同一物理设备可被多个台架名称映射
|
||||
if (!string.IsNullOrWhiteSpace(config.DeviceName))
|
||||
{
|
||||
DeviceMap[config.DeviceName] = instance;
|
||||
}
|
||||
|
||||
// 第五步:将当前作用域注册到指纹的作用域列表,用于引用计数与安全销毁
|
||||
if (!string.IsNullOrEmpty(_scopeName))
|
||||
{
|
||||
var scopeList = _globalInfo.DeviceAndScopeDic.GetOrAdd(fingerprint,
|
||||
_ => new Lazy<List<string>>(() => new List<string>())).Value;
|
||||
lock (scopeList)
|
||||
{
|
||||
if (!scopeList.Contains(_scopeName))
|
||||
scopeList.Add(_scopeName);
|
||||
}
|
||||
}
|
||||
|
||||
LoggerHelper.Info($"已加载设备 [{config.DeviceName} / {config.DeviceType} / {config.ConnectionType}] 指纹={fingerprint}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var inner = ex.InnerException?.Message ?? ex.Message;
|
||||
LoggerHelper.ErrorWithNotify(_scopeName, $"设备 [{config.DeviceName}] 实例化失败:{inner}");
|
||||
}
|
||||
}
|
||||
|
||||
// IOGroup 初始化:从已实例化的设备中取出前两个 IOBoard 实例组建 IOBoardGroup
|
||||
try
|
||||
{
|
||||
var ioBoards = DeviceMap.Values.OfType<IOBoard>().Take(2).ToList();
|
||||
if (ioBoards.Count == 2)
|
||||
{
|
||||
IOGroup = new IOBoardGroup(ioBoards[0], ioBoards[1]);
|
||||
LoggerHelper.Info($"IOBoardGroup 已初始化,Board1={ioBoards[0].GetType().Name}, Board2={ioBoards[1].GetType().Name}。");
|
||||
}
|
||||
else
|
||||
{
|
||||
LoggerHelper.Warn($"IOBoardGroup 初始化跳过:需要 2 个 IOBoard 实例,当前仅找到 {ioBoards.Count} 个。");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LoggerHelper.ErrorWithNotify(_scopeName, $"IOBoardGroup 初始化失败:{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ConnectAllDevices(CancellationToken ct = default)
|
||||
{
|
||||
if (_systemConfig?.DeviceList == null) return;
|
||||
|
||||
var tasks = new List<Task>();
|
||||
foreach (var info in _systemConfig.DeviceList)
|
||||
{
|
||||
if (info == null || !info.IsEnabled) continue;
|
||||
if (string.IsNullOrWhiteSpace(info.DeviceName)) continue;
|
||||
|
||||
// CAN 设备:直接打开 CAN 卡
|
||||
if (string.Equals(info.ConnectionType, "CAN", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
tasks.Add(ConnectCanAsync(info));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!DeviceMap.TryGetValue(info.DeviceName, out var device)) continue;
|
||||
tasks.Add(ConnectInternalAsync(info, device, ct));
|
||||
}
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
|
||||
|
||||
public async Task ConnectSpecifiedDevice(string deviceName, CancellationToken ct = default)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(deviceName))
|
||||
{
|
||||
LoggerHelper.Warn("ConnectSpecifiedDevice:设备名为空。");
|
||||
return;
|
||||
}
|
||||
|
||||
var info = _systemConfig?.DeviceList?
|
||||
.FirstOrDefault(d => d != null && string.Equals(d.DeviceName, deviceName, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (info == null)
|
||||
{
|
||||
LoggerHelper.Warn($"ConnectSpecifiedDevice:未找到设备配置 [{deviceName}]。");
|
||||
return;
|
||||
}
|
||||
|
||||
// CAN 设备:直接打开 CAN 卡
|
||||
if (string.Equals(info.ConnectionType, "CAN", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
await ConnectCanAsync(info);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!DeviceMap.TryGetValue(deviceName, out var device))
|
||||
{
|
||||
LoggerHelper.Warn($"ConnectSpecifiedDevice:未找到设备 [{deviceName}]。");
|
||||
return;
|
||||
}
|
||||
|
||||
await ConnectInternalAsync(info, device, ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步关闭指定设备,释放底层连接并更新 UI 状态
|
||||
/// </summary>
|
||||
public async Task CloseDeviceAsync(string deviceName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(deviceName)) return;
|
||||
|
||||
var info = _systemConfig?.DeviceList?
|
||||
.FirstOrDefault(d => d != null && string.Equals(d.DeviceName, deviceName, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
// CAN 设备:直接关闭 CAN 卡
|
||||
if (info != null && string.Equals(info.ConnectionType, "CAN", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
await CloseCanAsync(info);
|
||||
return;
|
||||
}
|
||||
|
||||
IBaseInterface? device;
|
||||
|
||||
lock (_lockObj)
|
||||
{
|
||||
if (!DeviceMap.TryGetValue(deviceName, out device)) return;
|
||||
}
|
||||
|
||||
await CloseInternalAsync(info, device);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步关闭所有设备
|
||||
/// </summary>
|
||||
public async Task CloseAllDevicesAsync()
|
||||
{
|
||||
List<Task> tasks = new List<Task>();
|
||||
|
||||
lock (_lockObj)
|
||||
{
|
||||
if (DeviceMap.Count == 0 && (CANFD == null)) return;
|
||||
|
||||
foreach (var kvp in DeviceMap)
|
||||
{
|
||||
string deviceName = kvp.Key;
|
||||
var device = kvp.Value;
|
||||
var info = _systemConfig?.DeviceList?
|
||||
.FirstOrDefault(d => d != null && string.Equals(d.DeviceName, deviceName, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
tasks.Add(CloseInternalAsync(info, device));
|
||||
}
|
||||
}
|
||||
|
||||
// CAN 设备:直接关闭 CAN 卡
|
||||
if (CANFD != null)
|
||||
{
|
||||
var canInfo = _systemConfig?.DeviceList?
|
||||
.FirstOrDefault(d => d != null && string.Equals(d.ConnectionType, "CAN", StringComparison.OrdinalIgnoreCase));
|
||||
if (canInfo != null)
|
||||
{
|
||||
tasks.Add(CloseCanAsync(canInfo));
|
||||
}
|
||||
}
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
LoggerHelper.Info("所有设备已执行关闭操作。");
|
||||
}
|
||||
|
||||
#region 辅助方法
|
||||
private async Task CloseInternalAsync(DeviceInfoVM? info, IBaseInterface device)
|
||||
{
|
||||
string name = info?.DeviceName ?? device.GetType().Name;
|
||||
string conn = info?.ConnectionType ?? "?";
|
||||
|
||||
try
|
||||
{
|
||||
// 如果设备本身已经是断开状态,直接更新 UI 并返回
|
||||
if (!device.IsConnected)
|
||||
{
|
||||
if (info != null) info.IsConnected = false;
|
||||
LoggerHelper.Info($"设备 [{name}] 本就处于断开状态。");
|
||||
return;
|
||||
}
|
||||
await Task.Run(() => device.Close());
|
||||
|
||||
LoggerHelper.Info($"设备 [{name}/{conn}] 已成功关闭连接。");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var inner = ex.InnerException?.Message ?? ex.Message;
|
||||
LoggerHelper.Error($"设备 [{name}/{conn}] 关闭连接时出现异常: {inner}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
// 无论关闭时是否抛出异常,均强制同步 UI 状态为未连接
|
||||
if (info != null)
|
||||
{
|
||||
info.IsConnected = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
private async Task ConnectInternalAsync(DeviceInfoVM? info, IBaseInterface device, CancellationToken ct)
|
||||
{
|
||||
string name = info?.DeviceName ?? device.GetType().Name;
|
||||
string conn = info?.ConnectionType ?? "?";
|
||||
|
||||
try
|
||||
{
|
||||
if (device.IsConnected)
|
||||
{
|
||||
if (info != null) info.IsConnected = true;
|
||||
LoggerHelper.Info($"设备 [{name}] 已连接,跳过。");
|
||||
return;
|
||||
}
|
||||
|
||||
bool ok = conn switch
|
||||
{
|
||||
"Tcp" => await ConnectTcpAsync(name, device, ct),
|
||||
"Serial" => await ConnectSerialAsync(name, device, ct),
|
||||
_ => false
|
||||
};
|
||||
|
||||
if (info != null) info.IsConnected = ok;
|
||||
|
||||
if (ok)
|
||||
LoggerHelper.Info($"设备 [{name}/{conn}] 连接成功。");
|
||||
else
|
||||
LoggerHelper.Warn($"设备 [{name}/{conn}] 连接失败。");
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
if (info != null) info.IsConnected = false;
|
||||
LoggerHelper.Warn($"设备 [{name}/{conn}] 连接已取消。");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (info != null) info.IsConnected = false;
|
||||
var inner = ex.InnerException?.Message ?? ex.Message;
|
||||
LoggerHelper.ErrorWithNotify(_scopeName, $"设备 [{name}/{conn}] 连接异常:{inner}");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<bool> ConnectTcpAsync(string name, IBaseInterface device, CancellationToken ct)
|
||||
{
|
||||
if (device is ITcp tcp)
|
||||
{
|
||||
return await tcp.ConnectAsync(ct);
|
||||
}
|
||||
if (device is IModbusDevice modbusTCP)
|
||||
{
|
||||
return await modbusTCP.ConnectAsync(ct);
|
||||
}
|
||||
LoggerHelper.Warn($"设备 [{name}] 配置为 Tcp 但未实现 ITcp,实际类型为 {device.GetType().Name}。");
|
||||
return false;
|
||||
}
|
||||
|
||||
private static async Task<bool> ConnectSerialAsync(string name, IBaseInterface device, CancellationToken ct)
|
||||
{
|
||||
if (device is not ISerialPort sp)
|
||||
{
|
||||
LoggerHelper.Warn($"设备 [{name}] 配置为 Serial 但未实现 ISerialPort,实际类型为 {device.GetType().Name}。");
|
||||
return false;
|
||||
}
|
||||
return await sp.ConnectAsync(ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打开 CAN 卡:打开设备 + 初始化并启动所有通道 + 自动加载 DBC + 启动信号广播
|
||||
/// </summary>
|
||||
private async Task<bool> ConnectCanAsync(DeviceInfoVM info)
|
||||
{
|
||||
string name = info.DeviceName ?? "CAN";
|
||||
|
||||
try
|
||||
{
|
||||
if (CANFD == null)
|
||||
{
|
||||
LoggerHelper.Warn($"CAN 设备 [{name}] 尚未实例化,无法连接。");
|
||||
info.IsConnected = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (info.IsConnected)
|
||||
{
|
||||
LoggerHelper.Info($"CAN 设备 [{name}] 已连接,跳过。");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ok = await Task.Run(() =>
|
||||
{
|
||||
if (!CANFD.打开设备()) return false;
|
||||
// 自动加载 DBC 文件
|
||||
if (_systemConfig?.DBCAutoLoadList != null)
|
||||
{
|
||||
foreach (var item in _systemConfig.DBCAutoLoadList)
|
||||
{
|
||||
if (item.DBCChannel < 0 || item.DBCChannel >= 4) continue;
|
||||
if (string.IsNullOrWhiteSpace(item.DBCFilePath)) continue;
|
||||
|
||||
if (!File.Exists(item.DBCFilePath))
|
||||
{
|
||||
// 发布 DBC 卸载事件,通知监控系统清除对应信号
|
||||
_eventAggregator.GetEvent<DBCUnloadedEvent>().Publish(new DBCUnloadedArgs
|
||||
{
|
||||
Channel = (uint)item.DBCChannel,
|
||||
Scope = _scopeName
|
||||
});
|
||||
LoggerHelper.Warn($"CAN 通道 {item.DBCChannel} 自动加载 DBC 失败:文件不存在 [{item.DBCFilePath}]");
|
||||
continue;
|
||||
}
|
||||
CANFD.初始化并启动通道((uint)item.DBCChannel);
|
||||
bool loadOk = CANFD.加载通道DBC文件((uint)item.DBCChannel, item.DBCFilePath);
|
||||
if (loadOk)
|
||||
{
|
||||
LoggerHelper.Info($"CAN 通道 {item.DBCChannel} 已自动加载 DBC:{item.DBCFilePath}");
|
||||
// 发布 DBC 加载完成事件,通知监控系统刷新信号列表
|
||||
_eventAggregator.GetEvent<DBCLoadedEvent>().Publish(new DBCLoadedArgs
|
||||
{
|
||||
Channel = (uint)item.DBCChannel,
|
||||
Scope = _scopeName
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
LoggerHelper.Warn($"CAN 通道 {item.DBCChannel} 自动加载 DBC 失败:{item.DBCFilePath}");
|
||||
_eventAggregator.GetEvent<DBCUnloadedEvent>().Publish(new DBCUnloadedArgs
|
||||
{
|
||||
Channel = (uint)item.DBCChannel,
|
||||
Scope = _scopeName
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
info.IsConnected = ok;
|
||||
|
||||
if (ok)
|
||||
LoggerHelper.Info($"CAN 设备 [{name}] 连接成功,已初始化 {CANFD.DBCParser.MaxChannels} 个通道。");
|
||||
else
|
||||
LoggerHelper.Warn($"CAN 设备 [{name}] 连接失败。");
|
||||
|
||||
return ok;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
info.IsConnected = false;
|
||||
var inner = ex.InnerException?.Message ?? ex.Message;
|
||||
LoggerHelper.ErrorWithNotify(_scopeName, $"CAN 设备 [{name}] 连接异常:{inner}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭 CAN 卡
|
||||
/// </summary>
|
||||
private async Task CloseCanAsync(DeviceInfoVM info)
|
||||
{
|
||||
string name = info.DeviceName ?? "CAN";
|
||||
|
||||
try
|
||||
{
|
||||
if (CANFD == null)
|
||||
{
|
||||
info.IsConnected = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!info.IsConnected)
|
||||
{
|
||||
LoggerHelper.Info($"CAN 设备 [{name}] 本就处于断开状态。");
|
||||
return;
|
||||
}
|
||||
|
||||
await Task.Run(() => CANFD.关闭CAN卡设备());
|
||||
LoggerHelper.Info($"CAN 设备 [{name}] 已成功关闭连接。");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var inner = ex.InnerException?.Message ?? ex.Message;
|
||||
LoggerHelper.Error($"CAN 设备 [{name}] 关闭连接时出现异常: {inner}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
info.IsConnected = false;
|
||||
}
|
||||
}
|
||||
|
||||
private static IReadOnlyDictionary<string, Type> BuildDeviceTypeMap()
|
||||
{
|
||||
try
|
||||
{
|
||||
return typeof(IBaseInterface).Assembly
|
||||
.GetTypes()
|
||||
.Where(t => t.IsClass && !t.IsAbstract && typeof(IBaseInterface).IsAssignableFrom(t))
|
||||
.ToDictionary(t => t.Name, t => t, StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
catch (ReflectionTypeLoadException ex)
|
||||
{
|
||||
LoggerHelper.Error($"扫描设备类型失败:{ex.Message}");
|
||||
return new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
private static IBaseInterface? CreateTcpDevice(Type type, TcpConfigVM? vm)
|
||||
{
|
||||
vm ??= new TcpConfigVM();
|
||||
var cfg = new TcpConfig
|
||||
{
|
||||
IPAddress = vm.IPAddress,
|
||||
Port = vm.Port,
|
||||
SendTimeout = vm.SendTimeout,
|
||||
ReceiveTimeout = vm.ReceiveTimeout
|
||||
};
|
||||
return Activator.CreateInstance(type, cfg) as IBaseInterface;
|
||||
}
|
||||
private static IBaseInterface? CreateSerialDevice(Type type, SerialPortConfigVM? vm)
|
||||
{
|
||||
vm ??= new SerialPortConfigVM();
|
||||
var cfg = new SerialPortConfig
|
||||
{
|
||||
PortName = vm.PortName,
|
||||
BaudRate = vm.BaudRate,
|
||||
DataBits = vm.DataBits,
|
||||
StopBits = Enum.TryParse<StopBits>(vm.StopBits, true, out var sb) ? sb : StopBits.One,
|
||||
Parity = Enum.TryParse<Parity>(vm.Parity, true, out var pa) ? pa : Parity.None,
|
||||
ReadTimeout = vm.ReadTimeout,
|
||||
WriteTimeout = vm.WriteTimeout
|
||||
};
|
||||
return Activator.CreateInstance(type, cfg) as IBaseInterface;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_scopeName)) return;
|
||||
|
||||
// 遍历当前作用域用到的所有指纹,逐一移除本作用域的引用
|
||||
var fingerprintsToRemove = new List<string>();
|
||||
|
||||
foreach (var kvp in _globalInfo.DeviceAndScopeDic)
|
||||
{
|
||||
string fingerprint = kvp.Key;
|
||||
var scopeList = kvp.Value.IsValueCreated ? kvp.Value.Value : null;
|
||||
if (scopeList == null) continue;
|
||||
|
||||
lock (scopeList)
|
||||
{
|
||||
scopeList.Remove(_scopeName);
|
||||
|
||||
// 引用归零 → 标记为待清理
|
||||
if (scopeList.Count == 0)
|
||||
fingerprintsToRemove.Add(fingerprint);
|
||||
}
|
||||
}
|
||||
|
||||
// 对引用归零的指纹:销毁设备实例 + 从全局池中移除
|
||||
foreach (var fingerprint in fingerprintsToRemove)
|
||||
{
|
||||
// 尝试从 HardwarePool 取出并销毁
|
||||
if (_globalInfo.HardwarePool.TryRemove(fingerprint, out var lazy))
|
||||
{
|
||||
if (lazy.IsValueCreated && lazy.Value is IBaseInterface device)
|
||||
{
|
||||
try { device.Close(); }
|
||||
catch { /* 销毁时忽略异常 */ }
|
||||
LoggerHelper.Info($"指纹 [{fingerprint}] 无作用域引用,已销毁设备实例。");
|
||||
}
|
||||
}
|
||||
|
||||
// 尝试从 CanPool 取出并销毁
|
||||
if (_globalInfo.CanPool.TryRemove(fingerprint, out var canLazy))
|
||||
{
|
||||
if (canLazy.IsValueCreated)
|
||||
{
|
||||
try { canLazy.Value.Dispose(); }
|
||||
catch { /* 销毁时忽略异常 */ }
|
||||
LoggerHelper.Info($"指纹 [{fingerprint}] 无作用域引用,已销毁 CAN 设备实例。");
|
||||
}
|
||||
}
|
||||
|
||||
// 同步清除 DeviceAndScopeDic 中的空条目
|
||||
_globalInfo.DeviceAndScopeDic.TryRemove(fingerprint, out _);
|
||||
}
|
||||
|
||||
DeviceMap.Clear();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
58
UIShare/GlobalVariable/GlobalInfo.cs
Normal file
58
UIShare/GlobalVariable/GlobalInfo.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using DeviceCommand.Base;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ZLGUSBCANFD;
|
||||
|
||||
namespace UIShare.GlobalVariable
|
||||
{
|
||||
public class GlobalInfo:BindableBase
|
||||
{
|
||||
public event EventHandler? ScopeChanged;
|
||||
public Dictionary<string,ScopedContext> ContextDic { get; set; }
|
||||
public Dictionary<string,StepRunning> StepRunningDic { get; set; }
|
||||
public Dictionary<string, SystemConfig> ConfigDic { get; set; }
|
||||
public Dictionary<string, IScopedProvider> ScopeDic { get; set; }
|
||||
|
||||
/// <summary>硬件指纹 → 设备实例的并发池,确保同一物理硬件全局只创建一个驱动实例。</summary>
|
||||
public ConcurrentDictionary<string, Lazy<IBaseInterface>> HardwarePool { get; set; }
|
||||
|
||||
/// <summary>CAN 硬件指纹 → ZLGCANFD 实例的并发池,确保同一 CAN 卡全局只创建一个驱动实例。</summary>
|
||||
public ConcurrentDictionary<string, Lazy<ZLGCANFD>> CanPool { get; set; }
|
||||
|
||||
/// <summary>硬件指纹 → 正在使用该设备的作用域名称列表,用于引用计数与安全销毁。</summary>
|
||||
public ConcurrentDictionary<string, Lazy<List<string>>> DeviceAndScopeDic { get; set; }
|
||||
|
||||
public String UserName { get; set; } = "Not Logged in";
|
||||
public bool IsAdmin { get; set; } = true;
|
||||
public string CurrentOpeningScope;
|
||||
private string _currentScope = "default";
|
||||
public string CurrentScope
|
||||
{
|
||||
get => _currentScope;
|
||||
set
|
||||
{
|
||||
if (_currentScope != value)
|
||||
{
|
||||
_currentScope = value;
|
||||
ScopeChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
public GlobalInfo()
|
||||
{
|
||||
ContextDic = new();
|
||||
StepRunningDic = new();
|
||||
ConfigDic = new();
|
||||
ScopeDic = new();
|
||||
HardwarePool = new ConcurrentDictionary<string, Lazy<IBaseInterface>>(StringComparer.OrdinalIgnoreCase);
|
||||
CanPool = new ConcurrentDictionary<string, Lazy<ZLGCANFD>>(StringComparer.OrdinalIgnoreCase);
|
||||
DeviceAndScopeDic = new ConcurrentDictionary<string, Lazy<List<string>>>(StringComparer.OrdinalIgnoreCase);
|
||||
CurrentScope = "default";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
214
UIShare/GlobalVariable/HardwareDataBroadcaster.cs
Normal file
214
UIShare/GlobalVariable/HardwareDataBroadcaster.cs
Normal file
@@ -0,0 +1,214 @@
|
||||
using Common.Attributes;
|
||||
using DeviceCommand.Base;
|
||||
using Model.Models;
|
||||
using Prism.Events;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Threading;
|
||||
using UIShare.PubEvent;
|
||||
|
||||
namespace UIShare.GlobalVariable
|
||||
{
|
||||
/// <summary>
|
||||
/// 硬件数据广播器(中间层,全局单例):
|
||||
/// 直接基于 GlobalInfo.HardwarePool 中的设备指纹字典进行统一采样,
|
||||
/// 每个物理设备只采样一次,然后向所有引用该设备的作用域分别广播 HardwareDataReportedEvent。
|
||||
/// 解决多作用域共享同一物理设备时的重复采样问题。
|
||||
/// DeviceCommand 层无需引用 Prism,完全解耦。
|
||||
/// </summary>
|
||||
public class HardwareDataBroadcaster : IDisposable
|
||||
{
|
||||
private readonly IEventAggregator _eventAggregator;
|
||||
private readonly GlobalInfo _globalInfo;
|
||||
|
||||
/// <summary>采样定时器</summary>
|
||||
private readonly DispatcherTimer _sampleTimer;
|
||||
|
||||
/// <summary>
|
||||
/// 已注册的采样项:(Fingerprint, MethodName, 编译后的委托, DeviceInstance)
|
||||
/// 委托签名统一为 Func<object, CancellationToken, Task<string>>
|
||||
/// - 第一个参数 = 设备实例(open delegate 风格,编译时已做类型转换)
|
||||
/// - 第二个参数 = CancellationToken(无参方法会忽略它)
|
||||
/// - 返回值 = Task<string>
|
||||
/// </summary>
|
||||
private readonly List<(string Fingerprint, string MethodName, Func<object, CancellationToken, Task<string>> Invoker, object Device)> _registeredMethods = new();
|
||||
|
||||
private CancellationTokenSource? _cts;
|
||||
private bool _disposed;
|
||||
|
||||
/// <summary>采样间隔(默认 1000ms)</summary>
|
||||
public TimeSpan SampleInterval
|
||||
{
|
||||
get => _sampleTimer.Interval;
|
||||
set => _sampleTimer.Interval = value;
|
||||
}
|
||||
|
||||
public HardwareDataBroadcaster(GlobalInfo globalInfo, IEventAggregator eventAggregator)
|
||||
{
|
||||
_globalInfo = globalInfo;
|
||||
_eventAggregator = eventAggregator;
|
||||
|
||||
_sampleTimer = new DispatcherTimer(DispatcherPriority.Normal)
|
||||
{
|
||||
Interval = TimeSpan.FromMilliseconds(1000)
|
||||
};
|
||||
_sampleTimer.Tick += OnSampleTick;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 MethodInfo 编译为强类型委托,后续调用零反射开销。
|
||||
/// 统一签名为 Func<object, CancellationToken, Task<string>>:
|
||||
/// - 无参方法:忽略 CancellationToken
|
||||
/// - 带 CancellationToken 方法:直接透传
|
||||
/// </summary>
|
||||
private static Func<object, CancellationToken, Task<string>> BuildInvoker(MethodInfo method)
|
||||
{
|
||||
var deviceParam = Expression.Parameter(typeof(object), "device");
|
||||
var ctParam = Expression.Parameter(typeof(CancellationToken), "ct");
|
||||
|
||||
// 将 object 转型为方法的声明类型
|
||||
var castDevice = Expression.Convert(deviceParam, method.DeclaringType!);
|
||||
|
||||
// 根据方法签名决定传参
|
||||
var parms = method.GetParameters();
|
||||
Expression[] callArgs = parms.Length == 1
|
||||
? new Expression[] { ctParam }
|
||||
: Array.Empty<Expression>();
|
||||
|
||||
var call = Expression.Call(castDevice, method, callArgs);
|
||||
|
||||
return Expression.Lambda<Func<object, CancellationToken, Task<string>>>(
|
||||
call, deviceParam, ctParam).Compile();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 扫描 GlobalInfo.HardwarePool 中所有已创建的物理设备,
|
||||
/// 反查可监测方法并编译为委托。每个指纹只注册一次,反射只执行一次。
|
||||
/// </summary>
|
||||
public void Discover()
|
||||
{
|
||||
_registeredMethods.Clear();
|
||||
|
||||
foreach (var poolEntry in _globalInfo.HardwarePool)
|
||||
{
|
||||
string fingerprint = poolEntry.Key;
|
||||
var lazy = poolEntry.Value;
|
||||
|
||||
// 只监控已经实例化的设备
|
||||
if (!lazy.IsValueCreated || lazy.Value == null) continue;
|
||||
|
||||
var device = lazy.Value;
|
||||
var deviceType = device.GetType();
|
||||
|
||||
var methods = deviceType.GetMethods(BindingFlags.Public | BindingFlags.Instance)
|
||||
.Where(m =>
|
||||
{
|
||||
if (m.GetCustomAttribute<MonitorableAttribute>() == null) return false;
|
||||
if (m.ReturnType != typeof(Task<string>)) return false;
|
||||
var parms = m.GetParameters();
|
||||
return parms.Length == 0 ||
|
||||
(parms.Length == 1 && parms[0].ParameterType == typeof(CancellationToken));
|
||||
});
|
||||
|
||||
foreach (var method in methods)
|
||||
{
|
||||
var invoker = BuildInvoker(method);
|
||||
_registeredMethods.Add((fingerprint, method.Name, invoker, device));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>获取指定硬件指纹当前被哪些作用域引用</summary>
|
||||
private List<string> GetScopesForFingerprint(string fingerprint)
|
||||
{
|
||||
if (_globalInfo.DeviceAndScopeDic.TryGetValue(fingerprint, out var lazy))
|
||||
{
|
||||
var scopeList = lazy.Value;
|
||||
lock (scopeList) return scopeList.ToList();
|
||||
}
|
||||
return new List<string>();
|
||||
}
|
||||
|
||||
/// <summary>启动采样广播(幂等)</summary>
|
||||
public void Start()
|
||||
{
|
||||
if (_disposed) return;
|
||||
_cts ??= new CancellationTokenSource();
|
||||
if (!_sampleTimer.IsEnabled) _sampleTimer.Start();
|
||||
}
|
||||
|
||||
/// <summary>停止采样</summary>
|
||||
public void Stop()
|
||||
{
|
||||
_sampleTimer.Stop();
|
||||
}
|
||||
|
||||
private void OnSampleTick(object? sender, EventArgs e)
|
||||
{
|
||||
if (_registeredMethods.Count == 0 || _disposed) return;
|
||||
|
||||
var token = _cts?.Token ?? CancellationToken.None;
|
||||
var now = DateTime.Now;
|
||||
|
||||
foreach (var entry in _registeredMethods)
|
||||
{
|
||||
// fire-and-forget:每个通道独立采样,完成后自行广播
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
// 直接委托调用,零反射开销
|
||||
string raw = await entry.Invoker(entry.Device, token).ConfigureAwait(false);
|
||||
|
||||
if (!double.TryParse(raw, out double value)) return;
|
||||
|
||||
// 向所有引用该物理设备的作用域分别广播
|
||||
var scopes = GetScopesForFingerprint(entry.Fingerprint);
|
||||
foreach (var scope in scopes)
|
||||
{
|
||||
_eventAggregator.GetEvent<HardwareDataReportedEvent>().Publish(new HardwareReportArgs
|
||||
{
|
||||
Scope = scope,
|
||||
HardwareFingerprint = entry.Fingerprint,
|
||||
MethodName = entry.MethodName,
|
||||
Value = value,
|
||||
Time = now
|
||||
});
|
||||
|
||||
// 同步检查该作用域 ValueLimitList 是否超限
|
||||
string MonitorStatus= ValueLimitAlarmHelper.CheckAlarm(scope, entry.Fingerprint, entry.MethodName, value, _globalInfo);
|
||||
if (MonitorStatus != "" && MonitorStatus != "未报警")
|
||||
{
|
||||
_eventAggregator.GetEvent<AlarmEvent>().Publish((scope,entry.Fingerprint, MonitorStatus));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 单个通道故障不干扰其他通道
|
||||
}
|
||||
}, token);
|
||||
}
|
||||
|
||||
// 不在 UI 线程 await——任务在线程池上自行完成并广播事件,
|
||||
// DispatcherTimer 按设定间隔准时触发下一次 Tick。
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
_sampleTimer.Stop();
|
||||
_sampleTimer.Tick -= OnSampleTick;
|
||||
try { _cts?.Cancel(); } catch { }
|
||||
_cts?.Dispose();
|
||||
_cts = null;
|
||||
_registeredMethods.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
48
UIShare/GlobalVariable/ScopeLogDispatcher.cs
Normal file
48
UIShare/GlobalVariable/ScopeLogDispatcher.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using Logger;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace UIShare.GlobalVariable
|
||||
{
|
||||
/// <summary>
|
||||
/// 作用域日志分发器:根据显式传入的 scope 参数把日志路由到对应
|
||||
/// <see cref="ScopedContext.LogBuffer"/>,实现每个台架/作用域拥有独立 LogArea。
|
||||
/// 直接写入 ConcurrentQueue,不走 Progress<T> / SynchronizationContext,避免 UI 线程压力。
|
||||
/// </summary>
|
||||
public class ScopeLogDispatcher : IProgress<(string scope, string message, string color, int depth)>
|
||||
{
|
||||
private readonly GlobalInfo _globalInfo;
|
||||
|
||||
/// <summary>Brush 缓存,避免每次都 new BrushConverter</summary>
|
||||
private static readonly ConcurrentDictionary<string, Brush> _brushCache = new();
|
||||
|
||||
public ScopeLogDispatcher(GlobalInfo globalInfo)
|
||||
{
|
||||
_globalInfo = globalInfo ?? throw new ArgumentNullException(nameof(globalInfo));
|
||||
}
|
||||
|
||||
public void Report((string scope, string message, string color, int depth) value)
|
||||
{
|
||||
var scope = value.scope;
|
||||
if (string.IsNullOrEmpty(scope)) return;
|
||||
|
||||
if (!_globalInfo.ContextDic.TryGetValue(scope, out var context)) return;
|
||||
|
||||
Brush brush = _brushCache.GetOrAdd(value.color, color =>
|
||||
{
|
||||
try
|
||||
{
|
||||
return (Brush)new BrushConverter().ConvertFromString(color);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return Brushes.Black;
|
||||
}
|
||||
});
|
||||
|
||||
// 直接入队到后台线程安全的 ConcurrentQueue,不走 SynchronizationContext
|
||||
context.LogBuffer.Enqueue((value.message, brush, value.depth));
|
||||
}
|
||||
}
|
||||
}
|
||||
55
UIShare/GlobalVariable/ScopedContext.cs
Normal file
55
UIShare/GlobalVariable/ScopedContext.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using DeviceCommand.Base;
|
||||
using MaterialDesignThemes.Wpf;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Media;
|
||||
using UIShare.UIViewModel;
|
||||
|
||||
namespace UIShare.GlobalVariable
|
||||
{
|
||||
public class ScopedContext
|
||||
{
|
||||
private static readonly Random _randomSeed = new Random();
|
||||
public ProgramVM Program { get; set; } = new();
|
||||
public String SelectedStepList { get; set; } = "主程序";
|
||||
public string CurrentFilePath { get; set; }
|
||||
public bool? IsStop { get; set; }
|
||||
public bool SingleStep { get; set; }
|
||||
public string RunState { get; set; } = "运行";
|
||||
public TimeSpan RunningTime { get; set; } = TimeSpan.Zero;
|
||||
public Stopwatch SW { get; set; } = new();
|
||||
public bool IsTerminate { get; set; } = false;
|
||||
public ObservableCollection<Assembly> Assemblies { get; set; } = new();
|
||||
public PackIconKind RunIcon { get; set; } = PackIconKind.Play;
|
||||
public StepVM SelectedStep { get; set; }
|
||||
public ParameterVM SelectedParameter { get; set; }
|
||||
|
||||
public List<IBaseInterface> DeviceList { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// 日志缓冲队列:后台线程(ScopeLogDispatcher)直接入队,
|
||||
/// UI 线程(DispatcherTimer)定时出队并刷新到 ObservableCollection。
|
||||
/// 不走 Progress<T> / SynchronizationContext,避免 Post 淹没 UI 消息队列。
|
||||
/// </summary>
|
||||
public ConcurrentQueue<(string Message, Brush Color, int Depth)> LogBuffer { get; } = new();
|
||||
|
||||
// 【新增测试属性】:每个实例被 new 出来时独一无二的随机身份
|
||||
// 证 ID
|
||||
public int DebugRandomId { get; private set; }
|
||||
public ScopedContext()
|
||||
{
|
||||
lock (_randomSeed)
|
||||
{
|
||||
// 每次诞生一个新上下文,就在 10000 到 99999 之间随机摇一个数
|
||||
DebugRandomId = _randomSeed.Next(10000, 100000);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
846
UIShare/GlobalVariable/StepRunning.cs
Normal file
846
UIShare/GlobalVariable/StepRunning.cs
Normal file
@@ -0,0 +1,846 @@
|
||||
using UIShare.UIViewModel;
|
||||
using UIShare.PubEvent;
|
||||
using Common.Tools;
|
||||
using Logger;
|
||||
using MaterialDesignThemes.Wpf;
|
||||
using Model.Entity;
|
||||
using Service.Interface;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UIShare.GlobalVariable;
|
||||
using static UIShare.UIViewModel.ParameterVM;
|
||||
|
||||
|
||||
namespace UIShare
|
||||
{
|
||||
public class StepRunning:IDisposable
|
||||
{
|
||||
private ScopedContext _scopedContext;
|
||||
private SystemConfig _systemConfig;
|
||||
private DeviceManager _deviceManager;
|
||||
//private Devices _devices;
|
||||
private IContainerProvider containerProvider;
|
||||
private IEventAggregator _eventAggregator;
|
||||
private ITestReportService _testReportService;
|
||||
|
||||
private readonly Dictionary<Guid, ParameterVM> tmpParameters = [];
|
||||
|
||||
private readonly Stopwatch stepStopwatch = new();
|
||||
|
||||
private readonly Stack<Stopwatch> loopStopwatchStack = new();
|
||||
|
||||
private readonly Stack<LoopContext> loopStack = new();
|
||||
|
||||
public CancellationTokenSource stepCTS = new();
|
||||
public CancellationTokenSource errorStepCTS = new();
|
||||
private bool SubSingleStep = false;
|
||||
|
||||
/// <summary>标记是否已被注销,执行方法据此安全退出</summary>
|
||||
private volatile bool _disposed = false;
|
||||
|
||||
public Guid TestRoundID;
|
||||
public StepRunning(ScopedContext ScopedContext, SystemConfig systemConfig,IEventAggregator eventAggregator, DeviceManager deviceManager, ITestReportService testReportService)
|
||||
{
|
||||
_scopedContext = ScopedContext;
|
||||
_systemConfig = systemConfig;
|
||||
_eventAggregator = eventAggregator;
|
||||
_deviceManager= deviceManager;
|
||||
_testReportService = testReportService;
|
||||
//_devices = containerProvider.Resolve<Devices>();
|
||||
}
|
||||
public async Task<bool> ExecuteErrorSteps(ProgramVM program, int depth = 0, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_disposed) return false;
|
||||
int index = 0;
|
||||
bool stepSuccess = false;
|
||||
if (depth == 0)
|
||||
{
|
||||
loopStack.Clear();
|
||||
loopStopwatchStack.Clear();
|
||||
ResetAllStepStatus(program.ErrorStepCollection);
|
||||
tmpParameters.Clear();
|
||||
TestRoundID = Guid.NewGuid();
|
||||
}
|
||||
foreach (var item in program.Parameters)
|
||||
{
|
||||
tmpParameters.TryAdd(item.ID, item);
|
||||
}
|
||||
|
||||
while (index < program.ErrorStepCollection.Count)
|
||||
{
|
||||
if (_disposed || cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
break;
|
||||
}
|
||||
var step = program.ErrorStepCollection[index];
|
||||
if (!step.IsUsed)
|
||||
{
|
||||
index++;
|
||||
continue;
|
||||
}
|
||||
|
||||
step.Result = 0;
|
||||
if (step.StepType == "循环开始")
|
||||
{
|
||||
var endStep = program.ErrorStepCollection.FirstOrDefault(x => x.LoopStartStepId == step.ID);
|
||||
if (endStep != null)
|
||||
{
|
||||
endStep.Result = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
LoggerHelper.ErrorWithNotify(_systemConfig.Title, "程序循环指令未闭合,请检查后重试");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理循环开始
|
||||
if (step.StepType == "循环开始")
|
||||
{
|
||||
Stopwatch loopStopwatch = new();
|
||||
loopStopwatch.Start();
|
||||
loopStopwatchStack.Push(loopStopwatch);
|
||||
var context = new LoopContext
|
||||
{
|
||||
LoopCount = step.LoopCount ?? 1,
|
||||
CurrentLoop = 0,
|
||||
StartIndex = index,
|
||||
LoopStartStep = step
|
||||
};
|
||||
loopStack.Push(context);
|
||||
step.CurrentLoopCount = context.LoopCount;
|
||||
LoggerHelper.InfoWithNotify(_systemConfig.Title, $"循环开始,共{context.LoopCount}次", depth);
|
||||
index++;
|
||||
await SaveStepRecordAsync(step, depth, true);
|
||||
}
|
||||
|
||||
// 处理循环结束
|
||||
else if (step.StepType == "循环结束")
|
||||
{
|
||||
if (loopStack.Count == 0)
|
||||
{
|
||||
LoggerHelper.ErrorWithNotify(_systemConfig.Title, "未匹配的循环结束指令", depth: depth);
|
||||
step.Result = 2;
|
||||
index++;
|
||||
await SaveStepRecordAsync(step, depth, true);
|
||||
continue;
|
||||
}
|
||||
|
||||
var context = loopStack.Peek();
|
||||
context.CurrentLoop++;
|
||||
|
||||
// 更新循环开始步骤的显示
|
||||
context.LoopStartStep!.CurrentLoopCount = context.LoopCount - context.CurrentLoop;
|
||||
|
||||
if (context.CurrentLoop < context.LoopCount)
|
||||
{
|
||||
// 继续循环:跳转到循环开始后的第一条指令
|
||||
index = context.StartIndex + 1;
|
||||
LoggerHelper.InfoWithNotify(_systemConfig.Title, $"循环第{context.CurrentLoop}次结束,跳回开始,剩余{context.LoopCount - context.CurrentLoop}次", depth);
|
||||
await SaveStepRecordAsync(step, depth, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 循环结束
|
||||
loopStack.Pop();
|
||||
var loopStopwatch = loopStopwatchStack.Peek();
|
||||
index++;
|
||||
LoggerHelper.InfoWithNotify(_systemConfig.Title, $"循环结束,共执行{context.LoopCount}次", depth);
|
||||
if (depth == 0 && loopStopwatch.IsRunning)
|
||||
{
|
||||
loopStopwatch.Stop();
|
||||
step.RunTime = (int)loopStopwatch.ElapsedMilliseconds;
|
||||
step.Result = 1;
|
||||
program.ErrorStepCollection.First(x => x.ID == step.LoopStartStepId).Result = 1;
|
||||
loopStopwatchStack.Pop();
|
||||
}
|
||||
await SaveStepRecordAsync(step, depth, true);
|
||||
}
|
||||
}
|
||||
|
||||
// 处理普通步骤
|
||||
else
|
||||
{
|
||||
if (depth == 0)
|
||||
{
|
||||
stepStopwatch.Restart();
|
||||
}
|
||||
|
||||
if (step.SubProgram != null)
|
||||
{
|
||||
if (_scopedContext.SingleStep)//子程序的单步执行将执行完保存下的所有Method
|
||||
{
|
||||
SubSingleStep = true;
|
||||
_scopedContext.SingleStep = false;
|
||||
}
|
||||
LoggerHelper.InfoWithNotify(_systemConfig.Title, $"开始执行子程序 [ {step.Index} ] [ {step.Name} ] ", depth);
|
||||
stepSuccess = await ExecuteSteps(step.SubProgram, depth + 1, cancellationToken);
|
||||
UpdateCurrentStepResult(step, true, stepSuccess, depth);
|
||||
if (SubSingleStep)
|
||||
{
|
||||
SubSingleStep = false;
|
||||
_scopedContext.SingleStep = true;
|
||||
}
|
||||
}
|
||||
else if (step.Method != null)
|
||||
{
|
||||
LoggerHelper.InfoWithNotify(_systemConfig.Title, $"开始执行指令 [ {step.Index} ] [ {step.Method!.FullName}.{step.Method.Name} ] ", depth);
|
||||
await ExecuteMethodStep(step, tmpParameters, depth, cancellationToken);
|
||||
stepSuccess = step.Result == 1;
|
||||
if (step.NGGotoStepID != null && !stepSuccess)
|
||||
{
|
||||
var tmp = program.ErrorStepCollection.FirstOrDefault(x => x.ID == step.NGGotoStepID);
|
||||
if (tmp != null)
|
||||
{
|
||||
index = tmp.Index - 2;
|
||||
LoggerHelper.InfoWithNotify(_systemConfig.Title, $"指令跳转 [ {tmp.Index} ] [ {tmp.Name} ]", depth);
|
||||
}
|
||||
}
|
||||
if (step.OKGotoStepID != null && stepSuccess)
|
||||
{
|
||||
var tmp = program.ErrorStepCollection.FirstOrDefault(x => x.ID == step.OKGotoStepID);
|
||||
if (tmp != null)
|
||||
{
|
||||
index = tmp.Index - 2;
|
||||
LoggerHelper.InfoWithNotify(_systemConfig.Title, $"指令跳转 [ {tmp.Index} ] [ {tmp.Name} ]", depth);
|
||||
}
|
||||
}
|
||||
}
|
||||
index++;
|
||||
|
||||
if (depth == 0 && stepStopwatch.IsRunning)
|
||||
{
|
||||
stepStopwatch.Stop();
|
||||
step.RunTime = (int)stepStopwatch.ElapsedMilliseconds;
|
||||
}
|
||||
await SaveStepRecordAsync(step, depth, true);
|
||||
}
|
||||
}
|
||||
|
||||
return loopStack.Count == 0 && stepSuccess;
|
||||
}
|
||||
public async Task<bool> ExecuteSteps(ProgramVM program, int depth = 0, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_disposed) return false;
|
||||
int index = 0;
|
||||
bool stepSuccess = false;
|
||||
if (depth == 0)
|
||||
{
|
||||
loopStack.Clear();
|
||||
loopStopwatchStack.Clear();
|
||||
ResetAllStepStatus(program.StepCollection);
|
||||
tmpParameters.Clear();
|
||||
TestRoundID = Guid.NewGuid();
|
||||
}
|
||||
foreach (var item in program.Parameters)
|
||||
{
|
||||
tmpParameters.TryAdd(item.ID, item);
|
||||
}
|
||||
|
||||
while (index < program.StepCollection.Count)
|
||||
{
|
||||
while (!_disposed && _scopedContext.IsStop == true)
|
||||
{
|
||||
await Task.Delay(50, cancellationToken);
|
||||
}
|
||||
if (_disposed || cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
break;
|
||||
}
|
||||
var step = program.StepCollection[index];
|
||||
if (!step.IsUsed)
|
||||
{
|
||||
index++;
|
||||
continue;
|
||||
}
|
||||
|
||||
step.Result = 0;
|
||||
if (step.StepType == "循环开始")
|
||||
{
|
||||
var endStep = program.StepCollection.FirstOrDefault(x => x.LoopStartStepId == step.ID);
|
||||
if (endStep != null)
|
||||
{
|
||||
endStep.Result = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
LoggerHelper.ErrorWithNotify(_systemConfig.Title, "程序循环指令未闭合,请检查后重试");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理循环开始
|
||||
if (step.StepType == "循环开始")
|
||||
{
|
||||
Stopwatch loopStopwatch = new();
|
||||
loopStopwatch.Start();
|
||||
loopStopwatchStack.Push(loopStopwatch);
|
||||
var context = new LoopContext
|
||||
{
|
||||
LoopCount = step.LoopCount ?? 1,
|
||||
CurrentLoop = 0,
|
||||
StartIndex = index,
|
||||
LoopStartStep = step
|
||||
};
|
||||
loopStack.Push(context);
|
||||
step.CurrentLoopCount = context.LoopCount;
|
||||
LoggerHelper.InfoWithNotify(_systemConfig.Title, $"循环开始({step.Name}),共{context.LoopCount}次", depth);
|
||||
index++;
|
||||
await SaveStepRecordAsync(step, depth, false);
|
||||
}
|
||||
|
||||
// 处理循环结束
|
||||
else if (step.StepType == "循环结束")
|
||||
{
|
||||
if (loopStack.Count == 0)
|
||||
{
|
||||
LoggerHelper.ErrorWithNotify(_systemConfig.Title, "未匹配的循环结束指令", depth:depth);
|
||||
step.Result = 2;
|
||||
index++;
|
||||
await SaveStepRecordAsync(step, depth, false);
|
||||
continue;
|
||||
}
|
||||
|
||||
var context = loopStack.Peek();
|
||||
context.CurrentLoop++;
|
||||
|
||||
// 更新循环开始步骤的显示
|
||||
context.LoopStartStep!.CurrentLoopCount = context.LoopCount - context.CurrentLoop;
|
||||
|
||||
if (context.CurrentLoop < context.LoopCount)
|
||||
{
|
||||
// 继续循环:跳转到循环开始后的第一条指令
|
||||
index = context.StartIndex + 1;
|
||||
LoggerHelper.InfoWithNotify(_systemConfig.Title, $"循环第{context.CurrentLoop}次结束,跳回开始,剩余{context.LoopCount - context.CurrentLoop}次", depth);
|
||||
await SaveStepRecordAsync(step, depth, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 循环结束
|
||||
loopStack.Pop();
|
||||
var loopStopwatch = loopStopwatchStack.Peek();
|
||||
index++;
|
||||
LoggerHelper.InfoWithNotify(_systemConfig.Title, $"循环结束,共执行{context.LoopCount}次", depth);
|
||||
if (depth == 0 && loopStopwatch.IsRunning)
|
||||
{
|
||||
loopStopwatch.Stop();
|
||||
step.RunTime = (int)loopStopwatch.ElapsedMilliseconds;
|
||||
step.Result = 1;
|
||||
program.StepCollection.First(x => x.ID == step.LoopStartStepId).Result = 1;
|
||||
loopStopwatchStack.Pop();
|
||||
}
|
||||
await SaveStepRecordAsync(step, depth, false);
|
||||
}
|
||||
}
|
||||
|
||||
// 处理普通步骤
|
||||
else
|
||||
{
|
||||
if (depth == 0)
|
||||
{
|
||||
stepStopwatch.Restart();
|
||||
}
|
||||
|
||||
if (step.SubProgram != null)
|
||||
{
|
||||
if (_scopedContext.SingleStep)//子程序的单步执行将执行完保存下的所有Method
|
||||
{
|
||||
SubSingleStep = true;
|
||||
_scopedContext.SingleStep = false;
|
||||
}
|
||||
LoggerHelper.InfoWithNotify(_systemConfig.Title, $"开始执行子程序 [ {step.Index} ] [ {step.Name} ] ", depth);
|
||||
stepSuccess = await ExecuteSteps(step.SubProgram, depth + 1, cancellationToken);
|
||||
UpdateCurrentStepResult(step, true, stepSuccess, depth);
|
||||
if (SubSingleStep)
|
||||
{
|
||||
SubSingleStep = false;
|
||||
_scopedContext.SingleStep = true;
|
||||
}
|
||||
}
|
||||
else if (step.Method != null)
|
||||
{
|
||||
LoggerHelper.InfoWithNotify(_systemConfig.Title, $"开始执行指令 [ {step.Index} ] [ {step.Method!.FullName}.{step.Method.Name} ] ", depth);
|
||||
await ExecuteMethodStep(step, tmpParameters, depth, cancellationToken);
|
||||
stepSuccess = step.Result == 1;
|
||||
if (step.NGGotoStepID != null && !stepSuccess)
|
||||
{
|
||||
var tmp = program.StepCollection.FirstOrDefault(x => x.ID == step.NGGotoStepID);
|
||||
if (tmp != null)
|
||||
{
|
||||
index = tmp.Index - 2;
|
||||
LoggerHelper.InfoWithNotify(_systemConfig.Title, $"指令跳转 [ {tmp.Index} ] [ {tmp.Name} ]", depth);
|
||||
}
|
||||
}
|
||||
if (step.OKGotoStepID != null && stepSuccess)
|
||||
{
|
||||
var tmp = program.StepCollection.FirstOrDefault(x => x.ID == step.OKGotoStepID);
|
||||
if (tmp != null)
|
||||
{
|
||||
index = tmp.Index - 2;
|
||||
LoggerHelper.InfoWithNotify(_systemConfig.Title, $"指令跳转 [ {tmp.Index} ] [ {tmp.Name} ]", depth);
|
||||
}
|
||||
}
|
||||
}
|
||||
index++;
|
||||
|
||||
if (depth == 0 && stepStopwatch.IsRunning)
|
||||
{
|
||||
stepStopwatch.Stop();
|
||||
step.RunTime = (int)stepStopwatch.ElapsedMilliseconds;
|
||||
}
|
||||
if (_scopedContext.SingleStep)
|
||||
{
|
||||
_scopedContext.IsStop = true;
|
||||
_scopedContext.RunState = "运行";
|
||||
_scopedContext.SingleStep = false;
|
||||
_eventAggregator.GetEvent<RunSingalCompletedEvent>().Publish("Play");
|
||||
}
|
||||
await SaveStepRecordAsync(step, depth, false);
|
||||
}
|
||||
}
|
||||
|
||||
return loopStack.Count == 0 && stepSuccess;
|
||||
}
|
||||
|
||||
public async Task ExecuteMethodStep(StepVM step, Dictionary<Guid, ParameterVM> parameters, int depth, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_disposed) return;
|
||||
try
|
||||
{
|
||||
if(_scopedContext.Program.StepCollection.Count>1)
|
||||
_scopedContext.SelectedStep = null;
|
||||
await Task.Delay(_systemConfig.PerformanceLevel, cancellationToken);
|
||||
|
||||
|
||||
// 1. 查找类型
|
||||
Type? targetType = null;
|
||||
foreach (var assembly in _scopedContext.Assemblies)
|
||||
{
|
||||
targetType = assembly.GetType(step.Method!.FullName!);
|
||||
if (targetType != null) break;
|
||||
}
|
||||
if (targetType == null)
|
||||
{
|
||||
LoggerHelper.ErrorWithNotify(_systemConfig.Title, $"指令 [ {step.Index} ] 执行错误:未找到类型 {step.Method!.FullName}", depth: depth);
|
||||
step.Result = 2;
|
||||
}
|
||||
|
||||
// 2. 创建实例(仅当方法不是静态时才需要)
|
||||
object? instance = null;
|
||||
bool isMethod = false;
|
||||
|
||||
// 3. 准备参数
|
||||
var inputParams = new List<object?>();
|
||||
var paramTypes = new List<Type>();
|
||||
ParameterVM? outputParam = null;
|
||||
foreach (var param in step.Method!.Parameters)
|
||||
{
|
||||
if (param.Category == ParameterCategory.Input)
|
||||
{
|
||||
if (param.Type == typeof(CancellationToken))
|
||||
{
|
||||
inputParams.Add(stepCTS.Token);
|
||||
paramTypes.Add(param.Type!);
|
||||
continue;
|
||||
}
|
||||
var actualValue = param.GetActualValue(tmpParameters);
|
||||
// 类型转换处理
|
||||
if (actualValue != null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(actualValue.ToString()))
|
||||
{
|
||||
actualValue = null;
|
||||
}
|
||||
if (actualValue != null && param.Type != null && actualValue.GetType() != param.Type)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (param.Type.IsArray)
|
||||
{
|
||||
// 获取数组元素类型
|
||||
Type elementType = param.Type.GetElementType()!;
|
||||
|
||||
// 解析字符串为字符串数组
|
||||
string[] stringArray = actualValue.ToString()!
|
||||
.Trim('[', ']')
|
||||
.Split(',', StringSplitOptions.RemoveEmptyEntries)
|
||||
.Select(s => s.Trim())
|
||||
.ToArray();
|
||||
|
||||
// 创建目标类型数组
|
||||
Array array = Array.CreateInstance(elementType, stringArray.Length);
|
||||
|
||||
// 转换每个元素
|
||||
for (int i = 0; i < stringArray.Length; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 特殊处理字符串类型
|
||||
if (elementType == typeof(string))
|
||||
{
|
||||
array.SetValue(stringArray[i], i);
|
||||
}
|
||||
// 特殊处理枚举类型
|
||||
else if (elementType.IsEnum)
|
||||
{
|
||||
array.SetValue(Enum.Parse(elementType, stringArray[i]), i);
|
||||
}
|
||||
// 常规类型转换
|
||||
else
|
||||
{
|
||||
if (stringArray[i] is string s && s.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// 先转成整数
|
||||
var intValue = Convert.ToInt64(s, 16);
|
||||
|
||||
// 再转成目标类型
|
||||
array.SetValue(Convert.ChangeType(intValue, elementType), i);
|
||||
}
|
||||
else
|
||||
{
|
||||
array.SetValue(Convert.ChangeType(stringArray[i], elementType), i);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw new InvalidCastException($"指令 [ {step.Index} ] 执行错误:元素 '{stringArray[i]}' 无法转换为 {elementType.Name}[]");
|
||||
}
|
||||
}
|
||||
actualValue = array;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (param.Type.BaseType == typeof(Enum))
|
||||
{
|
||||
actualValue = Enum.Parse(param.Type, param.Value!.ToString()!);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (actualValue is string s && s.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// 先转成整数
|
||||
var intValue = Convert.ToInt64(s, 16);
|
||||
|
||||
// 再转成目标类型
|
||||
actualValue = Convert.ChangeType(intValue, param.Type);
|
||||
}
|
||||
else
|
||||
{
|
||||
actualValue = Convert.ChangeType(actualValue, param.Type);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LoggerHelper.WarnWithNotify(_systemConfig.Title, $"指令 [ {step.Index} ] 执行错误:参数 {param.Name} 类型转换失败: {ex.Message}", depth: depth);
|
||||
}
|
||||
}
|
||||
}
|
||||
inputParams.Add(actualValue);
|
||||
paramTypes.Add(param.Type!);
|
||||
}
|
||||
else if (param.Category == ParameterCategory.Output)
|
||||
{
|
||||
outputParam = param;
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 获取方法
|
||||
var method = targetType!.GetMethod(
|
||||
step.Method.Name!,
|
||||
BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance,
|
||||
null,
|
||||
paramTypes.ToArray(),
|
||||
null
|
||||
);
|
||||
|
||||
if (method == null)
|
||||
{
|
||||
LoggerHelper.ErrorWithNotify(_systemConfig.Title, $"指令 [ {step.Index} ] 执行错误:未找到方法{step.Method.Name}", depth: depth);
|
||||
step.Result = 2;
|
||||
}
|
||||
|
||||
// 检查是否是静态方法
|
||||
bool isStaticMethod = method!.IsStatic;
|
||||
|
||||
// 如果是实例方法,需要创建实例
|
||||
if (!isStaticMethod)
|
||||
{
|
||||
try
|
||||
{
|
||||
if(targetType.Name== "ZLGCANFD")
|
||||
{
|
||||
instance = _deviceManager.CANFD;
|
||||
}
|
||||
else if (targetType.Name == "IOBoardGroup")
|
||||
{
|
||||
instance = _deviceManager.IOGroup;
|
||||
}
|
||||
else instance = _deviceManager.DeviceMap[targetType.Name];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LoggerHelper.ErrorWithNotify(_systemConfig.Title, $"指令 [ {step.Index} ] 执行错误:创建实例失败 - {ex.Message}", depth: depth);
|
||||
step.Result = 2;
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 执行方法
|
||||
|
||||
object? returnValue = method.Invoke(instance, inputParams.ToArray());
|
||||
try
|
||||
{
|
||||
// 处理异步方法
|
||||
if (returnValue is Task task)
|
||||
{
|
||||
await task.ConfigureAwait(false);
|
||||
// 获取结果(如果是Task<T>)
|
||||
if (task.GetType().IsGenericType)
|
||||
{
|
||||
var returnValueProperty = task.GetType().GetProperty("Result");
|
||||
returnValue = returnValueProperty?.GetValue(task);
|
||||
}
|
||||
else
|
||||
{
|
||||
returnValue = null;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理VoidTaskreturnValue类型
|
||||
if (returnValue != null && returnValue.GetType().FullName == "System.Threading.Tasks.VoidTaskreturnValue")
|
||||
{
|
||||
returnValue = null;
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LoggerHelper.ErrorWithNotify(_systemConfig.Title, $"指令 [ {step.Index} ] 执行错误: {ex.InnerException?.Message ?? ex.Message}", depth: depth);
|
||||
step.Result = 2;
|
||||
return;
|
||||
}
|
||||
|
||||
// 6. 处理输出
|
||||
bool paraResult = true; //记录参数上下限是否NG
|
||||
if (outputParam != null)
|
||||
{
|
||||
outputParam.Value = returnValue;
|
||||
var currentPara = outputParam.GetCurrentParameter(tmpParameters);
|
||||
if (currentPara != null)
|
||||
{
|
||||
currentPara.Value = returnValue;
|
||||
var tmp = currentPara.GetResult();
|
||||
currentPara.Result = tmp.Item1;
|
||||
paraResult = tmp.Item1;
|
||||
if (tmp.Item2 != null)
|
||||
{
|
||||
LoggerHelper.WarnWithNotify(_systemConfig.Title, tmp.Item2);
|
||||
}
|
||||
|
||||
}
|
||||
var returnType = returnValue?.GetType();
|
||||
if (returnType != null)
|
||||
{
|
||||
if (!returnType.IsArray)
|
||||
{
|
||||
LoggerHelper.SuccessWithNotify(_systemConfig.Title, $"输出 [ {outputParam.Name} ] = {returnValue} ({returnType.Name})", depth);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (returnValue is IEnumerable enumerable)
|
||||
{
|
||||
var elements = enumerable.Cast<object>().Select(item => item?.ToString() ?? "null");
|
||||
LoggerHelper.SuccessWithNotify(_systemConfig.Title, $"输出 [ {outputParam.Name} ] = [ {string.Join(", ", elements)} ] ({returnType.Name})", depth);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
LoggerHelper.SuccessWithNotify(_systemConfig.Title, $"指令 [ {step.Index} ] 执行成功", depth);
|
||||
UpdateCurrentStepResult(step, paraResult: paraResult, depth: depth);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LoggerHelper.ErrorWithNotify(_systemConfig.Title, $"指令 [ {step.Index} ] 执行错误: {ex.InnerException?.Message ?? ex.Message}", depth: depth);
|
||||
step.Result = 2;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将单个步骤的执行结果保存到数据库(测试报告)。
|
||||
/// 同一次运行的所有步骤共享 TestRoundID,导出时按此 Guid 查询。
|
||||
/// </summary>
|
||||
private async Task SaveStepRecordAsync(StepVM step, int depth, bool isErrorStep)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 获取输出参数值
|
||||
string? outputValue = null;
|
||||
if (step.Method != null)
|
||||
{
|
||||
var outputParam = step.Method.Parameters.FirstOrDefault(p => p.Category == ParameterCategory.Output);
|
||||
if (outputParam?.Value != null)
|
||||
outputValue = outputParam.Value.ToString();
|
||||
}
|
||||
|
||||
var entity = new TestReportEntity
|
||||
{
|
||||
TestRoundId = TestRoundID,
|
||||
Scope = _systemConfig.Title,
|
||||
FileName = _systemConfig.CurrentACPFile ?? "",
|
||||
StepIndex = step.Index,
|
||||
StepName = step.Name ?? "",
|
||||
StepType = step.StepType ?? "普通步骤",
|
||||
MethodName = step.Method?.Name,
|
||||
MethodFullName = step.Method?.FullName,
|
||||
Result = step.Result switch
|
||||
{
|
||||
-1 => "未执行",
|
||||
0 => "执行中",
|
||||
1 => "成功",
|
||||
2 => "失败",
|
||||
_ => "未知"
|
||||
},
|
||||
RunTimeMs = step.RunTime,
|
||||
OutputValue = outputValue,
|
||||
Depth = depth,
|
||||
IsErrorStep = isErrorStep,
|
||||
LoopRemaining = step.CurrentLoopCount,
|
||||
CreateTime = DateTime.Now
|
||||
};
|
||||
|
||||
var result = await _testReportService.InsertAsync(entity);
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
LoggerHelper.Error($"保存步骤记录失败 [{step.Index}]: {result.Msg}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LoggerHelper.Error($"保存步骤记录失败 [{step.Index}]: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public void ResetAllStepStatus(ObservableCollection<StepVM> StepCollection)
|
||||
{
|
||||
foreach (var step in StepCollection)
|
||||
{
|
||||
step.Result = -1;
|
||||
step.RunTime = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateCurrentStepResult(StepVM step, bool paraResult = true, bool stepResult = true, int depth = 0)
|
||||
{
|
||||
if (stepResult && paraResult)
|
||||
{
|
||||
if (string.IsNullOrEmpty(step.OKExpression))
|
||||
{
|
||||
step.Result = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
Dictionary<string, object> paraDic = [];
|
||||
foreach (var item in tmpParameters)
|
||||
{
|
||||
paraDic.TryAdd(item.Value.Name, item.Value.Value!);
|
||||
}
|
||||
if (step.SubProgram != null)
|
||||
{
|
||||
foreach (var item in step.SubProgram.Parameters.Where(x => x.Category == ParameterCategory.Output))
|
||||
{
|
||||
paraDic.TryAdd(item.Name, item.Value!);
|
||||
}
|
||||
}
|
||||
else if (step.Method != null)
|
||||
{
|
||||
foreach (var item in step.Method.Parameters.Where(x => x.Category == ParameterCategory.Output))
|
||||
{
|
||||
paraDic.TryAdd(item.Name, item.Value!);
|
||||
}
|
||||
}
|
||||
bool re = ExpressionEvaluator.EvaluateExpression(step.OKExpression, paraDic);
|
||||
step.Result = re ? 1 : 2;
|
||||
if (step.Result == 2)
|
||||
{
|
||||
LoggerHelper.WarnWithNotify(_systemConfig.Title, $"指令 [ {step.Index} ] NG:条件表达式验证失败", depth: depth);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!paraResult)
|
||||
{
|
||||
LoggerHelper.WarnWithNotify(_systemConfig.Title, "参数限值校验失败", depth: depth);
|
||||
}
|
||||
step.Result = 2;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
|
||||
// 1. 唤醒暂停循环(IsStop 可能卡住后台线程)
|
||||
try
|
||||
{
|
||||
if (_scopedContext != null)
|
||||
{
|
||||
_scopedContext.IsStop = false;
|
||||
_scopedContext.IsTerminate = true;
|
||||
_scopedContext.RunState = "运行";
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
try
|
||||
{
|
||||
if (stepCTS != null && !stepCTS.IsCancellationRequested) stepCTS.Cancel();
|
||||
}
|
||||
catch (ObjectDisposedException) { }
|
||||
|
||||
try
|
||||
{
|
||||
if (errorStepCTS != null && !errorStepCTS.IsCancellationRequested) errorStepCTS.Cancel();
|
||||
}
|
||||
catch (ObjectDisposedException) { }
|
||||
|
||||
tmpParameters.Clear();
|
||||
loopStack.Clear();
|
||||
loopStopwatchStack.Clear();
|
||||
stepStopwatch.Stop();
|
||||
}
|
||||
|
||||
|
||||
|
||||
#region 私有类
|
||||
|
||||
private class LoopContext
|
||||
{
|
||||
public int LoopCount { get; set; }
|
||||
public int CurrentLoop { get; set; }
|
||||
public int StartIndex { get; set; }
|
||||
public StepVM? LoopStartStep { get; set; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
175
UIShare/GlobalVariable/SystemConfig.cs
Normal file
175
UIShare/GlobalVariable/SystemConfig.cs
Normal file
@@ -0,0 +1,175 @@
|
||||
using Logger;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UIShare.UIViewModel;
|
||||
using ZLGUSBCANFD;
|
||||
using static UIShare.UIViewModel.ParameterVM;
|
||||
|
||||
namespace UIShare.GlobalVariable
|
||||
{
|
||||
public class SystemConfig
|
||||
{
|
||||
[JsonIgnore]
|
||||
public string SystemPath { get; set; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ACP");
|
||||
[JsonIgnore]
|
||||
public string DLLFilePath { get; set; } = @"D:\ACP\指令\";
|
||||
[JsonIgnore]
|
||||
public string TSMasterName { get; set; } = "ACP测试上位机";
|
||||
public string SubProgramFilePath { get; set; } = @"D:\ACP\子程序\";
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string CurrentACPFile { get; set; }
|
||||
public int PerformanceLevel { get; set; } = 50;
|
||||
public string DefaultProgramFilePath { get; set; } = "";
|
||||
public string DefaultBLFFilePath { get; set; } = "";
|
||||
public string DefaultDBCFilePath { get; set; } = "";
|
||||
public ObservableCollection<DeviceInfoVM> DeviceList = new();
|
||||
public ObservableCollection<SharedParameter> SharedParameterList = new();
|
||||
public ObservableCollection<CANSignalConfig> ConfigurationList = new();
|
||||
public ObservableCollection<AutoDBCLoadItem> DBCAutoLoadList = new();
|
||||
public ObservableCollection<ValueLimitVM> ValueLimitList = new();
|
||||
[JsonIgnore]
|
||||
public ObservableCollection<MonitorChannelVM> Channels = new();
|
||||
/// <summary>
|
||||
/// 监测通道持久化配置列表(包含 Fingerprint、MethodName、IsDisplayed)
|
||||
/// </summary>
|
||||
public ObservableCollection<MonitorChannelConfig> MonitorChannels = new();
|
||||
public ZLGCANFD CANFD = new();
|
||||
[JsonIgnore]
|
||||
public ObservableCollection<ParameterVM> ParameterList = new()
|
||||
{
|
||||
new ParameterVM
|
||||
{
|
||||
Category = ParameterCategory.Input,
|
||||
Type = typeof(int),
|
||||
Name = "台架序号",
|
||||
Value = 1,
|
||||
IsEditable=false
|
||||
},
|
||||
new ParameterVM
|
||||
{
|
||||
Category = ParameterCategory.Input,
|
||||
Type = typeof(int),
|
||||
Name = "直流负载通道",
|
||||
Value = 1,
|
||||
IsEditable=false
|
||||
},
|
||||
new ParameterVM
|
||||
{
|
||||
Category = ParameterCategory.Input,
|
||||
Type = typeof(int),
|
||||
Name = "交流电源通道",
|
||||
Value = 1,
|
||||
IsEditable=false
|
||||
},
|
||||
new ParameterVM
|
||||
{
|
||||
Category = ParameterCategory.Input,
|
||||
Type = typeof(int),
|
||||
Name = "CAN通道",
|
||||
Value = 0,
|
||||
IsEditable=false
|
||||
},
|
||||
new ParameterVM
|
||||
{
|
||||
Category = ParameterCategory.Input,
|
||||
Type = typeof(int),
|
||||
Name = "示波器通道1",
|
||||
Value = 0,
|
||||
IsEditable=false
|
||||
},
|
||||
new ParameterVM
|
||||
{
|
||||
Category = ParameterCategory.Input,
|
||||
Type = typeof(int),
|
||||
Name = "示波器通道2",
|
||||
Value = 0,
|
||||
IsEditable=false
|
||||
},
|
||||
new ParameterVM
|
||||
{
|
||||
Category = ParameterCategory.Input,
|
||||
Type = typeof(int),
|
||||
Name = "功率分析仪通道1",
|
||||
Value = 0,
|
||||
IsEditable=false
|
||||
},
|
||||
new ParameterVM
|
||||
{
|
||||
Category = ParameterCategory.Input,
|
||||
Type = typeof(int),
|
||||
Name = "功率分析仪通道2",
|
||||
Value = 0,
|
||||
IsEditable=false
|
||||
},
|
||||
};
|
||||
// public ObservableCollection<DeviceInfoVM> DeviceList { get; set; } = new()
|
||||
//{
|
||||
// new DeviceInfoVM
|
||||
// {
|
||||
// DeviceName = "IT7800E",
|
||||
// DeviceType = "IT7800E",
|
||||
// Remark = "交流可编程电源供应器",
|
||||
// ConnectionType = "Tcp",
|
||||
// IsEnabled = true,
|
||||
// IsConnected = false
|
||||
// },
|
||||
|
||||
// new DeviceInfoVM
|
||||
// {
|
||||
// DeviceName = "N36200",
|
||||
// DeviceType = "N36200",
|
||||
// Remark = "宽范围可编程直流电源",
|
||||
// ConnectionType = "Tcp",
|
||||
// IsEnabled = true,
|
||||
// IsConnected = false
|
||||
// },
|
||||
|
||||
// new DeviceInfoVM
|
||||
// {
|
||||
// DeviceName = "N36600",
|
||||
// DeviceType = "N36600",
|
||||
// Remark = "便携式宽范围可编程直流电源",
|
||||
// ConnectionType = "Tcp",
|
||||
// IsEnabled = false,
|
||||
// IsConnected = false
|
||||
// },
|
||||
|
||||
// new DeviceInfoVM
|
||||
// {
|
||||
// DeviceName = "N69200",
|
||||
// DeviceType = "N69200",
|
||||
// Remark = "可编程直流电子负载",
|
||||
// ConnectionType = "Tcp",
|
||||
// IsEnabled = true,
|
||||
// IsConnected = false
|
||||
// },
|
||||
|
||||
// new DeviceInfoVM
|
||||
// {
|
||||
// DeviceName = "SDS2000X_HD",
|
||||
// DeviceType = "SDS2000X_HD",
|
||||
// Remark = "数字存储示波器",
|
||||
// ConnectionType = "Tcp",
|
||||
// IsEnabled = true,
|
||||
// IsConnected = false
|
||||
// },
|
||||
|
||||
// new DeviceInfoVM
|
||||
// {
|
||||
// DeviceName = "SPAW7000",
|
||||
// DeviceType = "SPAW7000",
|
||||
// Remark = "功率分析记录仪",
|
||||
// ConnectionType = "Tcp",
|
||||
// IsEnabled = true,
|
||||
// IsConnected = false
|
||||
// }
|
||||
//};
|
||||
}
|
||||
}
|
||||
56
UIShare/GlobalVariable/ValueLimitAlarmHelper.cs
Normal file
56
UIShare/GlobalVariable/ValueLimitAlarmHelper.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System.Linq;
|
||||
using UIShare.UIViewModel;
|
||||
|
||||
namespace UIShare.GlobalVariable
|
||||
{
|
||||
/// <summary>
|
||||
/// 值限制报警检查辅助类:供各广播器在采样到信号值时统一判断是否超限。
|
||||
/// </summary>
|
||||
public static class ValueLimitAlarmHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 根据指定作用域的 ValueLimitList 检查当前值是否超限,并更新报警状态。
|
||||
/// </summary>
|
||||
/// <param name="scope">作用域名称</param>
|
||||
/// <param name="fingerprint">硬件指纹</param>
|
||||
/// <param name="methodName">方法名/信号标识</param>
|
||||
/// <param name="value">当前采样值</param>
|
||||
/// <param name="globalInfo">全局信息</param>
|
||||
public static string CheckAlarm(string scope, string fingerprint, string methodName, double value, GlobalInfo globalInfo)
|
||||
{
|
||||
if (globalInfo?.ConfigDic == null) return "";
|
||||
if (!globalInfo.ConfigDic.TryGetValue(scope, out var systemConfig)) return "";
|
||||
if (systemConfig.ValueLimitList == null) return "";
|
||||
|
||||
var limit = systemConfig.ValueLimitList.FirstOrDefault(x =>
|
||||
x.Fingerprint == fingerprint && x.MethodName == methodName);
|
||||
if (limit == null) return "";
|
||||
if (value > limit.UpperExtreme)
|
||||
{
|
||||
limit.IsAlarm = true;
|
||||
limit.AlarmSatus = AlarmStatus.超上极限;
|
||||
}
|
||||
else if (value < limit.LowerExtreme)
|
||||
{
|
||||
limit.IsAlarm = true;
|
||||
limit.AlarmSatus = AlarmStatus.超下极限;
|
||||
}
|
||||
else if (value > limit.Upper)
|
||||
{
|
||||
limit.IsAlarm = true;
|
||||
limit.AlarmSatus = AlarmStatus.超上限;
|
||||
}
|
||||
else if (value < limit.Lower)
|
||||
{
|
||||
limit.IsAlarm = true;
|
||||
limit.AlarmSatus = AlarmStatus.超下限;
|
||||
}
|
||||
else
|
||||
{
|
||||
limit.IsAlarm = false;
|
||||
limit.AlarmSatus = AlarmStatus.未报警;
|
||||
}
|
||||
return limit.AlarmSatus.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user