138 lines
6.3 KiB
C#
138 lines
6.3 KiB
C#
using DeviceCommand.Devices;
|
|
using Prism.Commands;
|
|
using Prism.Ioc;
|
|
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Input;
|
|
using UIShare.GlobalVariable;
|
|
using UIShare.ViewModelBase;
|
|
|
|
namespace DeviceEditModule.ViewModels
|
|
{
|
|
/// <summary>
|
|
/// IT6720 低压电源一拖三 控制面板 ViewModel
|
|
/// </summary>
|
|
public class IT6720ViewModel : NavigateViewModelBase, IDisposable
|
|
{
|
|
private readonly DeviceManager _dm;
|
|
private IT6720? _dev;
|
|
private CancellationTokenSource? _cts;
|
|
|
|
private string _deviceName = "IT6720";
|
|
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); }
|
|
|
|
#region 输入参数
|
|
private double _voltage; public double Voltage { get => _voltage; set => SetProperty(ref _voltage, value); }
|
|
private double _current; public double Current { get => _current; set => SetProperty(ref _current, value); }
|
|
private double _voltageLimit; public double VoltageLimit { get => _voltageLimit; set => SetProperty(ref _voltageLimit, 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 string _outputMode = "";
|
|
public string OutputMode { get => _outputMode; set => SetProperty(ref _outputMode, value); }
|
|
private string _responseLog = "";
|
|
public string ResponseLog { get => _responseLog; set => SetProperty(ref _responseLog, value); }
|
|
#endregion
|
|
|
|
#region 命令
|
|
public ICommand QueryIdn { get; } public ICommand OutOn { get; } public ICommand OutOff { get; }
|
|
public ICommand SetRemote { get; } public ICommand SetLocal { get; }
|
|
public ICommand SetV { get; } public ICommand SetI { get; } public ICommand SetVLim { get; }
|
|
public ICommand QMeas { get; } public ICommand QMode { get; }
|
|
#endregion
|
|
|
|
public IT6720ViewModel(IContainerProvider cp) : base(cp)
|
|
{
|
|
_dm = cp.Resolve<DeviceManager>();
|
|
QueryIdn = new DelegateCommand(async () => await Exec(async () => Log("IDN:" + await _dev!.查询设备信息(Ct()))));
|
|
OutOn = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置输出开关(true, Ct()); Log("输出已开启"); }));
|
|
OutOff = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置输出开关(false, Ct()); Log("输出已关闭"); }));
|
|
SetRemote = new DelegateCommand(async () => await Exec(async () => { await _dev!.切换远程控制模式(true, Ct()); Log("远程控制"); }));
|
|
SetLocal = new DelegateCommand(async () => await Exec(async () => { await _dev!.切换本地控制模式(Ct()); Log("本地控制"); }));
|
|
SetV = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置输出电压(Voltage, Ct()); Log($"电压={Voltage}V"); }));
|
|
SetI = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置输出电流(Current, Ct()); Log($"电流={Current}A"); }));
|
|
SetVLim = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置电压上限(VoltageLimit, Ct()); Log($"电压上限={VoltageLimit}V"); }));
|
|
QMeas = new DelegateCommand(async () => await Exec(async () =>
|
|
{
|
|
MeasuredVoltage = await _dev!.查询实际电压(Ct());
|
|
MeasuredCurrent = await _dev!.查询实际电流(Ct());
|
|
Log($"测量→V:{MeasuredVoltage} I:{MeasuredCurrent}");
|
|
}));
|
|
QMode = new DelegateCommand(async () => await Exec(async () => { OutputMode = await _dev!.查询输出模式(Ct()); Log($"输出模式={OutputMode}"); }));
|
|
Initialize();
|
|
}
|
|
|
|
#region 初始化 / Navigation
|
|
|
|
public void Initialize(string? deviceName = null)
|
|
{
|
|
IT6720? found = null; string? fn = null;
|
|
if (deviceName != null && _dm.DeviceMap.TryGetValue(deviceName, out var d) && d is IT6720 e)
|
|
{ found = e; fn = deviceName; }
|
|
else
|
|
{
|
|
foreach (var kv in _dm.DeviceMap)
|
|
if (kv.Value is IT6720 it) { found = it; fn = kv.Key; break; }
|
|
}
|
|
_dev = found;
|
|
DeviceName = fn ?? "IT6720 (未找到)";
|
|
IsConnected = _dev?.IsConnected ?? false;
|
|
Log(found != null
|
|
? $"已关联设备 [{DeviceName}],连接:{(IsConnected ? "已连接" : "未连接")}"
|
|
: "未在 DeviceManager 中找到 IT6720 设备");
|
|
}
|
|
|
|
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 (_dev == null) { Log("错误:未关联到设备实例,请检查设备配置。"); return; }
|
|
if (IsBusy) return;
|
|
IsBusy = true;
|
|
try
|
|
{
|
|
await action();
|
|
IsConnected = _dev.IsConnected;
|
|
}
|
|
catch (OperationCanceledException) { Log("命令超时或已取消。"); }
|
|
catch (Exception ex) { Log($"错误:{ex.Message}"); }
|
|
finally { IsBusy = false; }
|
|
}
|
|
|
|
private void Log(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();
|
|
}
|
|
}
|
|
}
|