添加项目文件。

This commit is contained in:
“hsc”
2026-07-29 13:12:27 +08:00
parent d6da766e2d
commit 8cf45d36b9
297 changed files with 35814 additions and 0 deletions

View File

@@ -0,0 +1,134 @@
using System.Collections.ObjectModel;
using System.Windows.Input;
using UIShare.PubEvent;
using UIShare.UIViewModel;
using UIShare.ViewModelBase;
namespace SettingModule.ViewModels.Dialogs
{
/// <summary>
/// CAN 连接配置对话框 VM。
/// 通过 DialogParameters 接收宿主 DeviceInfoVM保存时把副本写回宿主。
/// 参数对应 ZLGCANFD 构造函数 + 初始化并启动通道 方法。
/// </summary>
public class CANConfigViewModel : DialogViewModelBase
{
#region
private string _title = "CAN 连接配置";
public string Title
{
get => _title;
set => SetProperty(ref _title, value);
}
/// <summary>编辑用的副本,取消时不会污染宿主对象。</summary>
private CANConfigVM _config = new();
public CANConfigVM Config
{
get => _config;
set => SetProperty(ref _config, value);
}
/// <summary>常用仲裁域波特率。</summary>
public ObservableCollection<string> CommonABitBauds { get; } = new()
{
"250000", "500000", "1000000"
};
/// <summary>常用数据域波特率。</summary>
public ObservableCollection<string> CommonDBitBauds { get; } = new()
{
"1000000", "2000000", "4000000", "5000000", "8000000"
};
private string _errorMessage = string.Empty;
public string ErrorMessage
{
get => _errorMessage;
set => SetProperty(ref _errorMessage, value);
}
#endregion
#region
public ICommand SaveCommand { get; }
public ICommand CancelCommand { get; }
#endregion
// 用于保存时把副本回写到原对象
private DeviceInfoVM? _hostDevice;
public CANConfigViewModel(IContainerProvider containerProvider) : base(containerProvider)
{
SaveCommand = new DelegateCommand(OnSave);
CancelCommand = new DelegateCommand(OnCancel);
}
private bool Validate(out string error)
{
error = string.Empty;
if (Config.DeviceType == 0)
{
error = "设备类型号不能为 0";
return false;
}
if (string.IsNullOrWhiteSpace(Config.ABitBaud))
{
error = "仲裁域波特率不能为空";
return false;
}
if (string.IsNullOrWhiteSpace(Config.DBitBaud))
{
error = "数据域波特率不能为空";
return false;
}
return true;
}
private void OnSave()
{
if (!Validate(out var error))
{
ErrorMessage = error;
return;
}
ErrorMessage = string.Empty;
// 把副本写回宿主
if (_hostDevice != null)
{
_hostDevice.CANConfig ??= new CANConfigVM();
Config.CopyTo(_hostDevice.CANConfig);
_hostDevice.ConnectionType = "CAN";
}
RequestClose.Invoke(ButtonResult.OK);
}
private void OnCancel() => RequestClose.Invoke(ButtonResult.Cancel);
#region Prism Dialog
public override void OnDialogOpened(IDialogParameters parameters)
{
_eventAggregator.GetEvent<OverlayEvent>().Publish(true);
if (parameters.ContainsKey("Device"))
{
_hostDevice = parameters.GetValue<DeviceInfoVM>("Device");
Title = $"CAN 连接配置 - {_hostDevice?.DeviceName}";
Config = new CANConfigVM(_hostDevice?.CANConfig);
}
else if (parameters.ContainsKey("Config"))
{
Config = new CANConfigVM(parameters.GetValue<CANConfigVM>("Config"));
}
}
public override void OnDialogClosed()
{
_eventAggregator.GetEvent<OverlayEvent>().Publish(false);
}
#endregion
}
}

View File

