SettingViewModel.cs 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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. ScrewTeachNum = sys.ScrewTeachNum;
  1183. }
  1184. if (isFirstLoad)
  1185. {
  1186. Title1 = (double)Application.Current.Resources["Font.Size.Title1"];
  1187. Title2 = (double)Application.Current.Resources["Font.Size.Title2"];
  1188. Title3 = (double)Application.Current.Resources["Font.Size.Title3"];
  1189. Title4 = (double)Application.Current.Resources["Font.Size.Title4"];
  1190. Body1 = (double)Application.Current.Resources["Font.Size.Body1"];
  1191. Body2 = (double)Application.Current.Resources["Font.Size.Body2"];
  1192. Body3 = (double)Application.Current.Resources["Font.Size.Body3"];
  1193. Body4 = (double)Application.Current.Resources["Font.Size.Body4"];
  1194. Captions1 = (double)Application.Current.Resources["Font.Size.Captions1"];
  1195. Captions2 = (double)Application.Current.Resources["Font.Size.Captions2"];
  1196. Captions3 = (double)Application.Current.Resources["Font.Size.Captions3"];
  1197. Captions4 = (double)Application.Current.Resources["Font.Size.Captions4"];
  1198. this.PropertyChanged += OnPropertyChanged;
  1199. isFirstLoad = false;
  1200. }
  1201. }
  1202. /// <summary>
  1203. /// 是否允许导航到此视图模型。
  1204. /// </summary>
  1205. /// <param name="navigationContext"></param>
  1206. /// <returns></returns>
  1207. public bool IsNavigationTarget(NavigationContext navigationContext)
  1208. {
  1209. return true;
  1210. }
  1211. /// <summary>
  1212. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  1213. /// </summary>
  1214. /// <param name="navigationContext"></param>
  1215. public void OnNavigatedFrom(NavigationContext navigationContext)
  1216. {
  1217. }
  1218. #endregion
  1219. private bool _IsAllowEdit = false;
  1220. public bool IsAllowEdit
  1221. {
  1222. get { return _IsAllowEdit; }
  1223. set { SetProperty(ref _IsAllowEdit, value); }
  1224. }
  1225. private void LoginChange(Models.User user)
  1226. {
  1227. if (user.userPart == Enums.UserPart.Operator)
  1228. {
  1229. IsAllowEdit = false;
  1230. }
  1231. else
  1232. {
  1233. IsAllowEdit = true;
  1234. }
  1235. }
  1236. #region 系统参数
  1237. private Int16 _WorkMode = 0;
  1238. /// <summary>
  1239. /// 工作模式 =1 拍1打1 =2 拍1打所有
  1240. /// </summary>
  1241. public Int16 WorkMode
  1242. {
  1243. get { return _WorkMode; }
  1244. set { SetProperty(ref _WorkMode, value); }
  1245. }
  1246. private Int16 _PickPointNum = 0;
  1247. /// <summary>
  1248. /// 取料位置 =0 不需要取料 =1 需要取料
  1249. /// </summary>
  1250. public Int16 PickPointNum
  1251. {
  1252. get { return _PickPointNum; }
  1253. set { SetProperty(ref _PickPointNum, value); }
  1254. }
  1255. private Int16 _DowmCameraNum = 0;
  1256. /// <summary>
  1257. /// 下相机拍照次数
  1258. /// </summary>
  1259. public Int16 DowmCameraNum
  1260. {
  1261. get => _DowmCameraNum;
  1262. set => SetProperty(ref _DowmCameraNum, value);
  1263. }
  1264. private Int16 _UpCameraPickNum = 0;
  1265. /// <summary>
  1266. /// 上相机取料拍照次数
  1267. /// </summary>
  1268. public Int16 UpCameraPickNum
  1269. {
  1270. get => _UpCameraPickNum;
  1271. set => SetProperty(ref _UpCameraPickNum, value);
  1272. }
  1273. private Int16 _UpCameraPutNum = 0;
  1274. /// <summary>
  1275. /// 上相机放料拍照次数
  1276. /// </summary>
  1277. public Int16 UpCameraPutNum
  1278. {
  1279. get => _UpCameraPutNum;
  1280. set => SetProperty(ref _UpCameraPutNum, value);
  1281. }
  1282. private Int16 _UPCameracheckNum = 0;
  1283. /// <summary>
  1284. /// 上相机锁附/组装次数
  1285. /// </summary>
  1286. public Int16 UPCameracheckNum
  1287. {
  1288. get => _UPCameracheckNum;
  1289. set => SetProperty(ref _UPCameracheckNum, value);
  1290. }
  1291. private Int16 _FixedCameraPickNum = 0;
  1292. /// <summary>
  1293. /// 取料固定位置拍产品相机数量
  1294. /// </summary>
  1295. public Int16 FixedCameraPickNum
  1296. {
  1297. get => _FixedCameraPickNum;
  1298. set => SetProperty(ref _FixedCameraPickNum, value);
  1299. }
  1300. private Int16 _FixedCameraPutNum = 0;
  1301. /// <summary>
  1302. /// 锁附/组装固定位置拍产品相机数量
  1303. /// </summary>
  1304. public Int16 FixedCameraPutNum
  1305. {
  1306. get => _FixedCameraPutNum;
  1307. set => SetProperty(ref _FixedCameraPutNum, value);
  1308. }
  1309. private Int16 _FixedCameracheckNum = 0;
  1310. /// <summary>
  1311. /// 固定相机检测锁附/组装次数
  1312. /// </summary>
  1313. public Int16 FixedCameracheckNum
  1314. {
  1315. get => _FixedCameracheckNum;
  1316. set => SetProperty(ref _FixedCameracheckNum, value);
  1317. }
  1318. private Int16 _ScrewTeachNum = 0;
  1319. /// <summary>
  1320. /// 披头矫正的模式,1:初始化时矫正,2:每个产品矫正一次,3:每颗螺丝矫正一次
  1321. /// </summary>
  1322. public Int16 ScrewTeachNum
  1323. {
  1324. get => _ScrewTeachNum;
  1325. set => SetProperty(ref _ScrewTeachNum, value);
  1326. }
  1327. async void ExecuteSaveSystemParametersCommand()
  1328. {
  1329. var view = new ShowMessage($"AAS", "是否保存系统参数?");
  1330. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  1331. if (result != null && result is bool && (bool)result)
  1332. {
  1333. try
  1334. {
  1335. var sysConfig = _configService.GetSystemConfiguration();
  1336. sysConfig.WorkMode = WorkMode;
  1337. sysConfig.PickPointNum = PickPointNum;
  1338. sysConfig.DowmCameraNum = DowmCameraNum;
  1339. sysConfig.UpCameraPickNum = UpCameraPickNum;
  1340. sysConfig.UpCameraPutNum = UpCameraPutNum;
  1341. sysConfig.UPCameracheckNum = UPCameracheckNum;
  1342. sysConfig.FixedCameraPickNum = FixedCameraPickNum;
  1343. sysConfig.FixedCameraPutNum = FixedCameraPutNum;
  1344. sysConfig.FixedCameracheckNum = FixedCameracheckNum;
  1345. sysConfig.ScrewTeachNum= ScrewTeachNum;
  1346. _configService.SaveSystemConfiguration(sysConfig);
  1347. await management.WriteSystemParameterToPlcAsync(sysConfig);
  1348. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  1349. }
  1350. catch (Exception ex)
  1351. {
  1352. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg =ex.Message, Duration = 1 });
  1353. }
  1354. }
  1355. }
  1356. #endregion
  1357. }
  1358. }