143 lines
6.7 KiB
C#
143 lines
6.7 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>
|
|
/// Chroma61800 交流源一拖三 控制面板 ViewModel
|
|
/// </summary>
|
|
public class Chroma61800ViewModel : NavigateViewModelBase, IDisposable
|
|
{
|
|
private readonly DeviceManager _dm;
|
|
private Chroma61800? _dev;
|
|
private CancellationTokenSource? _cts;
|
|
|
|
private string _deviceName = "Chroma61800";
|
|
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 _frequency = 50; public double Frequency { get => _frequency; set => SetProperty(ref _frequency, value); }
|
|
private string _waveform = "SINE"; public string Waveform { get => _waveform; set => SetProperty(ref _waveform, 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 _measuredFrequency;
|
|
public double MeasuredFrequency { get => _measuredFrequency; set => SetProperty(ref _measuredFrequency, value); }
|
|
private double _measuredPower;
|
|
public double MeasuredPower { get => _measuredPower; set => SetProperty(ref _measuredPower, 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 OutOn { get; } public ICommand OutOff { get; }
|
|
public ICommand SetV { get; } public ICommand SetF { get; } public ICommand SetWave { get; }
|
|
public ICommand SetThreePhase { get; } public ICommand SetSinglePhase { get; }
|
|
public ICommand QMeas { get; }
|
|
#endregion
|
|
|
|
public Chroma61800ViewModel(IContainerProvider cp) : base(cp)
|
|
{
|
|
_dm = cp.Resolve<DeviceManager>();
|
|
QueryIdn = new DelegateCommand(async () => await Exec(async () => Log("IDN:" + await _dev!.查询设备信息Async(Ct()))));
|
|
Reset = new DelegateCommand(async () => await Exec(async () => { await _dev!.复位Async(Ct()); Log("设备已复位"); }));
|
|
OutOn = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置输出开关Async(true, Ct()); Log("输出已开启"); }));
|
|
OutOff = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置输出开关Async(false, Ct()); Log("输出已关闭"); }));
|
|
SetV = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置交流电压Async(Voltage, Ct()); Log($"电压={Voltage}V"); }));
|
|
SetF = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置频率Async(Frequency, Ct()); Log($"频率={Frequency}Hz"); }));
|
|
SetWave = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置波形Async(Waveform, Ct()); Log($"波形={Waveform}"); }));
|
|
SetThreePhase = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置单三相模式Async(true, Ct()); Log("三相模式"); }));
|
|
SetSinglePhase = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置单三相模式Async(false, Ct()); Log("单相模式"); }));
|
|
QMeas = new DelegateCommand(async () => await Exec(async () =>
|
|
{
|
|
MeasuredVoltage = await _dev!.读取交流电压Async(Ct());
|
|
MeasuredCurrent = await _dev!.读取交流电流Async(Ct());
|
|
MeasuredFrequency = await _dev!.读取频率Async(Ct());
|
|
MeasuredPower = await _dev!.读取真实功率Async(Ct());
|
|
Log($"测量→V:{MeasuredVoltage} I:{MeasuredCurrent} F:{MeasuredFrequency} P:{MeasuredPower}");
|
|
}));
|
|
Initialize();
|
|
}
|
|
|
|
#region 初始化 / Navigation
|
|
|
|
public void Initialize(string? deviceName = null)
|
|
{
|
|
Chroma61800? found = null; string? fn = null;
|
|
if (deviceName != null && _dm.DeviceMap.TryGetValue(deviceName, out var d) && d is Chroma61800 e)
|
|
{ found = e; fn = deviceName; }
|
|
else
|
|
{
|
|
foreach (var kv in _dm.DeviceMap)
|
|
if (kv.Value is Chroma61800 it) { found = it; fn = kv.Key; break; }
|
|
}
|
|
_dev = found;
|
|
DeviceName = fn ?? "Chroma61800 (未找到)";
|
|
IsConnected = _dev?.IsConnected ?? false;
|
|
Log(found != null
|
|
? $"已关联设备 [{DeviceName}],连接:{(IsConnected ? "已连接" : "未连接")}"
|
|
: "未在 DeviceManager 中找到 Chroma61800 设备");
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|