| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- 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<string> _MotorCommand;
- public DelegateCommand<string> MotorCommand =>
- _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
- private DelegateCommand<object> _JogCommand;
- public DelegateCommand<object> JogCommand =>
- _JogCommand ?? (_JogCommand = new DelegateCommand<object>(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<Management>();
- }
- 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));
- }
- /// <summary>
- /// 电机
- /// </summary>
- /// <param name="obj"></param>
- private async void ExecuteMotorCommand(string obj)
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- await Robot.MotorAsync(Convert.ToBoolean(obj));
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("机器人点击操作时出错!", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 点动
- /// </summary>
- /// <param name="obj"></param>
- 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<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("机器人点位-机器人点动时出错", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 示教点位
- /// </summary>
- 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<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- /// <summary>
- /// 修改
- /// </summary>
- void ExecuteModifyCommand()
- {
- if (MessageBox.Show($"确认要修改扭力阈值吗?", "修改", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- Tc.TorqueValue = torqueValue;
- FileHelper.WriteJsonFile(Tc, FilePath.TorqueCheckPath);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().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<TorqueTest>(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<TorqueTest>(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<IDialogResult> RequestClose;
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- try
- {
- var res = FileHelper.ReadJsonFile<TorqueTest>(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;
- }
- }
- }
- }
|