SettingViewModel.cs 61 KB

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