@@ -0,0 +1,164 @@
using System.Collections.ObjectModel;
using System.IO.Ports;
using System.Linq;
using System.Windows.Input;
using UIShare.PubEvent;
using UIShare.UIViewModel;
using UIShare.ViewModelBase;
namespace SettingModule.ViewModels.Dialogs
{
/// <summary>
/// 串口连接配置对话框 VM。
/// 通过 DialogParameters 接收宿主 DeviceInfoVM保存时把副本写回宿主。
/// </summary>
public class SerialPortConfigViewModel : DialogViewModelBase
{
#region
private string _title = "串口连接配置";
public string Title
{
get => _title;
set => SetProperty(ref _title, value);
}
private SerialPortConfigVM _config = new();
public SerialPortConfigVM Config
{
get => _config;
set => SetProperty(ref _config, value);
}
/// <summary>当前可用串口列表OnDialogOpened 刷新)。</summary>
public ObservableCollection<string> AvailablePorts { get; } = new();
public ObservableCollection<int> CommonBaudRates { get; } = new()
{
1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200, 230400, 460800, 921600
};
public ObservableCollection<int> DataBitsList { get; } = new() { 5, 6, 7, 8 };
public ObservableCollection<string> StopBitsList { get; } = new()
{
"One", "OnePointFive", "Two"
};
public ObservableCollection<string> ParityList { get; } = new()
{
"None", "Odd", "Even", "Mark", "Space"
};
public ObservableCollection<int> CommonTimeouts { get; } = new()
{
500, 1000, 2000, 3000, 5000, 10000
};
private string _errorMessage = string.Empty;
public string ErrorMessage
{
get => _errorMessage;
set => SetProperty(ref _errorMessage, value);
}
#endregion
#region
public ICommand SaveCommand { get; }
public ICommand CancelCommand { get; }
public ICommand RefreshPortsCommand { get; }
#endregion
private DeviceInfoVM? _hostDevice;
public SerialPortConfigViewModel(IContainerProvider containerProvider) : base(containerProvider)
{
SaveCommand = new DelegateCommand(OnSave);
CancelCommand = new DelegateCommand(OnCancel);
RefreshPortsCommand = new DelegateCommand(RefreshPorts);
}
private void RefreshPorts()
{
AvailablePorts.Clear();
// 若当前选中的串口不在可用列表中,仍保留显示,便于离线编辑
if (!string.IsNullOrEmpty(Config.PortName) && !AvailablePorts.Contains(Config.PortName))
AvailablePorts.Insert(0, Config.PortName);
}
private bool Validate(out string error)
{
error = string.Empty;
if (string.IsNullOrWhiteSpace(Config.PortName))
{
error = "串口名称不能为空";
return false;
}
if (Config.BaudRate <= 0)
{
error = "波特率必须大于 0";
return false;
}
if (Config.DataBits < 5 || Config.DataBits > 8)
{
error = "数据位必须在 5 - 8 之间";
return false;
}
if (Config.ReadTimeout < 0 || Config.WriteTimeout < 0)
{
error = "超时时间不能为负数";
return false;
}
return true;
}
private void OnSave()
{
if (!Validate(out var error))
{
ErrorMessage = error;
return;
}
ErrorMessage = string.Empty;
if (_hostDevice != null)
{
_hostDevice.SerialPortConfig ??= new SerialPortConfigVM();
Config.CopyTo(_hostDevice.SerialPortConfig);
_hostDevice.ConnectionType = "Serial";
}
RequestClose.Invoke(ButtonResult.OK);
}
private void OnCancel() => RequestClose.Invoke(ButtonResult.Cancel);
#region Prism Dialog
public override void OnDialogOpened(IDialogParameters parameters)
{
_eventAggregator.GetEvent<OverlayEvent>().Publish(true);
if (parameters.ContainsKey("Device"))
{
_hostDevice = parameters.GetValue<DeviceInfoVM>("Device");
Title = $"串口连接配置 - {_hostDevice?.DeviceName}";
Config = new SerialPortConfigVM(_hostDevice?.SerialPortConfig);
}
else if (parameters.ContainsKey("Config"))
{
Config = new SerialPortConfigVM(parameters.GetValue<SerialPortConfigVM>("Config"));
}
RefreshPorts();
}
public override void OnDialogClosed()
{
_eventAggregator.GetEvent<OverlayEvent>().Publish(false);
}
#endregion
}
}

View File

