SettingViewModel.cs 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  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. var robot = _robotService.GetRobot(item.Id);
  428. if (robot != null)
  429. {
  430. //更新连接参数
  431. robot.SafeZ=item.SafeZ;
  432. }
  433. _configService.AddOrUpdateRobot(item);
  434. }
  435. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  436. }
  437. }
  438. }
  439. #endregion
  440. #region 相机
  441. /// <summary>
  442. /// 保存所有相机
  443. /// </summary>
  444. async void ExecuteSaveCamerasCommand()
  445. {
  446. var view = new ShowMessage($"AAS", Lang.是否保存相机配置);
  447. //show the dialog
  448. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  449. if (result != null && result is bool)
  450. {
  451. if (((bool)result))
  452. {
  453. //management.DeviceConfig.Cameras = DeviceConfig.Cameras;
  454. //management.SaveCamerasDeviceConfig();
  455. //management.DeviceConfig.Cameras = DeviceConfig.Cameras;
  456. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  457. }
  458. }
  459. }
  460. /// <summary>
  461. /// 移除相机
  462. /// </summary>
  463. void ExecuteRemoveCameraCommand()
  464. {
  465. if (MessageBox.Show(Lang.是否移除相机, $"AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  466. {
  467. // 找到要删除的相机
  468. var cameraToRemove = CameraList.FirstOrDefault(r => r.Id == SelectCamera.Id);
  469. if (cameraToRemove != null)
  470. {
  471. _configService.RemoveCamera(cameraToRemove.Id);
  472. _cameraService.RemoveCamera(cameraToRemove.Id);
  473. var root = _configService.GetAllCameras();
  474. CameraList = new ObservableCollection<CameraInfo>(root);
  475. foreach (var item in CameraList)
  476. {
  477. _cameraService.UpdateCameraNumber(item.Id, item.CameraNo);
  478. }
  479. //对所有产品也进行移除
  480. _productService.RemoveCameraFromProduct(cameraToRemove.Id);
  481. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  482. }
  483. }
  484. }
  485. /// <summary>
  486. /// 编辑相机
  487. /// </summary>
  488. void ExecuteEditCameraCommand()
  489. {
  490. if (SelectCamera == null) return;
  491. bool isSelectedCameraBrandSucced = false;
  492. CameraBrand cameraBrand = CameraBrand.HikRobot_MVS;
  493. IDialogParameters parameters1 = new DialogParameters();
  494. parameters1.Add("CameraBrand", SelectCamera.CameraBrand);
  495. _dialogService.ShowDialog("SelectCameraBrand", parameters1, rst =>
  496. {
  497. if (rst.Result == ButtonResult.OK)
  498. {
  499. isSelectedCameraBrandSucced = true;
  500. cameraBrand = rst.Parameters.GetValue<CameraBrand>("CameraBrand");
  501. }
  502. else
  503. {
  504. return;
  505. }
  506. });
  507. if (!isSelectedCameraBrandSucced) { return; }
  508. (bool isInstalled, string version) result = (false, "");
  509. string softwarename = "";
  510. switch (cameraBrand)
  511. {
  512. case CameraBrand.HikRobot_MVS:
  513. softwarename = "MVS";
  514. result = MvCamera.CheckSoftwareInstalled();
  515. break;
  516. case CameraBrand.Basler_Pylon:
  517. softwarename = "Pylon 7";
  518. result = BaslerCamera.CheckSoftwareInstalled();
  519. break;
  520. case CameraBrand.IRayple:
  521. softwarename = "MV Viewer";
  522. result = AHuaCamera.CheckSoftwareInstalled();
  523. break;
  524. case CameraBrand.Cognex:
  525. softwarename = "Cognex VisionPro";
  526. result = CognexCamera.CheckSoftwareInstalled();
  527. break;
  528. case CameraBrand.Galaxy:
  529. softwarename = "Galaxy Viewer";
  530. result = GalaxyCamera.CheckSoftwareInstalled();
  531. break;
  532. case CameraBrand.Opt:
  533. softwarename = "OPT Camera Viewer";
  534. result = OptCamera.CheckSoftwareInstalled();
  535. break;
  536. default:
  537. break;
  538. }
  539. if (!result.isInstalled)
  540. {
  541. if (string.IsNullOrEmpty(result.version))
  542. {
  543. //未安装
  544. MessageBox.Show(Lang.CameraSDKCheck_Message1.Replace("{name}", softwarename), "AAS", MessageBoxButton.OK, MessageBoxImage.Asterisk);
  545. }
  546. else
  547. {
  548. //版本较低
  549. MessageBox.Show(Lang.CameraSDKCheck_Message2.Replace("{name}", softwarename).Replace("{version}", result.version).Replace("\\n", Environment.NewLine), "AAS", MessageBoxButton.OK, MessageBoxImage.Asterisk);
  550. }
  551. return;
  552. }
  553. //编辑前的相机名称
  554. string oldCameraName = SelectCamera.CameraName;
  555. IDialogParameters parameters = new DialogParameters();
  556. parameters.Add("CameraBrand", cameraBrand);
  557. parameters.Add("Camera", SelectCamera.Copy());
  558. _dialogService.ShowDialog("EditCamera", parameters, rst =>
  559. {
  560. //对话框关闭之后的回调函数,可以在这解析结果。
  561. ButtonResult result1 = rst.Result;
  562. if (result1 == ButtonResult.OK)
  563. {
  564. var param = rst.Parameters.GetValue<CameraInfo>("Camera");
  565. SelectCamera.CameraName = param.CameraName;
  566. SelectCamera.SerialNumber = param.SerialNumber;
  567. SelectCamera.Model = param.Model;
  568. SelectCamera.ManufacturerName = param.ManufacturerName;
  569. SelectCamera.CameraType = param.CameraType;
  570. SelectCamera.CameraBrand = param.CameraBrand;
  571. SelectCamera.CameraIp = param.CameraIp;
  572. _configService.AddOrUpdateCamera(SelectCamera);
  573. if (oldCameraName != param.CameraName)
  574. {
  575. //对所有产品也进行修改
  576. _productService.UpdateCameraNameInProduct(SelectCamera.Id, SelectCamera.CameraName);
  577. }
  578. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  579. }
  580. });
  581. }
  582. /// <summary>
  583. /// 增加相机
  584. /// </summary>
  585. void ExecuteAddCameraCommand()
  586. {
  587. bool isSelectedCameraBrandSucced = false;
  588. CameraBrand cameraBrand = CameraBrand.HikRobot_MVS;
  589. _dialogService.ShowDialog("SelectCameraBrand", rst =>
  590. {
  591. if (rst.Result == ButtonResult.OK)
  592. {
  593. isSelectedCameraBrandSucced = true;
  594. cameraBrand = rst.Parameters.GetValue<CameraBrand>("CameraBrand");
  595. }
  596. else
  597. {
  598. return;
  599. }
  600. });
  601. if (!isSelectedCameraBrandSucced) { return; }
  602. ;
  603. (bool isInstalled, string version) result = (false, "");
  604. string softwarename = "";
  605. switch (cameraBrand)
  606. {
  607. case CameraBrand.HikRobot_MVS:
  608. softwarename = "MVS";
  609. result = MvCamera.CheckSoftwareInstalled();
  610. break;
  611. case CameraBrand.Basler_Pylon:
  612. softwarename = "Pylon 7";
  613. result = BaslerCamera.CheckSoftwareInstalled();
  614. break;
  615. case CameraBrand.IRayple:
  616. softwarename = "MV Viewer";
  617. result = AHuaCamera.CheckSoftwareInstalled();
  618. break;
  619. case CameraBrand.Cognex:
  620. softwarename = "Cognex VisionPro";
  621. result = CognexCamera.CheckSoftwareInstalled();
  622. break;
  623. case CameraBrand.Galaxy:
  624. softwarename = "Galaxy Viewer";
  625. result = GalaxyCamera.CheckSoftwareInstalled();
  626. break;
  627. case CameraBrand.Opt:
  628. softwarename = "OPT Camera Viewer";
  629. result = OptCamera.CheckSoftwareInstalled();
  630. break;
  631. default:
  632. break;
  633. }
  634. if (!result.isInstalled)
  635. {
  636. if (string.IsNullOrEmpty(result.version))
  637. {
  638. //未安装
  639. MessageBox.Show(Lang.CameraSDKCheck_Message1.Replace("{name}", softwarename), "AAS", MessageBoxButton.OK, MessageBoxImage.Asterisk);
  640. }
  641. else
  642. {
  643. //版本较低
  644. MessageBox.Show(Lang.CameraSDKCheck_Message2.Replace("{name}", softwarename).Replace("{version}", result.version).Replace("\\n", Environment.NewLine), "AAS", MessageBoxButton.OK, MessageBoxImage.Asterisk);
  645. }
  646. return;
  647. }
  648. IDialogParameters parameters = new DialogParameters();
  649. parameters.Add("CameraBrand", cameraBrand);
  650. _dialogService.ShowDialog("AddCamera", parameters, rst =>
  651. {
  652. //对话框关闭之后的回调函数,可以在这解析结果。
  653. ButtonResult result1 = rst.Result;
  654. if (result1 == ButtonResult.OK)
  655. {
  656. var param = rst.Parameters.GetValue<CameraInfo>("Camera");
  657. var camera = new CameraInfo()
  658. {
  659. Id = param.Id,
  660. CameraName = param.CameraName,
  661. SerialNumber = param.SerialNumber,
  662. Model = param.Model,
  663. ManufacturerName = param.ManufacturerName,
  664. CameraType = param.CameraType,
  665. CameraBrand = param.CameraBrand,
  666. CameraIp = param.CameraIp
  667. };
  668. CameraList.Add(camera);
  669. _configService.AddOrUpdateCamera(camera);
  670. _cameraService.CreateCamera(camera.Id, camera);
  671. //对所有产品也进行添加
  672. _productService.AddCameraToProduct(camera.Id, camera.CameraName);
  673. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  674. }
  675. });
  676. }
  677. #endregion
  678. #region Feeder
  679. void ExecuteAddFeederCommand()
  680. {
  681. _dialogService.ShowDialog("AddFeeder", rst =>
  682. {
  683. //对话框关闭之后的回调函数,可以在这解析结果。
  684. ButtonResult result1 = rst.Result;
  685. if (result1 == ButtonResult.OK)
  686. {
  687. var param = rst.Parameters.GetValue<FeederInfo>("Feeder");
  688. var feeder = new FeederInfo()
  689. {
  690. Id = param.Id,
  691. FeederName = param.FeederName,
  692. ConnectType = param.ConnectType,
  693. FeederBrand = param.FeederBrand,
  694. IP = param.IP,
  695. Port = param.Port
  696. };
  697. FeederList.Add(feeder);
  698. _configService.AddOrUpdateFeeder(feeder);
  699. _feederService.CreateFeeder(feeder.Id, feeder.FeederNo, feeder.FeederName, feeder.IP, feeder.Port, feeder.FeederBrand);
  700. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.添加完成, Duration = 1 });
  701. }
  702. });
  703. }
  704. void ExecuteRemoveFeederCommand()
  705. {
  706. if (MessageBox.Show(Lang.是否移除Feeder, " AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  707. {
  708. // 找到要删除的机器人
  709. var feederToRemove = FeederList.FirstOrDefault(r => r.Id == SelectFeeder.Id);
  710. if (feederToRemove != null)
  711. {
  712. _configService.RemoveFeeder(feederToRemove.Id);
  713. _feederService.RemoveFeeder(feederToRemove.Id);
  714. var root = _configService.GetAllFeeders();
  715. FeederList = new ObservableCollection<FeederInfo>(root);
  716. foreach (var item in FeederList)
  717. {
  718. _feederService.UpdateFeederNo(item.Id, item.FeederNo);
  719. }
  720. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.移除完成, Duration = 1 });
  721. }
  722. }
  723. }
  724. async void ExecuteSaveFeedersCommand()
  725. {
  726. var view = new ShowMessage($"AAS", Lang.是否保存Feeder);
  727. //show the dialog
  728. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  729. if (result != null && result is bool)
  730. {
  731. if (((bool)result))
  732. {
  733. foreach (var item in FeederList)
  734. {
  735. _configService.AddOrUpdateFeeder(item);
  736. }
  737. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  738. }
  739. }
  740. }
  741. #endregion
  742. #region PLC
  743. /// <summary>
  744. /// 保存PLC
  745. /// </summary>
  746. async void ExecuteSavePlcsCommand()
  747. {
  748. var view = new ShowMessage($"AAS", Lang.是否保存PLC);
  749. //show the dialog
  750. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  751. if (result != null && result is bool)
  752. {
  753. if (((bool)result))
  754. {
  755. foreach (var item in PlcList)
  756. {
  757. _configService.AddOrUpdatePlc(item);
  758. }
  759. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  760. }
  761. }
  762. }
  763. /// <summary>
  764. /// 移除PLC
  765. /// </summary>
  766. void ExecuteRemovePlcCommand()
  767. {
  768. if (MessageBox.Show(Lang.是否移除PLC, $"AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  769. {
  770. // 找到要删除的plc
  771. var plcToRemove = PlcList.FirstOrDefault(r => r.Id == SelectPlc.Id);
  772. if (plcToRemove != null)
  773. {
  774. _configService.RemovePlc(plcToRemove.Id);
  775. _plcService.RemovePlc(plcToRemove.Id);
  776. var root = _configService.GetAllPlcs();
  777. PlcList = new ObservableCollection<PlcInfo>(root);
  778. foreach (var item in PlcList)
  779. {
  780. _plcService.UpdatePlcNumber(item.Id, item.Index);
  781. }
  782. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.移除完成, Duration = 1 });
  783. }
  784. }
  785. }
  786. /// <summary>
  787. /// 增加PLC
  788. /// </summary>
  789. void ExecuteAddPlcCommand()
  790. {
  791. _dialogService.ShowDialog("AddPlc", rst =>
  792. {
  793. //对话框关闭之后的回调函数,可以在这解析结果。
  794. ButtonResult result1 = rst.Result;
  795. if (result1 == ButtonResult.OK)
  796. {
  797. var param = rst.Parameters.GetValue<PlcInfo>("PLC");
  798. var plc = new PlcInfo()
  799. {
  800. Id = param.Id,
  801. Name = param.Name,
  802. CommunicationType = param.CommunicationType,
  803. EndpointUrl = param.EndpointUrl,
  804. };
  805. PlcList.Add(plc);
  806. _configService.AddOrUpdatePlc(plc);
  807. _plcService.CreatePlc(plc.Id, plc);
  808. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.添加完成, Duration = 1 });
  809. }
  810. });
  811. }
  812. #endregion
  813. #region Light handlers
  814. void ExecuteAddLightControllerCommand()
  815. {
  816. // open dialog to add light controller (similar to robots)
  817. _dialogService.ShowDialog("AddLightController", rst =>
  818. {
  819. if (rst.Result == ButtonResult.OK)
  820. {
  821. var param = rst.Parameters.GetValue<LightControllerConfig>("LightController");
  822. // assign id and defaults if needed
  823. if (param.Id == 0)
  824. param.Id = (LightControllerList?.Count ?? 0) + 1;
  825. LightControllerList.Add(param);
  826. _configService.AddOrUpdateLightController(param);
  827. SelectLightController = param;
  828. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.添加完成, Duration = 1 });
  829. }
  830. });
  831. }
  832. void ExecuteRemoveLightControllerCommand()
  833. {
  834. if (SelectLightController == null) return;
  835. if (MessageBox.Show(Lang.是否移除光源控制器, "AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  836. {
  837. _configService.RemoveLightController(SelectLightController.Id);
  838. LightControllerList.Remove(SelectLightController);
  839. SelectLightController = null;
  840. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.移除完成, Duration = 1 });
  841. }
  842. }
  843. async void ExecuteSaveLightControllersCommand()
  844. {
  845. var view = new ShowMessage($"AAS", Lang.是否保存光源控制器配置);
  846. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  847. if (result != null && result is bool && (bool)result)
  848. {
  849. foreach (var item in LightControllerList)
  850. {
  851. _configService.AddOrUpdateLightController(item);
  852. }
  853. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  854. }
  855. }
  856. #endregion
  857. #region 螺丝供料器
  858. /// <summary>
  859. /// 保存所有螺丝供料器
  860. /// </summary>
  861. async void ExecuteSaveScrewFeedersCommand()
  862. {
  863. var view = new ShowMessage($"AAS", Lang.是否保存螺丝供料器);
  864. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  865. if (result != null && result is bool && (bool)result)
  866. {
  867. if (ScrewFeederList != null)
  868. {
  869. foreach (var item in ScrewFeederList)
  870. {
  871. try
  872. {
  873. _configService.AddOrUpdateScrewFeeder(item);
  874. }
  875. catch (Exception ex)
  876. {
  877. // 记录或通知错误,但继续处理其它项
  878. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  879. }
  880. }
  881. }
  882. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  883. }
  884. }
  885. /// <summary>
  886. /// 移除螺丝供料器
  887. /// </summary>
  888. void ExecuteRemoveScrewFeederCommand()
  889. {
  890. if (SelectScrewFeeder == null) return;
  891. if (MessageBox.Show(Lang.是否移除螺丝供料器, "AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  892. {
  893. try
  894. {
  895. var toRemove = ScrewFeederList?.FirstOrDefault(s => s.Id == SelectScrewFeeder.Id);
  896. if (toRemove != null)
  897. {
  898. _configService.RemoveScrewFeeder(toRemove.Id);
  899. // 重新加载列表以保持序号/配置一致(与其他类似实现一致)
  900. var root = _configService.GetAllScrewFeeders();
  901. ScrewFeederList = new ObservableCollection<ScrewFeederInfo>(root);
  902. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.移除完成, Duration = 1 });
  903. }
  904. }
  905. catch (Exception ex)
  906. {
  907. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  908. }
  909. }
  910. }
  911. /// <summary>
  912. /// 添加螺丝供料器
  913. /// </summary>
  914. void ExecuteAddScrewFeederCommand()
  915. {
  916. _dialogService.ShowDialog("AddScrewFeeder", rst =>
  917. {
  918. ButtonResult result1 = rst.Result;
  919. if (result1 == ButtonResult.OK)
  920. {
  921. var param = rst.Parameters.GetValue<ScrewFeederInfo>("ScrewFeeder");
  922. if (param != null)
  923. {
  924. if (ScrewFeederList == null)
  925. {
  926. ScrewFeederList = new ObservableCollection<ScrewFeederInfo>();
  927. }
  928. try
  929. {
  930. param = _configService.AddOrUpdateScrewFeeder(param);
  931. // 直接添加对话框返回的对象(也可以根据需要映射字段)
  932. ScrewFeederList.Add(param);
  933. }
  934. catch (Exception ex)
  935. {
  936. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  937. }
  938. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.添加完成, Duration = 1 });
  939. }
  940. }
  941. });
  942. }
  943. #endregion
  944. #region 通讯
  945. async void ExecuteSaveCommunicateCommand(string parm)
  946. {
  947. if (parm == "TCPIP")
  948. {
  949. var view = new ShowMessage($"AAS", Lang.是否保存后台TCP通讯配置);
  950. //show the dialog
  951. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  952. if (result != null && result is bool)
  953. {
  954. if (((bool)result))
  955. {
  956. _configService.SetBgTcp(BgTcp);
  957. _configService.SaveBgTcp();
  958. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  959. }
  960. }
  961. }
  962. else if (parm == "ModbusTCP")
  963. {
  964. var view = new ShowMessage($"AAS", Lang.是否保存后台ModbusTCP通讯配置);
  965. //show the dialog
  966. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  967. if (result != null && result is bool)
  968. {
  969. if (((bool)result))
  970. {
  971. _configService.SetBgModbusTcp(BgModbus);
  972. _configService.SaveBgModbusTcp();
  973. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  974. }
  975. }
  976. }
  977. else
  978. {
  979. return;
  980. }
  981. }
  982. #endregion
  983. #region 语言
  984. void ExecuteSelectLanguageCommand(string language)
  985. {
  986. if (string.Equals(language, "ChineseSimplified"))
  987. {
  988. CurrentLanguage = Enums.Language.ChineseSimplified;
  989. var sysConfig = _configService.GetSystemConfiguration();
  990. sysConfig.CurrentLanguage = CurrentLanguage;
  991. _configService.SaveSystemConfiguration(sysConfig);
  992. var culture = new CultureInfo("zh-CN");
  993. App.Current.Dispatcher.Thread.CurrentCulture = culture;
  994. App.Current.Dispatcher.Thread.CurrentUICulture = culture;
  995. LocalizeDictionary.Instance.Culture = culture;
  996. Lang.Culture = culture;
  997. Application.Current.Resources["DefaultFont"] = new System.Windows.Media.FontFamily("Microsoft YaHei");
  998. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.5 });
  999. }
  1000. else if (string.Equals(language, "ChineseTraditional"))
  1001. {
  1002. CurrentLanguage = Enums.Language.ChineseTraditional;
  1003. var sysConfig = _configService.GetSystemConfiguration();
  1004. sysConfig.CurrentLanguage = CurrentLanguage;
  1005. _configService.SaveSystemConfiguration(sysConfig);
  1006. var culture = new CultureInfo("zh-TW");
  1007. App.Current.Dispatcher.Thread.CurrentCulture = culture;
  1008. App.Current.Dispatcher.Thread.CurrentUICulture = culture;
  1009. LocalizeDictionary.Instance.Culture = culture;
  1010. Lang.Culture = culture;
  1011. Application.Current.Resources["DefaultFont"] = new System.Windows.Media.FontFamily("Microsoft YaHei");
  1012. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.5 });
  1013. }
  1014. else if (string.Equals(language, "English"))
  1015. {
  1016. CurrentLanguage = Enums.Language.English;
  1017. var sysConfig = _configService.GetSystemConfiguration();
  1018. sysConfig.CurrentLanguage = CurrentLanguage;
  1019. _configService.SaveSystemConfiguration(sysConfig);
  1020. var culture = new CultureInfo("en");
  1021. App.Current.Dispatcher.Thread.CurrentCulture = culture;
  1022. App.Current.Dispatcher.Thread.CurrentUICulture = culture;
  1023. LocalizeDictionary.Instance.Culture = culture;
  1024. Lang.Culture = culture;
  1025. Application.Current.Resources["DefaultFont"] = new System.Windows.Media.FontFamily("Segoe UI");
  1026. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.5 });
  1027. }
  1028. else if (string.Equals(language, "Japanese"))
  1029. {
  1030. CurrentLanguage = Enums.Language.Japanese;
  1031. var sysConfig = _configService.GetSystemConfiguration();
  1032. sysConfig.CurrentLanguage = CurrentLanguage;
  1033. _configService.SaveSystemConfiguration(sysConfig);
  1034. var culture = new CultureInfo("ja-JP");
  1035. App.Current.Dispatcher.Thread.CurrentCulture = culture;
  1036. App.Current.Dispatcher.Thread.CurrentUICulture = culture;
  1037. LocalizeDictionary.Instance.Culture = culture;
  1038. Lang.Culture = culture;
  1039. Application.Current.Resources["DefaultFont"] = new System.Windows.Media.FontFamily("Segoe UI");
  1040. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.5 });
  1041. }
  1042. }
  1043. #endregion
  1044. private void OnLoad()
  1045. {
  1046. }
  1047. private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
  1048. {
  1049. switch (e.PropertyName)
  1050. {
  1051. case "Title1":
  1052. Application.Current.Resources["Font.Size.Title1"] = Title1;
  1053. break;
  1054. case "Title2":
  1055. Application.Current.Resources["Font.Size.Title2"] = Title2;
  1056. break;
  1057. case "Title3":
  1058. Application.Current.Resources["Font.Size.Title3"] = Title3;
  1059. break;
  1060. case "Title4":
  1061. Application.Current.Resources["Font.Size.Title4"] = Title4;
  1062. break;
  1063. case "Body1":
  1064. Application.Current.Resources["Font.Size.Body1"] = Body1;
  1065. break;
  1066. case "Body2":
  1067. Application.Current.Resources["Font.Size.Body2"] = Body2;
  1068. break;
  1069. case "Body3":
  1070. Application.Current.Resources["Font.Size.Body3"] = Body3;
  1071. break;
  1072. case "Body4":
  1073. Application.Current.Resources["Font.Size.Body4"] = Body4;
  1074. break;
  1075. case "Captions1":
  1076. Application.Current.Resources["Font.Size.Captions1"] = Captions1;
  1077. break;
  1078. case "Captions2":
  1079. Application.Current.Resources["Font.Size.Captions2"] = Captions2;
  1080. break;
  1081. case "Captions3":
  1082. Application.Current.Resources["Font.Size.Captions3"] = Captions3;
  1083. break;
  1084. case "Captions4":
  1085. Application.Current.Resources["Font.Size.Captions4"] = Captions4;
  1086. break;
  1087. default:
  1088. break;
  1089. }
  1090. }
  1091. #endregion
  1092. #region 继承
  1093. /// <summary>
  1094. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  1095. /// </summary>
  1096. /// <param name="navigationContext"></param>
  1097. /// <param name="continuationCallback"></param>
  1098. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  1099. {
  1100. continuationCallback(true);
  1101. }
  1102. /// <summary>
  1103. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  1104. /// </summary>
  1105. /// <param name="navigationContext"></param>
  1106. /// <exception cref="NotImplementedException"></exception>
  1107. public async void OnNavigatedTo(NavigationContext navigationContext)
  1108. {
  1109. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  1110. {
  1111. IsAllowEdit = false;
  1112. }
  1113. else
  1114. {
  1115. IsAllowEdit = true;
  1116. }
  1117. if (RobotList == null)
  1118. {
  1119. RobotList = new ObservableCollection<RobotInfo>();
  1120. var robots = _configService.GetAllRobots();
  1121. foreach (var robot in robots)
  1122. {
  1123. RobotList.Add(robot);
  1124. }
  1125. }
  1126. if (CameraList == null)
  1127. {
  1128. CameraList = new ObservableCollection<CameraInfo>();
  1129. var cameras = _configService.GetAllCameras();
  1130. foreach (var camera in cameras)
  1131. {
  1132. CameraList.Add(camera);
  1133. }
  1134. }
  1135. if (FeederList == null)
  1136. {
  1137. FeederList = new ObservableCollection<FeederInfo>();
  1138. var feeders = _configService.GetAllFeeders();
  1139. foreach (var feeder in feeders)
  1140. {
  1141. FeederList.Add(feeder);
  1142. }
  1143. }
  1144. if (PlcList == null)
  1145. {
  1146. PlcList = new ObservableCollection<PlcInfo>();
  1147. var plcs = _configService.GetAllPlcs();
  1148. foreach (var plc in plcs)
  1149. {
  1150. PlcList.Add(plc);
  1151. }
  1152. }
  1153. // load light controllers
  1154. if (LightControllerList == null)
  1155. {
  1156. LightControllerList = new ObservableCollection<LightControllerConfig>();
  1157. var controllers = _configService.GetAllLightControllers();
  1158. foreach (var ctrl in controllers)
  1159. {
  1160. LightControllerList.Add(ctrl);
  1161. }
  1162. }
  1163. if (BgTcp == null)
  1164. {
  1165. BgTcp = _configService.GetBgTcp();
  1166. }
  1167. if (BgModbus == null)
  1168. {
  1169. BgModbus = _configService.GetBgModbusTcp();
  1170. }
  1171. CurrentLanguage = _configService.GetSystemConfiguration().CurrentLanguage;
  1172. // load system parameters into UI
  1173. var sys = _configService.GetSystemConfiguration();
  1174. if (sys != null)
  1175. {
  1176. WorkMode = sys.WorkMode;
  1177. PickPointNum = sys.PickPointNum;
  1178. DowmCameraNum = sys.DowmCameraNum;
  1179. WorkMode = sys.WorkMode;
  1180. PickPointNum = sys.PickPointNum;
  1181. DowmCameraNum = sys.DowmCameraNum;
  1182. UpCameraPickNum = sys.UpCameraPickNum;
  1183. UpCameraPutNum = sys.UpCameraPutNum;
  1184. UPCameracheckNum = sys.UPCameracheckNum;
  1185. FixedCameraPickNum = sys.FixedCameraPickNum;
  1186. FixedCameraPutNum = sys.FixedCameraPutNum;
  1187. FixedCameracheckNum = sys.FixedCameracheckNum;
  1188. ScrewTeachNum = sys.ScrewTeachNum;
  1189. }
  1190. if (isFirstLoad)
  1191. {
  1192. Title1 = (double)Application.Current.Resources["Font.Size.Title1"];
  1193. Title2 = (double)Application.Current.Resources["Font.Size.Title2"];
  1194. Title3 = (double)Application.Current.Resources["Font.Size.Title3"];
  1195. Title4 = (double)Application.Current.Resources["Font.Size.Title4"];
  1196. Body1 = (double)Application.Current.Resources["Font.Size.Body1"];
  1197. Body2 = (double)Application.Current.Resources["Font.Size.Body2"];
  1198. Body3 = (double)Application.Current.Resources["Font.Size.Body3"];
  1199. Body4 = (double)Application.Current.Resources["Font.Size.Body4"];
  1200. Captions1 = (double)Application.Current.Resources["Font.Size.Captions1"];
  1201. Captions2 = (double)Application.Current.Resources["Font.Size.Captions2"];
  1202. Captions3 = (double)Application.Current.Resources["Font.Size.Captions3"];
  1203. Captions4 = (double)Application.Current.Resources["Font.Size.Captions4"];
  1204. this.PropertyChanged += OnPropertyChanged;
  1205. isFirstLoad = false;
  1206. }
  1207. }
  1208. /// <summary>
  1209. /// 是否允许导航到此视图模型。
  1210. /// </summary>
  1211. /// <param name="navigationContext"></param>
  1212. /// <returns></returns>
  1213. public bool IsNavigationTarget(NavigationContext navigationContext)
  1214. {
  1215. return true;
  1216. }
  1217. /// <summary>
  1218. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  1219. /// </summary>
  1220. /// <param name="navigationContext"></param>
  1221. public void OnNavigatedFrom(NavigationContext navigationContext)
  1222. {
  1223. }
  1224. #endregion
  1225. private bool _IsAllowEdit = false;
  1226. public bool IsAllowEdit
  1227. {
  1228. get { return _IsAllowEdit; }
  1229. set { SetProperty(ref _IsAllowEdit, value); }
  1230. }
  1231. private void LoginChange(Models.User user)
  1232. {
  1233. if (user.userPart == Enums.UserPart.Operator)
  1234. {
  1235. IsAllowEdit = false;
  1236. }
  1237. else
  1238. {
  1239. IsAllowEdit = true;
  1240. }
  1241. }
  1242. #region 系统参数
  1243. private Int16 _WorkMode = 0;
  1244. /// <summary>
  1245. /// 工作模式 =1 拍1打1 =2 拍1打所有
  1246. /// </summary>
  1247. public Int16 WorkMode
  1248. {
  1249. get { return _WorkMode; }
  1250. set { SetProperty(ref _WorkMode, value); }
  1251. }
  1252. private Int16 _PickPointNum = 0;
  1253. /// <summary>
  1254. /// 取料位置 =0 不需要取料 =1 需要取料
  1255. /// </summary>
  1256. public Int16 PickPointNum
  1257. {
  1258. get { return _PickPointNum; }
  1259. set { SetProperty(ref _PickPointNum, value); }
  1260. }
  1261. private Int16 _DowmCameraNum = 0;
  1262. /// <summary>
  1263. /// 下相机拍照次数
  1264. /// </summary>
  1265. public Int16 DowmCameraNum
  1266. {
  1267. get => _DowmCameraNum;
  1268. set => SetProperty(ref _DowmCameraNum, value);
  1269. }
  1270. private Int16 _UpCameraPickNum = 0;
  1271. /// <summary>
  1272. /// 上相机取料拍照次数
  1273. /// </summary>
  1274. public Int16 UpCameraPickNum
  1275. {
  1276. get => _UpCameraPickNum;
  1277. set => SetProperty(ref _UpCameraPickNum, value);
  1278. }
  1279. private Int16 _UpCameraPutNum = 0;
  1280. /// <summary>
  1281. /// 上相机放料拍照次数
  1282. /// </summary>
  1283. public Int16 UpCameraPutNum
  1284. {
  1285. get => _UpCameraPutNum;
  1286. set => SetProperty(ref _UpCameraPutNum, value);
  1287. }
  1288. private Int16 _UPCameracheckNum = 0;
  1289. /// <summary>
  1290. /// 上相机锁附/组装次数
  1291. /// </summary>
  1292. public Int16 UPCameracheckNum
  1293. {
  1294. get => _UPCameracheckNum;
  1295. set => SetProperty(ref _UPCameracheckNum, value);
  1296. }
  1297. private Int16 _FixedCameraPickNum = 0;
  1298. /// <summary>
  1299. /// 取料固定位置拍产品相机数量
  1300. /// </summary>
  1301. public Int16 FixedCameraPickNum
  1302. {
  1303. get => _FixedCameraPickNum;
  1304. set => SetProperty(ref _FixedCameraPickNum, value);
  1305. }
  1306. private Int16 _FixedCameraPutNum = 0;
  1307. /// <summary>
  1308. /// 锁附/组装固定位置拍产品相机数量
  1309. /// </summary>
  1310. public Int16 FixedCameraPutNum
  1311. {
  1312. get => _FixedCameraPutNum;
  1313. set => SetProperty(ref _FixedCameraPutNum, value);
  1314. }
  1315. private Int16 _FixedCameracheckNum = 0;
  1316. /// <summary>
  1317. /// 固定相机检测锁附/组装次数
  1318. /// </summary>
  1319. public Int16 FixedCameracheckNum
  1320. {
  1321. get => _FixedCameracheckNum;
  1322. set => SetProperty(ref _FixedCameracheckNum, value);
  1323. }
  1324. private Int16 _ScrewTeachNum = 0;
  1325. /// <summary>
  1326. /// 披头矫正的模式,1:初始化时矫正,2:每个产品矫正一次,3:每颗螺丝矫正一次
  1327. /// </summary>
  1328. public Int16 ScrewTeachNum
  1329. {
  1330. get => _ScrewTeachNum;
  1331. set => SetProperty(ref _ScrewTeachNum, value);
  1332. }
  1333. async void ExecuteSaveSystemParametersCommand()
  1334. {
  1335. var view = new ShowMessage($"AAS", Lang.是否保存系统参数);
  1336. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  1337. if (result != null && result is bool && (bool)result)
  1338. {
  1339. try
  1340. {
  1341. var sysConfig = _configService.GetSystemConfiguration();
  1342. sysConfig.WorkMode = WorkMode;
  1343. sysConfig.PickPointNum = PickPointNum;
  1344. sysConfig.DowmCameraNum = DowmCameraNum;
  1345. sysConfig.UpCameraPickNum = UpCameraPickNum;
  1346. sysConfig.UpCameraPutNum = UpCameraPutNum;
  1347. sysConfig.UPCameracheckNum = UPCameracheckNum;
  1348. sysConfig.FixedCameraPickNum = FixedCameraPickNum;
  1349. sysConfig.FixedCameraPutNum = FixedCameraPutNum;
  1350. sysConfig.FixedCameracheckNum = FixedCameracheckNum;
  1351. sysConfig.ScrewTeachNum = ScrewTeachNum;
  1352. _configService.SaveSystemConfiguration(sysConfig);
  1353. await management.WriteSystemParameterToPlcAsync(sysConfig);
  1354. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  1355. }
  1356. catch (Exception ex)
  1357. {
  1358. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1359. }
  1360. }
  1361. }
  1362. #endregion
  1363. }
  1364. }