| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- using MathNet.Numerics;
- using Prism.Commands;
- using Prism.Events;
- using Prism.Ioc;
- using Prism.Mvvm;
- using Prism.Regions;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Windows;
- using Team.FFFeederService;
- using Team.FFFeederService.Interfaces;
- using TeamAAS_VP.Core;
- using TeamAAS_VP.Events;
- using TeamAAS_VP.Interfaces;
- using TeamAAS_VP.Models;
- using TeamAAS_VP.Resources.Languages;
- using TeamAAS_VP.Services;
- namespace TeamAAS_VP.ViewModels.DebugMod
- {
- public class RobotManualStepViewModel : BindableBase, IConfirmNavigationRequest
- {
- IContainerProvider _container;
- IEventAggregator _eventAggregator;
- IRobotService _robotService;
- ISystemDatabaseService _systemDatabaseService;
- IConfigService _configService;
- #region 属性
- public Management management { get; set; }
- private IRobot _Robot;
- public IRobot Robot
- {
- get { return _Robot; }
- set
- {
- SetProperty(ref _Robot, value);
- try
- {
- if (value != null)
- {
- this.Distance = _configService.GetRobotStepDistance(value.Id);
- }
- }
- catch (Exception)
- {
- }
- }
- }
- 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 bool CanExecute
- {
- get
- {
- if (Robot==null)
- return false;
- else return Robot.CanExecute;
- }
- }
- #endregion
- #region 命令
- private DelegateCommand _LoadedCommand;
- public DelegateCommand LoadedCommand =>
- _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
- private DelegateCommand _ConnectCommand;
- public DelegateCommand ConnectCommand =>
- _ConnectCommand ?? (_ConnectCommand = new DelegateCommand(ExecuteConnectCommand));
-
- private DelegateCommand _DisConnectCommand;
- public DelegateCommand DisConnectCommand =>
- _DisConnectCommand ?? (_DisConnectCommand = new DelegateCommand(ExecuteDisConnectCommand));
-
- private DelegateCommand<string> _MotorCommand;
- public DelegateCommand<string> MotorCommand =>
- _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(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<object> _JogCommand;
- public DelegateCommand<object> JogCommand =>
- _JogCommand ?? (_JogCommand = new DelegateCommand<object>(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
- public RobotManualStepViewModel(IContainerProvider container, IEventAggregator ea, IRobotService robotService, ISystemDatabaseService systemDatabaseService, IConfigService configService)
- {
- _container = container;
- _eventAggregator = ea;
- _robotService = robotService;
- _systemDatabaseService = systemDatabaseService;
- _configService = configService;
- //订阅权限登录事件
- _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
- }
- #region 方法
- void ExecuteLoadedCommand()
- {
- if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
- {
- IsAllowEdit = false;
- }
- else
- {
- IsAllowEdit = true;
- }
- }
- /// <summary>
- /// 连接机器人
- /// </summary>
- private async void ExecuteConnectCommand()
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- await Robot.ConnectAsync();
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.连接成功}!", Duration = 1 });
- }
- catch (Exception ex)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 断开机器人
- /// </summary>
- private void ExecuteDisConnectCommand()
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- Robot.Disconnect();
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.已断开}!", Duration = 1 });
- }
- catch (Exception ex)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <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)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 重置
- /// </summary>
- private async void ExecuteResetCommand()
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- await Robot.ResetAsync();
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 });
- }
- catch (Exception 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)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 锁定刹车
- /// </summary>
- private async void ExecuteSLockCommand()
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- await Robot.SLockAsync();
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 });
- }
- catch (Exception ex)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 释放刹车
- /// </summary>
- private async void ExecutesFreeCommand()
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- await Robot.SFreeAsync();
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 });
- }
- catch (Exception ex)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- #endregion
- #region 继承
- /// <summary>
- /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
- /// </summary>
- /// <param name="navigationContext"></param>
- /// <param name="continuationCallback"></param>
- public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
- {
- continuationCallback(true);
- }
- /// <summary>
- /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
- /// </summary>
- /// <param name="navigationContext"></param>
- /// <exception cref="NotImplementedException"></exception>
- public async void OnNavigatedTo(NavigationContext navigationContext)
- {
- var robotInfo = navigationContext.Parameters["RobotInfo"] as Models.RobotInfo;
- if (robotInfo != null)
- {
- Robot = _robotService.GetRobot(robotInfo.Id);
- Robot.EnterDebugMode= true;
- }
- if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
- {
- IsAllowEdit = false;
- }
- else
- {
- IsAllowEdit = true;
- }
- }
- /// <summary>
- /// 是否允许导航到此视图模型。
- /// </summary>
- /// <param name="navigationContext"></param>
- /// <returns></returns>
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- /// <summary>
- /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
- /// </summary>
- /// <param name="navigationContext"></param>
- 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;
- }
- }
- }
- }
|