@@ -0,0 +1,139 @@
using System.Collections.ObjectModel;
using System.Windows.Input;
using UIShare.PubEvent;
using UIShare.UIViewModel;
using UIShare.ViewModelBase;
namespace SettingModule.ViewModels.Dialogs
{
/// <summary>
/// Tcp 连接配置对话框 VM。
/// 通过 DialogParameters 接收宿主 DeviceInfoVM保存时把副本写回宿主。
/// </summary>
public class TCPConfigViewModel : DialogViewModelBase
{
#region
private string _title = "Tcp 连接配置";
public string Title
{
get => _title;
set => SetProperty(ref _title, value);
}
/// <summary>编辑用的副本,取消时不会污染宿主对象。</summary>
private TcpConfigVM _config = new();
public TcpConfigVM Config
{
get => _config;
set => SetProperty(ref _config, value);
}
/// <summary>常用 Modbus / 自定义协议端口建议。</summary>
public ObservableCollection<int> CommonPorts { get; } = new()
{
502, 102, 80, 8080, 5020, 4840
};
/// <summary>常用超时(毫秒)。</summary>
public ObservableCollection<int> CommonTimeouts { get; } = new()
{
500, 1000, 2000, 3000, 5000, 10000
};
private string _errorMessage = string.Empty;
public string ErrorMessage
{
get => _errorMessage;
set => SetProperty(ref _errorMessage, value);
}
#endregion
#region
public ICommand SaveCommand { get; }
public ICommand CancelCommand { get; }
#endregion
// 用于保存时把副本回写到原对象
private DeviceInfoVM? _hostDevice;
public TCPConfigViewModel(IContainerProvider containerProvider) : base(containerProvider)
{
SaveCommand = new DelegateCommand(OnSave);
CancelCommand = new DelegateCommand(OnCancel);
}
private bool Validate(out string error)
{
error = string.Empty;
if (string.IsNullOrWhiteSpace(Config.IPAddress))
{
error = "IP 地址不能为空";
return false;
}
if (!System.Net.IPAddress.TryParse(Config.IPAddress, out _))
{
error = "IP 地址格式不正确";
return false;
}
if (Config.Port <= 0 || Config.Port > 65535)
{
error = "端口范围应在 1 - 65535";
return false;
}
if (Config.SendTimeout < 0 || Config.ReceiveTimeout < 0)
{
error = "超时时间不能为负数";
return false;
}
return true;
}
private void OnSave()
{
if (!Validate(out var error))
{
ErrorMessage = error;
return;
}
ErrorMessage = string.Empty;
// 把副本写回宿主
if (_hostDevice != null)
{
_hostDevice.TcpConfig ??= new TcpConfigVM();
Config.CopyTo(_hostDevice.TcpConfig);
_hostDevice.ConnectionType = "Tcp";
}
RequestClose.Invoke(ButtonResult.OK);
}
private void OnCancel() => RequestClose.Invoke(ButtonResult.Cancel);
#region Prism Dialog
public override void OnDialogOpened(IDialogParameters parameters)
{
_eventAggregator.GetEvent<OverlayEvent>().Publish(true);
if (parameters.ContainsKey("Device"))
{
_hostDevice = parameters.GetValue<DeviceInfoVM>("Device");
Title = $"Tcp 连接配置 - {_hostDevice?.DeviceName}";
Config = new TcpConfigVM(_hostDevice?.TcpConfig);
}
else if (parameters.ContainsKey("Config"))
{
Config = new TcpConfigVM(parameters.GetValue<TcpConfigVM>("Config"));
}
}
public override void OnDialogClosed()
{
_eventAggregator.GetEvent<OverlayEvent>().Publish(false);
}
#endregion
}
}

View File

