软件独立控制弹窗添加
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
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>
|
||||
/// S7200 高压直流源载一体机 控制面板 ViewModel
|
||||
/// </summary>
|
||||
public class S7200ViewModel : NavigateViewModelBase, IDisposable
|
||||
{
|
||||
private readonly DeviceManager _dm;
|
||||
private S7200? _dev;
|
||||
private CancellationTokenSource? _cts;
|
||||
|
||||
private string _deviceName = "S7200";
|
||||
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 _cvPosI = 10; public double CvPosI { get => _cvPosI; set => SetProperty(ref _cvPosI, value); }
|
||||
private double _cvNegI = -10; public double CvNegI { get => _cvNegI; set => SetProperty(ref _cvNegI, value); }
|
||||
private double _ovpValue; public double OvpValue { get => _ovpValue; set => SetProperty(ref _ovpValue, value); }
|
||||
private double _ocpValue; public double OcpValue { get => _ocpValue; set => SetProperty(ref _ocpValue, 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 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 ClearProt { get; }
|
||||
public ICommand SetV { get; } public ICommand SetCC { get; } public ICommand SetCVPos { get; } public ICommand SetCVNeg { get; }
|
||||
public ICommand SetOVP { get; } public ICommand SetOCP { get; }
|
||||
public ICommand QMeas { get; }
|
||||
#endregion
|
||||
|
||||
public S7200ViewModel(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!.设置为远程模式(Ct()); Log("远程控制"); }));
|
||||
ClearProt = new DelegateCommand(async () => await Exec(async () => { await _dev!.清除保护状态(Ct()); Log("保护已清除"); }));
|
||||
SetV = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置电压(Voltage, Ct()); Log($"电压={Voltage}V"); }));
|
||||
SetCC = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置CC模式电流(Current, Ct()); Log($"CC电流={Current}A"); }));
|
||||
SetCVPos = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置CV正向电流(CvPosI, Ct()); Log($"CV正向电流={CvPosI}A"); }));
|
||||
SetCVNeg = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置CV反向电流(CvNegI, Ct()); Log($"CV反向电流={CvNegI}A"); }));
|
||||
SetOVP = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置电压保护OVP电压(OvpValue, Ct()); Log($"OVP={OvpValue}V"); }));
|
||||
SetOCP = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置电流保护OCP电流(OcpValue, Ct()); Log($"OCP={OcpValue}A"); }));
|
||||
QMeas = new DelegateCommand(async () => await Exec(async () =>
|
||||
{
|
||||
MeasuredVoltage = await _dev!.查询实时电压(Ct());
|
||||
MeasuredCurrent = await _dev!.查询实时电流(Ct());
|
||||
MeasuredPower = await _dev!.查询功率(Ct());
|
||||
Log($"测量→V:{MeasuredVoltage} I:{MeasuredCurrent} P:{MeasuredPower}");
|
||||
}));
|
||||
Initialize();
|
||||
}
|
||||
|
||||
#region 初始化 / Navigation
|
||||
|
||||
public void Initialize(string? deviceName = null)
|
||||
{
|
||||
S7200? found = null; string? fn = null;
|
||||
if (deviceName != null && _dm.DeviceMap.TryGetValue(deviceName, out var d) && d is S7200 e)
|
||||
{ found = e; fn = deviceName; }
|
||||
else
|
||||
{
|
||||
foreach (var kv in _dm.DeviceMap)
|
||||
if (kv.Value is S7200 it) { found = it; fn = kv.Key; break; }
|
||||
}
|
||||
_dev = found;
|
||||
DeviceName = fn ?? "S7200 (未找到)";
|
||||
IsConnected = _dev?.IsConnected ?? false;
|
||||
Log(found != null
|
||||
? $"已关联设备 [{DeviceName}],连接:{(IsConnected ? "已连接" : "未连接")}"
|
||||
: "未在 DeviceManager 中找到 S7200 设备");
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user