Files
ACP/DeviceEditModule/ViewModels/RLT1000ViewModel.cs
T

132 lines
5.5 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>
/// RLT1000 环境箱 控制面板 ViewModel
/// </summary>
public class RLT1000ViewModel : NavigateViewModelBase, IDisposable
{
private readonly DeviceManager _dm;
private RLT1000? _dev;
private CancellationTokenSource? _cts;
private string _deviceName = "RLT1000";
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 float _targetTemp = 25f; public float TargetTemp { get => _targetTemp; set => SetProperty(ref _targetTemp, value); }
private float _targetHumidity = 50f; public float TargetHumidity { get => _targetHumidity; set => SetProperty(ref _targetHumidity, value); }
#endregion
#region 测量结果
private double _currentTemp;
public double CurrentTemp { get => _currentTemp; set => SetProperty(ref _currentTemp, value); }
private double _currentHumidity;
public double CurrentHumidity { get => _currentHumidity; set => SetProperty(ref _currentHumidity, value); }
private string _responseLog = "";
public string ResponseLog { get => _responseLog; set => SetProperty(ref _responseLog, value); }
#endregion
#region 命令
public ICommand PowerOn { get; } public ICommand PowerOff { get; }
public ICommand SetRemote { get; } public ICommand SetLocal { get; }
public ICommand SetTemp { get; } public ICommand SetHumidity { get; }
public ICommand ReadAll { get; }
#endregion
public RLT1000ViewModel(IContainerProvider cp) : base(cp)
{
_dm = cp.Resolve<DeviceManager>();
PowerOn = new DelegateCommand(async () => await Exec(async () => { await _dev!.远程模式开机(Ct()); Log("环境箱已开机"); }));
PowerOff = new DelegateCommand(async () => await Exec(async () => { await _dev!.远程模式关机(Ct()); Log("环境箱已关机"); }));
SetRemote = new DelegateCommand(async () => await Exec(async () => { await _dev!.切换为远程模式(Ct()); Log("远程控制"); }));
SetLocal = new DelegateCommand(async () => await Exec(async () => { await _dev!.切换为本地模式(Ct()); Log("本地控制"); }));
SetTemp = new DelegateCommand(async () => await Exec(async () => { await _dev!.定值温度设定(TargetTemp, Ct()); Log($"温度={TargetTemp}℃"); }));
SetHumidity = new DelegateCommand(async () => await Exec(async () => { await _dev!.定值湿度设定(TargetHumidity, Ct()); Log($"湿度={TargetHumidity}%RH"); }));
ReadAll = new DelegateCommand(async () => await Exec(async () =>
{
CurrentTemp = await _dev!.读取当前温度(Ct());
CurrentHumidity = await _dev!.读取当前湿度(Ct());
Log($"环境→T:{CurrentTemp}℃ H:{CurrentHumidity}%RH");
}));
Initialize();
}
#region 初始化 / Navigation
public void Initialize(string? deviceName = null)
{
RLT1000? found = null; string? fn = null;
if (deviceName != null && _dm.DeviceMap.TryGetValue(deviceName, out var d) && d is RLT1000 e)
{ found = e; fn = deviceName; }
else
{
foreach (var kv in _dm.DeviceMap)
if (kv.Value is RLT1000 it) { found = it; fn = kv.Key; break; }
}
_dev = found;
DeviceName = fn ?? "RLT1000 (未找到)";
IsConnected = _dev?.IsConnected ?? false;
Log(found != null
? $"已关联设备 [{DeviceName}],连接:{(IsConnected ? "已连接" : "未连接")}"
: "未在 DeviceManager 中找到 RLT1000 设备");
}
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();
}
}
}