using Prism.Commands; using Prism.Ioc; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Linq; using Team.FFFeederService.Interfaces; using Team.FFFeederService; using TeamAAS_VP.Core; using TeamAAS_VP.Interfaces; using System.Security.Cryptography; using TeamAAS_VP.Events; using Prism.Events; using System.Windows; using TeamAAS_VP.Resources.Languages; namespace TeamAAS_VP.ViewModels.DebugMod { public class RobotManualStepViewModel : BindableBase { IContainerProvider _container; IEventAggregator _eventAggregator; #region 属性 public Management management { get; set; } public RobotService robotService { get; set; } private IRobot _Robot; public IRobot Robot { get { return _Robot; } set { SetProperty(ref _Robot, value); try { var config = FileHelper.ReadApplicationConfiguration(); if (config.RobotJogSetpDistance.TryGetValue(value.Id, out double distance)) { this.Distance = distance; } } catch (Exception) { } } } private double _Distance = 1; public double Distance { get { return _Distance; } set { SetProperty(ref _Distance, value); try { if (Robot == null) { return; } var config = FileHelper.ReadApplicationConfiguration(); if (config.RobotJogSetpDistance.ContainsKey(Robot.Id)) { config.RobotJogSetpDistance[Robot.Id] = value; } else { config.RobotJogSetpDistance.Add(Robot.Id, value); } FileHelper.SaveApplicationConfiguration(config); } catch (Exception) { } } } private bool CanExecute { get { if (Robot==null) return false; else return Robot.CanExecute; } } #endregion #region 命令 private DelegateCommand _LoadedCommand; public DelegateCommand LoadedCommand => _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand)); private DelegateCommand _ConnectCommand; public DelegateCommand ConnectCommand => _ConnectCommand ?? (_ConnectCommand = new DelegateCommand(ExecuteConnectCommand)); private DelegateCommand _DisConnectCommand; public DelegateCommand DisConnectCommand => _DisConnectCommand ?? (_DisConnectCommand = new DelegateCommand(ExecuteDisConnectCommand)); private DelegateCommand _MotorCommand; public DelegateCommand MotorCommand => _MotorCommand ?? (_MotorCommand = new DelegateCommand(ExecuteMotorCommand).ObservesCanExecute(()=> CanExecute).ObservesProperty(()=> Robot).ObservesProperty(()=> Robot.CanExecute)); private DelegateCommand _ResetCommand; public DelegateCommand ResetCommand => _ResetCommand ?? (_ResetCommand = new DelegateCommand(ExecuteResetCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute)); private DelegateCommand _JogCommand; public DelegateCommand JogCommand => _JogCommand ?? (_JogCommand = new DelegateCommand(ExecuteJogCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute)); private DelegateCommand _sFreeCommand; public DelegateCommand sFreeCommand => _sFreeCommand ?? (_sFreeCommand = new DelegateCommand(ExecutesFreeCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute)); private DelegateCommand _SLockCommand; public DelegateCommand SLockCommand => _SLockCommand ?? (_SLockCommand = new DelegateCommand(ExecuteSLockCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute)); #endregion #region 事件 #endregion public RobotManualStepViewModel(IContainerProvider container, IEventAggregator ea) { _container = container; _eventAggregator = ea; _container.Resolve().PropertyChanged += RobotManualStepViewModel_PropertyChanged; ; } #region 方法 void ExecuteLoadedCommand() { try { if (_container.Resolve().CurrentUser.userPart == Enums.UserPart.Operator) { IsAllowEdit = false; } else { IsAllowEdit = true; } management = _container.Resolve(); robotService = management.RobotService; var srobot = _container.Resolve().SelectRobot; if (srobot != null) { Robot = robotService.GetRobot(srobot.Id); } } catch (Exception) { } } /// /// 连接机器人 /// private async void ExecuteConnectCommand() { if (Robot == null || !Robot.IsConnected) return; try { await Robot.ConnectAsync(); _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = $"{Lang.连接成功}!", Duration = 1 }); } catch (Exception ex) { _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 }); } } /// /// 断开机器人 /// private void ExecuteDisConnectCommand() { if (Robot == null || !Robot.IsConnected) return; try { Robot.Disconnect(); _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = $"{Lang.已断开}!", Duration = 1 }); } catch (Exception ex) { _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 }); } } /// /// 电机 /// /// private async void ExecuteMotorCommand(string obj) { if (Robot == null || !Robot.IsConnected) return; try { await Robot.MotorAsync(Convert.ToBoolean(obj)); _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 }); } catch (Exception ex) { _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 }); } } /// /// 重置 /// private async void ExecuteResetCommand() { if (Robot == null || !Robot.IsConnected) return; try { await Robot.ResetAsync(); _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 }); } catch (Exception ex) { _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 }); } } /// /// 点动 /// /// private async void ExecuteJogCommand(object obj) { if (Robot == null || !Robot.IsConnected) return; try { Robot.Timeout = 6000000; var items = ((string)obj).Split(':'); switch (items[0]) { case "X+": await Robot.JogAsync("X", double.Parse(items[1])); break; case "X-": await Robot.JogAsync("X", -double.Parse(items[1])); break; case "Y+": await Robot.JogAsync("Y", double.Parse(items[1])); break; case "Y-": await Robot.JogAsync("Y", -double.Parse(items[1])); break; case "Z+": await Robot.JogAsync("Z", double.Parse(items[1])); break; case "Z-": await Robot.JogAsync("Z", -double.Parse(items[1])); break; case "U+": await Robot.JogAsync("U", double.Parse(items[1])); break; case "U-": await Robot.JogAsync("U", -double.Parse(items[1])); break; case "V+": await Robot.JogAsync("V", double.Parse(items[1])); break; case "V-": await Robot.JogAsync("V", -double.Parse(items[1])); break; case "W+": await Robot.JogAsync("W", double.Parse(items[1])); break; case "W-": await Robot.JogAsync("W", -double.Parse(items[1])); break; default: break; } Robot.Timeout = 5000; _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 }); } catch (Exception ex) { _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 }); } } /// /// 锁定刹车 /// private async void ExecuteSLockCommand() { if (Robot == null || !Robot.IsConnected) return; try { await Robot.SLockAsync(); _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 }); } catch (Exception ex) { _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 }); } } /// /// 释放刹车 /// private async void ExecutesFreeCommand() { if (Robot == null || !Robot.IsConnected) return; try { await Robot.SFreeAsync(); _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 }); } catch (Exception ex) { _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 }); } } private void RobotManualStepViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { if (e.PropertyName == "SelectRobot") { management = _container.Resolve(); robotService = management.RobotService; var srobot = _container.Resolve().SelectRobot; if(srobot != null) { Robot = robotService.GetRobot(srobot.Id); } } } #endregion private bool _IsAllowEdit = false; public bool IsAllowEdit { get { return _IsAllowEdit; } set { SetProperty(ref _IsAllowEdit, value); } } private void LoginChange(Models.User user) { if (user.userPart == Enums.UserPart.Operator) { IsAllowEdit = false; } else { IsAllowEdit = true; } } } }