软件独立控制弹窗添加
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
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>
|
||||
/// ANEVH80 电子负载 控制面板 ViewModel
|
||||
/// </summary>
|
||||
public class ANEVH80ViewModel : NavigateViewModelBase, IDisposable
|
||||
{
|
||||
private readonly DeviceManager _dm;
|
||||
private ANEVH80? _dev;
|
||||
private CancellationTokenSource? _cts;
|
||||
|
||||
private string _deviceName = "ANEVH80";
|
||||
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 输入参数
|
||||
|
||||
#endregion
|
||||
|
||||
#region 测量结果
|
||||
private double _measuredValue;
|
||||
public double MeasuredValue { get => _measuredValue; set => SetProperty(ref _measuredValue, value); }
|
||||
private string _responseLog = "";
|
||||
public string ResponseLog { get => _responseLog; set => SetProperty(ref _responseLog, value); }
|
||||
#endregion
|
||||
|
||||
#region 命令
|
||||
public ICommand QueryIdn { get; } public ICommand Measure { get; }
|
||||
#endregion
|
||||
|
||||
public ANEVH80ViewModel(IContainerProvider cp) : base(cp)
|
||||
{
|
||||
_dm = cp.Resolve<DeviceManager>();
|
||||
QueryIdn = new DelegateCommand(async () => await Exec(async () => Log("IDN: 占位符设备")));
|
||||
Measure = new DelegateCommand(async () => await Exec(async () => { MeasuredValue = await _dev!.占位符(Ct()); Log($"测量值={MeasuredValue}"); }));
|
||||
Initialize();
|
||||
}
|
||||
|
||||
#region 初始化 / Navigation
|
||||
|
||||
public void Initialize(string? deviceName = null)
|
||||
{
|
||||
ANEVH80? found = null; string? fn = null;
|
||||
if (deviceName != null && _dm.DeviceMap.TryGetValue(deviceName, out var d) && d is ANEVH80 e)
|
||||
{ found = e; fn = deviceName; }
|
||||
else
|
||||
{
|
||||
foreach (var kv in _dm.DeviceMap)
|
||||
if (kv.Value is ANEVH80 it) { found = it; fn = kv.Key; break; }
|
||||
}
|
||||
_dev = found;
|
||||
DeviceName = fn ?? "ANEVH80 (未找到)";
|
||||
IsConnected = _dev?.IsConnected ?? false;
|
||||
Log(found != null
|
||||
? $"已关联设备 [{DeviceName}],连接:{(IsConnected ? "已连接" : "未连接")}"
|
||||
: "未在 DeviceManager 中找到 ANEVH80 设备");
|
||||
}
|
||||
|
||||
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