SettingViewModel.cs 44 KB

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