@@ -0,0 +1,229 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Prism.Commands;
using Prism.Ioc;
using UIShare.GlobalVariable;
using UIShare.PubEvent;
using UIShare.UIViewModel;
using UIShare.ViewModelBase;
namespace SettingModule.ViewModels
{
public class SettingViewModel : NavigateViewModelBase, IRegionMemberLifetime, IDisposable
{
#region
private SystemConfig _systemConfig;
public SystemConfig SystemConfig
{
get => _systemConfig;
set => SetProperty(ref _systemConfig, value);
}
public bool KeepAlive => true;
public string TestStatus
{
get => _testStatus;
set => SetProperty(ref _testStatus, value);
}
public DeviceInfoVM? SelectedDevice
{
get => _selectedDevice;
set
{
if (SetProperty(ref _selectedDevice, value))
{
OnSelectedDeviceChanged();
}
}
}
public ObservableCollection<DeviceInfoVM> DeviceList
{
get => _deviceList;
set => SetProperty(ref _deviceList, value);
}
public ObservableCollection<SharedParameter> SharedParameterList
{
get => _sharedParameterList;
set => SetProperty(ref _sharedParameterList, value);
}
public string StatusMessage
{
get => _statusMessage;
set => SetProperty(ref _statusMessage, value);
}
public ObservableCollection<string> ConnectionTypes { get; } = new()
{
"None", "Tcp", "Serial", "CAN"
};
#endregion
#region
public ICommand RefreshCommand { get; }
public ICommand SaveCommand { get; }
public ICommand OpenConnectionConfigCommand { get; }
#endregion
#region
private IScopedProvider _scope;
private ScopedContext _scopedContext { get; set; }
private GlobalInfo _globalInfo { get; }
private bool IsInitiated = false;
private string _testStatus = string.Empty;
private DeviceInfoVM? _selectedDevice;
private ObservableCollection<DeviceInfoVM> _deviceList;
private ObservableCollection<SharedParameter> _sharedParameterList;
private string _statusMessage = "请在左侧选择设备查看 / 编辑配置";
#endregion
public SettingViewModel(IContainerExtension container) : base(container)
{
_globalInfo = container.Resolve<GlobalInfo>();
RefreshCommand = new DelegateCommand(OnExpand);
SaveCommand = new DelegateCommand(OnSave);
OpenConnectionConfigCommand = new DelegateCommand(OnOpenConnectionConfig);
}
public void Dispose()
{
try
{
if (DeviceList != null)
{
DeviceList = null!;
}
SelectedDevice = null;
_scopedContext = null!;
}
catch (Exception ex)
{
Logger.LoggerHelper.ErrorWithNotify(TestStatus,$"释放配置管理组件SettingViewModel资源失败: {ex.Message}");
}
}
#region
/// <summary>
/// 选中设备切换:右侧配置面板靠 SelectedDevice 绑定自动刷新,
/// 这里只负责更新状态栏提示。
/// </summary>
private void OnSelectedDeviceChanged()
{
if (SelectedDevice == null)
{
StatusMessage = "未选中设备";
return;
}
StatusMessage = $"当前配置:{SelectedDevice.DeviceName}{SelectedDevice.DeviceType}";
}
/// <summary>双击展开 / 折叠九宫格。</summary>
private void OnExpand()
{
if (string.IsNullOrEmpty(TestStatus)) return;
_globalInfo.CurrentScope = TestStatus;
_eventAggregator.GetEvent<ExpandViewEvent>().Publish(TestStatus);
}
/// <summary>保存当前 SystemConfig 到 SystemPath 下,文件名为 {Title}.json。</summary>
private void OnSave()
{
if (SystemConfig == null)
{
StatusMessage = "无可保存的配置";
return;
}
if (string.IsNullOrWhiteSpace(SystemConfig.Title))
{
StatusMessage = "保存失败标题Title不能为空";
return;
}
ConfigService.Save(SystemConfig);
StatusMessage = $"已保存配置 [{SystemConfig.Title}.json] 至 {SystemConfig.SystemPath}{DateTime.Now:HH:mm:ss}";
}
/// <summary>重置当前选中设备的配置(占位)。</summary>
/// <summary>
/// 打开"连接配置"对话框。按 SelectedDevice.ConnectionType 决定开 Tcp 还是串口对话框。
/// </summary>
private void OnOpenConnectionConfig()
{
if (SelectedDevice == null)
{
StatusMessage = "请先在左侧选择设备";
return;
}
var dialogName = SelectedDevice.ConnectionType switch
{
"Tcp" => "TCPConfig",
"Serial" => "SerialPortConfig",
"CAN" => "CANConfig",
_ => string.Empty
};
if (string.IsNullOrEmpty(dialogName))
{
StatusMessage = "当前设备未配置连接方式(请先选择 Tcp 或 Serial";
return;
}
var p = new DialogParameters
{
{ "Device", SelectedDevice },
{ "SystemConfig", SystemConfig }
};
_dialogService.ShowDialog(dialogName, p, r =>
{
if (r.Result == ButtonResult.OK)
{
StatusMessage = $"已更新 [{SelectedDevice.DeviceName}] 的连接参数({DateTime.Now:HH:mm:ss}";
}
});
}
#endregion
#region
public override void OnNavigatedTo(NavigationContext navigationContext)
{
base.OnNavigatedTo(navigationContext);
if (!IsInitiated && navigationContext.Parameters.ContainsKey("Name"))
{
TestStatus = navigationContext.Parameters.GetValue<string>("Name");
_scope = _globalInfo.ScopeDic[TestStatus];
_scopedContext = _globalInfo.ContextDic[TestStatus];
SystemConfig = _scope.Resolve<SystemConfig>();
ConfigService.EnsureDefaultCanDevice(SystemConfig);
if (DeviceList != null && DeviceList.Count > 0)
{
SelectedDevice = DeviceList[0];
}
DeviceList = SystemConfig.DeviceList;
SharedParameterList=SystemConfig.SharedParameterList;
if (SharedParameterList.Count == 0)
{
foreach (var device in SystemConfig.ParameterList)
{
var sharedParam = new SharedParameter
{
Id = device.ID.ToString(),
ParameterName = device.Name,
Value = device.Value is int intVal ? intVal : Convert.ToInt32(device.Value)
};
SharedParameterList.Add(sharedParam);
}
}
IsInitiated = true;
}
}
#endregion
}
}