CalibrationBasicsViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. using Prism.Commands;
  2. using Prism.Events;
  3. using Prism.Ioc;
  4. using Prism.Mvvm;
  5. using Prism.Regions;
  6. using Prism.Services.Dialogs;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Linq;
  11. using System.Windows;
  12. using TeamAAS_VP.Events;
  13. using TeamAAS_VP.Core;
  14. using TeamAAS_VP.Interfaces;
  15. using TeamAAS_VP.Models;
  16. using TeamAAS_VP;
  17. using System.ComponentModel;
  18. using TeamAAS_VP.Enums;
  19. namespace TeamAAS_VP.ViewModels.Calibration
  20. {
  21. public class CalibrationBasicsViewModel : BindableBase, IDataErrorInfo
  22. {
  23. IRegionManager _regionManager;
  24. IRegionNavigationService _regionNavigationService;
  25. IContainerProvider _container;
  26. IDialogService _dialogService;
  27. IEventAggregator _eventAggregator;
  28. #region 属性
  29. private ObservableCollection<RobotInfo> _Robots;
  30. public ObservableCollection<RobotInfo> Robots
  31. {
  32. get { return _Robots; }
  33. set
  34. {
  35. SetProperty(ref _Robots, value);
  36. RaisePropertyChanged(nameof(Error));
  37. }
  38. }
  39. private ObservableCollection<FeederInfo> _Feeders;
  40. public ObservableCollection<FeederInfo> Feeders
  41. {
  42. get { return _Feeders; }
  43. set { SetProperty(ref _Feeders, value);
  44. }
  45. }
  46. private CalibrationInfo _SelectCalibration;
  47. public CalibrationInfo SelectCalibration
  48. {
  49. get { return _SelectCalibration; }
  50. set
  51. {
  52. SetProperty(ref _SelectCalibration, value);
  53. if (value != null)
  54. {
  55. SelectRobot = Robots.FirstOrDefault(r => r.Id == value.RobotId);
  56. SelectFeeder = Feeders.FirstOrDefault(r => r.Id == value.FeederId);
  57. }
  58. }
  59. }
  60. private RobotInfo _SelectRobot;
  61. public RobotInfo SelectRobot
  62. {
  63. get { return _SelectRobot; }
  64. set
  65. {
  66. SetProperty(ref _SelectRobot, value);
  67. if (value != null && SelectCalibration != null)
  68. {
  69. SelectCalibration.RobotId = value.Id;
  70. }
  71. }
  72. }
  73. private FeederInfo _SelectFeeder;
  74. public FeederInfo SelectFeeder
  75. {
  76. get { return _SelectFeeder; }
  77. set
  78. {
  79. SetProperty(ref _SelectFeeder, value);
  80. if (value != null && SelectCalibration != null)
  81. {
  82. SelectCalibration.FeederId = value.Id;
  83. }
  84. }
  85. }
  86. private CameraInfo _SelectCamera;
  87. public CameraInfo SelectCamera
  88. {
  89. get { return _SelectCamera; }
  90. set
  91. {
  92. SetProperty(ref _SelectCamera, value);
  93. if (value != null && SelectCalibration != null)
  94. {
  95. SelectCalibration.CameraID = value.Id;
  96. SelectCalibration.CameraName = value.CameraName;
  97. }
  98. else
  99. {
  100. SelectCalibration.CameraID = Guid.Empty;
  101. SelectCalibration.CameraName = "";
  102. }
  103. }
  104. }
  105. private Management _management;
  106. public Management management
  107. {
  108. get { return _management; }
  109. set { SetProperty(ref _management, value); }
  110. }
  111. #endregion
  112. #region 命令
  113. private DelegateCommand _LoadedCommand;
  114. public DelegateCommand LoadedCommand =>
  115. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  116. private DelegateCommand _NextCommand;
  117. public DelegateCommand NextCommand =>
  118. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  119. private DelegateCommand _BackCommand;
  120. public DelegateCommand BackCommand =>
  121. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  122. private DelegateCommand _SelectCameraMountChangedCommand;
  123. public DelegateCommand SelectCameraMountChangedCommand =>
  124. _SelectCameraMountChangedCommand ?? (_SelectCameraMountChangedCommand = new DelegateCommand(ExecuteSelectCameraMountChangedCommand));
  125. #endregion
  126. #region 事件
  127. #endregion
  128. public CalibrationBasicsViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService)
  129. {
  130. _regionManager = regionManager;
  131. _regionNavigationService = regionNavigationService;
  132. _container = container;
  133. _dialogService = dialogService;
  134. _eventAggregator = ea;
  135. management = _container.Resolve<Management>();
  136. Robots = _container.Resolve<Management>().DeviceConfig.Robots;
  137. Feeders = _container.Resolve<Management>().DeviceConfig.Feeders;
  138. SelectCalibration = _container.Resolve<CalibrationHomeViewModel>().SelectCalibration;
  139. }
  140. #region 方法
  141. void ExecuteLoadedCommand()
  142. {
  143. if (_container.Resolve<Management>().CurrentUser.userPart == Enums.UserPart.Operator)
  144. {
  145. IsAllowEdit = false;
  146. }
  147. else
  148. {
  149. IsAllowEdit = true;
  150. }
  151. SelectCalibration = _container.Resolve<CalibrationHomeViewModel>().SelectCalibration;
  152. if (SelectCalibration.CameraID != Guid.Empty)
  153. {
  154. SelectCamera = management.DeviceConfig.Cameras.FirstOrDefault(c => c.Id == SelectCalibration.CameraID);
  155. }
  156. }
  157. /// <summary>
  158. /// 下一步
  159. /// </summary>
  160. async void ExecuteNextCommand()
  161. {
  162. try
  163. {
  164. SelectCalibration = _container.Resolve<CalibrationHomeViewModel>().SelectCalibration;
  165. if (SelectCalibration != null)
  166. {
  167. if (SelectCalibration.Pick != null && (SelectCalibration.Pick.Inhal > -1 || SelectCalibration.Pick.Blow > -1))
  168. {
  169. IRobot Robot = management.RobotService.GetRobot(SelectCalibration.RobotId);
  170. if (Robot.IsConnected)
  171. {
  172. //发送校准的参数
  173. await Robot.CalibParameAsync(SelectCalibration.Pick, SelectCalibration.Speed, SelectCalibration.Accel, SelectCalibration.Power, SelectCalibration.WaitSuction, SelectCalibration.WaitBlow);
  174. await Robot.CalibOutIOAsync(true);
  175. }
  176. }
  177. if (SelectCalibration.FeederId != Guid.Empty)
  178. {
  179. var feeder = _container.Resolve<Management>().FeederService.GetFeeder(SelectCalibration.FeederId);
  180. if (feeder.IsConnected)
  181. {
  182. await feeder.OpenLightAsync();
  183. }
  184. }
  185. }
  186. }
  187. catch (Exception ex)
  188. {
  189. LogHelper.WriteLogError("相机校准参数配置点击下一步时出错!", ex);
  190. MessageBox.Show(ex.Message);
  191. }
  192. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationCameraParams");
  193. }
  194. /// <summary>
  195. /// 上一步
  196. /// </summary>
  197. void ExecuteBackCommand()
  198. {
  199. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationHome");
  200. }
  201. void ExecuteSelectCameraMountChangedCommand()
  202. {
  203. ValidateProperty(nameof(SelectCalibration));
  204. RaisePropertyChanged(nameof(Error));
  205. }
  206. // 手动触发验证
  207. public string ValidateProperty(string propertyName)
  208. {
  209. return this[propertyName]; // 主动调用索引器
  210. }
  211. #endregion
  212. private bool _IsAllowEdit = false;
  213. public bool IsAllowEdit
  214. {
  215. get { return _IsAllowEdit; }
  216. set { SetProperty(ref _IsAllowEdit, value); }
  217. }
  218. public string Error{
  219. get
  220. {
  221. //var errors = new List<string>()
  222. //{
  223. // this[nameof(SelectCalibration)],
  224. // this[nameof(SelectRobot)],
  225. //};
  226. //return string.Join(Environment.NewLine, errors.Where(e => !string.IsNullOrEmpty(e)));
  227. string err1 = this[nameof(SelectCalibration)];
  228. string err2 = this[nameof(SelectRobot)];
  229. if (!string.IsNullOrEmpty(err1))
  230. {
  231. return err1;
  232. }
  233. if (!string.IsNullOrEmpty(err2))
  234. {
  235. return err2;
  236. }
  237. return string.Empty;
  238. }
  239. }
  240. public string this[string columnName]
  241. {
  242. get
  243. {
  244. string result = string.Empty;
  245. switch (columnName)
  246. {
  247. case nameof(SelectCalibration):
  248. if (SelectCalibration != null && SelectRobot != null)
  249. {
  250. switch (SelectRobot.RobotBrand)
  251. {
  252. case RobotBrand.Default:
  253. break;
  254. case RobotBrand.EPSON:
  255. case RobotBrand.FANUC:
  256. case RobotBrand.Schneider:
  257. if (SelectCalibration.CameraMount == CameraMount.MobileDown_XYPlatform)
  258. {
  259. result = "Unsupported camera installation method!";
  260. }
  261. break;
  262. case RobotBrand.XYZ_Platform:
  263. case RobotBrand.XYZU_Platform:
  264. if (SelectCalibration.CameraMount == CameraMount.MobileJ2 || SelectCalibration.CameraMount == CameraMount.MobileJ4)
  265. {
  266. result = "Unsupported camera installation method!";
  267. }
  268. break;
  269. default:
  270. break;
  271. }
  272. }
  273. break;
  274. case nameof(SelectRobot):
  275. if (SelectCalibration != null && SelectRobot != null)
  276. {
  277. switch (SelectRobot.RobotBrand)
  278. {
  279. case RobotBrand.Default:
  280. break;
  281. case RobotBrand.EPSON:
  282. case RobotBrand.FANUC:
  283. case RobotBrand.Schneider:
  284. if (SelectCalibration.CameraMount == CameraMount.MobileDown_XYPlatform)
  285. {
  286. result = "不支持的相机校准方式!";
  287. }
  288. break;
  289. case RobotBrand.XYZ_Platform:
  290. case RobotBrand.XYZU_Platform:
  291. if (SelectCalibration.CameraMount == CameraMount.MobileJ2 || SelectCalibration.CameraMount == CameraMount.MobileJ4)
  292. {
  293. result = "不支持的相机校准方式!";
  294. }
  295. break;
  296. default:
  297. break;
  298. }
  299. }
  300. break;
  301. //case nameof(SelectFeeder):
  302. // if (SelectFeeder == null)
  303. // {
  304. // result = "请选择喂料器";
  305. // }
  306. // break;
  307. //case nameof(SelectCamera):
  308. // if (SelectCamera == null)
  309. // {
  310. // result = "请选择相机";
  311. // }
  312. // break;
  313. }
  314. return result;
  315. }
  316. }
  317. private void LoginChange(Models.User user)
  318. {
  319. if (user.userPart == Enums.UserPart.Operator)
  320. {
  321. IsAllowEdit = false;
  322. }
  323. else
  324. {
  325. IsAllowEdit = true;
  326. }
  327. }
  328. }
  329. }