using Prism.Commands; using Prism.Events; using Prism.Ioc; using Prism.Mvvm; using Prism.Regions; using Prism.Unity; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Windows; using TeamAAS_VP.Core.Robots; using TeamAAS_VP.Events; using TeamAAS_VP.Interfaces; using TeamAAS_VP.Resources.Languages; namespace TeamAAS_VP.ViewModels.DebugMod { public class PlcRobotManualStepViewModel : BindableBase, IConfirmNavigationRequest { IContainerProvider _container; IEventAggregator _eventAggregator; IRobotService _robotService; ISystemDatabaseService _systemDatabaseService; IConfigService _configService; public PlcRobotManualStepViewModel(IContainerProvider container, IEventAggregator ea, IRobotService robotService, ISystemDatabaseService systemDatabaseService, IConfigService configService) { _container = container; _eventAggregator = ea; _robotService = robotService; _systemDatabaseService = systemDatabaseService; _configService = configService; //订阅权限登录事件 _eventAggregator.GetEvent().Subscribe(LoginChange); } #region 属性 private ObservableCollection _AdditionalAxisList; public ObservableCollection AdditionalAxisList { get { return _AdditionalAxisList; } set { SetProperty(ref _AdditionalAxisList, value); } } private XYZU_Robot _Robot; public XYZU_Robot Robot { get { return _Robot; } set { SetProperty(ref _Robot, value); try { if (value != null) { this.Distance = _configService.GetRobotStepDistance(value.Id); } } catch (Exception) { } } } private double _Distance = 1; public double Distance { get { return _Distance; } set { SetProperty(ref _Distance, value); try { if (Robot == null) { return; } if (_configService == null) _configService = ((PrismApplication)(App.Current)).Container.Resolve(); _configService.UpdateRobotStepDistance(Robot.Id, value); } catch (Exception) { } } } #endregion #region 命令 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 _AdditionalAxisJogPlusCommand; public DelegateCommand AdditionalAxisJogPlusCommand => _AdditionalAxisJogPlusCommand ?? (_AdditionalAxisJogPlusCommand = new DelegateCommand(ExecuteAdditionalAxisJogPlusCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute)); //附加轴,轴点动-命令 private DelegateCommand _AdditionalAxisJogMinusCommand; public DelegateCommand AdditionalAxisJogMinusCommand => _AdditionalAxisJogMinusCommand ?? (_AdditionalAxisJogMinusCommand = new DelegateCommand(ExecuteAdditionalAxisJogMinusCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute)); private DelegateCommand _AdditionalAxisMotorOnCommand; public DelegateCommand AdditionalAxisMotorOnCommand => _AdditionalAxisMotorOnCommand ?? (_AdditionalAxisMotorOnCommand = new DelegateCommand(ExecuteAdditionalAxisMotorOnCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute)); private DelegateCommand _AdditionalAxisMotorOffCommand; public DelegateCommand AdditionalAxisMotorOffCommand => _AdditionalAxisMotorOffCommand ?? (_AdditionalAxisMotorOffCommand = new DelegateCommand(ExecuteAdditionalAxisMotorOffCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute)); //private DelegateCommand _CameraAxisModerOnCommand; //public DelegateCommand CameraAxisModerOnCommand => // _CameraAxisModerOnCommand ?? (_CameraAxisModerOnCommand = new DelegateCommand(ExecuteCameraAxisModerOnCommand)); //private DelegateCommand _CameraAxisModerOffCommand; //public DelegateCommand CameraAxisModerOffCommand => // _CameraAxisModerOffCommand ?? (_CameraAxisModerOffCommand = new DelegateCommand(ExecuteCameraAxisModerOffCommand)); //private DelegateCommand _CameraAxisJogCommand; //public DelegateCommand CameraAxisJogCommand => // _CameraAxisJogCommand ?? (_CameraAxisJogCommand = new DelegateCommand(ExecuteCameraAxisJogCommand)); #endregion #region 事件 #endregion #region 方法 private void ShowMsg(string msg) { _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 }); } /// /// 电机 /// /// private async void ExecuteMotorCommand(string obj) { if (Robot == null || !Robot.IsConnected) return; try { await Robot.MotorAsync(Convert.ToBoolean(obj)); ShowMsg($"{Lang.完成}!"); } catch (Exception ex) { LogHelper.WriteLogError("机器人点击操作时出错!", ex); ShowMsg(ex.Message); } } /// /// 重置 /// private async void ExecuteResetCommand() { if (Robot == null || !Robot.IsConnected) return; try { await Robot.ResetAsync(); ShowMsg($"{Lang.完成}!"); } catch (Exception ex) { LogHelper.WriteLogError("重置机器人时出错!", ex); ShowMsg(ex.Message); } } /// /// 点动 /// /// private async void ExecuteJogCommand(object obj) { if (Robot == null || !Robot.IsConnected) return; try { var items = ((string)obj).Split(':'); //如果移动距离大于10mm,则提示用户确认 double distance = double.Parse(items[1]); if (Math.Abs(distance) >= 10) { if (MessageBox.Show($"确认要点动距离 {distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes) { return; } } Robot.Timeout = 6000000; switch (items[0]) { case "X+": await Robot.JogAsync("X", distance); break; case "X-": await Robot.JogAsync("X", -distance); break; case "Y+": await Robot.JogAsync("Y", distance); break; case "Y-": await Robot.JogAsync("Y", -distance); break; case "Z+": await Robot.JogAsync("Z", distance); break; case "Z-": await Robot.JogAsync("Z", -distance); break; case "U+": await Robot.JogAsync("U", distance); break; case "U-": await Robot.JogAsync("U", -distance); break; case "V+": await Robot.JogAsync("V", distance); break; case "V-": await Robot.JogAsync("V", -distance); break; case "W+": await Robot.JogAsync("W", distance); break; case "W-": await Robot.JogAsync("W", -distance); break; default: break; } Robot.Timeout = 5000; ShowMsg($"{Lang.完成}!"); } catch (Exception ex) { LogHelper.WriteLogError("机器点动时出错!", ex); ShowMsg(ex.Message); } } private bool CanExecute { get { if (Robot != null) { return Robot.CanExecute; } else { return false; } } } /// /// 附加轴+点动 /// /// async void ExecuteAdditionalAxisJogPlusCommand(object obj) { if (Robot == null || !Robot.IsConnected) return; try { if (obj is AxisExtended axis) { //如果移动距离大于10mm,则提示用户确认 if (Math.Abs(Distance) >= 10) { if (MessageBox.Show($"确认要点动距离 {Distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes) { return; } } await Robot.AdditionalAxisJogAsync(axis.Name, Distance); ShowMsg($"{Lang.完成}!"); } } catch (Exception ex) { LogHelper.WriteLogError("附加轴点动+时出错!", ex); ShowMsg(ex.Message); } } /// /// 附加轴-点动 /// /// async void ExecuteAdditionalAxisJogMinusCommand(object obj) { if (Robot == null || !Robot.IsConnected) return; try { if (obj is AxisExtended axis) { //如果移动距离大于10mm,则提示用户确认 if (Math.Abs(Distance) >= 10) { if (MessageBox.Show($"确认要点动距离 {Distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes) { return; } } await Robot.AdditionalAxisJogNegAsync(axis.Name, Distance); ShowMsg($"{Lang.完成}!"); } } catch (Exception ex) { LogHelper.WriteLogError("附加轴点动-时出错!", ex); ShowMsg(ex.Message); } } /// /// 附加轴下使能 /// /// async void ExecuteAdditionalAxisMotorOffCommand(object obj) { if (Robot == null || !Robot.IsConnected) return; try { if (obj is AxisExtended axis) { await Robot.AdditionalAxisMotorAsync(axis.Name, false); ShowMsg($"{Lang.完成}!"); } } catch (Exception ex) { LogHelper.WriteLogError("附加轴上使能时出错!", ex); ShowMsg(ex.Message); } } /// /// 附加轴上使能 /// /// async void ExecuteAdditionalAxisMotorOnCommand(object obj) { if (Robot == null || !Robot.IsConnected) return; try { if (obj is AxisExtended axis) { await Robot.AdditionalAxisMotorAsync(axis.Name, true); ShowMsg($"{Lang.完成}!"); } } catch (Exception ex) { LogHelper.WriteLogError("附加轴下使能时出错!", ex); ShowMsg(ex.Message); } } #region 相机控制 ///// ///// 轴电机下使能操作 ///// ///// //async void ExecuteCameraAxisModerOffCommand(string parameter) //{ // try // { // int cameraIndex = int.Parse(parameter); // var isSuccess = await Robot.CameraMotorAsync(cameraIndex, false); // if (isSuccess) // { // ShowMsg($"{Lang.完成}!"); // } // else // { // ShowMsg("相机轴下使能失败!"); // } // } // catch (Exception ex) // { // LogHelper.WriteLogError("相机轴下使能时出错!", ex); // ShowMsg(ex.Message); // } //} ///// ///// 轴电机上使能操作 ///// ///// //async void ExecuteCameraAxisModerOnCommand(string parameter) //{ // try // { // int cameraIndex = int.Parse(parameter); // var isSuccess =await Robot.CameraMotorAsync(cameraIndex, true); // if (isSuccess) // { // ShowMsg($"{Lang.完成}!"); // } // else // { // ShowMsg("相机轴上使能失败!"); // } // } // catch (Exception ex) // { // LogHelper.WriteLogError("相机轴上使能时出错!", ex); // ShowMsg(ex.Message); // } //} ///// ///// 相机轴点动命令 ///// ///// //async void ExecuteCameraAxisJogCommand(string parameter) //{ // try // { // int cameraIndex = int.Parse(parameter.Split(',')[0]); // double jogDistance = Distance; // string axisName = ""; // switch (parameter.Split(',')[1]) // { // case "X+": // jogDistance = Distance; // axisName = "X"; // break; // case "X-": // jogDistance = -Distance; // axisName = "X"; // break; // case "Y+": // jogDistance = Distance; // axisName = "Y"; // break; // case "Y-": // jogDistance = -Distance; // axisName = "Y"; // break; // default: // break; // } // bool isSuccess = await Robot.CameraJogAsync(cameraIndex, axisName, jogDistance); // if (isSuccess) // { // ShowMsg($"{Lang.完成}!"); // } // else // { // ShowMsg("相机轴点动失败!"); // } // } // catch (Exception ex) // { // LogHelper.WriteLogError("相机轴点动时出错!", ex); // ShowMsg(ex.Message); // } //} #endregion #endregion #region 继承 /// /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。 /// /// /// public void ConfirmNavigationRequest(NavigationContext navigationContext, Action continuationCallback) { continuationCallback(true); } /// /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。 /// /// /// public async void OnNavigatedTo(NavigationContext navigationContext) { var robotInfo = navigationContext.Parameters["RobotInfo"] as Models.RobotInfo; if (robotInfo != null) { Robot = _robotService.GetRobot(robotInfo.Id) as XYZU_Robot; AdditionalAxisList = Robot.AdditionalAxisList; Robot.EnterDebugMode = true; } if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator) { IsAllowEdit = false; } else { IsAllowEdit = true; } } /// /// 是否允许导航到此视图模型。 /// /// /// public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } /// /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。 /// /// public void OnNavigatedFrom(NavigationContext navigationContext) { if (Robot != null) Robot.EnterDebugMode = false; } #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; } } } }