using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using SixLabors.Fonts.Unicode;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Windows;
using Team.FFFeederService.Interfaces;
using TeamAAS_VP;
using TeamAAS_VP.Core;
using TeamAAS_VP.Events;
using TeamAAS_VP.Interfaces;
using TeamAAS_VP.Models;
using TeamAAS_VP.Models.Calibration;
using TeamAAS_VP.Resources.Languages;
using TeamAAS_VP.Services;
using TeamAAS_VP.ViewModels.Product;
namespace TeamAAS_VP.ViewModels.Calibration
{
public class CalibrationTeachCenterPointViewModel : BindableBase, IConfirmNavigationRequest
{
IRegionManager _regionManager;
IRegionNavigationService _regionNavigationService;
IContainerProvider _container;
IDialogService _dialogService;
IEventAggregator _eventAggregator;
IConfigService _configService;
IRobotService _robotService;
ICameraService _cameraService;
IFeederService _feederService;
ISystemDatabaseService _systemDatabaseService;
#region 属性
private CalibrationInfo _SelectCalibration;
public CalibrationInfo SelectCalibration
{
get { return _SelectCalibration; }
set { SetProperty(ref _SelectCalibration, value); }
}
private bool _IsShowTechHome = false;
public bool IsShowTechHome
{
get { return _IsShowTechHome; }
set { SetProperty(ref _IsShowTechHome, value); }
}
private bool _IsShowTechP0ModeSelect = true;
///
/// 是否显示P0模式选择
///
public bool IsShowTechP0ModeSelect
{
get { return _IsShowTechP0ModeSelect; }
set { SetProperty(ref _IsShowTechP0ModeSelect, value); }
}
public Management management { get; set; }
private IRobot _Robot;
public IRobot Robot
{
get { return _Robot; }
set { SetProperty(ref _Robot, value); }
}
#endregion
#region 命令
private DelegateCommand _LoadedCommand;
public DelegateCommand LoadedCommand =>
_LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
private DelegateCommand _NextCommand;
public DelegateCommand NextCommand =>
_NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
private DelegateCommand _BackCommand;
public DelegateCommand BackCommand =>
_BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
private DelegateCommand _TechCenterCommand;
public DelegateCommand TechCenterCommand =>
_TechCenterCommand ?? (_TechCenterCommand = new DelegateCommand(ExecuteTechCenterCommand));
private DelegateCommand _TechHomeCommand;
public DelegateCommand TechHomeCommand =>
_TechHomeCommand ?? (_TechHomeCommand = new DelegateCommand(ExecuteTechHomeCommand));
private DelegateCommand _BlowOrInhalControlCommand;
public DelegateCommand BlowOrInhalControlCommand =>
_BlowOrInhalControlCommand ?? (_BlowOrInhalControlCommand = new DelegateCommand(ExecuteBlowOrInhalControlCommand));
private DelegateCommand _TechMarkPointCommand;
public DelegateCommand TechMarkPointCommand =>
_TechMarkPointCommand ?? (_TechMarkPointCommand = new DelegateCommand(ExecuteTechMarkPointCommand));
private DelegateCommand _MoveCenterCommand;
public DelegateCommand MoveCenterCommand =>
_MoveCenterCommand ?? (_MoveCenterCommand = new DelegateCommand(ExecuteMoveCenterCommand));
#endregion
#region 事件
#endregion
public CalibrationTeachCenterPointViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, ISystemDatabaseService systemDatabaseService)
{
_regionManager = regionManager;
_regionNavigationService = regionNavigationService;
_container = container;
_dialogService = dialogService;
_eventAggregator = ea;
_configService = configService;
_robotService = robotService;
_cameraService = cameraService;
_feederService = feederService;
_systemDatabaseService = systemDatabaseService;
}
#region 方法
void ExecuteLoadedCommand()
{
if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
{
IsAllowEdit = false;
}
else
{
IsAllowEdit = true;
}
}
///
/// 下一步
///
void ExecuteNextCommand()
{
NavigationParameters param = new NavigationParameters();
param.Add("SelectCalibration", SelectCalibration);
if (SelectCalibration.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
{
if (SelectCalibration.CalibTechP0Mode == Enums.CalibTechP0Mode.CameraFindP0)
{
_regionManager.RequestNavigate("CalibrationRegionContext", "FixedUpCameraFindP0", param);
return;
}
else
{
_regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationAuto", param);
}
}
else if (Robot.Brand == Enums.RobotBrand.XYZ_Platform)
{
_regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationAuto", param);
}
else
{
_regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationSelectTool", param);
}
}
///
/// 上一步
///
void ExecuteBackCommand()
{
NavigationParameters param = new NavigationParameters();
param.Add("SelectCalibration", SelectCalibration);
_regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationEditVision", param);
}
///
/// 示教中心点
///
async void ExecuteTechCenterCommand()
{
if (Robot == null || !Robot.IsConnected) return;
try
{
var curpos = await Robot.GetRobotPosAsync();
if (SelectCalibration.CenterPoint == null)
{
SelectCalibration.CenterPoint = new RPoint();
}
SelectCalibration.CenterPoint.X = curpos.X;
SelectCalibration.CenterPoint.Y = curpos.Y;
SelectCalibration.CenterPoint.Z = curpos.Z;
SelectCalibration.CenterPoint.U = curpos.U;
SelectCalibration.CenterPoint.V = curpos.V;
SelectCalibration.CenterPoint.W = curpos.W;
SelectCalibration.CenterPoint.Hand = curpos.Hand;
SelectCalibration.CenterPoint.Local = curpos.Local;
SelectCalibration.CenterPoint.Tool = curpos.Tool;
LogHelper.WriteLogInfo(string.Format(Lang.相机校准示教中心点界面示教待机点X0Y1Z2, SelectCalibration.CenterPoint.X, SelectCalibration.CenterPoint.Y, SelectCalibration.CenterPoint.Z));
_eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = string.Format(Lang.成功X0Y1Z2, SelectCalibration.CenterPoint.X, SelectCalibration.CenterPoint.Y, SelectCalibration.CenterPoint.Z), Duration = 1 });
}
catch (Exception ex)
{
LogHelper.WriteLogError(Lang.相机校准示教中心点界面示教中心点时出错, ex);
_eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
}
}
///
/// 示教待机点
///
async void ExecuteTechHomeCommand()
{
if (Robot == null || !Robot.IsConnected) return;
try
{
var curpos = await Robot.GetRobotPosAsync();
if (SelectCalibration.HomePoint == null)
{
SelectCalibration.HomePoint = new RPoint();
}
SelectCalibration.HomePoint.X = curpos.X;
SelectCalibration.HomePoint.Y = curpos.Y;
SelectCalibration.HomePoint.Z = curpos.Z;
SelectCalibration.HomePoint.U = curpos.U;
SelectCalibration.HomePoint.V = curpos.V;
SelectCalibration.HomePoint.W = curpos.W;
SelectCalibration.HomePoint.Hand = curpos.Hand;
SelectCalibration.HomePoint.Local = curpos.Local;
SelectCalibration.HomePoint.Tool = curpos.Tool;
_eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = Lang.成功, Duration = 1 });
}
catch (Exception ex)
{
LogHelper.WriteLogError(Lang.相机校准示教中心点界面示教待机点时出错, ex);
_eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
}
}
///
/// 夹爪打开/关闭
///
///
async void ExecuteBlowOrInhalControlCommand(object obj)
{
try
{
await Robot.CalibOutIOAsync(!(bool)obj);
}
catch (Exception ex)
{
LogHelper.WriteLogError(Lang.相机校准夹爪打开关闭点时出错, ex);
_eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
}
}
///
/// 示教P0
///
async void ExecuteTechMarkPointCommand()
{
if (Robot == null || !Robot.IsConnected) return;
try
{
var curpos = await Robot.GetRobotPosAsync();
if (SelectCalibration.MarkPoint == null)
{
SelectCalibration.MarkPoint = new RPoint();
}
SelectCalibration.MarkPoint.X = curpos.X;
SelectCalibration.MarkPoint.Y = curpos.Y;
SelectCalibration.MarkPoint.Z = curpos.Z;
SelectCalibration.MarkPoint.U = curpos.U;
SelectCalibration.MarkPoint.V = curpos.V;
SelectCalibration.MarkPoint.W = curpos.W;
SelectCalibration.MarkPoint.Hand = curpos.Hand;
SelectCalibration.MarkPoint.Local = curpos.Local;
SelectCalibration.MarkPoint.Tool = curpos.Tool;
_eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = Lang.成功, Duration = 1 });
}
catch (Exception ex)
{
LogHelper.WriteLogError(Lang.相机校准示教中心点界面示教P0时出错, ex);
_eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
}
}
///
/// 移动至中心点
///
async void ExecuteMoveCenterCommand()
{
if (Robot == null || !Robot.IsConnected) return;
try
{
if (SelectCalibration.CenterPoint.X==0 && SelectCalibration.CenterPoint.Y==0)
{
MessageBox.Show(Lang.检测到机器人的XY为0可能点位未示教将不允许移动机器人);
return;
}
if (MessageBox.Show(Lang.是否移动机器人至中心点请确认安全,Lang.移动机器人,MessageBoxButton.YesNo,MessageBoxImage.Asterisk)!= MessageBoxResult.Yes)
{
return;
}
await Robot.CalibMotionAsync(SelectCalibration.CenterPoint,0);
LogHelper.WriteLogInfo(Lang.相机校准移动机器人至中心点界面示教待机点);
_eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = string.Format(Lang.成功X0Y1Z2, SelectCalibration.CenterPoint.X, SelectCalibration.CenterPoint.Y, SelectCalibration.CenterPoint.Z), Duration = 1 });
}
catch (Exception ex)
{
LogHelper.WriteLogError(Lang.相机校准示教中心点界面示教中心点时出错, ex);
_eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
}
}
#endregion
#region 继承
///
/// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
///
///
///
public void ConfirmNavigationRequest(NavigationContext navigationContext, Action continuationCallback)
{
continuationCallback(true);
}
///
/// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
///
///
///
public async void OnNavigatedTo(NavigationContext navigationContext)
{
SelectCalibration = navigationContext.Parameters.GetValue("SelectCalibration");
if (SelectCalibration != null)
{
Robot = _robotService.GetRobot(SelectCalibration.RobotId);
if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
{
IsShowTechHome = true;
}
if (SelectCalibration.CameraMount == Enums.CameraMount.FixedUp)
{
IsShowTechP0ModeSelect = false;
}
else if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
{
IsShowTechP0ModeSelect = false;
}
else if (SelectCalibration.CameraMount == Enums.CameraMount.MobileJ4)
{
IsShowTechP0ModeSelect = true;
}
else if (SelectCalibration.CameraMount == Enums.CameraMount.MobileJ2)
{
IsShowTechP0ModeSelect = true;
}
else if (SelectCalibration.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
{
IsShowTechP0ModeSelect = true;
}
}
else
{
IsShowTechHome = false;
}
}
///
/// 是否允许导航到此视图模型。
///
///
///
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
///
/// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
///
///
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
#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;
}
}
}
}