| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- 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<EquipmentArea> Areas { get; set; }
- public ObservableCollection<PositionHotlist> PositionHotlists { get; set; }
- public ObservableCollection<MotorSpeed> SpeedNames { get; set; }
- public List<ComboBoxItem> 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<ComboBoxItem>();
- Areas = new ObservableCollection<EquipmentArea>(_service.GetData<EquipmentArea>());
- PositionHotlists = new ObservableCollection<PositionHotlist>(_service.GetData<PositionHotlist>());
- SpeedNames = new ObservableCollection<MotorSpeed>(_service.GetData<MotorSpeed>());
- (from EquipmentArea item in Areas select item.PointName).ToList().ForEach(item =>
- {
- AreaPoint.Add(new ComboBoxItem() { Content = item });
- });
- AreaCurrentPoint = AreaPoint.FirstOrDefault();
- }
- #region Command
- private DelegateCommand<object> _forwardCommand;
- public DelegateCommand<object> ForwardCommand => _forwardCommand ??= new DelegateCommand<object>(ExecuteForwardCommand);
- private DelegateCommand<object> _inversionCommand;
- public DelegateCommand<object> InversionCommand => _inversionCommand ??= new DelegateCommand<object>(ExecuteInversionCommand);
- private DelegateCommand<object> _areaSelectionChangedCommand;
- public DelegateCommand<object> AreaSelectionChangedCommand => _areaSelectionChangedCommand ??= new DelegateCommand<object>((obj) =>
- {
- Area = Areas.FirstOrDefault(item => item.PointName.Equals(AreaCurrentPoint.Content));
- });
- private DelegateCommand<object> _motorGoBackCommand;
- public DelegateCommand<object> MotorGoBackCommand => _motorGoBackCommand ??= new DelegateCommand<object>(ExecuteMotorGoBackCommand);
- private DelegateCommand<object> _motorStopCommand;
- public DelegateCommand<object> MotorStopCommand => _motorStopCommand ??= new DelegateCommand<object>(ExecuteMotorStopCommand);
- private DelegateCommand<object> _updateAreaCommand;
- public DelegateCommand<object> UpdateAreaCommand => _updateAreaCommand ??= new DelegateCommand<object>((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<object> _speedSettingCommand;
- public DelegateCommand<object> SpeedSettingCommand => _speedSettingCommand ??= new DelegateCommand<object>(ExecuteSpeedSettingCommand);
- public DelegateCommand<object> _maniManualControlCommand;
- public DelegateCommand<object> ManiManualControlCommand => _maniManualControlCommand ??= new DelegateCommand<object>(ExecuteManiManualControlCommand);
- public DelegateCommand _singleMoveCommand;
- public DelegateCommand SingleMoveCommand => _singleMoveCommand ??= new DelegateCommand(() =>
- {
- CustomMoveWindow customMove = new(AreaCurrentPoint.Content.ToString());
- customMove.ShowDialog();
- });
- #endregion
- #region Execute
- /// <summary>
- /// 机械抓手操作
- /// </summary>
- /// <param name="obj"></param>
- private void ExecuteManiManualControlCommand(object obj)
- {
- RegisterExecuteType type = obj.ToString() switch
- {
- "杯抓" => RegisterExecuteType.Open,
- "杯松" => RegisterExecuteType.Close,
- _ => throw new ArgumentNullException(obj.ToString())
- };
- _service.ManualControlRegister(EquipmentNames.Tongs, type);
- }
- /// <summary>
- /// 电机正转
- /// </summary>
- /// <param name="obj"></param>
- 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);
- }
- /// <summary>
- /// 电机反转
- /// </summary>
- /// <param name="obj"></param>
- 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);
- }
- /// <summary>
- /// 返回原点
- /// </summary>
- /// <param name="obj"></param>
- 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);
- }
- /// <summary>
- /// 电机停止
- /// </summary>
- /// <param name="obj"></param>
- 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
- }
- }
|