SettingViewModel.cs 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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.Drawing.Drawing2D;
  16. using System.Globalization;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Windows;
  20. using Team.FFFeederService.Interfaces;
  21. using TeamAAS_VP.Controls;
  22. using TeamAAS_VP.Core;
  23. using TeamAAS_VP.Core.Cameras;
  24. using TeamAAS_VP.Core.Lights;
  25. using TeamAAS_VP.Enums;
  26. using TeamAAS_VP.Events;
  27. using TeamAAS_VP.Interfaces;
  28. using TeamAAS_VP.Models;
  29. using TeamAAS_VP.Models.Feeder;
  30. using TeamAAS_VP.Models.Lights;
  31. using TeamAAS_VP.Resources.Languages;
  32. using TeamAAS_VP.Services;
  33. using TeamAAS_VP.ViewModels.User;
  34. using TouchSocket.Core;
  35. using WPFLocalizeExtension.Engine;
  36. namespace TeamAAS_VP.ViewModels
  37. {
  38. public class SettingViewModel : BindableBase, IConfirmNavigationRequest
  39. {
  40. IRegionManager _regionManager;
  41. IEventAggregator _eventAggregator;
  42. IContainerProvider _container;
  43. IDialogService _dialogService;
  44. IConfigService _configService;
  45. IRobotService _robotService;
  46. ICameraService _cameraService;
  47. IFeederService _feeder_service;
  48. IFeederService _feederService;
  49. IPlcService _plcService;
  50. IProductService _productService;
  51. ISystemDatabaseService _systemDatabaseService;
  52. bool isFirstLoad = true;
  53. #region 属性
  54. private Management _management;
  55. public Management management
  56. {
  57. get { return _management; }
  58. set { SetProperty(ref _management, value); }
  59. }
  60. private ObservableCollection<RobotInfo> _RobotList;
  61. public ObservableCollection<RobotInfo> RobotList
  62. {
  63. get { return _RobotList; }
  64. set { SetProperty(ref _RobotList, value); }
  65. }
  66. private ObservableCollection<CameraInfo> _CameraList;
  67. public ObservableCollection<CameraInfo> CameraList
  68. {
  69. get { return _CameraList; }
  70. set { SetProperty(ref _CameraList, value); }
  71. }
  72. private ObservableCollection<FeederInfo> _FeederList;
  73. public ObservableCollection<FeederInfo> FeederList
  74. {
  75. get { return _FeederList; }
  76. set { SetProperty(ref _FeederList, value); }
  77. }
  78. private ObservableCollection<PlcInfo> _PlcList;
  79. public ObservableCollection<PlcInfo> PlcList
  80. {
  81. get { return _PlcList; }
  82. set { SetProperty(ref _PlcList, value); }
  83. }
  84. private Language _CurrentLanguage;
  85. public Language CurrentLanguage
  86. {
  87. get { return _CurrentLanguage; }
  88. set { SetProperty(ref _CurrentLanguage, value); }
  89. }
  90. private RobotInfo _SelectRobot;
  91. /// <summary>
  92. /// 选中的机器人
  93. /// </summary>
  94. public RobotInfo SelectRobot
  95. {
  96. get { return _SelectRobot; }
  97. set
  98. {
  99. SetProperty(ref _SelectRobot, value);
  100. if (value != null && (value.RobotBrand == RobotBrand.XYZ_Platform || value.RobotBrand == RobotBrand.XYZU_Platform))
  101. {
  102. RobotSelectedPlc = PlcList.FirstOrDefault(p => p.Id == value.PLC);
  103. var root = _robotService.GetRobot(SelectRobot.Id);
  104. if (root != null)
  105. {
  106. //ResultsValues.Clear();
  107. //var res = root.GetNodesValue();
  108. //for (int i = 0; i < res.Count; i++)
  109. //{
  110. // if (res[i].StatusCode.Code != 2150891520)
  111. // {
  112. // ResultsValues.Add(res[i].Value.ToString());
  113. // }
  114. // else
  115. // {
  116. // break;
  117. // }
  118. //}
  119. }
  120. }
  121. }
  122. }
  123. private FeederInfo _SelectFeeder;
  124. /// <summary>
  125. /// 选中的Feeder
  126. /// </summary>
  127. public FeederInfo SelectFeeder
  128. {
  129. get { return _SelectFeeder; }
  130. set { SetProperty(ref _SelectFeeder, value); }
  131. }
  132. private PlcInfo _SelectPlc;
  133. public PlcInfo SelectPlc
  134. {
  135. get { return _SelectPlc; }
  136. set { SetProperty(ref _SelectPlc, value); }
  137. }
  138. private ObservableCollection<string> _ResultsValues = new ObservableCollection<string>();
  139. public ObservableCollection<string> ResultsValues { get => _ResultsValues; set => _ResultsValues = value; }
  140. private CameraInfo _SelectCamera;
  141. /// <summary>
  142. /// 选中的Camera
  143. /// </summary>
  144. public CameraInfo SelectCamera
  145. {
  146. get { return _SelectCamera; }
  147. set { SetProperty(ref _SelectCamera, value); }
  148. }
  149. private ObservableCollection<ScrewFeederInfo> _ScrewFeederList;
  150. /// <summary>
  151. /// 螺丝供料器列表
  152. /// </summary>
  153. public ObservableCollection<ScrewFeederInfo> ScrewFeederList
  154. {
  155. get { return _ScrewFeederList; }
  156. set { SetProperty(ref _ScrewFeederList, value); }
  157. }
  158. private ScrewFeederInfo _SelectScrewFeeder;
  159. public ScrewFeederInfo SelectScrewFeeder
  160. {
  161. get { return _SelectScrewFeeder; }
  162. set { SetProperty(ref _SelectScrewFeeder, value); }
  163. }
  164. private double _Title1;
  165. public double Title1
  166. {
  167. get { return _Title1; }
  168. set { SetProperty(ref _Title1, value); }
  169. }
  170. private double _Title2;
  171. public double Title2
  172. {
  173. get { return _Title2; }
  174. set { SetProperty(ref _Title2, value); }
  175. }
  176. private double _Title3;
  177. public double Title3
  178. {
  179. get { return _Title3; }
  180. set { SetProperty(ref _Title3, value); }
  181. }
  182. private double _Title4;
  183. public double Title4
  184. {
  185. get { return _Title4; }
  186. set { SetProperty(ref _Title4, value); }
  187. }
  188. private double _Body1;
  189. public double Body1
  190. {
  191. get { return _Body1; }
  192. set { SetProperty(ref _Body1, value); }
  193. }
  194. private double _Body2;
  195. public double Body2
  196. {
  197. get { return _Body2; }
  198. set { SetProperty(ref _Body2, value); }
  199. }
  200. private double _Body3;
  201. public double Body3
  202. {
  203. get { return _Body3; }
  204. set { SetProperty(ref _Body3, value); }
  205. }
  206. private double _Body4;
  207. public double Body4
  208. {
  209. get { return _Body4; }
  210. set { SetProperty(ref _Body4, value); }
  211. }
  212. private double _Captions1;
  213. public double Captions1
  214. {
  215. get { return _Captions1; }
  216. set { SetProperty(ref _Captions1, value); }
  217. }
  218. private double _Captions2;
  219. public double Captions2
  220. {
  221. get { return _Captions2; }
  222. set { SetProperty(ref _Captions2, value); }
  223. }
  224. private double _Captions3;
  225. public double Captions3
  226. {
  227. get { return _Captions3; }
  228. set { SetProperty(ref _Captions3, value); }
  229. }
  230. private double _Captions4;
  231. public double Captions4
  232. {
  233. get { return _Captions4; }
  234. set { SetProperty(ref _Captions4, value); }
  235. }
  236. private PlcInfo _RobotSelectedPlc;
  237. public PlcInfo RobotSelectedPlc
  238. {
  239. get { return _RobotSelectedPlc; }
  240. set
  241. {
  242. SetProperty(ref _RobotSelectedPlc, value);
  243. if (SelectRobot != null)
  244. {
  245. if (value != null)
  246. {
  247. SelectRobot.PLC = value.Id;
  248. }
  249. else
  250. {
  251. SelectRobot.PLC = Guid.Empty;
  252. }
  253. }
  254. }
  255. }
  256. private BgModbusTcp _BgModbus;
  257. public BgModbusTcp BgModbus
  258. {
  259. get { return _BgModbus; }
  260. set { SetProperty(ref _BgModbus, value); }
  261. }
  262. private BgTcpIP _BgTcp;
  263. public BgTcpIP BgTcp
  264. {
  265. get { return _BgTcp; }
  266. set { SetProperty(ref _BgTcp, value); }
  267. }
  268. private ObservableCollection<LightControllerConfig> _LightControllerList;
  269. public ObservableCollection<LightControllerConfig> LightControllerList
  270. {
  271. get { return _LightControllerList; }
  272. set { SetProperty(ref _LightControllerList, value); }
  273. }
  274. private LightControllerConfig _SelectLightController;
  275. public LightControllerConfig SelectLightController
  276. {
  277. get { return _SelectLightController; }
  278. set { SetProperty(ref _SelectLightController, value); }
  279. }
  280. #endregion
  281. #region 命令
  282. public DelegateCommand LoadedCommand { get; set; }
  283. private DelegateCommand _SaveSystemParametersCommand;
  284. public DelegateCommand SaveSystemParametersCommand =>
  285. _SaveSystemParametersCommand ?? (_SaveSystemParametersCommand = new DelegateCommand(ExecuteSaveSystemParametersCommand));
  286. private DelegateCommand _AddRobotCommand;
  287. public DelegateCommand AddRobotCommand =>
  288. _AddRobotCommand ?? (_AddRobotCommand = new DelegateCommand(ExecuteAddRobotCommand));
  289. private DelegateCommand _RemoveCommand;
  290. public DelegateCommand RemoveCommand =>
  291. _RemoveCommand ?? (_RemoveCommand = new DelegateCommand(ExecuteRemoveCommand));
  292. private DelegateCommand _AddFeederCommand;
  293. public DelegateCommand AddFeederCommand =>
  294. _AddFeederCommand ?? (_AddFeederCommand = new DelegateCommand(ExecuteAddFeederCommand));
  295. private DelegateCommand _RemoveFeederCommand;
  296. public DelegateCommand RemoveFeederCommand =>
  297. _RemoveFeederCommand ?? (_RemoveFeederCommand = new DelegateCommand(ExecuteRemoveFeederCommand));
  298. private DelegateCommand<string> _SelectLanguageCommand;
  299. public DelegateCommand<string> SelectLanguageCommand =>
  300. _SelectLanguageCommand ?? (_SelectLanguageCommand = new DelegateCommand<string>(ExecuteSelectLanguageCommand));
  301. private DelegateCommand _SaveRobotsCommand;
  302. public DelegateCommand SaveRobotsCommand =>
  303. _SaveRobotsCommand ?? (_SaveRobotsCommand = new DelegateCommand(ExecuteSaveRobotsCommand));
  304. private DelegateCommand _SaveFeedersCommand;
  305. public DelegateCommand SaveFeedersCommand =>
  306. _SaveFeedersCommand ?? (_SaveFeedersCommand = new DelegateCommand(ExecuteSaveFeedersCommand));
  307. private DelegateCommand<string> _SaveCommunicateCommand;
  308. public DelegateCommand<string> SaveCommunicateCommand =>
  309. _SaveCommunicateCommand ?? (_SaveCommunicateCommand = new DelegateCommand<string>(ExecuteSaveCommunicateCommand));
  310. private DelegateCommand _AddCameraCommand;
  311. public DelegateCommand AddCameraCommand =>
  312. _AddCameraCommand ?? (_AddCameraCommand = new DelegateCommand(ExecuteAddCameraCommand));
  313. private DelegateCommand _EditCameraCommand;
  314. public DelegateCommand EditCameraCommand =>
  315. _EditCameraCommand ?? (_EditCameraCommand = new DelegateCommand(ExecuteEditCameraCommand));
  316. private DelegateCommand _RemoveCameraCommand;
  317. public DelegateCommand RemoveCameraCommand =>
  318. _RemoveCameraCommand ?? (_RemoveCameraCommand = new DelegateCommand(ExecuteRemoveCameraCommand));
  319. private DelegateCommand _SaveCamerasCommand;
  320. public DelegateCommand SaveCamerasCommand =>
  321. _SaveCamerasCommand ?? (_SaveCamerasCommand = new DelegateCommand(ExecuteSaveCamerasCommand));
  322. private DelegateCommand _AddPlcCommand;
  323. public DelegateCommand AddPlcCommand =>
  324. _AddPlcCommand ?? (_AddPlcCommand = new DelegateCommand(ExecuteAddPlcCommand));
  325. private DelegateCommand _RemovePlcCommand;
  326. public DelegateCommand RemovePlcCommand =>
  327. _RemovePlcCommand ?? (_RemovePlcCommand = new DelegateCommand(ExecuteRemovePlcCommand));
  328. private DelegateCommand _SavePlcsCommand;
  329. public DelegateCommand SavePlcsCommand =>
  330. _SavePlcsCommand ?? (_SavePlcsCommand = new DelegateCommand(ExecuteSavePlcsCommand));
  331. private DelegateCommand _AddLightControllerCommand;
  332. public DelegateCommand AddLightControllerCommand => _AddLightControllerCommand ?? (_AddLightControllerCommand = new DelegateCommand(ExecuteAddLightControllerCommand));
  333. private DelegateCommand _RemoveLightControllerCommand;
  334. public DelegateCommand RemoveLightControllerCommand => _RemoveLightControllerCommand ?? (_RemoveLightControllerCommand = new DelegateCommand(ExecuteRemoveLightControllerCommand));
  335. private DelegateCommand _SaveLightControllersCommand;
  336. public DelegateCommand SaveLightControllersCommand => _SaveLightControllersCommand ?? (_SaveLightControllersCommand = new DelegateCommand(ExecuteSaveLightControllersCommand));
  337. private DelegateCommand _AddScrewFeederCommand;
  338. public DelegateCommand AddScrewFeederCommand =>
  339. _AddScrewFeederCommand ?? (_AddScrewFeederCommand = new DelegateCommand(ExecuteAddScrewFeederCommand));
  340. private DelegateCommand _RemoveScrewFeederCommand;
  341. public DelegateCommand RemoveScrewFeederCommand =>
  342. _RemoveScrewFeederCommand ?? (_RemoveScrewFeederCommand = new DelegateCommand(ExecuteRemoveScrewFeederCommand));
  343. private DelegateCommand _SaveScrewFeedersCommand;
  344. public DelegateCommand SaveScrewFeedersCommand =>
  345. _SaveScrewFeedersCommand ?? (_SaveScrewFeedersCommand = new DelegateCommand(ExecuteSaveScrewFeedersCommand));
  346. #endregion
  347. #region 事件
  348. #endregion
  349. public SettingViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService,
  350. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, IPlcService plcService,
  351. IProductService productService, ISystemDatabaseService systemDatabaseService)
  352. {
  353. _regionManager = regionManager;
  354. _eventAggregator = ea;
  355. _container = container;
  356. _dialogService = dialogService;
  357. _configService = configService;
  358. _robotService = robotService;
  359. _cameraService = cameraService;
  360. _feederService = feederService;
  361. _plcService = plcService;
  362. _productService = productService;
  363. _systemDatabaseService = systemDatabaseService;
  364. management = _container.Resolve<Management>();
  365. LoadedCommand = new DelegateCommand(OnLoad);
  366. //订阅权限登录事件
  367. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  368. }
  369. #region 方法
  370. #region 机器人
  371. private void ExecuteAddRobotCommand()
  372. {
  373. _dialogService.ShowDialog("AddRobot", rst =>
  374. {
  375. //对话框关闭之后的回调函数,可以在这解析结果。
  376. ButtonResult result1 = rst.Result;
  377. if (result1 == ButtonResult.OK)
  378. {
  379. var param = rst.Parameters.GetValue<RobotInfo>("Robot");
  380. var robot = new RobotInfo()
  381. {
  382. Id = param.Id,
  383. RobotName = param.RobotName,
  384. ConnectType = param.ConnectType,
  385. RobotBrand = param.RobotBrand,
  386. IP = param.IP,
  387. Port = param.Port,
  388. Terminator = param.Terminator,
  389. };
  390. RobotList.Add(robot);
  391. _configService.AddOrUpdateRobot(robot);
  392. _robotService.CreateRobot(robot.Id, robot);
  393. //对所有产品也进行添加
  394. _productService.AddRobotPointToProduct(robot.Id, robot.RobotName);
  395. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.添加完成}", Duration = 1 });
  396. }
  397. });
  398. }
  399. void ExecuteRemoveCommand()
  400. {
  401. if (MessageBox.Show(Lang.是否移除机器人, $"AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  402. {
  403. // 找到要删除的机器人
  404. var robotToRemove = RobotList.FirstOrDefault(r => r.Id == SelectRobot.Id);
  405. if (robotToRemove != null)
  406. {
  407. RobotList.Remove(robotToRemove);
  408. _configService.RemoveRobot(robotToRemove.Id);
  409. _robotService.RemoveRobot(robotToRemove.Id);
  410. //对所有产品也进行移除
  411. _productService.RemoveRobotPointFromProduct(robotToRemove.Id);
  412. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.移除完成, Duration = 1 });
  413. }
  414. }
  415. }
  416. async void ExecuteSaveRobotsCommand()
  417. {
  418. var view = new ShowMessage($"AAS", Lang.是否保存机器人);
  419. //show the dialog
  420. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  421. if (result != null && result is bool)
  422. {
  423. if (((bool)result))
  424. {
  425. foreach (var item in RobotList)
  426. {
  427. _configService.AddOrUpdateRobot(item);
  428. }
  429. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  430. }
  431. }
  432. }
  433. #endregion
  434. #region 相机
  435. /// <summary>
  436. /// 保存所有相机
  437. /// </summary>
  438. async void ExecuteSaveCamerasCommand()
  439. {
  440. var view = new ShowMessage($"AAS", Lang.是否保存相机配置);
  441. //show the dialog
  442. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  443. if (result != null && result is bool)
  444. {
  445. if (((bool)result))
  446. {
  447. //management.DeviceConfig.Cameras = DeviceConfig.Cameras;
  448. //management.SaveCamerasDeviceConfig();
  449. //management.DeviceConfig.Cameras = DeviceConfig.Cameras;
  450. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  451. }
  452. }
  453. }
  454. /// <summary>
  455. /// 移除相机
  456. /// </summary>
  457. void ExecuteRemoveCameraCommand()
  458. {
  459. if (MessageBox.Show(Lang.是否移除相机, $"AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  460. {
  461. // 找到要删除的相机
  462. var cameraToRemove = CameraList.FirstOrDefault(r => r.Id == SelectCamera.Id);
  463. if (cameraToRemove != null)
  464. {
  465. _configService.RemoveCamera(cameraToRemove.Id);
  466. _cameraService.RemoveCamera(cameraToRemove.Id);
  467. var root = _configService.GetAllCameras();
  468. CameraList = new ObservableCollection<CameraInfo>(root);
  469. foreach (var item in CameraList)
  470. {
  471. _cameraService.UpdateCameraNumber(item.Id, item.CameraNo);
  472. }
  473. //对所有产品也进行移除
  474. _productService.RemoveCameraFromProduct(cameraToRemove.Id);
  475. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  476. }
  477. }
  478. }
  479. /// <summary>
  480. /// 编辑相机
  481. /// </summary>
  482. void ExecuteEditCameraCommand()
  483. {
  484. if (SelectCamera == null) return;
  485. bool isSelectedCameraBrandSucced = false;
  486. CameraBrand cameraBrand = CameraBrand.HikRobot_MVS;
  487. IDialogParameters parameters1 = new DialogParameters();
  488. parameters1.Add("CameraBrand", SelectCamera.CameraBrand);
  489. _dialogService.ShowDialog("SelectCameraBrand", parameters1, rst =>
  490. {
  491. if (rst.Result == ButtonResult.OK)
  492. {
  493. isSelectedCameraBrandSucced = true;
  494. cameraBrand = rst.Parameters.GetValue<CameraBrand>("CameraBrand");
  495. }
  496. else
  497. {
  498. return;
  499. }
  500. });
  501. if (!isSelectedCameraBrandSucced) { return; }
  502. (bool isInstalled, string version) result = (false, "");
  503. string softwarename = "";
  504. switch (cameraBrand)
  505. {
  506. case CameraBrand.HikRobot_MVS:
  507. softwarename = "MVS";
  508. result = MvCamera.CheckSoftwareInstalled();
  509. break;
  510. case CameraBrand.Basler_Pylon:
  511. softwarename = "Pylon 7";
  512. result = BaslerCamera.CheckSoftwareInstalled();
  513. break;
  514. case CameraBrand.IRayple:
  515. softwarename = "MV Viewer";
  516. result = AHuaCamera.CheckSoftwareInstalled();
  517. break;
  518. case CameraBrand.Cognex:
  519. softwarename = "Cognex VisionPro";
  520. result = CognexCamera.CheckSoftwareInstalled();
  521. break;
  522. case CameraBrand.Galaxy:
  523. softwarename = "Galaxy Viewer";
  524. result = GalaxyCamera.CheckSoftwareInstalled();
  525. break;
  526. case CameraBrand.Opt:
  527. softwarename = "OPT Camera Viewer";
  528. result = OptCamera.CheckSoftwareInstalled();
  529. break;
  530. default:
  531. break;
  532. }
  533. if (!result.isInstalled)
  534. {
  535. if (string.IsNullOrEmpty(result.version))
  536. {
  537. //未安装
  538. MessageBox.Show(Lang.CameraSDKCheck_Message1.Replace("{name}", softwarename), "AAS", MessageBoxButton.OK, MessageBoxImage.Asterisk);
  539. }
  540. else
  541. {
  542. //版本较低
  543. MessageBox.Show(Lang.CameraSDKCheck_Message2.Replace("{name}", softwarename).Replace("{version}", result.version).Replace("\\n", Environment.NewLine), "AAS", MessageBoxButton.OK, MessageBoxImage.Asterisk);
  544. }
  545. return;
  546. }
  547. //编辑前的相机名称
  548. string oldCameraName = SelectCamera.CameraName;
  549. IDialogParameters parameters = new DialogParameters();
  550. parameters.Add("CameraBrand", cameraBrand);
  551. parameters.Add("Camera", SelectCamera.Copy());
  552. _dialogService.ShowDialog("EditCamera", parameters, rst =>
  553. {
  554. //对话框关闭之后的回调函数,可以在这解析结果。
  555. ButtonResult result1 = rst.Result;
  556. if (result1 == ButtonResult.OK)
  557. {
  558. var param = rst.Parameters.GetValue<CameraInfo>("Camera");
  559. SelectCamera.CameraName = param.CameraName;
  560. SelectCamera.SerialNumber = param.SerialNumber;
  561. SelectCamera.Model = param.Model;
  562. SelectCamera.ManufacturerName = param.ManufacturerName;
  563. SelectCamera.CameraType = param.CameraType;
  564. SelectCamera.CameraBrand = param.CameraBrand;
  565. SelectCamera.CameraIp = param.CameraIp;
  566. _configService.AddOrUpdateCamera(SelectCamera);
  567. if (oldCameraName != param.CameraName)
  568. {
  569. //对所有产品也进行修改
  570. _productService.UpdateCameraNameInProduct(SelectCamera.Id, SelectCamera.CameraName);
  571. }
  572. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  573. }
  574. });
  575. }
  576. /// <summary>
  577. /// 增加相机
  578. /// </summary>
  579. void ExecuteAddCameraCommand()
  580. {
  581. bool isSelectedCameraBrandSucced = false;
  582. CameraBrand cameraBrand = CameraBrand.HikRobot_MVS;
  583. _dialogService.ShowDialog("SelectCameraBrand", rst =>
  584. {
  585. if (rst.Result == ButtonResult.OK)
  586. {
  587. isSelectedCameraBrandSucced = true;
  588. cameraBrand = rst.Parameters.GetValue<CameraBrand>("CameraBrand");
  589. }
  590. else
  591. {
  592. return;
  593. }
  594. });
  595. if (!isSelectedCameraBrandSucced) { return; }
  596. ;
  597. (bool isInstalled, string version) result = (false, "");
  598. string softwarename = "";
  599. switch (cameraBrand)
  600. {
  601. case CameraBrand.HikRobot_MVS:
  602. softwarename = "MVS";
  603. result = MvCamera.CheckSoftwareInstalled();
  604. break;
  605. case CameraBrand.Basler_Pylon:
  606. softwarename = "Pylon 7";
  607. result = BaslerCamera.CheckSoftwareInstalled();
  608. break;
  609. case CameraBrand.IRayple:
  610. softwarename = "MV Viewer";
  611. result = AHuaCamera.CheckSoftwareInstalled();
  612. break;
  613. case CameraBrand.Cognex:
  614. softwarename = "Cognex VisionPro";
  615. result = CognexCamera.CheckSoftwareInstalled();
  616. break;
  617. case CameraBrand.Galaxy:
  618. softwarename = "Galaxy Viewer";
  619. result = GalaxyCamera.CheckSoftwareInstalled();
  620. break;
  621. case CameraBrand.Opt:
  622. softwarename = "OPT Camera Viewer";
  623. result = OptCamera.CheckSoftwareInstalled();
  624. break;
  625. default:
  626. break;
  627. }
  628. if (!result.isInstalled)
  629. {
  630. if (string.IsNullOrEmpty(result.version))
  631. {
  632. //未安装
  633. MessageBox.Show(Lang.CameraSDKCheck_Message1.Replace("{name}", softwarename), "AAS", MessageBoxButton.OK, MessageBoxImage.Asterisk);
  634. }
  635. else
  636. {
  637. //版本较低
  638. MessageBox.Show(Lang.CameraSDKCheck_Message2.Replace("{name}", softwarename).Replace("{version}", result.version).Replace("\\n", Environment.NewLine), "AAS", MessageBoxButton.OK, MessageBoxImage.Asterisk);
  639. }
  640. return;
  641. }
  642. IDialogParameters parameters = new DialogParameters();
  643. parameters.Add("CameraBrand", cameraBrand);
  644. _dialogService.ShowDialog("AddCamera", parameters, rst =>
  645. {
  646. //对话框关闭之后的回调函数,可以在这解析结果。
  647. ButtonResult result1 = rst.Result;
  648. if (result1 == ButtonResult.OK)
  649. {
  650. var param = rst.Parameters.GetValue<CameraInfo>("Camera");
  651. var camera = new CameraInfo()
  652. {
  653. Id = param.Id,
  654. CameraName = param.CameraName,
  655. SerialNumber = param.SerialNumber,
  656. Model = param.Model,
  657. ManufacturerName = param.ManufacturerName,
  658. CameraType = param.CameraType,
  659. CameraBrand = param.CameraBrand,
  660. CameraIp = param.CameraIp
  661. };
  662. CameraList.Add(camera);
  663. _configService.AddOrUpdateCamera(camera);
  664. _cameraService.CreateCamera(camera.Id, camera);
  665. //对所有产品也进行添加
  666. _productService.AddCameraToProduct(camera.Id, camera.CameraName);
  667. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  668. }
  669. });
  670. }
  671. #endregion
  672. #region Feeder
  673. void ExecuteAddFeederCommand()
  674. {
  675. _dialogService.ShowDialog("AddFeeder", rst =>
  676. {
  677. //对话框关闭之后的回调函数,可以在这解析结果。
  678. ButtonResult result1 = rst.Result;
  679. if (result1 == ButtonResult.OK)
  680. {
  681. var param = rst.Parameters.GetValue<FeederInfo>("Feeder");
  682. var feeder = new FeederInfo()
  683. {
  684. Id = param.Id,
  685. FeederName = param.FeederName,
  686. ConnectType = param.ConnectType,
  687. FeederBrand = param.FeederBrand,
  688. IP = param.IP,
  689. Port = param.Port
  690. };
  691. FeederList.Add(feeder);
  692. _configService.AddOrUpdateFeeder(feeder);
  693. _feederService.CreateFeeder(feeder.Id, feeder.FeederNo, feeder.FeederName, feeder.IP, feeder.Port, feeder.FeederBrand);
  694. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.添加完成, Duration = 1 });
  695. }
  696. });
  697. }
  698. void ExecuteRemoveFeederCommand()
  699. {
  700. if (MessageBox.Show(Lang.是否移除Feeder, " AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  701. {
  702. // 找到要删除的机器人
  703. var feederToRemove = FeederList.FirstOrDefault(r => r.Id == SelectFeeder.Id);
  704. if (feederToRemove != null)
  705. {
  706. _configService.RemoveFeeder(feederToRemove.Id);
  707. _feederService.RemoveFeeder(feederToRemove.Id);
  708. var root = _configService.GetAllFeeders();
  709. FeederList = new ObservableCollection<FeederInfo>(root);
  710. foreach (var item in FeederList)
  711. {
  712. _feederService.UpdateFeederNo(item.Id, item.FeederNo);
  713. }
  714. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.移除完成, Duration = 1 });
  715. }
  716. }
  717. }
  718. async void ExecuteSaveFeedersCommand()
  719. {
  720. var view = new ShowMessage($"AAS", Lang.是否保存Feeder);
  721. //show the dialog
  722. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  723. if (result != null && result is bool)
  724. {
  725. if (((bool)result))
  726. {
  727. foreach (var item in FeederList)
  728. {
  729. _configService.AddOrUpdateFeeder(item);
  730. }
  731. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  732. }
  733. }
  734. }
  735. #endregion
  736. #region PLC
  737. /// <summary>
  738. /// 保存PLC
  739. /// </summary>
  740. async void ExecuteSavePlcsCommand()
  741. {
  742. var view = new ShowMessage($"AAS", Lang.是否保存PLC);
  743. //show the dialog
  744. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  745. if (result != null && result is bool)
  746. {
  747. if (((bool)result))
  748. {
  749. foreach (var item in PlcList)
  750. {
  751. _configService.AddOrUpdatePlc(item);
  752. }
  753. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  754. }
  755. }
  756. }
  757. /// <summary>
  758. /// 移除PLC
  759. /// </summary>
  760. void ExecuteRemovePlcCommand()
  761. {
  762. if (MessageBox.Show(Lang.是否移除PLC, $"AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  763. {
  764. // 找到要删除的plc
  765. var plcToRemove = PlcList.FirstOrDefault(r => r.Id == SelectPlc.Id);
  766. if (plcToRemove != null)
  767. {
  768. _configService.RemovePlc(plcToRemove.Id);
  769. _plcService.RemovePlc(plcToRemove.Id);
  770. var root = _configService.GetAllPlcs();
  771. PlcList = new ObservableCollection<PlcInfo>(root);
  772. foreach (var item in PlcList)
  773. {
  774. _plcService.UpdatePlcNumber(item.Id, item.Index);
  775. }
  776. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.移除完成, Duration = 1 });
  777. }
  778. }
  779. }
  780. /// <summary>
  781. /// 增加PLC
  782. /// </summary>
  783. void ExecuteAddPlcCommand()
  784. {
  785. _dialogService.ShowDialog("AddPlc", rst =>
  786. {
  787. //对话框关闭之后的回调函数,可以在这解析结果。
  788. ButtonResult result1 = rst.Result;
  789. if (result1 == ButtonResult.OK)
  790. {
  791. var param = rst.Parameters.GetValue<PlcInfo>("PLC");
  792. var plc = new PlcInfo()
  793. {
  794. Id = param.Id,
  795. Name = param.Name,
  796. CommunicationType = param.CommunicationType,
  797. EndpointUrl = param.EndpointUrl,
  798. };
  799. PlcList.Add(plc);
  800. _configService.AddOrUpdatePlc(plc);
  801. _plcService.CreatePlc(plc.Id, plc);
  802. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.添加完成, Duration = 1 });
  803. }
  804. });
  805. }
  806. #endregion
  807. #region Light handlers
  808. void ExecuteAddLightControllerCommand()
  809. {
  810. // open dialog to add light controller (similar to robots)
  811. _dialogService.ShowDialog("AddLightController", rst =>
  812. {
  813. if (rst.Result == ButtonResult.OK)
  814. {
  815. var param = rst.Parameters.GetValue<LightControllerConfig>("LightController");
  816. // assign id and defaults if needed
  817. if (param.Id == 0)
  818. param.Id = (LightControllerList?.Count ?? 0) + 1;
  819. LightControllerList.Add(param);
  820. _configService.AddOrUpdateLightController(param);
  821. SelectLightController = param;
  822. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "添加完成", Duration = 1 });
  823. }
  824. });
  825. }
  826. void ExecuteRemoveLightControllerCommand()
  827. {
  828. if (SelectLightController == null) return;
  829. if (MessageBox.Show("是否移除光源控制器?", "AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  830. {
  831. _configService.RemoveLightController(SelectLightController.Id);
  832. LightControllerList.Remove(SelectLightController);
  833. SelectLightController = null;
  834. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "移除完成", Duration = 1 });
  835. }
  836. }
  837. async void ExecuteSaveLightControllersCommand()
  838. {
  839. var view = new ShowMessage($"AAS", "是否保存光源控制器配置?");
  840. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  841. if (result != null && result is bool && (bool)result)
  842. {
  843. foreach (var item in LightControllerList)
  844. {
  845. _configService.AddOrUpdateLightController(item);
  846. }
  847. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "完成", Duration = 1 });
  848. }
  849. }
  850. #endregion
  851. #region 螺丝供料器
  852. /// <summary>
  853. /// 保存所有螺丝供料器
  854. /// </summary>
  855. async void ExecuteSaveScrewFeedersCommand()
  856. {
  857. var view = new ShowMessage($"AAS", "是否保存螺丝供料器?");
  858. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  859. if (result != null && result is bool && (bool)result)
  860. {
  861. if (ScrewFeederList != null)
  862. {
  863. foreach (var item in ScrewFeederList)
  864. {
  865. try
  866. {
  867. _configService.AddOrUpdateScrewFeeder(item);
  868. }
  869. catch (Exception ex)
  870. {
  871. // 记录或通知错误,但继续处理其它项
  872. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  873. }
  874. }
  875. }
  876. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  877. }
  878. }
  879. /// <summary>
  880. /// 移除螺丝供料器
  881. /// </summary>
  882. void ExecuteRemoveScrewFeederCommand()
  883. {
  884. if (SelectScrewFeeder == null) return;
  885. if (MessageBox.Show("是否移除螺丝供料器?", "AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  886. {
  887. try
  888. {
  889. var toRemove = ScrewFeederList?.FirstOrDefault(s => s.Id == SelectScrewFeeder.Id);
  890. if (toRemove != null)
  891. {
  892. _configService.RemoveScrewFeeder(toRemove.Id);
  893. // 重新加载列表以保持序号/配置一致(与其他类似实现一致)
  894. var root = _configService.GetAllScrewFeeders();
  895. ScrewFeederList = new ObservableCollection<ScrewFeederInfo>(root);
  896. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "移除完成", Duration = 1 });
  897. }
  898. }
  899. catch (Exception ex)
  900. {
  901. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  902. }
  903. }
  904. }
  905. /// <summary>
  906. /// 添加螺丝供料器
  907. /// </summary>
  908. void ExecuteAddScrewFeederCommand()
  909. {
  910. _dialogService.ShowDialog("AddScrewFeeder", rst =>
  911. {
  912. ButtonResult result1 = rst.Result;
  913. if (result1 == ButtonResult.OK)
  914. {
  915. var param = rst.Parameters.GetValue<ScrewFeederInfo>("ScrewFeeder");
  916. if (param != null)
  917. {
  918. if (ScrewFeederList == null)
  919. {
  920. ScrewFeederList = new ObservableCollection<ScrewFeederInfo>();
  921. }
  922. try
  923. {
  924. param=_configService.AddOrUpdateScrewFeeder(param);
  925. // 直接添加对话框返回的对象(也可以根据需要映射字段)
  926. ScrewFeederList.Add(param);
  927. }
  928. catch (Exception ex)
  929. {
  930. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  931. }
  932. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "添加完成", Duration = 1 });
  933. }
  934. }
  935. });
  936. }
  937. #endregion
  938. #region 通讯
  939. async void ExecuteSaveCommunicateCommand(string parm)
  940. {
  941. if (parm == "TCPIP")
  942. {
  943. var view = new ShowMessage($"AAS", Lang.是否保存后台TCP通讯配置);
  944. //show the dialog
  945. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  946. if (result != null && result is bool)
  947. {
  948. if (((bool)result))
  949. {
  950. _configService.SetBgTcp(BgTcp);
  951. _configService.SaveBgTcp();
  952. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  953. }
  954. }
  955. }
  956. else if (parm == "ModbusTCP")
  957. {
  958. var view = new ShowMessage($"AAS", Lang.是否保存后台ModbusTCP通讯配置);
  959. //show the dialog
  960. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  961. if (result != null && result is bool)
  962. {
  963. if (((bool)result))
  964. {
  965. _configService.SetBgModbusTcp(BgModbus);
  966. _configService.SaveBgModbusTcp();
  967. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  968. }
  969. }
  970. }
  971. else
  972. {
  973. return;
  974. }
  975. }
  976. #endregion
  977. #region 语言
  978. void ExecuteSelectLanguageCommand(string language)
  979. {
  980. if (string.Equals(language, "ChineseSimplified"))
  981. {
  982. CurrentLanguage = Enums.Language.ChineseSimplified;
  983. var sysConfig = _configService.GetSystemConfiguration();
  984. sysConfig.CurrentLanguage = CurrentLanguage;
  985. _configService.SaveSystemConfiguration(sysConfig);
  986. var culture = new CultureInfo("zh-CN");
  987. App.Current.Dispatcher.Thread.CurrentCulture = culture;
  988. App.Current.Dispatcher.Thread.CurrentUICulture = culture;
  989. LocalizeDictionary.Instance.Culture = culture;
  990. Lang.Culture = culture;
  991. Application.Current.Resources["DefaultFont"] = new System.Windows.Media.FontFamily("Microsoft YaHei");
  992. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.5 });
  993. }
  994. else if (string.Equals(language, "ChineseTraditional"))
  995. {
  996. CurrentLanguage = Enums.Language.ChineseTraditional;
  997. var sysConfig = _configService.GetSystemConfiguration();
  998. sysConfig.CurrentLanguage = CurrentLanguage;
  999. _configService.SaveSystemConfiguration(sysConfig);
  1000. var culture = new CultureInfo("zh-TW");
  1001. App.Current.Dispatcher.Thread.CurrentCulture = culture;
  1002. App.Current.Dispatcher.Thread.CurrentUICulture = culture;
  1003. LocalizeDictionary.Instance.Culture = culture;
  1004. Lang.Culture = culture;
  1005. Application.Current.Resources["DefaultFont"] = new System.Windows.Media.FontFamily("Microsoft YaHei");
  1006. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.5 });
  1007. }
  1008. else if (string.Equals(language, "English"))
  1009. {
  1010. CurrentLanguage = Enums.Language.English;
  1011. var sysConfig = _configService.GetSystemConfiguration();
  1012. sysConfig.CurrentLanguage = CurrentLanguage;
  1013. _configService.SaveSystemConfiguration(sysConfig);
  1014. var culture = new CultureInfo("en");
  1015. App.Current.Dispatcher.Thread.CurrentCulture = culture;
  1016. App.Current.Dispatcher.Thread.CurrentUICulture = culture;
  1017. LocalizeDictionary.Instance.Culture = culture;
  1018. Lang.Culture = culture;
  1019. Application.Current.Resources["DefaultFont"] = new System.Windows.Media.FontFamily("Segoe UI");
  1020. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.5 });
  1021. }
  1022. else if (string.Equals(language, "Japanese"))
  1023. {
  1024. CurrentLanguage = Enums.Language.Japanese;
  1025. var sysConfig = _configService.GetSystemConfiguration();
  1026. sysConfig.CurrentLanguage = CurrentLanguage;
  1027. _configService.SaveSystemConfiguration(sysConfig);
  1028. var culture = new CultureInfo("ja-JP");
  1029. App.Current.Dispatcher.Thread.CurrentCulture = culture;
  1030. App.Current.Dispatcher.Thread.CurrentUICulture = culture;
  1031. LocalizeDictionary.Instance.Culture = culture;
  1032. Lang.Culture = culture;
  1033. Application.Current.Resources["DefaultFont"] = new System.Windows.Media.FontFamily("Segoe UI");
  1034. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.5 });
  1035. }
  1036. }
  1037. #endregion
  1038. private void OnLoad()
  1039. {
  1040. }
  1041. private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
  1042. {
  1043. switch (e.PropertyName)
  1044. {
  1045. case "Title1":
  1046. Application.Current.Resources["Font.Size.Title1"] = Title1;
  1047. break;
  1048. case "Title2":
  1049. Application.Current.Resources["Font.Size.Title2"] = Title2;
  1050. break;
  1051. case "Title3":
  1052. Application.Current.Resources["Font.Size.Title3"] = Title3;
  1053. break;
  1054. case "Title4":
  1055. Application.Current.Resources["Font.Size.Title4"] = Title4;
  1056. break;
  1057. case "Body1":
  1058. Application.Current.Resources["Font.Size.Body1"] = Body1;
  1059. break;
  1060. case "Body2":
  1061. Application.Current.Resources["Font.Size.Body2"] = Body2;
  1062. break;
  1063. case "Body3":
  1064. Application.Current.Resources["Font.Size.Body3"] = Body3;
  1065. break;
  1066. case "Body4":
  1067. Application.Current.Resources["Font.Size.Body4"] = Body4;
  1068. break;
  1069. case "Captions1":
  1070. Application.Current.Resources["Font.Size.Captions1"] = Captions1;
  1071. break;
  1072. case "Captions2":
  1073. Application.Current.Resources["Font.Size.Captions2"] = Captions2;
  1074. break;
  1075. case "Captions3":
  1076. Application.Current.Resources["Font.Size.Captions3"] = Captions3;
  1077. break;
  1078. case "Captions4":
  1079. Application.Current.Resources["Font.Size.Captions4"] = Captions4;
  1080. break;
  1081. default:
  1082. break;
  1083. }
  1084. }
  1085. #endregion
  1086. #region 继承
  1087. /// <summary>
  1088. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  1089. /// </summary>
  1090. /// <param name="navigationContext"></param>
  1091. /// <param name="continuationCallback"></param>
  1092. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  1093. {
  1094. continuationCallback(true);
  1095. }
  1096. /// <summary>
  1097. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  1098. /// </summary>
  1099. /// <param name="navigationContext"></param>
  1100. /// <exception cref="NotImplementedException"></exception>
  1101. public async void OnNavigatedTo(NavigationContext navigationContext)
  1102. {
  1103. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  1104. {
  1105. IsAllowEdit = false;
  1106. }
  1107. else
  1108. {
  1109. IsAllowEdit = true;
  1110. }
  1111. if (RobotList == null)
  1112. {
  1113. RobotList = new ObservableCollection<RobotInfo>();
  1114. var robots = _configService.GetAllRobots();
  1115. foreach (var robot in robots)
  1116. {
  1117. RobotList.Add(robot);
  1118. }
  1119. }
  1120. if (CameraList == null)
  1121. {
  1122. CameraList = new ObservableCollection<CameraInfo>();
  1123. var cameras = _configService.GetAllCameras();
  1124. foreach (var camera in cameras)
  1125. {
  1126. CameraList.Add(camera);
  1127. }
  1128. }
  1129. if (FeederList == null)
  1130. {
  1131. FeederList = new ObservableCollection<FeederInfo>();
  1132. var feeders = _configService.GetAllFeeders();
  1133. foreach (var feeder in feeders)
  1134. {
  1135. FeederList.Add(feeder);
  1136. }
  1137. }
  1138. if (PlcList == null)
  1139. {
  1140. PlcList = new ObservableCollection<PlcInfo>();
  1141. var plcs = _configService.GetAllPlcs();
  1142. foreach (var plc in plcs)
  1143. {
  1144. PlcList.Add(plc);
  1145. }
  1146. }
  1147. // load light controllers
  1148. if (LightControllerList == null)
  1149. {
  1150. LightControllerList = new ObservableCollection<LightControllerConfig>();
  1151. var controllers = _configService.GetAllLightControllers();
  1152. foreach (var ctrl in controllers)
  1153. {
  1154. LightControllerList.Add(ctrl);
  1155. }
  1156. }
  1157. if (BgTcp == null)
  1158. {
  1159. BgTcp = _configService.GetBgTcp();
  1160. }
  1161. if (BgModbus == null)
  1162. {
  1163. BgModbus = _configService.GetBgModbusTcp();
  1164. }
  1165. CurrentLanguage = _configService.GetSystemConfiguration().CurrentLanguage;
  1166. // load system parameters into UI
  1167. var sys = _configService.GetSystemConfiguration();
  1168. if (sys != null)
  1169. {
  1170. WorkMode = sys.WorkMode;
  1171. PickPointNum = sys.PickPointNum;
  1172. DowmCameraNum = sys.DowmCameraNum;
  1173. WorkMode = sys.WorkMode;
  1174. PickPointNum = sys.PickPointNum;
  1175. DowmCameraNum = sys.DowmCameraNum;
  1176. UpCameraPickNum = sys.UpCameraPickNum;
  1177. UpCameraPutNum = sys.UpCameraPutNum;
  1178. UPCameracheckNum = sys.UPCameracheckNum;
  1179. FixedCameraPickNum = sys.FixedCameraPickNum;
  1180. FixedCameraPutNum = sys.FixedCameraPutNum;
  1181. FixedCameracheckNum = sys.FixedCameracheckNum;
  1182. }
  1183. if (isFirstLoad)
  1184. {
  1185. Title1 = (double)Application.Current.Resources["Font.Size.Title1"];
  1186. Title2 = (double)Application.Current.Resources["Font.Size.Title2"];
  1187. Title3 = (double)Application.Current.Resources["Font.Size.Title3"];
  1188. Title4 = (double)Application.Current.Resources["Font.Size.Title4"];
  1189. Body1 = (double)Application.Current.Resources["Font.Size.Body1"];
  1190. Body2 = (double)Application.Current.Resources["Font.Size.Body2"];
  1191. Body3 = (double)Application.Current.Resources["Font.Size.Body3"];
  1192. Body4 = (double)Application.Current.Resources["Font.Size.Body4"];
  1193. Captions1 = (double)Application.Current.Resources["Font.Size.Captions1"];
  1194. Captions2 = (double)Application.Current.Resources["Font.Size.Captions2"];
  1195. Captions3 = (double)Application.Current.Resources["Font.Size.Captions3"];
  1196. Captions4 = (double)Application.Current.Resources["Font.Size.Captions4"];
  1197. this.PropertyChanged += OnPropertyChanged;
  1198. isFirstLoad = false;
  1199. }
  1200. }
  1201. /// <summary>
  1202. /// 是否允许导航到此视图模型。
  1203. /// </summary>
  1204. /// <param name="navigationContext"></param>
  1205. /// <returns></returns>
  1206. public bool IsNavigationTarget(NavigationContext navigationContext)
  1207. {
  1208. return true;
  1209. }
  1210. /// <summary>
  1211. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  1212. /// </summary>
  1213. /// <param name="navigationContext"></param>
  1214. public void OnNavigatedFrom(NavigationContext navigationContext)
  1215. {
  1216. }
  1217. #endregion
  1218. private bool _IsAllowEdit = false;
  1219. public bool IsAllowEdit
  1220. {
  1221. get { return _IsAllowEdit; }
  1222. set { SetProperty(ref _IsAllowEdit, value); }
  1223. }
  1224. private void LoginChange(Models.User user)
  1225. {
  1226. if (user.userPart == Enums.UserPart.Operator)
  1227. {
  1228. IsAllowEdit = false;
  1229. }
  1230. else
  1231. {
  1232. IsAllowEdit = true;
  1233. }
  1234. }
  1235. #region 系统参数
  1236. private Int16 _WorkMode = 0;
  1237. /// <summary>
  1238. /// 工作模式 =1 拍1打1 =2 拍1打所有
  1239. /// </summary>
  1240. public Int16 WorkMode
  1241. {
  1242. get { return _WorkMode; }
  1243. set { SetProperty(ref _WorkMode, value); }
  1244. }
  1245. private Int16 _PickPointNum = 0;
  1246. /// <summary>
  1247. /// 取料位置 =0 不需要取料 =1 需要取料
  1248. /// </summary>
  1249. public Int16 PickPointNum
  1250. {
  1251. get { return _PickPointNum; }
  1252. set { SetProperty(ref _PickPointNum, value); }
  1253. }
  1254. private Int16 _DowmCameraNum = 0;
  1255. /// <summary>
  1256. /// 下相机拍照次数
  1257. /// </summary>
  1258. public Int16 DowmCameraNum
  1259. {
  1260. get => _DowmCameraNum;
  1261. set => SetProperty(ref _DowmCameraNum, value);
  1262. }
  1263. private Int16 _UpCameraPickNum = 0;
  1264. /// <summary>
  1265. /// 上相机取料拍照次数
  1266. /// </summary>
  1267. public Int16 UpCameraPickNum
  1268. {
  1269. get => _UpCameraPickNum;
  1270. set => SetProperty(ref _UpCameraPickNum, value);
  1271. }
  1272. private Int16 _UpCameraPutNum = 0;
  1273. /// <summary>
  1274. /// 上相机放料拍照次数
  1275. /// </summary>
  1276. public Int16 UpCameraPutNum
  1277. {
  1278. get => _UpCameraPutNum;
  1279. set => SetProperty(ref _UpCameraPutNum, value);
  1280. }
  1281. private Int16 _UPCameracheckNum = 0;
  1282. /// <summary>
  1283. /// 上相机锁附/组装次数
  1284. /// </summary>
  1285. public Int16 UPCameracheckNum
  1286. {
  1287. get => _UPCameracheckNum;
  1288. set => SetProperty(ref _UPCameracheckNum, value);
  1289. }
  1290. private Int16 _FixedCameraPickNum = 0;
  1291. /// <summary>
  1292. /// 取料固定位置拍产品相机数量
  1293. /// </summary>
  1294. public Int16 FixedCameraPickNum
  1295. {
  1296. get => _FixedCameraPickNum;
  1297. set => SetProperty(ref _FixedCameraPickNum, value);
  1298. }
  1299. private Int16 _FixedCameraPutNum = 0;
  1300. /// <summary>
  1301. /// 锁附/组装固定位置拍产品相机数量
  1302. /// </summary>
  1303. public Int16 FixedCameraPutNum
  1304. {
  1305. get => _FixedCameraPutNum;
  1306. set => SetProperty(ref _FixedCameraPutNum, value);
  1307. }
  1308. private Int16 _FixedCameracheckNum = 0;
  1309. /// <summary>
  1310. /// 固定相机检测锁附/组装次数
  1311. /// </summary>
  1312. public Int16 FixedCameracheckNum
  1313. {
  1314. get => _FixedCameracheckNum;
  1315. set => SetProperty(ref _FixedCameracheckNum, value);
  1316. }
  1317. async void ExecuteSaveSystemParametersCommand()
  1318. {
  1319. var view = new ShowMessage($"AAS", "是否保存系统参数?");
  1320. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  1321. if (result != null && result is bool && (bool)result)
  1322. {
  1323. try
  1324. {
  1325. var sysConfig = _configService.GetSystemConfiguration();
  1326. sysConfig.WorkMode = WorkMode;
  1327. sysConfig.PickPointNum = PickPointNum;
  1328. sysConfig.DowmCameraNum = DowmCameraNum;
  1329. sysConfig.UpCameraPickNum = UpCameraPickNum;
  1330. sysConfig.UpCameraPutNum = UpCameraPutNum;
  1331. sysConfig.UPCameracheckNum = UPCameracheckNum;
  1332. sysConfig.FixedCameraPickNum = FixedCameraPickNum;
  1333. sysConfig.FixedCameraPutNum = FixedCameraPutNum;
  1334. sysConfig.FixedCameracheckNum = FixedCameracheckNum;
  1335. _configService.SaveSystemConfiguration(sysConfig);
  1336. await management.WriteSystemParameterToPlcAsync(sysConfig);
  1337. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  1338. }
  1339. catch (Exception ex)
  1340. {
  1341. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg =ex.Message, Duration = 1 });
  1342. }
  1343. }
  1344. }
  1345. #endregion
  1346. }
  1347. }