SettingViewModel.cs 62 KB

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