using Prism.Commands; using Prism.Events; using Prism.Ioc; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Windows; using TeamAAS_VP.Events; using TeamAAS_VP.Core; using TeamAAS_VP.Interfaces; using TeamAAS_VP.Models; using TeamAAS_VP; using System.ComponentModel; using TeamAAS_VP.Enums; namespace TeamAAS_VP.ViewModels.Calibration { public class CalibrationBasicsViewModel : BindableBase, IDataErrorInfo { IRegionManager _regionManager; IRegionNavigationService _regionNavigationService; IContainerProvider _container; IDialogService _dialogService; IEventAggregator _eventAggregator; #region 属性 private ObservableCollection _Robots; public ObservableCollection Robots { get { return _Robots; } set { SetProperty(ref _Robots, value); RaisePropertyChanged(nameof(Error)); } } private ObservableCollection _Feeders; public ObservableCollection Feeders { get { return _Feeders; } set { SetProperty(ref _Feeders, value); } } private CalibrationInfo _SelectCalibration; public CalibrationInfo SelectCalibration { get { return _SelectCalibration; } set { SetProperty(ref _SelectCalibration, value); if (value != null) { SelectRobot = Robots.FirstOrDefault(r => r.Id == value.RobotId); SelectFeeder = Feeders.FirstOrDefault(r => r.Id == value.FeederId); } } } private RobotInfo _SelectRobot; public RobotInfo SelectRobot { get { return _SelectRobot; } set { SetProperty(ref _SelectRobot, value); if (value != null && SelectCalibration != null) { SelectCalibration.RobotId = value.Id; } } } private FeederInfo _SelectFeeder; public FeederInfo SelectFeeder { get { return _SelectFeeder; } set { SetProperty(ref _SelectFeeder, value); if (value != null && SelectCalibration != null) { SelectCalibration.FeederId = value.Id; } } } private CameraInfo _SelectCamera; public CameraInfo SelectCamera { get { return _SelectCamera; } set { SetProperty(ref _SelectCamera, value); if (value != null && SelectCalibration != null) { SelectCalibration.CameraID = value.Id; SelectCalibration.CameraName = value.CameraName; } else { SelectCalibration.CameraID = Guid.Empty; SelectCalibration.CameraName = ""; } } } private Management _management; public Management management { get { return _management; } set { SetProperty(ref _management, 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 _SelectCameraMountChangedCommand; public DelegateCommand SelectCameraMountChangedCommand => _SelectCameraMountChangedCommand ?? (_SelectCameraMountChangedCommand = new DelegateCommand(ExecuteSelectCameraMountChangedCommand)); #endregion #region 事件 #endregion public CalibrationBasicsViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService) { _regionManager = regionManager; _regionNavigationService = regionNavigationService; _container = container; _dialogService = dialogService; _eventAggregator = ea; management = _container.Resolve(); Robots = _container.Resolve().DeviceConfig.Robots; Feeders = _container.Resolve().DeviceConfig.Feeders; SelectCalibration = _container.Resolve().SelectCalibration; } #region 方法 void ExecuteLoadedCommand() { if (_container.Resolve().CurrentUser.userPart == Enums.UserPart.Operator) { IsAllowEdit = false; } else { IsAllowEdit = true; } SelectCalibration = _container.Resolve().SelectCalibration; if (SelectCalibration.CameraID != Guid.Empty) { SelectCamera = management.DeviceConfig.Cameras.FirstOrDefault(c => c.Id == SelectCalibration.CameraID); } } /// /// 下一步 /// async void ExecuteNextCommand() { try { SelectCalibration = _container.Resolve().SelectCalibration; if (SelectCalibration != null) { if (SelectCalibration.Pick != null && (SelectCalibration.Pick.Inhal > -1 || SelectCalibration.Pick.Blow > -1)) { IRobot Robot = management.RobotService.GetRobot(SelectCalibration.RobotId); if (Robot.IsConnected) { //发送校准的参数 await Robot.CalibParameAsync(SelectCalibration.Pick, SelectCalibration.Speed, SelectCalibration.Accel, SelectCalibration.Power, SelectCalibration.WaitSuction, SelectCalibration.WaitBlow); await Robot.CalibOutIOAsync(true); } } if (SelectCalibration.FeederId != Guid.Empty) { var feeder = _container.Resolve().FeederService.GetFeeder(SelectCalibration.FeederId); if (feeder.IsConnected) { await feeder.OpenLightAsync(); } } } } catch (Exception ex) { LogHelper.WriteLogError("相机校准参数配置点击下一步时出错!", ex); MessageBox.Show(ex.Message); } _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationCameraParams"); } /// /// 上一步 /// void ExecuteBackCommand() { _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationHome"); } void ExecuteSelectCameraMountChangedCommand() { ValidateProperty(nameof(SelectCalibration)); RaisePropertyChanged(nameof(Error)); } // 手动触发验证 public string ValidateProperty(string propertyName) { return this[propertyName]; // 主动调用索引器 } #endregion private bool _IsAllowEdit = false; public bool IsAllowEdit { get { return _IsAllowEdit; } set { SetProperty(ref _IsAllowEdit, value); } } public string Error{ get { //var errors = new List() //{ // this[nameof(SelectCalibration)], // this[nameof(SelectRobot)], //}; //return string.Join(Environment.NewLine, errors.Where(e => !string.IsNullOrEmpty(e))); string err1 = this[nameof(SelectCalibration)]; string err2 = this[nameof(SelectRobot)]; if (!string.IsNullOrEmpty(err1)) { return err1; } if (!string.IsNullOrEmpty(err2)) { return err2; } return string.Empty; } } public string this[string columnName] { get { string result = string.Empty; switch (columnName) { case nameof(SelectCalibration): if (SelectCalibration != null && SelectRobot != null) { switch (SelectRobot.RobotBrand) { case RobotBrand.Default: break; case RobotBrand.EPSON: case RobotBrand.FANUC: case RobotBrand.Schneider: if (SelectCalibration.CameraMount == CameraMount.MobileDown_XYPlatform) { result = "Unsupported camera installation method!"; } break; case RobotBrand.XYZ_Platform: case RobotBrand.XYZU_Platform: if (SelectCalibration.CameraMount == CameraMount.MobileJ2 || SelectCalibration.CameraMount == CameraMount.MobileJ4) { result = "Unsupported camera installation method!"; } break; default: break; } } break; case nameof(SelectRobot): if (SelectCalibration != null && SelectRobot != null) { switch (SelectRobot.RobotBrand) { case RobotBrand.Default: break; case RobotBrand.EPSON: case RobotBrand.FANUC: case RobotBrand.Schneider: if (SelectCalibration.CameraMount == CameraMount.MobileDown_XYPlatform) { result = "不支持的相机校准方式!"; } break; case RobotBrand.XYZ_Platform: case RobotBrand.XYZU_Platform: if (SelectCalibration.CameraMount == CameraMount.MobileJ2 || SelectCalibration.CameraMount == CameraMount.MobileJ4) { result = "不支持的相机校准方式!"; } break; default: break; } } break; //case nameof(SelectFeeder): // if (SelectFeeder == null) // { // result = "请选择喂料器"; // } // break; //case nameof(SelectCamera): // if (SelectCamera == null) // { // result = "请选择相机"; // } // break; } return result; } } private void LoginChange(Models.User user) { if (user.userPart == Enums.UserPart.Operator) { IsAllowEdit = false; } else { IsAllowEdit = true; } } } }