using MaterialDesignThemes.Wpf; using Prism.Commands; using Prism.Events; using Prism.Ioc; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using System.Windows; using TeamAAS_VP; using TeamAAS_VP.Controls; using TeamAAS_VP.Core; using TeamAAS_VP.Enums; using TeamAAS_VP.Events; using TeamAAS_VP.Interfaces; using TeamAAS_VP.Models; using TeamAAS_VP.Models.Robot; using TeamAAS_VP.Resources.Languages; using TeamAAS_VP.Services; namespace TeamAAS_VP.ViewModels.Product { public class RobotPointParamsViewModel : BindableBase, IConfirmNavigationRequest { IRegionManager _regionManager; IEventAggregator _eventAggregator; IContainerProvider _container; IDialogService _dialogService; IConfigService _configService; IRobotService _robotService; ISystemDatabaseService _systemDatabaseService; #region 属性 private ProductModel _SelectProduct; /// /// 选中的产品 /// public ProductModel SelectProduct { get { return _SelectProduct; } set { SetProperty(ref _SelectProduct, value); } } private RobotPointStore _SelectedRobot; public RobotPointStore SelectedRobot { get { return _SelectedRobot; } set { if (SetProperty(ref _SelectedRobot, value)) { try { if (value != null) { if (Robot != null) { Robot.EnterDebugMode = false; } Robot = _robotService.GetRobot(value.Id); Robot.EnterDebugMode = true; SelectedPointInDataGrid = value.Points.FirstOrDefault(); SelectedPointInComboBox = value.Points.FirstOrDefault(); this.Distance = _configService.GetRobotStepDistance(Robot.Id); } } catch (Exception) { } } } } private RPoint _SelectPoint; public RPoint SelectPoint { get { return _SelectPoint; } set { SetProperty(ref _SelectPoint,value); } } private RPoint _selectedPointInDataGrid; public RPoint SelectedPointInDataGrid { get { return _selectedPointInDataGrid; } set { if (SetProperty(ref _selectedPointInDataGrid, value)) { SelectedPointInComboBox = value; } // 可以同步到 SelectPoint 属性 SelectPoint = value; } } private RPoint _selectedPointInComboBox; public RPoint SelectedPointInComboBox { get { return _selectedPointInComboBox; } set { if (SetProperty(ref _selectedPointInComboBox, value)) { SelectedPointInDataGrid = value; } // 可以同步到 SelectPoint 属性 SelectPoint = value; } } private MotionCommand _SelectMotionCommand; /// /// 选中的运动指令 /// public MotionCommand SelectMotionCommand { get { return _SelectMotionCommand; } set { SetProperty(ref _SelectMotionCommand,value); } } public Management management { get; set; } private IRobot _Robot; public IRobot Robot { get { return _Robot; } set { SetProperty(ref _Robot, value); } } private double _Distance = 1; public double Distance { get { return _Distance; } set { SetProperty(ref _Distance, value); try { if (Robot == null) { return; } _configService.UpdateRobotStepDistance(Robot.Id, value); } catch (Exception) { } } } private ObservableCollection _Tools = new ObservableCollection(new int[] {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}); /// /// 机器人Tool集合 /// public ObservableCollection Tools { get { return _Tools; } set { SetProperty(ref _Tools, value); } } private int _SelectTool; /// /// 选中的Tool /// public int SelectTool { get { return _SelectTool; } set { SetProperty(ref _SelectTool, value); if (value >= 0) { if (Robot!=null && Robot.IsConnected) { try { Robot.SelectToolAsync(value); } catch (Exception) { } } } } } private bool _IsHaveRobot = false; public bool IsHaveRobot { get { return _IsHaveRobot; } set { SetProperty(ref _IsHaveRobot, value); } } #endregion #region 命令 private DelegateCommand _NextCommand; public DelegateCommand NextCommand => _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand)); private DelegateCommand _BackCommand; public DelegateCommand BackCommand => _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand)); private DelegateCommand _GoPalletCommand; public DelegateCommand GoPalletCommand => _GoPalletCommand ?? (_GoPalletCommand = new DelegateCommand(ExecuteGoPalletCommand)); private DelegateCommand _LoadedCommand; public DelegateCommand LoadedCommand => _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand)); private DelegateCommand _ExecuteMotion; public DelegateCommand ExecuteMotion => _ExecuteMotion ?? (_ExecuteMotion = new DelegateCommand(ExecuteExecuteMotion).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 _TechPointCommand; public DelegateCommand TechPointCommand => _TechPointCommand ?? (_TechPointCommand = new DelegateCommand(ExecuteTechPointCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute)); 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 _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)); private DelegateCommand _RobotSelectedChangedCommand; public DelegateCommand RobotSelectedChangedCommand => _RobotSelectedChangedCommand ?? (_RobotSelectedChangedCommand = new DelegateCommand(ExecuteRobotSelectedChangedCommand)); #endregion #region 事件 #endregion public RobotPointParamsViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService, IConfigService configService, IRobotService robotService, ISystemDatabaseService systemDatabaseService) { _regionManager = regionManager; _eventAggregator = ea; _container = container; _dialogService = dialogService; _configService = configService; _systemDatabaseService = systemDatabaseService; //订阅权限登录事件 _eventAggregator.GetEvent().Subscribe(LoginChange); management = _container.Resolve(); _robotService = robotService; } #region 方法 /// /// 下一步 /// void ExecuteNextCommand() { NavigationParameters param = new NavigationParameters(); param.Add("SelectProduct", SelectProduct); _regionManager.RequestNavigate("ProductRegionContext", "ProductParams", param); } /// /// 上一步 /// void ExecuteBackCommand() { NavigationParameters param = new NavigationParameters(); param.Add("SelectProduct", SelectProduct); param.Add("SelectedProcedure", null); _regionManager.RequestNavigate("ProductRegionContext", "ProcessBasics", param); } /// /// 跳转托盘 /// void ExecuteGoPalletCommand() { try { DialogParameters dialogParameters = new DialogParameters(); dialogParameters.Add("SelectProduct", SelectProduct); dialogParameters.Add("SelectedRobot", SelectedRobot); _dialogService.Show("PalletManage", dialogParameters, rst => { //对话框关闭之后的回调函数,可以在这解析结果。 ButtonResult result1 = rst.Result; if (result1 == ButtonResult.OK) { var param = rst.Parameters.GetValue>("Pallets"); SelectedRobot.Pallets = param; _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = Lang.已关闭, Duration = 1 }); } }); } catch (Exception ex) { LogHelper.WriteLogError(Lang.机器人点位跳转托盘界面时出错, ex); MessageBox.Show(ex.Message); } } /// /// 点动 /// /// 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) { LogHelper.WriteLogError(Lang.机器人点位机器人点动时出错, ex); _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 }); } } /// /// 执行运动 /// /// async void ExecuteExecuteMotion(object parameter) { var values = (object[])parameter; var point = values[0] as RPoint; MotionCommand motionCmd = (MotionCommand)values[1]; if (Robot == null || !Robot.IsConnected) return; try { Robot.Timeout = 6000000; switch (motionCmd) { case MotionCommand.Go: await Robot.GoAsync(point); break; case MotionCommand.Jump: await Robot.JumpAsync(point, 0); break; case MotionCommand.Move: await Robot.MoveAsync(point); break; } Robot.Timeout = 5000; _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 }); } catch (Exception ex) { LogHelper.WriteLogError(Lang.机器人点位执行点位时出错, ex); _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 }); } } /// /// 示教点位 /// /// async void ExecuteTechPointCommand(RPoint point) { if (Robot == null || !Robot.IsConnected) return; try { var curpos = await Robot.GetRobotPosAsync(); var p0 = SelectedRobot.Points.FirstOrDefault(x => x.Number == point.Number); p0.X = curpos.X; p0.Y = curpos.Y; p0.Z = curpos.Z; p0.U = curpos.U; p0.V = curpos.V; p0.W = curpos.W; p0.Hand = curpos.Hand; p0.Local = curpos.Local; p0.Tool = curpos.Tool; _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 }); } catch (Exception ex) { LogHelper.WriteLogError(Lang.机器人点位示教点位时出错, ex); _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 }); } } void ExecuteLoadedCommand() { if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator) { IsAllowEdit = false; } else { IsAllowEdit = true; } } /// /// 电机 /// /// 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) { LogHelper.WriteLogError(Lang.机器人点击操作时出错, 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) { LogHelper.WriteLogError(Lang.重置机器人时出错, 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) { LogHelper.WriteLogError(Lang.机器人锁定刹车时出错, 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) { LogHelper.WriteLogError(Lang.机器人释放刹车时出错, ex); _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 }); } } private bool CanExecute { get { if (Robot != null) { return Robot.CanExecute; } else { return false; } } } private bool _isProcessingRobotChange = false; /// /// 机器人选择更改时 /// void ExecuteRobotSelectedChangedCommand() { // 防止重复执行 //if (_isProcessingRobotChange) return; //try //{ // _isProcessingRobotChange = true; // if (SelectedRobot != null) // { // if (Robot!=null) // { // Robot.EnterDebugMode = false; // } // Robot = _robotService.GetRobot(SelectedRobot.Id); // Robot.EnterDebugMode = true; // SelectPoint = SelectedRobot.Points.FirstOrDefault(); // this.Distance = _configService.GetRobotStepDistance(Robot.Id); // } //} //catch (Exception ex) //{ // LogHelper.WriteLogError("机器人点位界面中切换机器人时出错!", ex); //} //finally //{ // _isProcessingRobotChange = false; //} } #endregion #region 继承 /// /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。 /// /// /// public void ConfirmNavigationRequest(NavigationContext navigationContext, Action continuationCallback) { continuationCallback(true); } /// /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。 /// /// /// public async void OnNavigatedTo(NavigationContext navigationContext) { try { SelectProduct = navigationContext.Parameters.GetValue("SelectProduct"); if (SelectProduct.RobotPoint.Robots.Count>0) { SelectedRobot = SelectProduct.RobotPoint.Robots.FirstOrDefault(); Robot = _robotService.GetRobot(SelectedRobot.Id); SelectedPointInDataGrid = SelectedRobot.Points.FirstOrDefault(); SelectedPointInComboBox = SelectedPointInDataGrid; } if (Robot != null && Robot.IsConnected) { this.Distance = _configService.GetRobotStepDistance(Robot.Id); Robot.EnterDebugMode = true; if (Tools.Count > 1) { SelectTool = Robot.SelectedTool; } } else if (Robot != null) { Robot.EnterDebugMode = true; } } catch (Exception ex) { LogHelper.WriteLogError(Lang.机器人点位界面进入时出错, ex); } } /// /// 是否允许导航到此视图模型。 /// /// /// 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; } } } }