using Prism.Commands; using Prism.Ioc; using Prism.Unity; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using TeamAAS_VP; using TeamAAS_VP.Core; using TeamAAS_VP.Core.Robots; using TeamAAS_VP.Enums; using TeamAAS_VP.Events; using TeamAAS_VP.Interfaces; using TeamAAS_VP.Resources.Languages; using TeamAAS_VP.Services; using TeamAAS_VP.ViewModels; using TeamAAS_VP.Views; namespace TeamAAS_VP.Controls { /// /// RobotManual.xaml 的交互逻辑 /// public partial class RobotManual : UserControl,INotifyPropertyChanged { IConfigService _configService; public RobotManual() { InitializeComponent(); DataContext = this; this.Loaded += RobotManual_Loaded; } private void RobotManual_Loaded(object sender, RoutedEventArgs e) { } #region 属性 private IRobot _Robot; public IRobot Robot { get { return _Robot; } set { SetProperty(ref _Robot,value); try { if (value!=null) { if (_configService==null) _configService = ((PrismApplication)(App.Current)).Container.Resolve(); this.Distance = _configService.GetRobotStepDistance(value.Id); } if (value is FanucRobot) { 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; } } 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) { } } } 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) { } } } } } #endregion #region 命令 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 #region 方法 private void ShowMsg(string msg) { if (snackbar.MessageQueue==null) { snackbar.MessageQueue = new MaterialDesignThemes.Wpf.SnackbarMessageQueue(); } snackbar.MessageQueue.Clear(); snackbar.MessageQueue?.Enqueue( msg, null, null, null, false, true, TimeSpan.FromSeconds(0.4)); } /// /// 连接机器人 /// private async void ExecuteConnectCommand() { if (Robot == null || !Robot.IsConnected) return; try { await Robot.ConnectAsync(); ShowMsg($"{Lang.完成}!"); } catch (Exception ex) { LogHelper.WriteLogError("连接机器人时出错!",ex); ShowMsg(ex.Message); } } /// /// 断开机器人 /// private void ExecuteDisConnectCommand() { if (Robot == null || !Robot.IsConnected) return; try { Robot.Disconnect(); ShowMsg($"{Lang.完成}!"); } catch (Exception ex) { LogHelper.WriteLogError("断开机器人时出错!", ex); ShowMsg(ex.Message); } } /// /// 电机 /// /// 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 async void ExecuteSLockCommand() { if (Robot == null || !Robot.IsConnected) return; try { await Robot.SLockAsync(); ShowMsg($"{Lang.完成}!"); } catch (Exception ex) { LogHelper.WriteLogError("机器人锁定刹车时出错!", ex); ShowMsg(ex.Message); } } /// /// 释放刹车 /// private async void ExecutesFreeCommand() { if (Robot == null || !Robot.IsConnected) return; try { await Robot.SFreeAsync(); ShowMsg($"{Lang.完成}!"); } catch (Exception ex) { LogHelper.WriteLogError("机器人释放刹车时出错!", ex); ShowMsg(ex.Message); } } private bool CanExecute { get { if (Robot != null) { return Robot.CanExecute; } else { return false; } } } #endregion #region 属性通知 /// /// Occurs when a property value changes. /// public event PropertyChangedEventHandler PropertyChanged; /// /// Checks if a property already matches a desired value. Sets the property and /// notifies listeners only when necessary. /// /// Type of the property. /// Reference to a property with both getter and setter. /// Desired value for the property. /// Name of the property used to notify listeners. This /// value is optional and can be provided automatically when invoked from compilers that /// support CallerMemberName. /// True if the value was changed, false if the existing value matched the /// desired value. protected virtual bool SetProperty(ref T storage, T value, [CallerMemberName] string propertyName = null) { if (EqualityComparer.Default.Equals(storage, value)) return false; storage = value; RaisePropertyChanged(propertyName); return true; } /// /// Checks if a property already matches a desired value. Sets the property and /// notifies listeners only when necessary. /// /// Type of the property. /// Reference to a property with both getter and setter. /// Desired value for the property. /// Name of the property used to notify listeners. This /// value is optional and can be provided automatically when invoked from compilers that /// support CallerMemberName. /// Action that is called after the property value has been changed. /// True if the value was changed, false if the existing value matched the /// desired value. protected virtual bool SetProperty(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null) { if (EqualityComparer.Default.Equals(storage, value)) return false; storage = value; onChanged?.Invoke(); RaisePropertyChanged(propertyName); return true; } /// /// Raises this object's PropertyChanged event. /// /// Name of the property used to notify listeners. This /// value is optional and can be provided automatically when invoked from compilers /// that support . protected void RaisePropertyChanged([CallerMemberName] string propertyName = null) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } /// /// Raises this object's PropertyChanged event. /// /// The PropertyChangedEventArgs protected virtual void OnPropertyChanged(PropertyChangedEventArgs args) { PropertyChanged?.Invoke(this, args); } #endregion } }