CAN设置添加

This commit is contained in:
hsc
2026-07-02 15:36:55 +08:00
parent 63f43853fa
commit 60dc155ccf
20 changed files with 1250 additions and 413 deletions

View File

@@ -1,7 +1,10 @@
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Logger;
using UIShare.UIViewModel;
namespace UIShare.GlobalVariable
{
@@ -44,6 +47,7 @@ namespace UIShare.GlobalVariable
{
// 如果不存在,创建一个带 Title 的默认配置并保存
var defaultConfig = new SystemConfig { Title = title };
EnsureDefaultCanDevice(defaultConfig);
Save(defaultConfig);
return defaultConfig;
}
@@ -58,12 +62,16 @@ namespace UIShare.GlobalVariable
TypeNameHandling = TypeNameHandling.All
});
return config ?? new SystemConfig { Title = title };
config ??= new SystemConfig { Title = title };
EnsureDefaultCanDevice(config);
return config;
}
catch (Exception ex)
{
LoggerHelper.ErrorWithNotify(title, $"格子 [{title}] 配置加载失败: {ex.Message}");
return new SystemConfig { Title = title };
var fallback = new SystemConfig { Title = title };
EnsureDefaultCanDevice(fallback);
return fallback;
}
}
}
@@ -98,5 +106,34 @@ namespace UIShare.GlobalVariable
}
}
}
/// <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
});
}
}
}
}