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 { /// /// MCc30W 水冷机一拖三 控制面板 ViewModel /// public class MCc30WViewModel : NavigateViewModelBase, IDisposable { private readonly DeviceManager _dm; private MCc30W? _dev; private CancellationTokenSource? _cts; private string _deviceName = "MCc30W"; 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 int _loop = 1; public int Loop { get => _loop; set => SetProperty(ref _loop, value); } private float _tempSet = 25f; public float TempSet { get => _tempSet; set => SetProperty(ref _tempSet, value); } private float _flowSet = 10f; public float FlowSet { get => _flowSet; set => SetProperty(ref _flowSet, value); } private float _pressSet = 100f; public float PressSet { get => _pressSet; set => SetProperty(ref _pressSet, value); } #endregion #region 测量结果 private float _currentTemp; public float CurrentTemp { get => _currentTemp; set => SetProperty(ref _currentTemp, value); } private float _currentFlow; public float CurrentFlow { get => _currentFlow; set => SetProperty(ref _currentFlow, value); } private float _currentPress; public float CurrentPress { get => _currentPress; set => SetProperty(ref _currentPress, value); } private uint _alarmInfo; public uint AlarmInfo { get => _alarmInfo; set => SetProperty(ref _alarmInfo, value); } private string _responseLog = ""; public string ResponseLog { get => _responseLog; set => SetProperty(ref _responseLog, value); } #endregion #region 命令 public ICommand LoopOn { get; } public ICommand LoopOff { get; } public ICommand SetTemp { get; } public ICommand SetFlow { get; } public ICommand SetPress { get; } public ICommand AlarmReset { get; } public ICommand AlarmSilence { get; } public ICommand ReadAll { get; } public ICommand ReadAlarm { get; } #endregion public MCc30WViewModel(IContainerProvider cp) : base(cp) { _dm = cp.Resolve(); LoopOn = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置回路运行Async(Loop, true, Ct()); Log($"回路{Loop}已启动"); })); LoopOff = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置回路运行Async(Loop, false, Ct()); Log($"回路{Loop}已停止"); })); SetTemp = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置出液目标温度Async(Loop, TempSet, Ct()); Log($"回路{Loop}温度={TempSet}℃"); })); SetFlow = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置出液目标流量Async(Loop, FlowSet, Ct()); Log($"回路{Loop}流量={FlowSet}L/min"); })); SetPress = new DelegateCommand(async () => await Exec(async () => { await _dev!.设置出液目标压力Async(Loop, PressSet, Ct()); Log($"回路{Loop}压力={PressSet}kPa"); })); AlarmReset = new DelegateCommand(async () => await Exec(async () => { await _dev!.报警复位Async(true, Ct()); await _dev!.报警复位Async(false, Ct()); Log("报警已复位"); })); AlarmSilence = new DelegateCommand(async () => await Exec(async () => { await _dev!.报警消音Async(true, Ct()); Log("报警已消音"); })); ReadAll = new DelegateCommand(async () => await Exec(async () => { CurrentTemp = await _dev!.读取出液当前温度Async(Loop, Ct()); CurrentFlow = await _dev!.读取出液当前流量Async(Loop, Ct()); CurrentPress = await _dev!.读取出液当前压力Async(Loop, Ct()); Log($"回路{Loop}→T:{CurrentTemp}℃ F:{CurrentFlow}L/min P:{CurrentPress}kPa"); })); ReadAlarm = new DelegateCommand(async () => await Exec(async () => { AlarmInfo = await _dev!.读报警信息1Async(Ct()); Log($"报警信息={AlarmInfo}"); })); Initialize(); } #region 初始化 / Navigation public void Initialize(string? deviceName = null) { MCc30W? found = null; string? fn = null; if (deviceName != null && _dm.DeviceMap.TryGetValue(deviceName, out var d) && d is MCc30W e) { found = e; fn = deviceName; } else { foreach (var kv in _dm.DeviceMap) if (kv.Value is MCc30W it) { found = it; fn = kv.Key; break; } } _dev = found; DeviceName = fn ?? "MCc30W (未找到)"; IsConnected = _dev?.IsConnected ?? false; Log(found != null ? $"已关联设备 [{DeviceName}],连接:{(IsConnected ? "已连接" : "未连接")}" : "未在 DeviceManager 中找到 MCc30W 设备"); } public override void OnNavigatedTo(NavigationContext context) { var pName = context.Parameters.GetValue("DeviceName"); Initialize(pName); } #endregion #region 辅助 private CancellationToken Ct() => (_cts = new CancellationTokenSource(TimeSpan.FromSeconds(10))).Token; private async Task Exec(Func 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(); } } }