using System; using Prism.Mvvm; using Prism.Commands; using Panuon.UI.Silver; using SHJX.Service.Model.Enums; using SHJX.Service.Common.Constants; using SHJX.Service.Control.Interface; namespace SHJX.Service.Shell.ViewModels.Manual { public class LiquidManualWindowViewModel : BindableBase { #region Field private readonly IManualService _service; #endregion #region Properties #region In Wait private bool _sodium1OxalateInLiquidWait; public bool Sodium1OxalateInLiquidWait { get => _sodium1OxalateInLiquidWait; set => SetProperty(ref _sodium1OxalateInLiquidWait, value); } private bool _sodium2OxalateInLiquidWait; public bool Sodium2OxalateInLiquidWait { get => _sodium2OxalateInLiquidWait; set => SetProperty(ref _sodium2OxalateInLiquidWait, value); } //private bool _addLiquidPotassiumPermanganateInLiquidWait; //public bool AddLiquidPotassiumPermanganateInLiquidWait //{ // get => _addLiquidPotassiumPermanganateInLiquidWait; // set => SetProperty(ref _addLiquidPotassiumPermanganateInLiquidWait, value); //} private bool _titration1PotassiumPermanganateInLiquidWait; public bool Titration1PotassiumPermanganateInLiquidWait { get => _titration1PotassiumPermanganateInLiquidWait; set => SetProperty(ref _titration1PotassiumPermanganateInLiquidWait, value); } private bool _titration2PotassiumPermanganateInLiquidWait; public bool Titration2PotassiumPermanganateInLiquidWait { get => _titration2PotassiumPermanganateInLiquidWait; set => SetProperty(ref _titration2PotassiumPermanganateInLiquidWait, value); } #endregion #region Out Wait private bool _sodium1OxalateOutLiquidWait; public bool Sodium1OxalateOutLiquidWait { get => _sodium1OxalateOutLiquidWait; set => SetProperty(ref _sodium1OxalateOutLiquidWait, value); } private bool _sodium2OxalateOutLiquidWait; public bool Sodium2OxalateOutLiquidWait { get => _sodium2OxalateOutLiquidWait; set => SetProperty(ref _sodium2OxalateOutLiquidWait, value); } //private bool _addLiquidPotassiumPermanganateOutLiquidWait; //public bool AddLiquidPotassiumPermanganateOutLiquidWait //{ // get => _addLiquidPotassiumPermanganateOutLiquidWait; // set => SetProperty(ref _addLiquidPotassiumPermanganateOutLiquidWait, value); //} private bool _titration1PotassiumPermanganateOutLiquidWait; public bool Titration1PotassiumPermanganateOutLiquidWait { get => _titration1PotassiumPermanganateOutLiquidWait; set => SetProperty(ref _titration1PotassiumPermanganateOutLiquidWait, value); } private bool _titration2PotassiumPermanganateOutLiquidWait; public bool Titration2PotassiumPermanganateOutLiquidWait { get => _titration2PotassiumPermanganateOutLiquidWait; set => SetProperty(ref _titration2PotassiumPermanganateOutLiquidWait, value); } #endregion #endregion public LiquidManualWindowViewModel(IManualService service) { _service = service; Sodium1OxalateInLiquidWait = false; Sodium2OxalateInLiquidWait = false; //AddLiquidPotassiumPermanganateInLiquidWait = false; Titration1PotassiumPermanganateInLiquidWait = false; Titration2PotassiumPermanganateInLiquidWait = false; Sodium1OxalateOutLiquidWait = false; Sodium2OxalateOutLiquidWait = false; //AddLiquidPotassiumPermanganateOutLiquidWait = false; Titration1PotassiumPermanganateOutLiquidWait = false; Titration2PotassiumPermanganateOutLiquidWait = false; } #region Command private DelegateCommand _registerLiquidCommand; public DelegateCommand RegisterLiquidCommand => _registerLiquidCommand ??= new DelegateCommand(ExecuteRegisterLiquidCommand); private DelegateCommand _inLiquidCommand; public DelegateCommand InLiquidCommand => _inLiquidCommand ??= new DelegateCommand(ExecuteInLiquidCommand); private DelegateCommand _outLiquidCommand; public DelegateCommand OutLiquidCommand => _outLiquidCommand ??= new DelegateCommand(ExecuteOutLiquidCommand); private DelegateCommand _directLiquidCommand; public DelegateCommand DirectLiquidCommand => _directLiquidCommand ??= new DelegateCommand(ExecuteDirectLiquidCommand); private DelegateCommand _dropperFrontCommand; public DelegateCommand DropperFrontCommand => _dropperFrontCommand ??= new DelegateCommand(ExecuteDropperFrontCommand); private DelegateCommand _dropperInversionCommand; public DelegateCommand DropperInversionCommand => _dropperInversionCommand ??= new DelegateCommand(ExecuteDropperInversionCommand); private DelegateCommand _dropperPointCommand; public DelegateCommand DropperPointCommand => _dropperPointCommand ??= new DelegateCommand(ExecuteDropperPointCommand); private DelegateCommand _stirStartCommand; public DelegateCommand StirStartCommand => _stirStartCommand ??= new DelegateCommand(ExecuteStirStartCommand); private DelegateCommand _stirStopCommand; public DelegateCommand StirStopCommand => _stirStopCommand ??= new DelegateCommand(ExecuteStirStopCommand); private DelegateCommand _moveToYDCommand; public DelegateCommand MoveToYDCommand => _moveToYDCommand ??= new DelegateCommand(ExecuteMoveToYDCommand); private DelegateCommand _moveToJYCommand; public DelegateCommand MoveToJYCommand => _moveToJYCommand ??= new DelegateCommand(ExecuteMoveToJYCommand); private DelegateCommand _moveToDD2Command; public DelegateCommand MoveToDD2Command => _moveToDD2Command ??= new DelegateCommand(ExecuteMoveToDD2Command); #endregion #region Pump Execute #region 注射泵 /// /// 吸液 /// /// public async void ExecuteInLiquidCommand(object obj) { if (obj is null) { return; } object[] receiveObj = obj as object[]; if (receiveObj is null or { Length: 0 }) { return; } string typeName = receiveObj[0].ToString() switch { "草酸钠(滴定2)" => EquipmentNames.Sodium2Oxalate, "草酸钠(滴定1)" => EquipmentNames.Sodium1Oxalate, "高锰酸钾(加液滴定2)" => EquipmentNames.Titration2PotassiumPermanganate, "高锰酸钾(滴定1)" => EquipmentNames.Titration1PotassiumPermanganate, _ => throw new ArgumentException("未找到相关对应"), }; try { switch (typeName) { case EquipmentNames.Sodium1Oxalate: Sodium1OxalateInLiquidWait = true; break; case EquipmentNames.Sodium2Oxalate: Sodium2OxalateInLiquidWait = true; break; case EquipmentNames.Titration1PotassiumPermanganate: Titration1PotassiumPermanganateInLiquidWait = true; break; case EquipmentNames.Titration2PotassiumPermanganate: Titration2PotassiumPermanganateInLiquidWait = true; break; default: throw new ArgumentNullException(typeName); } if (typeName==EquipmentNames.Sodium1Oxalate|| typeName == EquipmentNames.Sodium2Oxalate) { await _service.ManualLiquidExecuteOxalate(typeName,LiquidSwitchPattern.In, receiveObj[1]); } else await _service.ManualLiquidExecute(typeName, LiquidSwitchPattern.In, receiveObj[1]); } catch (Exception) { Notice.Show("出了点状况,请重新再试~", "Error", 3, MessageBoxIcon.Error); } finally { switch (typeName) { case EquipmentNames.Sodium1Oxalate: Sodium1OxalateInLiquidWait = false; break; case EquipmentNames.Sodium2Oxalate: Sodium2OxalateInLiquidWait = false; break; case EquipmentNames.Titration1PotassiumPermanganate: Titration1PotassiumPermanganateInLiquidWait = false; break; case EquipmentNames.Titration2PotassiumPermanganate: Titration2PotassiumPermanganateInLiquidWait = false; break; } } } /// /// 出液 /// /// public async void ExecuteOutLiquidCommand(object obj) { if (obj is null) { return; } object[] receiveObj = obj as object[]; if (receiveObj is null or { Length: 0 }) { return; } string typeName = receiveObj[0].ToString() switch { "草酸钠(滴定2)" => EquipmentNames.Sodium2Oxalate, "草酸钠(滴定1)" => EquipmentNames.Sodium1Oxalate, "高锰酸钾(加液滴定2)" => EquipmentNames.Titration2PotassiumPermanganate, "高锰酸钾(滴定1)" => EquipmentNames.Titration1PotassiumPermanganate, _ => throw new ArgumentException("未找到相关对应"), }; try { switch (typeName) { case EquipmentNames.Sodium1Oxalate: Sodium1OxalateOutLiquidWait = true; break; case EquipmentNames.Sodium2Oxalate: Sodium2OxalateOutLiquidWait = true; break; case EquipmentNames.Titration1PotassiumPermanganate: Titration1PotassiumPermanganateOutLiquidWait = true; break; case EquipmentNames.Titration2PotassiumPermanganate: Titration2PotassiumPermanganateOutLiquidWait = true; break; default: throw new ArgumentNullException(typeName); } if (typeName == EquipmentNames.Sodium1Oxalate || typeName == EquipmentNames.Sodium2Oxalate) { await _service.ManualLiquidExecuteOxalate(typeName, LiquidSwitchPattern.Out, receiveObj[1]); } else await _service.ManualLiquidExecute(typeName, LiquidSwitchPattern.Out, receiveObj[1]); } catch (Exception) { Notice.Show("出了点状况,请重新再试~", "Error", 3, MessageBoxIcon.Error); } finally { switch (typeName) { case EquipmentNames.Sodium1Oxalate: Sodium1OxalateOutLiquidWait = false; break; case EquipmentNames.Sodium2Oxalate: Sodium2OxalateOutLiquidWait = false; break; case EquipmentNames.Titration1PotassiumPermanganate: Titration1PotassiumPermanganateOutLiquidWait = false; break; case EquipmentNames.Titration2PotassiumPermanganate: Titration2PotassiumPermanganateOutLiquidWait = false; break; } } } #endregion #region 柱塞泵(本项目未使用) /// /// 柱塞泵 /// /// private void ExecuteDirectLiquidCommand(object obj) { if (obj is null) { return; } object[] receiveObj = obj as object[]; if (receiveObj is null or { Length: 0 }) { return; } string typeName = receiveObj[0].ToString() switch { "硫酸亚铁铵" => "FAS", _ => throw new ArgumentException("未找到相关对应"), }; _service.ManualDirectOutLiquidAsync(typeName, receiveObj[1]); } #endregion #region 蠕动泵 /// /// 蠕动泵 /// /// private void ExecuteRegisterLiquidCommand(object obj) { if (obj is null) { return; } object[] receiveObj = obj as object[]; if (receiveObj is null or { Length: 0 }) { return; } double receiveValue = Math.Abs(Convert.ToDouble(receiveObj[1])); (string motorName, double value) = receiveObj[0].ToString()?.Trim() switch { "氢氧化钠(加液)" => (EquipmentNames.Sodium2Hydroxide, receiveValue), "氢氧化钠(滴定1)" => (EquipmentNames.Sodium1Hydroxide, receiveValue), "硫酸(加液滴定2)" => (EquipmentNames.Titration2Vitriol, receiveValue), "硫酸(滴定1)" => (EquipmentNames.Titration1Vitriol, receiveValue), _ => throw new ArgumentNullException(receiveObj[0].ToString()?.Trim()) }; _service.ManualWriteRegisterAsync(motorName, value); } #endregion #region 其他 /// /// 搅拌开始 /// /// private void ExecuteStirStartCommand(object obj) { OperationRegister(obj, "Stir", RegisterExecuteType.Open); } /// /// 搅拌结束 /// private void ExecuteStirStopCommand(object obj) { OperationRegister(obj, "Stir", RegisterExecuteType.Close); } /// /// 滴嘴移动到原点 /// private void ExecuteMoveToYDCommand(object obj) { MoveDropper("OrignalPoint"); } /// /// 滴嘴移动到加液 /// private void ExecuteMoveToJYCommand(object obj) { MoveDropper("AddLiquid"); } /// /// 滴嘴移动到滴定2 /// private void ExecuteMoveToDD2Command(object obj) { MoveDropper("Titration2"); } private async void MoveDropper(Object obj) { if (obj.ToString()?.Trim() == "OrignalPoint") { bool res3 = await _service.MotorGoBackAsync(EquipmentNames.AxisD); Notice.Show($"返回原点{(res3 ? "成功" : "失败")}", "Info", 3, MessageBoxIcon.Info); } else { double value = obj.ToString()?.Trim() switch { "Titration2" => -1*70, "AddLiquid" => -1*170, _ => throw new ArgumentNullException(obj.ToString()?.Trim()) }; bool res2 = await _service.ManualWriteMoveToDMotor(EquipmentNames.AxisD, value, MotorManualMoveType.Forward); Notice.Show($"滴嘴运动到位{(res2 ? "成功" : "失败")}", "Info", 3, MessageBoxIcon.Info); } } /// /// 滴嘴正向 /// /// private void ExecuteDropperFrontCommand(object obj) { string epType = obj.ToString() switch { "《" => "TitrationMove", "-" => "TitrationStop", "正" => "Titration", _ => throw new ArgumentNullException(obj.ToString()), }; _service.ElectricDropper(epType, RegisterExecuteType.Open); } /// /// 滴嘴反向 /// /// private void ExecuteDropperInversionCommand(object obj) { string epType = obj.ToString() switch { "》" => "TitrationMove", "反" => "Titration", _ => throw new ArgumentNullException(obj.ToString()), }; _service.ElectricDropper(epType, RegisterExecuteType.Close); } private void ExecuteDropperPointCommand(object obj) { string epType = obj.ToString() switch { "原点" => "Titration", _ => throw new ArgumentNullException(obj.ToString()), }; _service.ElectricDropper(epType, RegisterExecuteType.Point); } #endregion #endregion #region Method /// /// 寄存器操作 /// /// 名称 /// 类型 /// 操作符 private bool OperationRegister(object obj, string element, RegisterExecuteType type) { ConvertToEquipmentType(obj, out string epType); return _service.ManualControlRegister(string.Concat(epType, element), type); } /// /// 获取设备名称 /// private static void ConvertToEquipmentType(object obj, out string epType) { epType = obj.ToString() switch { "加液" => "AddLiquid", "滴定" => "Titration", "滴定2" => "Titration2", _ => throw new ArgumentNullException(obj.ToString()), }; } #endregion } }