using NPOI.OpenXml4Net.OPC.Internal; using NPOI.SS.Formula.Functions; using Prism.Commands; using Prism.Events; using Prism.Ioc; using Prism.Mvvm; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using TeamAAS_VP.Core; using TeamAAS_VP.Enums; using TeamAAS_VP.Events; using TeamAAS_VP.Interfaces; using TeamAAS_VP.Models; using TeamAAS_VP.Resources.Languages; using TeamAAS_VP.Services; using FileHelper = TeamAAS_VP.Core.FileHelper; namespace TeamAAS_VP.ViewModels.Home { public class TorqueCheckViewModel : BindableBase, IDialogAware { IEventAggregator _eventAggregator; IConfigService _configService; IContainerProvider _container; IRobotService _robotService; #region 属性 private Management _management; public Management management { get { return _management; } set { SetProperty(ref _management, value); } } private bool CanExecute { get { // 命令可执行仅当机器人可执行且已选择点位 if (Robot != null && Robot.IsConnected && Robot.CanExecute) { return true; } return false; } } 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 TorqueTest _tc = new TorqueTest(); public TorqueTest Tc { get { return _tc; } set { SetProperty(ref _tc, value); } } private double _torqueValue; public double torqueValue { get { return _torqueValue; } set { SetProperty(ref _torqueValue, value); } } private string _TValue; public string TValue { get { return _TValue; } set { SetProperty(ref _TValue, value); } } private string _PValue; public string PValue { get { return _PValue; } set { SetProperty(ref _PValue, value); } } private string _SubValue; public string SubValue { get { return _SubValue; } set { SetProperty(ref _SubValue, value); } } private string _Result; public string Result { get { return _Result; } set { SetProperty(ref _Result, value); } } private string _FontColor; public string FontColor { get { return _FontColor; } set { SetProperty(ref _FontColor, value); } } #endregion #region 命令 private DelegateCommand _CancelCommand; public DelegateCommand CancelCommand => _CancelCommand ?? (_CancelCommand = new DelegateCommand(ExecuteCancelCommand)); private DelegateCommand _ConfirmCommand; public DelegateCommand ConfirmCommand => _ConfirmCommand ?? (_ConfirmCommand = new DelegateCommand(ExecuteConfirmCommand)); private DelegateCommand _MotorCommand; public DelegateCommand MotorCommand => _MotorCommand ?? (_MotorCommand = new DelegateCommand(ExecuteMotorCommand).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)); private DelegateCommand _ModifyCommand; public DelegateCommand ModifyCommand => _ModifyCommand ?? (_ModifyCommand = new DelegateCommand(ExecuteModifyCommand)); private DelegateCommand _WorkCommand; public DelegateCommand WorkCommand => _WorkCommand ?? (_WorkCommand = new DelegateCommand(ExecuteWorkCommand)); #endregion #region 方法 public TorqueCheckViewModel(IEventAggregator ea, IConfigService configService, IContainerProvider container, IRobotService robotService) { _eventAggregator = ea; _configService = configService; _container = container; _robotService = robotService; management = _container.Resolve(); } void ExecuteCancelCommand() { IDialogParameters parameters = new DialogParameters(); RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel, parameters)); } void ExecuteConfirmCommand() { IDialogParameters parameters = new DialogParameters(); RequestClose?.Invoke(new DialogResult(ButtonResult.OK, parameters)); } /// /// 电机 /// /// 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 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; _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() { //弹窗用户是否确认要示教该点位 if (MessageBox.Show($"确认要示教点位吗?", "示教点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes) { return; } var curpos = await Robot.GetRobotPosAsync(); Tc.Xpoint = curpos.X; Tc.Ypoint = curpos.Y; Tc.Zpoint = curpos.Z; FileHelper.WriteJsonFile(Tc, FilePath.TorqueCheckPath); _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 }); } /// /// 修改 /// void ExecuteModifyCommand() { if (MessageBox.Show($"确认要修改扭力阈值吗?", "修改", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes) { return; } Tc.TorqueValue = torqueValue; FileHelper.WriteJsonFile(Tc, FilePath.TorqueCheckPath); _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.4 }); } void ExecuteWorkCommand() { try { MoveRobot(); Thread.Sleep(500); DisplayResult(); } catch (Exception ex) { LogHelper.WriteLogError("执行点检时出错!", ex); } } public async void MoveRobot() { if (!File.Exists(FilePath.TorqueCheckPath)) { MessageBox.Show("还未示教,请先示教"); return; } var res = FileHelper.ReadJsonFile(FilePath.TorqueCheckPath); var point = new RPoint() { X = (float)res.Xpoint, Y = (float)res.Ypoint, Z = (float)res.Zpoint, }; if (Robot == null || !Robot.IsConnected) return; try { //确认要执行该点位的该运动指令吗 if (MessageBox.Show($"确认要进行点检吗?", "执行点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes) { return; } await Robot.JumpAsync(point, 0); } catch (Exception ex) { LogHelper.WriteLogError("执行点检移动时出错!", ex); } } public void DisplayResult() { try { TValue = management.SignalValue; double tvalue = double.Parse(TValue.Split(' ')[0]); PValue = $"{management.Value} N.m"; double pvalue = management.Value; SubValue = $"{Math.Abs(tvalue - pvalue)} N.m"; double sub = Math.Abs(tvalue - pvalue); var res = FileHelper.ReadJsonFile(FilePath.TorqueCheckPath); if (res != null) { if (sub <= res.TorqueValue) { FontColor = "Green"; Result = "OK"; } else { FontColor = "Red"; Result = "NG"; } } else { if (sub <= torqueValue) { FontColor = "Green"; Result = "OK"; } else { FontColor = "Red"; Result = "NG"; } } } catch (Exception ex) { LogHelper.WriteLogError("执行点检显示时出错!", ex); } } #endregion public string Title { get; set; } = "扭力点检"; public event Action RequestClose; public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } public void OnDialogOpened(IDialogParameters parameters) { try { var res = FileHelper.ReadJsonFile(FilePath.TorqueCheckPath); if (res != null) { res.TorqueValue = torqueValue; } else { MessageBox.Show("还未示教,请先示教"); } } catch (Exception ex) { MessageBox.Show("还未示教,请先示教"); } if (Robot == null) { Robot = _robotService.GetRobotByNumber(1); } if (Robot != null && Robot.IsConnected) { this.Distance = _configService.GetRobotStepDistance(Robot.Id); Robot.EnterDebugMode = true; } else if (Robot != null) { Robot.EnterDebugMode = true; } } } }