304 lines
11 KiB
C#
304 lines
11 KiB
C#
using DeviceCommand.Device;
|
||
using Prism.Commands;
|
||
using Prism.Ioc;
|
||
using System;
|
||
using System.Threading;
|
||
using System.Windows.Input;
|
||
using UIShare.GlobalVariable;
|
||
using UIShare.ViewModelBase;
|
||
|
||
namespace DeviceEditModule.ViewModels
|
||
{
|
||
/// <summary>
|
||
/// SPAW7000 功率分析记录仪控制面板 ViewModel。
|
||
/// </summary>
|
||
public class SPAW7000ViewModel : NavigateViewModelBase, IDisposable
|
||
{
|
||
#region 私有字段
|
||
|
||
private readonly DeviceManager _deviceManager;
|
||
private SPAW7000? _device;
|
||
private CancellationTokenSource? _cts;
|
||
|
||
#endregion
|
||
|
||
#region 设备信息属性
|
||
|
||
private string _deviceName = "SPAW7000";
|
||
public string DeviceName
|
||
{
|
||
get => _deviceName;
|
||
set => SetProperty(ref _deviceName, value);
|
||
}
|
||
|
||
private bool _isConnected;
|
||
public bool IsConnected
|
||
{
|
||
get => _isConnected;
|
||
set => SetProperty(ref _isConnected, value);
|
||
}
|
||
|
||
private bool _isBusy;
|
||
public bool IsBusy
|
||
{
|
||
get => _isBusy;
|
||
set => SetProperty(ref _isBusy, value);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 输入参数属性
|
||
|
||
private int _channel = 1;
|
||
/// <summary>当前操作通道。</summary>
|
||
public int Channel
|
||
{
|
||
get => _channel;
|
||
set => SetProperty(ref _channel, value);
|
||
}
|
||
|
||
private double _voltageRange = 300.0;
|
||
/// <summary>电压量程(V)。</summary>
|
||
public double VoltageRange
|
||
{
|
||
get => _voltageRange;
|
||
set => SetProperty(ref _voltageRange, value);
|
||
}
|
||
|
||
private double _currentRange = 5.0;
|
||
/// <summary>电流量程(A)。</summary>
|
||
public double CurrentRange
|
||
{
|
||
get => _currentRange;
|
||
set => SetProperty(ref _currentRange, value);
|
||
}
|
||
|
||
private string _couplingMode = "DC";
|
||
/// <summary>耦合模式:AC / DC / ACDC。</summary>
|
||
public string CouplingMode
|
||
{
|
||
get => _couplingMode;
|
||
set => SetProperty(ref _couplingMode, value);
|
||
}
|
||
|
||
private int _resolution = 6;
|
||
/// <summary>显示分辨率(5 或 6)。</summary>
|
||
public int Resolution
|
||
{
|
||
get => _resolution;
|
||
set => SetProperty(ref _resolution, value);
|
||
}
|
||
|
||
private int _brightness = 5;
|
||
/// <summary>屏幕亮度(1~10)。</summary>
|
||
public int Brightness
|
||
{
|
||
get => _brightness;
|
||
set => SetProperty(ref _brightness, value);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 测量结果属性
|
||
|
||
private string _measuredVoltage = "—";
|
||
public string MeasuredVoltage
|
||
{
|
||
get => _measuredVoltage;
|
||
set => SetProperty(ref _measuredVoltage, value);
|
||
}
|
||
|
||
private string _measuredCurrent = "—";
|
||
public string MeasuredCurrent
|
||
{
|
||
get => _measuredCurrent;
|
||
set => SetProperty(ref _measuredCurrent, value);
|
||
}
|
||
|
||
private string _measuredPower = "—";
|
||
public string MeasuredPower
|
||
{
|
||
get => _measuredPower;
|
||
set => SetProperty(ref _measuredPower, value);
|
||
}
|
||
|
||
private string _measuredFrequency = "—";
|
||
public string MeasuredFrequency
|
||
{
|
||
get => _measuredFrequency;
|
||
set => SetProperty(ref _measuredFrequency, value);
|
||
}
|
||
|
||
private string _measuredPowerFactor = "—";
|
||
public string MeasuredPowerFactor
|
||
{
|
||
get => _measuredPowerFactor;
|
||
set => SetProperty(ref _measuredPowerFactor, value);
|
||
}
|
||
|
||
private string _deviceModel = "—";
|
||
public string DeviceModel
|
||
{
|
||
get => _deviceModel;
|
||
set => SetProperty(ref _deviceModel, value);
|
||
}
|
||
|
||
private string _deviceSerial = "—";
|
||
public string DeviceSerial
|
||
{
|
||
get => _deviceSerial;
|
||
set => SetProperty(ref _deviceSerial, value);
|
||
}
|
||
|
||
private string _responseLog = string.Empty;
|
||
/// <summary>命令响应日志(最新消息在顶部)。</summary>
|
||
public string ResponseLog
|
||
{
|
||
get => _responseLog;
|
||
set => SetProperty(ref _responseLog, value);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 命令
|
||
|
||
public ICommand QueryIdentityCommand { get; }
|
||
public ICommand ResetDeviceCommand { get; }
|
||
public ICommand QueryAllMeasureCommand { get; }
|
||
public ICommand SetVoltageRangeCommand { get; }
|
||
public ICommand SetCurrentRangeCommand { get; }
|
||
public ICommand SetCouplingModeCommand { get; }
|
||
public ICommand SetResolutionCommand { get; }
|
||
public ICommand SetBrightnessCommand { get; }
|
||
public ICommand SetTouchLockOnCommand { get; }
|
||
public ICommand SetTouchLockOffCommand { get; }
|
||
public ICommand QueryModelCommand { get; }
|
||
public ICommand QuerySerialCommand { get; }
|
||
public ICommand QueryStatusByteCommand { get; }
|
||
|
||
#endregion
|
||
|
||
public SPAW7000ViewModel(IContainerProvider containerProvider) : base(containerProvider)
|
||
{
|
||
_deviceManager = containerProvider.Resolve<DeviceManager>();
|
||
|
||
QueryIdentityCommand = new DelegateCommand(async () => await Exec(async () => AppendLog("IDN: " + await _device!.查询设备标识(Ct()))));
|
||
ResetDeviceCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.重置设备(Ct()); AppendLog("设备已重置"); }));
|
||
SetVoltageRangeCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置电压量程(Channel, VoltageRange, Ct()); AppendLog($"通道 {Channel} 电压量程已设为 {VoltageRange} V"); }));
|
||
SetCurrentRangeCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置电流量程(Channel, CurrentRange, Ct()); AppendLog($"通道 {Channel} 电流量程已设为 {CurrentRange} A"); }));
|
||
SetCouplingModeCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置通道耦合模式(Channel, CouplingMode, Ct()); AppendLog($"通道 {Channel} 耦合模式已设为 {CouplingMode}"); }));
|
||
SetResolutionCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置显示分辨率(Resolution, Ct()); AppendLog($"显示分辨率已设为 {Resolution} 位"); }));
|
||
SetBrightnessCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置显示亮度(Brightness, Ct()); AppendLog($"屏幕亮度已设为 {Brightness}"); }));
|
||
SetTouchLockOnCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置屏幕触摸锁定(true, Ct()); AppendLog("屏幕触摸已锁定"); }));
|
||
SetTouchLockOffCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置屏幕触摸锁定(false, Ct()); AppendLog("屏幕触摸已解锁"); }));
|
||
QueryModelCommand = new DelegateCommand(async () => await Exec(async () => { DeviceModel = await _device!.查询设备型号(Ct()); AppendLog($"型号: {DeviceModel}"); }));
|
||
QuerySerialCommand = new DelegateCommand(async () => await Exec(async () => { DeviceSerial = await _device!.查询设备序列号(Ct()); AppendLog($"序列号: {DeviceSerial}"); }));
|
||
QueryStatusByteCommand = new DelegateCommand(async () => await Exec(async () => AppendLog("STB: " + await _device!.读取状态字节(Ct()))));
|
||
|
||
QueryAllMeasureCommand = new DelegateCommand(async () => await Exec(async () =>
|
||
{
|
||
MeasuredVoltage = await _device!.查询实际电压(Channel, Ct());
|
||
MeasuredCurrent = await _device!.查询实际电流(Channel, Ct());
|
||
MeasuredPower = await _device!.查询实际功率(Channel, Ct());
|
||
MeasuredFrequency = await _device!.查询频率(Channel, Ct());
|
||
MeasuredPowerFactor = await _device!.查询功率因数(Channel, Ct());
|
||
AppendLog($"CH{Channel} 测量 → U:{MeasuredVoltage}V I:{MeasuredCurrent}A P:{MeasuredPower}W F:{MeasuredFrequency}Hz PF:{MeasuredPowerFactor}");
|
||
}));
|
||
|
||
Initialize();
|
||
}
|
||
|
||
#region 初始化 / Navigation
|
||
|
||
public void Initialize(string? deviceName = null)
|
||
{
|
||
SPAW7000? found = null;
|
||
string? foundName = null;
|
||
|
||
if (deviceName != null &&
|
||
_deviceManager.DeviceMap.TryGetValue(deviceName, out var d) &&
|
||
d is SPAW7000 s)
|
||
{
|
||
found = s;
|
||
foundName = deviceName;
|
||
}
|
||
else
|
||
{
|
||
foreach (var kv in _deviceManager.DeviceMap)
|
||
{
|
||
if (kv.Value is SPAW7000 spaw)
|
||
{
|
||
found = spaw;
|
||
foundName = kv.Key;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
_device = found;
|
||
DeviceName = foundName ?? "SPAW7000 (未找到)";
|
||
IsConnected = _device?.IsConnected ?? false;
|
||
|
||
AppendLog(found != null
|
||
? $"已关联设备 [{DeviceName}],连接状态:{(IsConnected ? "已连接" : "未连接")}"
|
||
: "未在 DeviceManager 中找到 SPAW7000 设备,请先初始化设备配置。");
|
||
}
|
||
|
||
public override void OnNavigatedTo(NavigationContext context)
|
||
{
|
||
var name = context.Parameters.GetValue<string?>("DeviceName");
|
||
Initialize(name);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 辅助
|
||
|
||
private CancellationToken Ct() => (_cts = new CancellationTokenSource(TimeSpan.FromSeconds(10))).Token;
|
||
|
||
private async Task Exec(Func<Task> action)
|
||
{
|
||
if (_device == null)
|
||
{
|
||
AppendLog("错误:未关联到设备实例,请检查设备配置。");
|
||
return;
|
||
}
|
||
if (IsBusy) return;
|
||
IsBusy = true;
|
||
try
|
||
{
|
||
await action();
|
||
IsConnected = _device.IsConnected;
|
||
}
|
||
catch (OperationCanceledException)
|
||
{
|
||
AppendLog("命令超时或已取消。");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
AppendLog($"错误:{ex.Message}");
|
||
}
|
||
finally
|
||
{
|
||
IsBusy = false;
|
||
}
|
||
}
|
||
|
||
private void AppendLog(string message)
|
||
{
|
||
var line = $"[{DateTime.Now:HH:mm:ss}] {message}";
|
||
ResponseLog = ResponseLog.Length > 4000
|
||
? line + "\n" + ResponseLog[..3000]
|
||
: line + "\n" + ResponseLog;
|
||
}
|
||
|
||
#endregion
|
||
|
||
public void Dispose()
|
||
{
|
||
_cts?.Cancel();
|
||
_cts?.Dispose();
|
||
}
|
||
}
|
||
}
|