自动加载dbc
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
using DeviceCommand.Base;
|
||||
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;
|
||||
|
||||
@@ -22,6 +25,7 @@ namespace UIShare.GlobalVariable
|
||||
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; }
|
||||
@@ -31,10 +35,11 @@ namespace UIShare.GlobalVariable
|
||||
private static readonly IReadOnlyDictionary<string, Type> _deviceTypeMap = BuildDeviceTypeMap();
|
||||
public ZLGCANFD CANFD { get; set; }
|
||||
|
||||
public DeviceManager(SystemConfig systemConfig, GlobalInfo globalInfo)
|
||||
public DeviceManager(SystemConfig systemConfig, GlobalInfo globalInfo, IEventAggregator eventAggregator)
|
||||
{
|
||||
_systemConfig = systemConfig;
|
||||
_globalInfo = globalInfo;
|
||||
_eventAggregator = eventAggregator;
|
||||
// 用 SystemConfig.Title 作为作用域唯一标识,无需反查 ConfigDic
|
||||
_scopeName = _systemConfig.Title;
|
||||
InitDevices();
|
||||
@@ -407,7 +412,7 @@ namespace UIShare.GlobalVariable
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打开 CAN 卡:打开设备 + 初始化并启动所有通道
|
||||
/// 打开 CAN 卡:打开设备 + 初始化并启动所有通道 + 自动加载 DBC
|
||||
/// </summary>
|
||||
private async Task<bool> ConnectCanAsync(DeviceInfoVM info)
|
||||
{
|
||||
@@ -431,6 +436,55 @@ namespace UIShare.GlobalVariable
|
||||
bool ok = await Task.Run(() =>
|
||||
{
|
||||
if (!CANFD.打开设备()) return false;
|
||||
|
||||
// 初始化并启动所有通道
|
||||
int maxCh = CANFD.DBCParser.MaxChannels;
|
||||
for (uint i = 0; i < maxCh; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
CANFD.初始化并启动通道(i);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LoggerHelper.Warn($"CAN 通道 {i} 初始化失败:{ex.Message}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 自动加载 DBC 文件
|
||||
if (_systemConfig?.DBCAutoLoadList != null)
|
||||
{
|
||||
foreach (var item in _systemConfig.DBCAutoLoadList)
|
||||
{
|
||||
if (item.DBCChannel < 0 || item.DBCChannel >= maxCh) continue;
|
||||
if (string.IsNullOrWhiteSpace(item.DBCFilePath)) continue;
|
||||
|
||||
if (!File.Exists(item.DBCFilePath))
|
||||
{
|
||||
// 文件不存在,发布事件广播
|
||||
_eventAggregator.GetEvent<DBCFileNotFoundEvent>().Publish(new DBCFileNotFoundArgs
|
||||
{
|
||||
Channel = item.DBCChannel,
|
||||
FilePath = item.DBCFilePath,
|
||||
Scope = _scopeName
|
||||
});
|
||||
LoggerHelper.Warn($"CAN 通道 {item.DBCChannel} 自动加载 DBC 失败:文件不存在 [{item.DBCFilePath}]");
|
||||
continue;
|
||||
}
|
||||
|
||||
bool loadOk = CANFD.加载通道DBC文件((uint)item.DBCChannel, item.DBCFilePath);
|
||||
if (loadOk)
|
||||
{
|
||||
LoggerHelper.Info($"CAN 通道 {item.DBCChannel} 已自动加载 DBC:{item.DBCFilePath}");
|
||||
}
|
||||
else
|
||||
{
|
||||
LoggerHelper.Warn($"CAN 通道 {item.DBCChannel} 自动加载 DBC 失败:{item.DBCFilePath}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace UIShare.GlobalVariable
|
||||
public ObservableCollection<DeviceInfoVM> DeviceList = new();
|
||||
public ObservableCollection<SharedParameter> SharedParameterList = new();
|
||||
public ObservableCollection<CANSignalConfig> ConfigurationList = new();
|
||||
public ObservableCollection<AutoDBCLoadItem> dbcAutoLoadList = new();
|
||||
public ObservableCollection<AutoDBCLoadItem> DBCAutoLoadList = new();
|
||||
public ObservableCollection<ValueLimitVM> ValueLimitList = new();
|
||||
[JsonIgnore]
|
||||
public ObservableCollection<MonitorChannelVM> Channels = new();
|
||||
|
||||
17
UIShare/PubEvent/DBCFileNotFoundEvent.cs
Normal file
17
UIShare/PubEvent/DBCFileNotFoundEvent.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace UIShare.PubEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// DBC 自动加载时找不到文件事件。
|
||||
/// 参数:(通道号, 文件路径)
|
||||
/// </summary>
|
||||
public class DBCFileNotFoundEvent : PubSubEvent<DBCFileNotFoundArgs>
|
||||
{
|
||||
}
|
||||
|
||||
public class DBCFileNotFoundArgs
|
||||
{
|
||||
public int Channel { get; set; }
|
||||
public string FilePath { get; set; } = string.Empty;
|
||||
public string Scope { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user