SettingViewModel.cs 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. using ICSharpCode.AvalonEdit.Search;
  2. using MaterialDesignThemes.Wpf;
  3. using NPOI.SS.Formula.Functions;
  4. using Opc.Ua;
  5. using Prism.Commands;
  6. using Prism.Events;
  7. using Prism.Ioc;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using Prism.Services.Dialogs;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Collections.ObjectModel;
  14. using System.ComponentModel;
  15. using System.Globalization;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Windows;
  19. using Team.FFFeederService.Interfaces;
  20. using TeamAAS_VP.Controls;
  21. using TeamAAS_VP.Core;
  22. using TeamAAS_VP.Core.Cameras;
  23. using TeamAAS_VP.Enums;
  24. using TeamAAS_VP.Events;
  25. using TeamAAS_VP.Interfaces;
  26. using TeamAAS_VP.Models;
  27. using TeamAAS_VP.Resources.Languages;
  28. using TeamAAS_VP.Services;
  29. using TeamAAS_VP.ViewModels.User;
  30. using TouchSocket.Core;
  31. using WPFLocalizeExtension.Engine;
  32. namespace TeamAAS_VP.ViewModels
  33. {
  34. public class SettingViewModel : BindableBase, IConfirmNavigationRequest
  35. {
  36. IRegionManager _regionManager;
  37. IEventAggregator _eventAggregator;
  38. IContainerProvider _container;
  39. IDialogService _dialogService;
  40. IConfigService _configService;
  41. IRobotService _robotService;
  42. ICameraService _cameraService;
  43. IFeederService _feederService;
  44. IPlcService _plcService;
  45. IProductService _productService;
  46. ISystemDatabaseService _systemDatabaseService;
  47. bool isFirstLoad = true;
  48. #region 属性
  49. private Management _management;
  50. public Management management
  51. {
  52. get { return _management; }
  53. set { SetProperty(ref _management, value); }
  54. }
  55. private ObservableCollection<RobotInfo> _RobotList;
  56. public ObservableCollection<RobotInfo> RobotList
  57. {
  58. get { return _RobotList; }
  59. set { SetProperty(ref _RobotList, value); }
  60. }
  61. private ObservableCollection<CameraInfo> _CameraList;
  62. public ObservableCollection<CameraInfo> CameraList
  63. {
  64. get { return _CameraList; }
  65. set { SetProperty(ref _CameraList, value); }
  66. }
  67. private ObservableCollection<FeederInfo> _FeederList;
  68. public ObservableCollection<FeederInfo> FeederList
  69. {
  70. get { return _FeederList; }
  71. set { SetProperty(ref _FeederList, value); }
  72. }
  73. private ObservableCollection<PlcInfo> _PlcList;
  74. public ObservableCollection<PlcInfo> PlcList
  75. {
  76. get { return _PlcList; }
  77. set { SetProperty(ref _PlcList, value); }
  78. }
  79. private Language _CurrentLanguage;
  80. public Language CurrentLanguage
  81. {
  82. get { return _CurrentLanguage; }
  83. set { SetProperty(ref _CurrentLanguage, value); }
  84. }
  85. private RobotInfo _SelectRobot;
  86. /// <summary>
  87. /// 选中的机器人
  88. /// </summary>
  89. public RobotInfo SelectRobot
  90. {
  91. get { return _SelectRobot; }
  92. set { SetProperty(ref _SelectRobot, value);
  93. if (value!=null && (value.RobotBrand == RobotBrand.XYZ_Platform || value.RobotBrand == RobotBrand.XYZU_Platform))
  94. {
  95. RobotSelectedPlc = PlcList.FirstOrDefault(p=>p.Id==value.PLC);
  96. var root= _robotService.GetRobot(SelectRobot.Id);
  97. if (root != null)
  98. {
  99. ResultsValues.Clear();
  100. var res= root.GetNodesValue();
  101. for (int i = 0; i < res.Count; i++)
  102. {
  103. if (res[i].StatusCode.Code != 2150891520)
  104. {
  105. ResultsValues.Add(res[i].Value.ToString());
  106. }
  107. else
  108. {
  109. break;
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }
  116. private FeederInfo _SelectFeeder;
  117. /// <summary>
  118. /// 选中的Feeder
  119. /// </summary>
  120. public FeederInfo SelectFeeder
  121. {
  122. get { return _SelectFeeder; }
  123. set { SetProperty(ref _SelectFeeder, value); }
  124. }
  125. private PlcInfo _SelectPlc;
  126. public PlcInfo SelectPlc
  127. {
  128. get { return _SelectPlc; }
  129. set { SetProperty(ref _SelectPlc, value); }
  130. }
  131. private ObservableCollection<string> _ResultsValues = new ObservableCollection<string>();
  132. public ObservableCollection<string> ResultsValues { get => _ResultsValues; set => _ResultsValues = value; }
  133. private CameraInfo _SelectCamera;
  134. /// <summary>
  135. /// 选中的Camera
  136. /// </summary>
  137. public CameraInfo SelectCamera
  138. {
  139. get { return _SelectCamera; }
  140. set { SetProperty(ref _SelectCamera, value); }
  141. }
  142. private double _Title1;
  143. public double Title1
  144. {
  145. get { return _Title1; }
  146. set { SetProperty(ref _Title1, value); }
  147. }
  148. private double _Title2;
  149. public double Title2
  150. {
  151. get { return _Title2; }
  152. set { SetProperty(ref _Title2, value); }
  153. }
  154. private double _Title3;
  155. public double Title3
  156. {
  157. get { return _Title3; }
  158. set { SetProperty(ref _Title3, value); }
  159. }
  160. private double _Title4;
  161. public double Title4
  162. {
  163. get { return _Title4; }
  164. set { SetProperty(ref _Title4, value); }
  165. }
  166. private double _Body1;
  167. public double Body1
  168. {
  169. get { return _Body1; }
  170. set { SetProperty(ref _Body1, value); }
  171. }
  172. private double _Body2;
  173. public double Body2
  174. {
  175. get { return _Body2; }
  176. set { SetProperty(ref _Body2, value); }
  177. }
  178. private double _Body3;
  179. public double Body3
  180. {
  181. get { return _Body3; }
  182. set { SetProperty(ref _Body3, value); }
  183. }
  184. private double _Body4;
  185. public double Body4
  186. {
  187. get { return _Body4; }
  188. set { SetProperty(ref _Body4, value); }
  189. }
  190. private double _Captions1;
  191. public double Captions1
  192. {
  193. get { return _Captions1; }
  194. set { SetProperty(ref _Captions1, value); }
  195. }
  196. private double _Captions2;
  197. public double Captions2
  198. {
  199. get { return _Captions2; }
  200. set { SetProperty(ref _Captions2, value); }
  201. }
  202. private double _Captions3;
  203. public double Captions3
  204. {
  205. get { return _Captions3; }
  206. set { SetProperty(ref _Captions3, value); }
  207. }
  208. private double _Captions4;
  209. public double Captions4
  210. {
  211. get { return _Captions4; }
  212. set { SetProperty(ref _Captions4, value); }
  213. }
  214. private PlcInfo _RobotSelectedPlc;
  215. public PlcInfo RobotSelectedPlc
  216. {
  217. get { return _RobotSelectedPlc; }
  218. set { SetProperty(ref _RobotSelectedPlc, value);
  219. if (SelectRobot != null)
  220. {
  221. if (value != null)
  222. {
  223. SelectRobot.PLC=value.Id;
  224. }
  225. else
  226. {
  227. SelectRobot.PLC = Guid.Empty;
  228. }
  229. }
  230. }
  231. }
  232. private BgModbusTcp _BgModbus;
  233. public BgModbusTcp BgModbus
  234. {
  235. get { return _BgModbus; }
  236. set { SetProperty(ref _BgModbus, value); }
  237. }
  238. private BgTcpIP _BgTcp;
  239. public BgTcpIP BgTcp
  240. {
  241. get { return _BgTcp; }
  242. set { SetProperty(ref _BgTcp, value); }
  243. }
  244. #endregion
  245. #region 命令
  246. public DelegateCommand LoadedCommand { get; set; }
  247. private DelegateCommand _AddRobotCommand;
  248. public DelegateCommand AddRobotCommand =>
  249. _AddRobotCommand ?? (_AddRobotCommand = new DelegateCommand(ExecuteAddRobotCommand));
  250. private DelegateCommand _RemoveCommand;
  251. public DelegateCommand RemoveCommand =>
  252. _RemoveCommand ?? (_RemoveCommand = new DelegateCommand(ExecuteRemoveCommand));
  253. private DelegateCommand _AddFeederCommand;
  254. public DelegateCommand AddFeederCommand =>
  255. _AddFeederCommand ?? (_AddFeederCommand = new DelegateCommand(ExecuteAddFeederCommand));
  256. private DelegateCommand _RemoveFeederCommand;
  257. public DelegateCommand RemoveFeederCommand =>
  258. _RemoveFeederCommand ?? (_RemoveFeederCommand = new DelegateCommand(ExecuteRemoveFeederCommand));
  259. private DelegateCommand<string> _SelectLanguageCommand;
  260. public DelegateCommand<string> SelectLanguageCommand =>
  261. _SelectLanguageCommand ?? (_SelectLanguageCommand = new DelegateCommand<string>(ExecuteSelectLanguageCommand));
  262. private DelegateCommand _SaveRobotsCommand;
  263. public DelegateCommand SaveRobotsCommand =>
  264. _SaveRobotsCommand ?? (_SaveRobotsCommand = new DelegateCommand(ExecuteSaveRobotsCommand));
  265. private DelegateCommand _SaveFeedersCommand;
  266. public DelegateCommand SaveFeedersCommand =>
  267. _SaveFeedersCommand ?? (_SaveFeedersCommand = new DelegateCommand(ExecuteSaveFeedersCommand));
  268. private DelegateCommand<string> _SaveCommunicateCommand;
  269. public DelegateCommand<string> SaveCommunicateCommand =>
  270. _SaveCommunicateCommand ?? (_SaveCommunicateCommand = new DelegateCommand<string>(ExecuteSaveCommunicateCommand));
  271. private DelegateCommand _AddCameraCommand;
  272. public DelegateCommand AddCameraCommand =>
  273. _AddCameraCommand ?? (_AddCameraCommand = new DelegateCommand(ExecuteAddCameraCommand));
  274. private DelegateCommand _EditCameraCommand;
  275. public DelegateCommand EditCameraCommand =>
  276. _EditCameraCommand ?? (_EditCameraCommand = new DelegateCommand(ExecuteEditCameraCommand));
  277. private DelegateCommand _RemoveCameraCommand;
  278. public DelegateCommand RemoveCameraCommand =>
  279. _RemoveCameraCommand ?? (_RemoveCameraCommand = new DelegateCommand(ExecuteRemoveCameraCommand));
  280. private DelegateCommand _SaveCamerasCommand;
  281. public DelegateCommand SaveCamerasCommand =>
  282. _SaveCamerasCommand ?? (_SaveCamerasCommand = new DelegateCommand(ExecuteSaveCamerasCommand));
  283. private DelegateCommand _AddPlcCommand;
  284. public DelegateCommand AddPlcCommand =>
  285. _AddPlcCommand ?? (_AddPlcCommand = new DelegateCommand(ExecuteAddPlcCommand));
  286. private DelegateCommand _RemovePlcCommand;
  287. public DelegateCommand RemovePlcCommand =>
  288. _RemovePlcCommand ?? (_RemovePlcCommand = new DelegateCommand(ExecuteRemovePlcCommand));
  289. private DelegateCommand _SavePlcsCommand;
  290. public DelegateCommand SavePlcsCommand =>
  291. _SavePlcsCommand ?? (_SavePlcsCommand = new DelegateCommand(ExecuteSavePlcsCommand));
  292. #endregion
  293. #region 事件
  294. #endregion
  295. public SettingViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService,
  296. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, IPlcService plcService,
  297. IProductService productService,ISystemDatabaseService systemDatabaseService)
  298. {
  299. _regionManager = regionManager;
  300. _eventAggregator = ea;
  301. _container = container;
  302. _dialogService = dialogService;
  303. _configService = configService;
  304. _robotService = robotService;
  305. _cameraService = cameraService;
  306. _feederService = feederService;
  307. _plcService = plcService;
  308. _productService= productService;
  309. _systemDatabaseService= systemDatabaseService;
  310. management = _container.Resolve<Management>();
  311. LoadedCommand = new DelegateCommand(OnLoad);
  312. //订阅权限登录事件
  313. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  314. }
  315. #region 方法
  316. #region 机器人
  317. private void ExecuteAddRobotCommand()
  318. {
  319. _dialogService.ShowDialog("AddRobot", rst =>
  320. {
  321. //对话框关闭之后的回调函数,可以在这解析结果。
  322. ButtonResult result1 = rst.Result;
  323. if (result1 == ButtonResult.OK)
  324. {
  325. var param = rst.Parameters.GetValue<RobotInfo>("Robot");
  326. var robot = new RobotInfo()
  327. {
  328. Id = param.Id,
  329. RobotName = param.RobotName,
  330. ConnectType = param.ConnectType,
  331. RobotBrand = param.RobotBrand,
  332. IP = param.IP,
  333. Port = param.Port,
  334. Terminator = param.Terminator,
  335. };
  336. RobotList.Add(robot);
  337. _configService.AddOrUpdateRobot(robot);
  338. _robotService.CreateRobot(robot.Id,robot);
  339. //对所有产品也进行添加
  340. _productService.AddRobotPointToProduct(robot.Id, robot.RobotName);
  341. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.添加完成}", Duration = 1 });
  342. }
  343. });
  344. }
  345. void ExecuteRemoveCommand()
  346. {
  347. if (MessageBox.Show(Lang.是否移除机器人, $"AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  348. {
  349. // 找到要删除的机器人
  350. var robotToRemove = RobotList.FirstOrDefault(r => r.Id == SelectRobot.Id);
  351. if (robotToRemove != null)
  352. {
  353. RobotList.Remove(robotToRemove);
  354. _configService.RemoveRobot(robotToRemove.Id);
  355. _robotService.RemoveRobot(robotToRemove.Id);
  356. //对所有产品也进行移除
  357. _productService.RemoveRobotPointFromProduct(robotToRemove.Id);
  358. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.移除完成, Duration = 1 });
  359. }
  360. }
  361. }
  362. async void ExecuteSaveRobotsCommand()
  363. {
  364. var view = new ShowMessage($"AAS", Lang.是否保存机器人);
  365. //show the dialog
  366. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  367. if (result != null && result is bool)
  368. {
  369. if (((bool)result))
  370. {
  371. foreach (var item in RobotList)
  372. {
  373. _configService.AddOrUpdateRobot(item);
  374. }
  375. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  376. }
  377. }
  378. }
  379. #endregion
  380. #region 相机
  381. /// <summary>
  382. /// 保存所有相机
  383. /// </summary>
  384. async void ExecuteSaveCamerasCommand()
  385. {
  386. var view = new ShowMessage($"AAS", Lang.是否保存相机配置);
  387. //show the dialog
  388. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  389. if (result != null && result is bool)
  390. {
  391. if (((bool)result))
  392. {
  393. //management.DeviceConfig.Cameras = DeviceConfig.Cameras;
  394. //management.SaveCamerasDeviceConfig();
  395. //management.DeviceConfig.Cameras = DeviceConfig.Cameras;
  396. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  397. }
  398. }
  399. }
  400. /// <summary>
  401. /// 移除相机
  402. /// </summary>
  403. void ExecuteRemoveCameraCommand()
  404. {
  405. if (MessageBox.Show(Lang.是否移除相机, $"AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  406. {
  407. // 找到要删除的相机
  408. var cameraToRemove = CameraList.FirstOrDefault(r => r.Id == SelectCamera.Id);
  409. if (cameraToRemove != null)
  410. {
  411. _configService.RemoveCamera(cameraToRemove.Id);
  412. _cameraService.RemoveCamera(cameraToRemove.Id);
  413. var root= _configService.GetAllCameras();
  414. CameraList = new ObservableCollection<CameraInfo>(root);
  415. foreach (var item in CameraList)
  416. {
  417. _cameraService.UpdateCameraNumber(item.Id, item.CameraNo);
  418. }
  419. //对所有产品也进行移除
  420. _productService.RemoveCameraFromProduct(cameraToRemove.Id);
  421. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  422. }
  423. }
  424. }
  425. /// <summary>
  426. /// 编辑相机
  427. /// </summary>
  428. void ExecuteEditCameraCommand()
  429. {
  430. if (SelectCamera == null) return;
  431. bool isSelectedCameraBrandSucced = false;
  432. CameraBrand cameraBrand = CameraBrand.HikRobot_MVS;
  433. IDialogParameters parameters1 = new DialogParameters();
  434. parameters1.Add("CameraBrand", SelectCamera.CameraBrand);
  435. _dialogService.ShowDialog("SelectCameraBrand", parameters1, rst =>
  436. {
  437. if (rst.Result == ButtonResult.OK)
  438. {
  439. isSelectedCameraBrandSucced = true;
  440. cameraBrand = rst.Parameters.GetValue<CameraBrand>("CameraBrand");
  441. }
  442. else
  443. {
  444. return;
  445. }
  446. });
  447. if (!isSelectedCameraBrandSucced) { return; }
  448. (bool isInstalled, string version) result = (false, "");
  449. string softwarename = "";
  450. switch (cameraBrand)
  451. {
  452. case CameraBrand.HikRobot_MVS:
  453. softwarename = "MVS";
  454. result = MvCamera.CheckSoftwareInstalled();
  455. break;
  456. case CameraBrand.Basler_Pylon:
  457. softwarename = "Pylon 7";
  458. result = BaslerCamera.CheckSoftwareInstalled();
  459. break;
  460. case CameraBrand.IRayple:
  461. softwarename = "MV Viewer";
  462. result = AHuaCamera.CheckSoftwareInstalled();
  463. break;
  464. case CameraBrand.Cognex:
  465. softwarename = "Cognex VisionPro";
  466. result = CognexCamera.CheckSoftwareInstalled();
  467. break;
  468. case CameraBrand.Galaxy:
  469. softwarename = "Galaxy Viewer";
  470. result = GalaxyCamera.CheckSoftwareInstalled();
  471. break;
  472. default:
  473. break;
  474. }
  475. if (!result.isInstalled)
  476. {
  477. if (string.IsNullOrEmpty(result.version))
  478. {
  479. //未安装
  480. MessageBox.Show(Lang.CameraSDKCheck_Message1.Replace("{name}", softwarename), "AAS", MessageBoxButton.OK, MessageBoxImage.Asterisk);
  481. }
  482. else
  483. {
  484. //版本较低
  485. MessageBox.Show(Lang.CameraSDKCheck_Message2.Replace("{name}", softwarename).Replace("{version}", result.version).Replace("\\n", Environment.NewLine), "AAS", MessageBoxButton.OK, MessageBoxImage.Asterisk);
  486. }
  487. return;
  488. }
  489. //编辑前的相机名称
  490. string oldCameraName = SelectCamera.CameraName;
  491. IDialogParameters parameters = new DialogParameters();
  492. parameters.Add("CameraBrand", cameraBrand);
  493. parameters.Add("Camera", SelectCamera.Copy());
  494. _dialogService.ShowDialog("EditCamera", parameters, rst =>
  495. {
  496. //对话框关闭之后的回调函数,可以在这解析结果。
  497. ButtonResult result1 = rst.Result;
  498. if (result1 == ButtonResult.OK)
  499. {
  500. var param = rst.Parameters.GetValue<CameraInfo>("Camera");
  501. SelectCamera.CameraName = param.CameraName;
  502. SelectCamera.SerialNumber = param.SerialNumber;
  503. SelectCamera.Model = param.Model;
  504. SelectCamera.ManufacturerName = param.ManufacturerName;
  505. SelectCamera.CameraType = param.CameraType;
  506. SelectCamera.CameraBrand = param.CameraBrand;
  507. SelectCamera.CameraIp = param.CameraIp;
  508. _configService.AddOrUpdateCamera(SelectCamera);
  509. if (oldCameraName!= param.CameraName)
  510. {
  511. //对所有产品也进行修改
  512. _productService.UpdateCameraNameInProduct(SelectCamera.Id, SelectCamera.CameraName);
  513. }
  514. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  515. }
  516. });
  517. }
  518. /// <summary>
  519. /// 增加相机
  520. /// </summary>
  521. void ExecuteAddCameraCommand()
  522. {
  523. bool isSelectedCameraBrandSucced = false;
  524. CameraBrand cameraBrand = CameraBrand.HikRobot_MVS;
  525. _dialogService.ShowDialog("SelectCameraBrand", rst =>
  526. {
  527. if (rst.Result == ButtonResult.OK)
  528. {
  529. isSelectedCameraBrandSucced = true;
  530. cameraBrand = rst.Parameters.GetValue<CameraBrand>("CameraBrand");
  531. }
  532. else
  533. {
  534. return;
  535. }
  536. });
  537. if (!isSelectedCameraBrandSucced) { return; }
  538. ;
  539. (bool isInstalled, string version) result = (false, "");
  540. string softwarename = "";
  541. switch (cameraBrand)
  542. {
  543. case CameraBrand.HikRobot_MVS:
  544. softwarename = "MVS";
  545. result = MvCamera.CheckSoftwareInstalled();
  546. break;
  547. case CameraBrand.Basler_Pylon:
  548. softwarename = "Pylon 7";
  549. result = BaslerCamera.CheckSoftwareInstalled();
  550. break;
  551. case CameraBrand.IRayple:
  552. softwarename = "MV Viewer";
  553. result = AHuaCamera.CheckSoftwareInstalled();
  554. break;
  555. case CameraBrand.Cognex:
  556. softwarename = "Cognex VisionPro";
  557. result = CognexCamera.CheckSoftwareInstalled();
  558. break;
  559. case CameraBrand.Galaxy:
  560. softwarename = "Galaxy Viewer";
  561. result = GalaxyCamera.CheckSoftwareInstalled();
  562. break;
  563. default:
  564. break;
  565. }
  566. if (!result.isInstalled)
  567. {
  568. if (string.IsNullOrEmpty(result.version))
  569. {
  570. //未安装
  571. MessageBox.Show(Lang.CameraSDKCheck_Message1.Replace("{name}", softwarename), "AAS", MessageBoxButton.OK, MessageBoxImage.Asterisk);
  572. }
  573. else
  574. {
  575. //版本较低
  576. MessageBox.Show(Lang.CameraSDKCheck_Message2.Replace("{name}", softwarename).Replace("{version}", result.version).Replace("\\n", Environment.NewLine), "AAS", MessageBoxButton.OK, MessageBoxImage.Asterisk);
  577. }
  578. return;
  579. }
  580. IDialogParameters parameters = new DialogParameters();
  581. parameters.Add("CameraBrand", cameraBrand);
  582. _dialogService.ShowDialog("AddCamera", parameters, rst =>
  583. {
  584. //对话框关闭之后的回调函数,可以在这解析结果。
  585. ButtonResult result1 = rst.Result;
  586. if (result1 == ButtonResult.OK)
  587. {
  588. var param = rst.Parameters.GetValue<CameraInfo>("Camera");
  589. var camera= new CameraInfo()
  590. {
  591. Id = param.Id,
  592. CameraName = param.CameraName,
  593. SerialNumber = param.SerialNumber,
  594. Model = param.Model,
  595. ManufacturerName = param.ManufacturerName,
  596. CameraType = param.CameraType,
  597. CameraBrand = param.CameraBrand,
  598. CameraIp = param.CameraIp
  599. };
  600. CameraList.Add(camera);
  601. _configService.AddOrUpdateCamera(camera);
  602. _cameraService.CreateCamera(camera.Id,camera);
  603. //对所有产品也进行添加
  604. _productService.AddCameraToProduct(camera.Id, camera.CameraName);
  605. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  606. }
  607. });
  608. }
  609. #endregion
  610. #region Feeder
  611. void ExecuteAddFeederCommand()
  612. {
  613. _dialogService.ShowDialog("AddFeeder", rst =>
  614. {
  615. //对话框关闭之后的回调函数,可以在这解析结果。
  616. ButtonResult result1 = rst.Result;
  617. if (result1 == ButtonResult.OK)
  618. {
  619. var param = rst.Parameters.GetValue<FeederInfo>("Feeder");
  620. var feeder = new FeederInfo()
  621. {
  622. Id = param.Id,
  623. FeederName = param.FeederName,
  624. ConnectType = param.ConnectType,
  625. FeederBrand = param.FeederBrand,
  626. IP = param.IP,
  627. Port = param.Port
  628. };
  629. FeederList.Add(feeder);
  630. _configService.AddOrUpdateFeeder(feeder);
  631. _feederService.CreateFeeder(feeder.Id, feeder.FeederNo, feeder.FeederName, feeder.IP, feeder.Port, feeder.FeederBrand);
  632. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.添加完成, Duration = 1 });
  633. }
  634. });
  635. }
  636. void ExecuteRemoveFeederCommand()
  637. {
  638. if (MessageBox.Show(Lang.是否移除Feeder, " AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  639. {
  640. // 找到要删除的机器人
  641. var feederToRemove = FeederList.FirstOrDefault(r => r.Id == SelectFeeder.Id);
  642. if (feederToRemove != null)
  643. {
  644. _configService.RemoveFeeder(feederToRemove.Id);
  645. _feederService.RemoveFeeder(feederToRemove.Id);
  646. var root = _configService.GetAllFeeders();
  647. FeederList = new ObservableCollection<FeederInfo>(root);
  648. foreach (var item in FeederList)
  649. {
  650. _feederService.UpdateFeederNo(item.Id, item.FeederNo);
  651. }
  652. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.移除完成, Duration = 1 });
  653. }
  654. }
  655. }
  656. async void ExecuteSaveFeedersCommand()
  657. {
  658. var view = new ShowMessage($"AAS", Lang.是否保存Feeder);
  659. //show the dialog
  660. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  661. if (result != null && result is bool)
  662. {
  663. if (((bool)result))
  664. {
  665. foreach (var item in FeederList)
  666. {
  667. _configService.AddOrUpdateFeeder(item);
  668. }
  669. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  670. }
  671. }
  672. }
  673. #endregion
  674. #region PLC
  675. /// <summary>
  676. /// 保存PLC
  677. /// </summary>
  678. async void ExecuteSavePlcsCommand()
  679. {
  680. var view = new ShowMessage($"AAS", Lang.是否保存PLC);
  681. //show the dialog
  682. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  683. if (result != null && result is bool)
  684. {
  685. if (((bool)result))
  686. {
  687. foreach (var item in PlcList)
  688. {
  689. _configService.AddOrUpdatePlc(item);
  690. }
  691. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  692. }
  693. }
  694. }
  695. /// <summary>
  696. /// 移除PLC
  697. /// </summary>
  698. void ExecuteRemovePlcCommand()
  699. {
  700. if (MessageBox.Show(Lang.是否移除PLC, $"AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  701. {
  702. // 找到要删除的plc
  703. var plcToRemove = PlcList.FirstOrDefault(r => r.Id == SelectPlc.Id);
  704. if (plcToRemove != null)
  705. {
  706. _configService.RemovePlc(plcToRemove.Id);
  707. _plcService.RemovePlc(plcToRemove.Id);
  708. var root = _configService.GetAllPlcs();
  709. PlcList = new ObservableCollection<PlcInfo>(root);
  710. foreach (var item in PlcList)
  711. {
  712. _plcService.UpdatePlcNumber(item.Id, item.Index);
  713. }
  714. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.移除完成, Duration = 1 });
  715. }
  716. }
  717. }
  718. /// <summary>
  719. /// 增加PLC
  720. /// </summary>
  721. void ExecuteAddPlcCommand()
  722. {
  723. _dialogService.ShowDialog("AddPlc", rst =>
  724. {
  725. //对话框关闭之后的回调函数,可以在这解析结果。
  726. ButtonResult result1 = rst.Result;
  727. if (result1 == ButtonResult.OK)
  728. {
  729. var param = rst.Parameters.GetValue<PlcInfo>("PLC");
  730. var plc = new PlcInfo()
  731. {
  732. Id = param.Id,
  733. Name = param.Name,
  734. CommunicationType = param.CommunicationType,
  735. EndpointUrl = param.EndpointUrl,
  736. };
  737. PlcList.Add(plc);
  738. _configService.AddOrUpdatePlc(plc);
  739. _plcService.CreatePlc(plc.Id, plc);
  740. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.添加完成, Duration = 1 });
  741. }
  742. });
  743. }
  744. #endregion
  745. #region 通讯
  746. async void ExecuteSaveCommunicateCommand(string parm)
  747. {
  748. if (parm == "TCPIP")
  749. {
  750. var view = new ShowMessage($"AAS", Lang.是否保存后台TCP通讯配置);
  751. //show the dialog
  752. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  753. if (result != null && result is bool)
  754. {
  755. if (((bool)result))
  756. {
  757. _configService.SetBgTcp(BgTcp);
  758. _configService.SaveBgTcp();
  759. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  760. }
  761. }
  762. }
  763. else if (parm == "ModbusTCP")
  764. {
  765. var view = new ShowMessage($"AAS", Lang.是否保存后台ModbusTCP通讯配置);
  766. //show the dialog
  767. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  768. if (result != null && result is bool)
  769. {
  770. if (((bool)result))
  771. {
  772. _configService.SetBgModbusTcp(BgModbus);
  773. _configService.SaveBgModbusTcp();
  774. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  775. }
  776. }
  777. }
  778. else
  779. {
  780. return;
  781. }
  782. }
  783. #endregion
  784. #region 语言
  785. void ExecuteSelectLanguageCommand(string language)
  786. {
  787. if (string.Equals(language, "ChineseSimplified"))
  788. {
  789. CurrentLanguage= Enums.Language.ChineseSimplified;
  790. var sysConfig=_configService.GetSystemConfiguration();
  791. sysConfig.CurrentLanguage = CurrentLanguage;
  792. _configService.SaveSystemConfiguration(sysConfig);
  793. var culture = new CultureInfo("zh-CN");
  794. App.Current.Dispatcher.Thread.CurrentCulture = culture;
  795. App.Current.Dispatcher.Thread.CurrentUICulture = culture;
  796. LocalizeDictionary.Instance.Culture = culture;
  797. Lang.Culture = culture;
  798. Application.Current.Resources["DefaultFont"] = new System.Windows.Media.FontFamily("Microsoft YaHei");
  799. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.5 });
  800. }
  801. else if (string.Equals(language, "ChineseTraditional"))
  802. {
  803. CurrentLanguage = Enums.Language.ChineseTraditional;
  804. var sysConfig = _configService.GetSystemConfiguration();
  805. sysConfig.CurrentLanguage = CurrentLanguage;
  806. _configService.SaveSystemConfiguration(sysConfig);
  807. var culture = new CultureInfo("zh-TW");
  808. App.Current.Dispatcher.Thread.CurrentCulture = culture;
  809. App.Current.Dispatcher.Thread.CurrentUICulture = culture;
  810. LocalizeDictionary.Instance.Culture = culture;
  811. Lang.Culture = culture;
  812. Application.Current.Resources["DefaultFont"] = new System.Windows.Media.FontFamily("Microsoft YaHei");
  813. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.5 });
  814. }
  815. else if (string.Equals(language, "English"))
  816. {
  817. CurrentLanguage = Enums.Language.English;
  818. var sysConfig = _configService.GetSystemConfiguration();
  819. sysConfig.CurrentLanguage = CurrentLanguage;
  820. _configService.SaveSystemConfiguration(sysConfig);
  821. var culture = new CultureInfo("en");
  822. App.Current.Dispatcher.Thread.CurrentCulture = culture;
  823. App.Current.Dispatcher.Thread.CurrentUICulture = culture;
  824. LocalizeDictionary.Instance.Culture = culture;
  825. Lang.Culture = culture;
  826. Application.Current.Resources["DefaultFont"] = new System.Windows.Media.FontFamily("Segoe UI");
  827. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.5 });
  828. }
  829. else if (string.Equals(language, "Japanese"))
  830. {
  831. CurrentLanguage = Enums.Language.Japanese;
  832. var sysConfig = _configService.GetSystemConfiguration();
  833. sysConfig.CurrentLanguage = CurrentLanguage;
  834. _configService.SaveSystemConfiguration(sysConfig);
  835. var culture = new CultureInfo("ja-JP");
  836. App.Current.Dispatcher.Thread.CurrentCulture = culture;
  837. App.Current.Dispatcher.Thread.CurrentUICulture = culture;
  838. LocalizeDictionary.Instance.Culture = culture;
  839. Lang.Culture = culture;
  840. Application.Current.Resources["DefaultFont"] = new System.Windows.Media.FontFamily("Segoe UI");
  841. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.5 });
  842. }
  843. }
  844. #endregion
  845. private void OnLoad()
  846. {
  847. }
  848. private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
  849. {
  850. switch (e.PropertyName)
  851. {
  852. case "Title1":
  853. Application.Current.Resources["Font.Size.Title1"] = Title1;
  854. break;
  855. case "Title2":
  856. Application.Current.Resources["Font.Size.Title2"] = Title2;
  857. break;
  858. case "Title3":
  859. Application.Current.Resources["Font.Size.Title3"] = Title3;
  860. break;
  861. case "Title4":
  862. Application.Current.Resources["Font.Size.Title4"] = Title4;
  863. break;
  864. case "Body1":
  865. Application.Current.Resources["Font.Size.Body1"] = Body1;
  866. break;
  867. case "Body2":
  868. Application.Current.Resources["Font.Size.Body2"] = Body2;
  869. break;
  870. case "Body3":
  871. Application.Current.Resources["Font.Size.Body3"] = Body3;
  872. break;
  873. case "Body4":
  874. Application.Current.Resources["Font.Size.Body4"] = Body4;
  875. break;
  876. case "Captions1":
  877. Application.Current.Resources["Font.Size.Captions1"] = Captions1;
  878. break;
  879. case "Captions2":
  880. Application.Current.Resources["Font.Size.Captions2"] = Captions2;
  881. break;
  882. case "Captions3":
  883. Application.Current.Resources["Font.Size.Captions3"] = Captions3;
  884. break;
  885. case "Captions4":
  886. Application.Current.Resources["Font.Size.Captions4"] = Captions4;
  887. break;
  888. default:
  889. break;
  890. }
  891. }
  892. #endregion
  893. #region 继承
  894. /// <summary>
  895. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  896. /// </summary>
  897. /// <param name="navigationContext"></param>
  898. /// <param name="continuationCallback"></param>
  899. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  900. {
  901. continuationCallback(true);
  902. }
  903. /// <summary>
  904. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  905. /// </summary>
  906. /// <param name="navigationContext"></param>
  907. /// <exception cref="NotImplementedException"></exception>
  908. public async void OnNavigatedTo(NavigationContext navigationContext)
  909. {
  910. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  911. {
  912. IsAllowEdit = false;
  913. }
  914. else
  915. {
  916. IsAllowEdit = true;
  917. }
  918. if (RobotList == null)
  919. {
  920. RobotList = new ObservableCollection<RobotInfo>();
  921. var robots = _configService.GetAllRobots();
  922. foreach (var robot in robots)
  923. {
  924. RobotList.Add(robot);
  925. }
  926. }
  927. if (CameraList==null)
  928. {
  929. CameraList = new ObservableCollection<CameraInfo>();
  930. var cameras = _configService.GetAllCameras();
  931. foreach (var camera in cameras)
  932. {
  933. CameraList.Add(camera);
  934. }
  935. }
  936. if (FeederList==null)
  937. {
  938. FeederList = new ObservableCollection<FeederInfo>();
  939. var feeders = _configService.GetAllFeeders();
  940. foreach (var feeder in feeders)
  941. {
  942. FeederList.Add(feeder);
  943. }
  944. }
  945. if (PlcList==null)
  946. {
  947. PlcList = new ObservableCollection<PlcInfo>();
  948. var plcs = _configService.GetAllPlcs();
  949. foreach (var plc in plcs)
  950. {
  951. PlcList.Add(plc);
  952. }
  953. }
  954. if (BgTcp==null)
  955. {
  956. BgTcp = _configService.GetBgTcp();
  957. }
  958. if (BgModbus == null)
  959. {
  960. BgModbus = _configService.GetBgModbusTcp();
  961. }
  962. CurrentLanguage=_configService.GetSystemConfiguration().CurrentLanguage;
  963. if (isFirstLoad)
  964. {
  965. Title1 = (double)Application.Current.Resources["Font.Size.Title1"];
  966. Title2 = (double)Application.Current.Resources["Font.Size.Title2"];
  967. Title3 = (double)Application.Current.Resources["Font.Size.Title3"];
  968. Title4 = (double)Application.Current.Resources["Font.Size.Title4"];
  969. Body1 = (double)Application.Current.Resources["Font.Size.Body1"];
  970. Body2 = (double)Application.Current.Resources["Font.Size.Body2"];
  971. Body3 = (double)Application.Current.Resources["Font.Size.Body3"];
  972. Body4 = (double)Application.Current.Resources["Font.Size.Body4"];
  973. Captions1 = (double)Application.Current.Resources["Font.Size.Captions1"];
  974. Captions2 = (double)Application.Current.Resources["Font.Size.Captions2"];
  975. Captions3 = (double)Application.Current.Resources["Font.Size.Captions3"];
  976. Captions4 = (double)Application.Current.Resources["Font.Size.Captions4"];
  977. this.PropertyChanged += OnPropertyChanged;
  978. isFirstLoad = false;
  979. }
  980. }
  981. /// <summary>
  982. /// 是否允许导航到此视图模型。
  983. /// </summary>
  984. /// <param name="navigationContext"></param>
  985. /// <returns></returns>
  986. public bool IsNavigationTarget(NavigationContext navigationContext)
  987. {
  988. return true;
  989. }
  990. /// <summary>
  991. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  992. /// </summary>
  993. /// <param name="navigationContext"></param>
  994. public void OnNavigatedFrom(NavigationContext navigationContext)
  995. {
  996. }
  997. #endregion
  998. private bool _IsAllowEdit = false;
  999. public bool IsAllowEdit
  1000. {
  1001. get { return _IsAllowEdit; }
  1002. set { SetProperty(ref _IsAllowEdit, value); }
  1003. }
  1004. private void LoginChange(Models.User user)
  1005. {
  1006. if (user.userPart == Enums.UserPart.Operator)
  1007. {
  1008. IsAllowEdit = false;
  1009. }
  1010. else
  1011. {
  1012. IsAllowEdit = true;
  1013. }
  1014. }
  1015. }
  1016. }