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 TeamAAS_VP.Enums; using TeamAAS_VP.Events; using TeamAAS_VP.Core; using TeamAAS_VP.Interfaces; using TeamAAS_VP.Models; using System.Windows; using TeamAAS_VP; using TeamAAS_VP.Resources.Languages; namespace TeamAAS_VP.ViewModels.Product { public class RobotPointParamsViewModel : BindableBase { IRegionManager _regionManager; IEventAggregator _eventAggregator; IContainerProvider _container; IDialogService _dialogService; ProductsViewModel productsViewModel; #region 属性 private ProductModel _SelectProduct; /// /// 选中的产品 /// public ProductModel SelectProduct { get { return _SelectProduct; } set { SetProperty(ref _SelectProduct, value); } } private RPoint _SelectPoint; public RPoint SelectPoint { get { return _SelectPoint; } set { SetProperty(ref _SelectPoint,value); } } private MotionCommand _SelectMotionCommand; /// /// 选中的运动指令 /// public MotionCommand SelectMotionCommand { get { return _SelectMotionCommand; } set { SetProperty(ref _SelectMotionCommand,value); } } private int _SelectPointIndex=-1; public int SelectPointIndex { get { return _SelectPointIndex; } set { SetProperty(ref _SelectPointIndex,value); } } 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 ObservableCollection _Robots = new ObservableCollection(); /// /// 机器人参数 /// public ObservableCollection Robots { get { return _Robots; } set { SetProperty(ref _Robots, value); if (value!=null) { if (value.Count<=0) { IsHaveRobot = false; } else { IsHaveRobot = true; } } } } private RobotInfo _RobotInfo = new RobotInfo(); /// /// 机器人 /// public RobotInfo SelectRobotInfo { get { return _RobotInfo; } set { SetProperty(ref _RobotInfo, value); if (value != null) { Robot = robotService.GetRobot(value.Id); if (value.RobotBrand == RobotBrand.FANUC) { Tools = new ObservableCollection { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; SelectTool = Robot.SelectedTool; } else { Tools = new ObservableCollection { 0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; SelectTool = Robot.SelectedTool; } } } } private ObservableCollection _Tools = new ObservableCollection(); /// /// 机器人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 _AddPointCommand; public DelegateCommand AddPointCommand => _AddPointCommand ?? (_AddPointCommand = new DelegateCommand(ExecuteAddPointCommand)); private DelegateCommand _RemovePointCommand; public DelegateCommand RemovePointCommand => _RemovePointCommand ?? (_RemovePointCommand = new DelegateCommand(ExecuteRemovePointCommand, () => { return SelectPointIndex >= 0 ? true : false; }).ObservesProperty(()=> SelectPointIndex)); 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)); #endregion #region 事件 #endregion public RobotPointParamsViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService) { _regionManager = regionManager; _eventAggregator = ea; _container = container; _dialogService = dialogService; //订阅权限登录事件 _eventAggregator.GetEvent().Subscribe(LoginChange); productsViewModel = _container.Resolve(); SelectProduct = productsViewModel.SelectProduct; management = _container.Resolve(); Robots = management.DeviceConfig.Robots; } #region 方法 /// /// 下一步 /// void ExecuteNextCommand() { _regionManager.RequestNavigate("ProductRegionContext", "ProductParams"); } /// /// 上一步 /// void ExecuteBackCommand() { _regionManager.RequestNavigate("ProductRegionContext", "ProcessBasics"); } /// /// 增加点位 /// void ExecuteAddPointCommand() { try { if (SelectProduct.RobotPoint == null) SelectProduct.RobotPoint = new RobotPoint(); if (SelectProduct.RobotPoint.Points == null) SelectProduct.RobotPoint.Points = new ObservableCollection(); if (SelectRobotInfo == null) { SelectProduct.RobotPoint.Points.Add(new MultipleRobotPoint() { Number = SelectProduct.RobotPoint.Points.Count }); } else { SelectProduct.RobotPoint.Points.Add(new MultipleRobotPoint() { Number = SelectProduct.RobotPoint.Points.Count, RobotId = SelectRobotInfo.Id }); } } catch (Exception ex) { LogHelper.WriteLogError("机器人点位-新建点时出错", ex); MessageBox.Show(ex.Message); } } /// /// 移除点位 /// void ExecuteRemovePointCommand() { try { if (SelectPointIndex >= 0) { SelectProduct.RobotPoint.Points.RemoveAt(SelectPointIndex); for (int i = 0; i < SelectProduct.RobotPoint.Points.Count; i++) { SelectProduct.RobotPoint.Points[i].Number = i; } } } catch (Exception ex) { LogHelper.WriteLogError("机器人点位-移除点时出错", ex); MessageBox.Show(ex.Message); } } /// /// 跳转托盘 /// void ExecuteGoPalletCommand() { try { _dialogService.Show("PalletManage", rst => { //对话框关闭之后的回调函数,可以在这解析结果。 ButtonResult result1 = rst.Result; if (result1 == ButtonResult.OK) { var param = rst.Parameters.GetValue>("Pallets"); SelectProduct.RobotPoint.Pallets = param; _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = Lang.已关闭, Duration = 1 }); } }); } catch (Exception ex) { LogHelper.WriteLogError("机器人点位-跳转托盘界面时出错", 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("机器人点位-机器人点动时出错", 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("机器人点位-执行点位时出错", 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(); MultipleRobotPoint p0 = SelectProduct.RobotPoint.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; p0.RobotId = Robot.Id; _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 }); } catch (Exception ex) { LogHelper.WriteLogError("机器人点位-示教点位时出错", ex); _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 }); } } void ExecuteLoadedCommand() { if (_container.Resolve().CurrentUser.userPart == Enums.UserPart.Operator) { IsAllowEdit = false; } else { IsAllowEdit = true; } productsViewModel = _container.Resolve(); SelectProduct = productsViewModel.SelectProduct; management = _container.Resolve(); Robots= management.DeviceConfig.Robots; robotService = management.RobotService; SelectRobotInfo = null; if (Robot != null && Robot.IsConnected) { if (Tools.Count > 1) { SelectTool = Robot.SelectedTool; } } } /// /// 电机 /// /// 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("机器人点击操作时出错!", 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("重置机器人时出错!", 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("机器人锁定刹车时出错!", 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("机器人释放刹车时出错!", ex); _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 }); } } private bool CanExecute { get { if (Robot != null) { return Robot.CanExecute; } else { return 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; } } } }