304 lines
11 KiB
C#
304 lines
11 KiB
C#
using DeviceCommand.Devices;
|
|
using Prism.Commands;
|
|
using Prism.Ioc;
|
|
using System;
|
|
using System.Globalization;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
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 SPAW7000.VoltageRange _voltageRange = SPAW7000.VoltageRange.V_300;
|
|
/// <summary>电压量程枚举。</summary>
|
|
public SPAW7000.VoltageRange VoltageRange
|
|
{
|
|
get => _voltageRange;
|
|
set => SetProperty(ref _voltageRange, value);
|
|
}
|
|
|
|
private SPAW7000.CurrentRange _currentRange = SPAW7000.CurrentRange.A_5;
|
|
/// <summary>电流量程枚举。</summary>
|
|
public SPAW7000.CurrentRange CurrentRange
|
|
{
|
|
get => _currentRange;
|
|
set => SetProperty(ref _currentRange, value);
|
|
}
|
|
|
|
public SPAW7000.VoltageRange[] VoltageRangeOptions { get; } =
|
|
(SPAW7000.VoltageRange[])Enum.GetValues(typeof(SPAW7000.VoltageRange));
|
|
|
|
public SPAW7000.CurrentRange[] CurrentRangeOptions { get; } =
|
|
(SPAW7000.CurrentRange[])Enum.GetValues(typeof(SPAW7000.CurrentRange));
|
|
|
|
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 double _measuredVoltage;
|
|
public double MeasuredVoltage
|
|
{
|
|
get => _measuredVoltage;
|
|
set => SetProperty(ref _measuredVoltage, value);
|
|
}
|
|
|
|
private double _measuredCurrent;
|
|
public double MeasuredCurrent
|
|
{
|
|
get => _measuredCurrent;
|
|
set => SetProperty(ref _measuredCurrent, value);
|
|
}
|
|
|
|
private double _measuredPower;
|
|
public double MeasuredPower
|
|
{
|
|
get => _measuredPower;
|
|
set => SetProperty(ref _measuredPower, value);
|
|
}
|
|
|
|
private double _measuredFrequency;
|
|
public double MeasuredFrequency
|
|
{
|
|
get => _measuredFrequency;
|
|
set => SetProperty(ref _measuredFrequency, value);
|
|
}
|
|
|
|
private double _measuredPowerFactor;
|
|
public double 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 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} 电压量程已设为 {(int)VoltageRange} V"); }));
|
|
SetCurrentRangeCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.设置电流量程(Channel, CurrentRange, Ct()); AppendLog($"通道 {Channel} 电流量程已设为 {(int)CurrentRange} A"); }));
|
|
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()))));
|
|
|
|
// 只修改这里:保持你原本的 5 次驱动查询调用不变,仅对读回来的科学计数法进行两位小数格式化
|
|
QueryAllMeasureCommand = new DelegateCommand(async () => await Exec(async () =>
|
|
{
|
|
// 设备层已返回 double 数值
|
|
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:F2}V I:{MeasuredCurrent:F3}A P:{MeasuredPower:F2}W F:{MeasuredFrequency:F2}Hz PF:{MeasuredPowerFactor:F3}");
|
|
}));
|
|
|
|
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();
|
|
}
|
|
}
|
|
} |