软件独立控制弹窗添加
This commit is contained in:
@@ -1,134 +1,128 @@
|
||||
using DeviceCommand.Device;
|
||||
using DeviceCommand.Devices;
|
||||
using DeviceCommand.Device;
|
||||
using Prism.Commands;
|
||||
using Prism.Ioc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
using UIShare.GlobalVariable;
|
||||
using UIShare.ViewModelBase;
|
||||
|
||||
namespace DeviceEditModule.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
/// PW8001 功率分析仪 控制面板 ViewModel
|
||||
/// </summary>
|
||||
public class PW8001ViewModel : NavigateViewModelBase, IDisposable
|
||||
{
|
||||
#region 私有字段
|
||||
private readonly DeviceManager _deviceManager;
|
||||
private PW8001? _device;
|
||||
private readonly DeviceManager _dm;
|
||||
private PW8001? _dev;
|
||||
private CancellationTokenSource? _cts;
|
||||
#endregion
|
||||
#region 属性
|
||||
|
||||
private string _deviceName = "PW8001";
|
||||
public string DeviceName
|
||||
{
|
||||
get => _deviceName;
|
||||
set => SetProperty(ref _deviceName, value);
|
||||
}
|
||||
|
||||
public string DeviceName { get => _deviceName; set => SetProperty(ref _deviceName, value); }
|
||||
private bool _isConnected;
|
||||
public bool IsConnected
|
||||
{
|
||||
get => _isConnected;
|
||||
set => SetProperty(ref _isConnected, value);
|
||||
}
|
||||
public bool IsConnected { get => _isConnected; set => SetProperty(ref _isConnected, value); }
|
||||
private bool _isBusy;
|
||||
/// <summary>正在执行设备命令时为 true,用于 UI 忙碌状态指示。</summary>
|
||||
public bool IsBusy
|
||||
public bool IsBusy { get => _isBusy; set => SetProperty(ref _isBusy, value); }
|
||||
|
||||
#region 输入参数
|
||||
private int _channel = 1; public int Channel { get => _channel; set => SetProperty(ref _channel, 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 _voltageTHD;
|
||||
public double VoltageTHD { get => _voltageTHD; set => SetProperty(ref _voltageTHD, value); }
|
||||
private double _currentTHD;
|
||||
public double CurrentTHD { get => _currentTHD; set => SetProperty(ref _currentTHD, value); }
|
||||
private double _totalPower;
|
||||
public double TotalPower { get => _totalPower; set => SetProperty(ref _totalPower, value); }
|
||||
private string _responseLog = "";
|
||||
public string ResponseLog { get => _responseLog; set => SetProperty(ref _responseLog, value); }
|
||||
#endregion
|
||||
|
||||
#region 命令
|
||||
public ICommand QueryIdn { get; } public ICommand Reset { get; } public ICommand Clear { get; }
|
||||
public ICommand ModeWIDE { get; } public ICommand ModeIEC { get; }
|
||||
public ICommand QV { get; } public ICommand QI { get; } public ICommand QP { get; }
|
||||
public ICommand QP123 { get; } public ICommand QTHD { get; }
|
||||
#endregion
|
||||
|
||||
public PW8001ViewModel(IContainerProvider cp) : base(cp)
|
||||
{
|
||||
get => _isBusy;
|
||||
set => SetProperty(ref _isBusy, value);
|
||||
}
|
||||
private string _responseLog = string.Empty;
|
||||
/// <summary>命令响应日志(最新消息在顶部)。</summary>
|
||||
public string ResponseLog
|
||||
{
|
||||
get => _responseLog;
|
||||
set => SetProperty(ref _responseLog, value);
|
||||
_dm = cp.Resolve<DeviceManager>();
|
||||
QueryIdn = new DelegateCommand(async () => await Exec(async () => Log("IDN:" + await _dev!.查询机器信息(Ct()))));
|
||||
Reset = new DelegateCommand(async () => await Exec(async () => { await _dev!.复位仪器(Ct()); Log("仪器已复位"); }));
|
||||
Clear = new DelegateCommand(async () => await Exec(async () => { await _dev!.清除状态(Ct()); Log("状态已清除"); }));
|
||||
ModeWIDE = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置测试模式_WIDE(Ct()); Log("WIDE模式"); }));
|
||||
ModeIEC = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置测试模式_IEC(Ct()); Log("IEC模式"); }));
|
||||
QV = new DelegateCommand(async () => await Exec(async () => { MeasuredVoltage = await _dev!.查询电压_不含变比(Channel, Ct()); Log($"CH{Channel}电压={MeasuredVoltage}V"); }));
|
||||
QI = new DelegateCommand(async () => await Exec(async () => { MeasuredCurrent = await _dev!.查询电流_不含变比(Channel, Ct()); Log($"CH{Channel}电流={MeasuredCurrent}A"); }));
|
||||
QP = new DelegateCommand(async () => await Exec(async () => { MeasuredPower = await _dev!.查询功率_不含变比(Channel, Ct()); Log($"CH{Channel}功率={MeasuredPower}W"); }));
|
||||
QP123 = new DelegateCommand(async () => await Exec(async () => { TotalPower = await _dev!.查询总功率P123(Ct()); Log($"三相总功率={TotalPower}W"); }));
|
||||
QTHD = new DelegateCommand(async () => await Exec(async () =>
|
||||
{
|
||||
VoltageTHD = await _dev!.查询电压THD(Channel, Ct());
|
||||
CurrentTHD = await _dev!.查询电流THD(Channel, Ct());
|
||||
Log($"CH{Channel} THD→U:{VoltageTHD}% I:{CurrentTHD}%");
|
||||
}));
|
||||
Initialize();
|
||||
}
|
||||
|
||||
#endregion
|
||||
#region 命令
|
||||
#endregion
|
||||
public PW8001ViewModel(IContainerProvider containerProvider) : base(containerProvider)
|
||||
{
|
||||
_deviceManager = containerProvider.Resolve<DeviceManager>();
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
_cts?.Cancel();
|
||||
_cts?.Dispose();
|
||||
}
|
||||
#region 初始化 / Navigation
|
||||
|
||||
/// <summary>
|
||||
/// 从 DeviceManager 中查找PW8001设备实例。
|
||||
/// 优先按 <paramref name="deviceName"/> 查找,否则取第一个匹配类型的设备。
|
||||
/// </summary>
|
||||
public void Initialize(string? deviceName = null)
|
||||
{
|
||||
PW8001? found = null;
|
||||
string? foundName = null;
|
||||
if (deviceName != null &&
|
||||
_deviceManager.DeviceMap.TryGetValue(deviceName, out var d) &&
|
||||
d is PW8001 e)
|
||||
{
|
||||
found = e;
|
||||
foundName = deviceName;
|
||||
}
|
||||
PW8001? found = null; string? fn = null;
|
||||
if (deviceName != null && _dm.DeviceMap.TryGetValue(deviceName, out var d) && d is PW8001 e)
|
||||
{ found = e; fn = deviceName; }
|
||||
else
|
||||
{
|
||||
foreach (var kv in _deviceManager.DeviceMap)
|
||||
{
|
||||
if (kv.Value is PW8001 it)
|
||||
{
|
||||
found = it;
|
||||
foundName = kv.Key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
foreach (var kv in _dm.DeviceMap)
|
||||
if (kv.Value is PW8001 it) { found = it; fn = kv.Key; break; }
|
||||
}
|
||||
|
||||
_device = found;
|
||||
DeviceName = foundName ?? "IT7800E (未找到)";
|
||||
IsConnected = _device?.IsConnected ?? false;
|
||||
|
||||
AppendLog(found != null
|
||||
? $"已关联设备 [{DeviceName}],连接状态:{(IsConnected ? "已连接" : "未连接")}"
|
||||
: "未在 DeviceManager 中找到 IT7800E 设备,请先初始化设备配置。");
|
||||
_dev = found;
|
||||
DeviceName = fn ?? "PW8001 (未找到)";
|
||||
IsConnected = _dev?.IsConnected ?? false;
|
||||
Log(found != null
|
||||
? $"已关联设备 [{DeviceName}],连接:{(IsConnected ? "已连接" : "未连接")}"
|
||||
: "未在 DeviceManager 中找到 PW8001 设备");
|
||||
}
|
||||
|
||||
public override void OnNavigatedTo(NavigationContext context)
|
||||
{
|
||||
var pName = context.Parameters.GetValue<string?>("DeviceName");
|
||||
Initialize(pName);
|
||||
}
|
||||
|
||||
#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 (_dev == null) { Log("错误:未关联到设备实例,请检查设备配置。"); return; }
|
||||
if (IsBusy) return;
|
||||
IsBusy = true;
|
||||
try
|
||||
{
|
||||
await action();
|
||||
IsConnected = _device.IsConnected;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
AppendLog("命令超时或已取消。");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AppendLog($"错误:{ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsBusy = false;
|
||||
IsConnected = _dev.IsConnected;
|
||||
}
|
||||
catch (OperationCanceledException) { Log("命令超时或已取消。"); }
|
||||
catch (Exception ex) { Log($"错误:{ex.Message}"); }
|
||||
finally { IsBusy = false; }
|
||||
}
|
||||
|
||||
private void AppendLog(string message)
|
||||
private void Log(string message)
|
||||
{
|
||||
var line = $"[{DateTime.Now:HH:mm:ss}] {message}";
|
||||
ResponseLog = ResponseLog.Length > 4000
|
||||
@@ -137,12 +131,11 @@ namespace DeviceEditModule.ViewModels
|
||||
}
|
||||
|
||||
#endregion
|
||||
public override void OnNavigatedTo(NavigationContext context)
|
||||
{
|
||||
var name = context.Parameters.GetValue<string?>("DeviceName");
|
||||
Initialize(name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
public void Dispose()
|
||||
{
|
||||
_cts?.Cancel();
|
||||
_cts?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user