using System; using System.Linq; using System.Collections; using System.Windows.Controls; using System.Collections.Generic; using System.Collections.ObjectModel; using Prism.Mvvm; using Prism.Commands; using Panuon.UI.Silver; using SHJX.Service.Model.Enums; using SHJX.Service.Control.Interface; using SHJX.Service.Model.CRUDModules; using SHJX.Service.Shell.Views.Manual; using SHJX.Service.Common.Constants; namespace SHJX.Service.Shell.ViewModels.Manual { public class MotorManualWindowViewModel : BindableBase { #region Fields private readonly IManualService _service; #endregion #region Properties public ObservableCollection Areas { get; set; } public ObservableCollection PositionHotlists { get; set; } public ObservableCollection SpeedNames { get; set; } public List AreaPoint { get; set; } private ComboBoxItem _areaCurrentPoint; public ComboBoxItem AreaCurrentPoint { get => _areaCurrentPoint; set => SetProperty(ref _areaCurrentPoint, value); } public EquipmentArea Area { get => Areas.FirstOrDefault(item => item.PointName.Equals(AreaCurrentPoint.Content)); set { Areas.Where(item => item.PointName.Equals(AreaCurrentPoint.Content)).ToArray()[0] = value; RaisePropertyChanged(nameof(Area)); } } #endregion public MotorManualWindowViewModel(IManualService service) { _service = service; AreaPoint = new List(); Areas = new ObservableCollection(_service.GetData()); PositionHotlists = new ObservableCollection(_service.GetData()); SpeedNames = new ObservableCollection(_service.GetData()); (from EquipmentArea item in Areas select item.PointName).ToList().ForEach(item => { AreaPoint.Add(new ComboBoxItem() { Content = item }); }); AreaCurrentPoint = AreaPoint.FirstOrDefault(); } #region Command private DelegateCommand _forwardCommand; public DelegateCommand ForwardCommand => _forwardCommand ??= new DelegateCommand(ExecuteForwardCommand); private DelegateCommand _inversionCommand; public DelegateCommand InversionCommand => _inversionCommand ??= new DelegateCommand(ExecuteInversionCommand); private DelegateCommand _areaSelectionChangedCommand; public DelegateCommand AreaSelectionChangedCommand => _areaSelectionChangedCommand ??= new DelegateCommand((obj) => { Area = Areas.FirstOrDefault(item => item.PointName.Equals(AreaCurrentPoint.Content)); }); private DelegateCommand _motorGoBackCommand; public DelegateCommand MotorGoBackCommand => _motorGoBackCommand ??= new DelegateCommand(ExecuteMotorGoBackCommand); private DelegateCommand _motorStopCommand; public DelegateCommand MotorStopCommand => _motorStopCommand ??= new DelegateCommand(ExecuteMotorStopCommand); private DelegateCommand _updateAreaCommand; public DelegateCommand UpdateAreaCommand => _updateAreaCommand ??= new DelegateCommand((obj) => { EquipmentArea area = Areas.FirstOrDefault(item => item.PointName.Equals(AreaCurrentPoint.Content)); bool res = _service.UpdateData(area); PositionHotlist ph = PositionHotlists.FirstOrDefault(item => item.PositionName.Equals(AreaCurrentPoint.Content)); if (ph != null) { ph.Enable = area.Enable; if (ph.Enable) ph.CurrentOccupy = ""; res = _service.UpdateData(ph); } _service.UpdateAreaAllData(area); Notice.Show($"{area.PointName}区域信息更新{(res ? "成功" : "失败")}", "Info", 3, MessageBoxIcon.Info); }); public DelegateCommand _speedSettingCommand; public DelegateCommand SpeedSettingCommand => _speedSettingCommand ??= new DelegateCommand(ExecuteSpeedSettingCommand); public DelegateCommand _maniManualControlCommand; public DelegateCommand ManiManualControlCommand => _maniManualControlCommand ??= new DelegateCommand(ExecuteManiManualControlCommand); public DelegateCommand _singleMoveCommand; public DelegateCommand SingleMoveCommand => _singleMoveCommand ??= new DelegateCommand(() => { CustomMoveWindow customMove = new(AreaCurrentPoint.Content.ToString()); customMove.ShowDialog(); }); #endregion #region Execute /// /// 机械抓手操作 /// /// private void ExecuteManiManualControlCommand(object obj) { RegisterExecuteType type = obj.ToString() switch { "杯抓" => RegisterExecuteType.Open, "杯松" => RegisterExecuteType.Close, _ => throw new ArgumentNullException(obj.ToString()) }; _service.ManualControlRegister(EquipmentNames.Tongs, type); } /// /// 电机正转 /// /// public async void ExecuteForwardCommand(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 { "X轴" => (EquipmentNames.AxisX, -1 * receiveValue), "Y轴" => (EquipmentNames.AxisY, -1 * receiveValue), "Z轴" => (EquipmentNames.AxisZ, -1 * receiveValue), _ => throw new ArgumentNullException(receiveObj[0].ToString()?.Trim()) }; bool res = await _service.ManualWriteMoveToMotorAsync(motorName, value, MotorManualMoveType.Forward); Notice.Show($"{receiveObj[0]}正转{(res ? "成功" : "失败")}", "Info", 3, MessageBoxIcon.Info); } /// /// 电机反转 /// /// public async void ExecuteInversionCommand(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 { "X轴" => (EquipmentNames.AxisX, receiveValue), "Y轴" => (EquipmentNames.AxisY, receiveValue), "Z轴" => (EquipmentNames.AxisZ, receiveValue), _ => throw new ArgumentNullException(receiveObj[0].ToString()?.Trim()) }; bool res = await _service.ManualWriteMoveToMotorAsync(motorName, value, MotorManualMoveType.Inversion); Notice.Show($"{receiveObj[0]}反转{(res ? "成功" : "失败")}", "Info", 3, MessageBoxIcon.Info); } /// /// 返回原点 /// /// private async void ExecuteMotorGoBackCommand(object obj) { string backName = obj.ToString() switch { "X轴" => EquipmentNames.AxisX, "Y轴" => EquipmentNames.AxisY, "Z轴" => EquipmentNames.AxisZ, _ => throw new ArgumentNullException(obj.ToString()), }; bool res = await _service.MotorGoBackAsync(backName); Notice.Show($"{obj}返回原点{(res ? "成功" : "失败")}", "Info", 3, MessageBoxIcon.Info); } /// /// 电机停止 /// /// private void ExecuteMotorStopCommand(object obj) { string backName = obj.ToString() switch { "X轴" => EquipmentNames.AxisX, "Y轴" => EquipmentNames.AxisY, "Z轴" => EquipmentNames.AxisZ, _ => throw new ArgumentNullException(obj.ToString()), }; _service.MotorStop(backName); } public async void ExecuteSpeedSettingCommand(object obj) { if (obj is null) { return; } IList lists = obj as IList; if (lists is null or { Count: 0 }) { Notice.Show($"未选择数据,无法设置!", "Error", 3, MessageBoxIcon.Error); return; } bool res = true; foreach (MotorSpeed speed in from object item in lists where item is MotorSpeed let speed = item as MotorSpeed select speed) { res = res && await _service.UpdateSpeedAsync(speed.DetailName); } Notice.Show($"速度设置{(res ? "成功" : "失败")}", "Info", 3, MessageBoxIcon.Info); } #endregion } }