SettingViewModel.cs 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  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 PressureModel _DownPressureConfig = new PressureModel();
  57. public PressureModel DownPressureConfig
  58. {
  59. get { return _DownPressureConfig; }
  60. set { SetProperty(ref _DownPressureConfig, value); }
  61. }
  62. private Management _management;
  63. public Management management
  64. {
  65. get { return _management; }
  66. set { SetProperty(ref _management, value); }
  67. }
  68. private ObservableCollection<RobotInfo> _RobotList;
  69. public ObservableCollection<RobotInfo> RobotList
  70. {
  71. get { return _RobotList; }
  72. set { SetProperty(ref _RobotList, value); }
  73. }
  74. private ObservableCollection<CameraInfo> _CameraList;
  75. public ObservableCollection<CameraInfo> CameraList
  76. {
  77. get { return _CameraList; }
  78. set { SetProperty(ref _CameraList, value); }
  79. }
  80. private ObservableCollection<FeederInfo> _FeederList;
  81. public ObservableCollection<FeederInfo> FeederList
  82. {
  83. get { return _FeederList; }
  84. set { SetProperty(ref _FeederList, value); }
  85. }
  86. private ObservableCollection<PlcInfo> _PlcList;
  87. public ObservableCollection<PlcInfo> PlcList
  88. {
  89. get { return _PlcList; }
  90. set { SetProperty(ref _PlcList, value); }
  91. }
  92. private Language _CurrentLanguage;
  93. public Language CurrentLanguage
  94. {
  95. get { return _CurrentLanguage; }
  96. set { SetProperty(ref _CurrentLanguage, value); }
  97. }
  98. private RobotInfo _SelectRobot;
  99. /// <summary>
  100. /// 选中的机器人
  101. /// </summary>
  102. public RobotInfo SelectRobot
  103. {
  104. get { return _SelectRobot; }
  105. set
  106. {
  107. SetProperty(ref _SelectRobot, value);
  108. if (value != null && (value.RobotBrand == RobotBrand.XYZ_Platform || value.RobotBrand == RobotBrand.XYZU_Platform))
  109. {
  110. RobotSelectedPlc = PlcList.FirstOrDefault(p => p.Id == value.PLC);
  111. var root = _robotService.GetRobot(SelectRobot.Id);
  112. if (root != null)
  113. {
  114. //ResultsValues.Clear();
  115. //var res = root.GetNodesValue();
  116. //for (int i = 0; i < res.Count; i++)
  117. //{
  118. // if (res[i].StatusCode.Code != 2150891520)
  119. // {
  120. // ResultsValues.Add(res[i].Value.ToString());
  121. // }
  122. // else
  123. // {
  124. // break;
  125. // }
  126. //}
  127. }
  128. }
  129. }
  130. }
  131. private FeederInfo _SelectFeeder;
  132. /// <summary>
  133. /// 选中的Feeder
  134. /// </summary>
  135. public FeederInfo SelectFeeder
  136. {
  137. get { return _SelectFeeder; }
  138. set { SetProperty(ref _SelectFeeder, value); }
  139. }
  140. private PlcInfo _SelectPlc;
  141. public PlcInfo SelectPlc
  142. {
  143. get { return _SelectPlc; }
  144. set { SetProperty(ref _SelectPlc, value); }
  145. }
  146. private ObservableCollection<string> _ResultsValues = new ObservableCollection<string>();
  147. public ObservableCollection<string> ResultsValues { get => _ResultsValues; set => _ResultsValues = value; }
  148. private CameraInfo _SelectCamera;
  149. /// <summary>
  150. /// 选中的Camera
  151. /// </summary>
  152. public CameraInfo SelectCamera
  153. {
  154. get { return _SelectCamera; }
  155. set { SetProperty(ref _SelectCamera, value); }
  156. }
  157. private ObservableCollection<ScrewFeederInfo> _ScrewFeederList;
  158. /// <summary>
  159. /// 螺丝供料器列表
  160. /// </summary>
  161. public ObservableCollection<ScrewFeederInfo> ScrewFeederList
  162. {
  163. get { return _ScrewFeederList; }
  164. set { SetProperty(ref _ScrewFeederList, value); }
  165. }
  166. private ScrewFeederInfo _SelectScrewFeeder;
  167. public ScrewFeederInfo SelectScrewFeeder
  168. {
  169. get { return _SelectScrewFeeder; }
  170. set { SetProperty(ref _SelectScrewFeeder, value); }
  171. }
  172. private double _Title1;
  173. public double Title1
  174. {
  175. get { return _Title1; }
  176. set { SetProperty(ref _Title1, value); }
  177. }
  178. private double _Title2;
  179. public double Title2
  180. {
  181. get { return _Title2; }
  182. set { SetProperty(ref _Title2, value); }
  183. }
  184. private double _Title3;
  185. public double Title3
  186. {
  187. get { return _Title3; }
  188. set { SetProperty(ref _Title3, value); }
  189. }
  190. private double _Title4;
  191. public double Title4
  192. {
  193. get { return _Title4; }
  194. set { SetProperty(ref _Title4, value); }
  195. }
  196. private double _Body1;
  197. public double Body1
  198. {
  199. get { return _Body1; }
  200. set { SetProperty(ref _Body1, value); }
  201. }
  202. private double _Body2;
  203. public double Body2
  204. {
  205. get { return _Body2; }
  206. set { SetProperty(ref _Body2, value); }
  207. }
  208. private double _Body3;
  209. public double Body3
  210. {
  211. get { return _Body3; }
  212. set { SetProperty(ref _Body3, value); }
  213. }
  214. private double _Body4;
  215. public double Body4
  216. {
  217. get { return _Body4; }
  218. set { SetProperty(ref _Body4, value); }
  219. }
  220. private double _Captions1;
  221. public double Captions1
  222. {
  223. get { return _Captions1; }
  224. set { SetProperty(ref _Captions1, value); }
  225. }
  226. private double _Captions2;
  227. public double Captions2
  228. {
  229. get { return _Captions2; }
  230. set { SetProperty(ref _Captions2, value); }
  231. }
  232. private double _Captions3;
  233. public double Captions3
  234. {
  235. get { return _Captions3; }
  236. set { SetProperty(ref _Captions3, value); }
  237. }
  238. private double _Captions4;
  239. public double Captions4
  240. {
  241. get { return _Captions4; }
  242. set { SetProperty(ref _Captions4, value); }
  243. }
  244. private PlcInfo _RobotSelectedPlc;
  245. public PlcInfo RobotSelectedPlc
  246. {
  247. get { return _RobotSelectedPlc; }
  248. set
  249. {
  250. SetProperty(ref _RobotSelectedPlc, value);
  251. if (SelectRobot != null)
  252. {
  253. if (value != null)
  254. {
  255. SelectRobot.PLC = value.Id;
  256. }
  257. else
  258. {
  259. SelectRobot.PLC = Guid.Empty;
  260. }
  261. }
  262. }
  263. }
  264. private BgModbusTcp _BgModbus;
  265. public BgModbusTcp BgModbus
  266. {
  267. get { return _BgModbus; }
  268. set { SetProperty(ref _BgModbus, value); }
  269. }
  270. private BgTcpIP _BgTcp;
  271. public BgTcpIP BgTcp
  272. {
  273. get { return _BgTcp; }
  274. set { SetProperty(ref _BgTcp, value); }
  275. }
  276. private ObservableCollection<LightControllerConfig> _LightControllerList;
  277. public ObservableCollection<LightControllerConfig> LightControllerList
  278. {
  279. get { return _LightControllerList; }
  280. set { SetProperty(ref _LightControllerList, value); }
  281. }
  282. private LightControllerConfig _SelectLightController;
  283. public LightControllerConfig SelectLightController
  284. {
  285. get { return _SelectLightController; }
  286. set { SetProperty(ref _SelectLightController, value); }
  287. }
  288. private DeviceInfo _DeviceInfo;
  289. public DeviceInfo DeviceInfo
  290. {
  291. get { return _DeviceInfo; }
  292. set { SetProperty(ref _DeviceInfo, value); }
  293. }
  294. private SerialPortConfig _CardReaderConfig;
  295. public SerialPortConfig CardReaderConfig
  296. {
  297. get { return _CardReaderConfig; }
  298. set { SetProperty(ref _CardReaderConfig, 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. try
  1066. {
  1067. if (!File.Exists(FilePath.PressureParamterConfigPath))
  1068. {
  1069. FileHelper.WriteJsonFile(DownPressureConfig, FilePath.PressureParamterConfigPath);
  1070. }
  1071. DownPressureConfig = FileHelper.ReadJsonFile<PressureModel>(FilePath.PressureParamterConfigPath);
  1072. }
  1073. catch (Exception)
  1074. {
  1075. }
  1076. }
  1077. private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
  1078. {
  1079. switch (e.PropertyName)
  1080. {
  1081. case "Title1":
  1082. Application.Current.Resources["Font.Size.Title1"] = Title1;
  1083. break;
  1084. case "Title2":
  1085. Application.Current.Resources["Font.Size.Title2"] = Title2;
  1086. break;
  1087. case "Title3":
  1088. Application.Current.Resources["Font.Size.Title3"] = Title3;
  1089. break;
  1090. case "Title4":
  1091. Application.Current.Resources["Font.Size.Title4"] = Title4;
  1092. break;
  1093. case "Body1":
  1094. Application.Current.Resources["Font.Size.Body1"] = Body1;
  1095. break;
  1096. case "Body2":
  1097. Application.Current.Resources["Font.Size.Body2"] = Body2;
  1098. break;
  1099. case "Body3":
  1100. Application.Current.Resources["Font.Size.Body3"] = Body3;
  1101. break;
  1102. case "Body4":
  1103. Application.Current.Resources["Font.Size.Body4"] = Body4;
  1104. break;
  1105. case "Captions1":
  1106. Application.Current.Resources["Font.Size.Captions1"] = Captions1;
  1107. break;
  1108. case "Captions2":
  1109. Application.Current.Resources["Font.Size.Captions2"] = Captions2;
  1110. break;
  1111. case "Captions3":
  1112. Application.Current.Resources["Font.Size.Captions3"] = Captions3;
  1113. break;
  1114. case "Captions4":
  1115. Application.Current.Resources["Font.Size.Captions4"] = Captions4;
  1116. break;
  1117. default:
  1118. break;
  1119. }
  1120. }
  1121. #endregion
  1122. #region 继承
  1123. /// <summary>
  1124. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  1125. /// </summary>
  1126. /// <param name="navigationContext"></param>
  1127. /// <param name="continuationCallback"></param>
  1128. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  1129. {
  1130. continuationCallback(true);
  1131. }
  1132. /// <summary>
  1133. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  1134. /// </summary>
  1135. /// <param name="navigationContext"></param>
  1136. /// <exception cref="NotImplementedException"></exception>
  1137. public async void OnNavigatedTo(NavigationContext navigationContext)
  1138. {
  1139. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  1140. {
  1141. IsAllowEdit = false;
  1142. }
  1143. else
  1144. {
  1145. IsAllowEdit = true;
  1146. }
  1147. if (RobotList == null)
  1148. {
  1149. RobotList = new ObservableCollection<RobotInfo>();
  1150. var robots = _configService.GetAllRobots();
  1151. foreach (var robot in robots)
  1152. {
  1153. RobotList.Add(robot);
  1154. }
  1155. }
  1156. if (CameraList == null)
  1157. {
  1158. CameraList = new ObservableCollection<CameraInfo>();
  1159. var cameras = _configService.GetAllCameras();
  1160. foreach (var camera in cameras)
  1161. {
  1162. CameraList.Add(camera);
  1163. }
  1164. }
  1165. if (FeederList == null)
  1166. {
  1167. FeederList = new ObservableCollection<FeederInfo>();
  1168. var feeders = _configService.GetAllFeeders();
  1169. foreach (var feeder in feeders)
  1170. {
  1171. FeederList.Add(feeder);
  1172. }
  1173. }
  1174. if (PlcList == null)
  1175. {
  1176. PlcList = new ObservableCollection<PlcInfo>();
  1177. var plcs = _configService.GetAllPlcs();
  1178. foreach (var plc in plcs)
  1179. {
  1180. PlcList.Add(plc);
  1181. }
  1182. }
  1183. // load light controllers
  1184. if (LightControllerList == null)
  1185. {
  1186. LightControllerList = new ObservableCollection<LightControllerConfig>();
  1187. var controllers = _configService.GetAllLightControllers();
  1188. foreach (var ctrl in controllers)
  1189. {
  1190. LightControllerList.Add(ctrl);
  1191. }
  1192. }
  1193. if (BgTcp == null)
  1194. {
  1195. BgTcp = _configService.GetBgTcp();
  1196. }
  1197. if (BgModbus == null)
  1198. {
  1199. BgModbus = _configService.GetBgModbusTcp();
  1200. }
  1201. if (DeviceInfo == null)
  1202. {
  1203. // load device info
  1204. DeviceInfo = _configService.GetDeviceInfo();
  1205. }
  1206. if (CardReaderConfig == null)
  1207. {
  1208. // load card reader config
  1209. CardReaderConfig = _configService.GetCardReaderConfig();
  1210. }
  1211. CurrentLanguage = _configService.GetSystemConfiguration().CurrentLanguage;
  1212. // load system parameters into UI
  1213. var sys = _configService.GetSystemConfiguration();
  1214. if (sys != null)
  1215. {
  1216. WorkMode = sys.WorkMode;
  1217. PickPointNum = sys.PickPointNum;
  1218. DowmCameraNum = sys.DowmCameraNum;
  1219. WorkMode = sys.WorkMode;
  1220. PickPointNum = sys.PickPointNum;
  1221. DowmCameraNum = sys.DowmCameraNum;
  1222. UpCameraPickNum = sys.UpCameraPickNum;
  1223. UpCameraPutNum = sys.UpCameraPutNum;
  1224. UPCameracheckNum = sys.UPCameracheckNum;
  1225. FixedCameraPickNum = sys.FixedCameraPickNum;
  1226. FixedCameraPutNum = sys.FixedCameraPutNum;
  1227. FixedCameracheckNum = sys.FixedCameracheckNum;
  1228. ScrewTeachNum = sys.ScrewTeachNum;
  1229. PressureSensorConfig = sys.PressureSensorConfig;
  1230. }
  1231. if (isFirstLoad)
  1232. {
  1233. Title1 = (double)Application.Current.Resources["Font.Size.Title1"];
  1234. Title2 = (double)Application.Current.Resources["Font.Size.Title2"];
  1235. Title3 = (double)Application.Current.Resources["Font.Size.Title3"];
  1236. Title4 = (double)Application.Current.Resources["Font.Size.Title4"];
  1237. Body1 = (double)Application.Current.Resources["Font.Size.Body1"];
  1238. Body2 = (double)Application.Current.Resources["Font.Size.Body2"];
  1239. Body3 = (double)Application.Current.Resources["Font.Size.Body3"];
  1240. Body4 = (double)Application.Current.Resources["Font.Size.Body4"];
  1241. Captions1 = (double)Application.Current.Resources["Font.Size.Captions1"];
  1242. Captions2 = (double)Application.Current.Resources["Font.Size.Captions2"];
  1243. Captions3 = (double)Application.Current.Resources["Font.Size.Captions3"];
  1244. Captions4 = (double)Application.Current.Resources["Font.Size.Captions4"];
  1245. this.PropertyChanged += OnPropertyChanged;
  1246. isFirstLoad = false;
  1247. }
  1248. }
  1249. /// <summary>
  1250. /// 是否允许导航到此视图模型。
  1251. /// </summary>
  1252. /// <param name="navigationContext"></param>
  1253. /// <returns></returns>
  1254. public bool IsNavigationTarget(NavigationContext navigationContext)
  1255. {
  1256. return true;
  1257. }
  1258. /// <summary>
  1259. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  1260. /// </summary>
  1261. /// <param name="navigationContext"></param>
  1262. public void OnNavigatedFrom(NavigationContext navigationContext)
  1263. {
  1264. }
  1265. #endregion
  1266. private bool _IsAllowEdit = false;
  1267. public bool IsAllowEdit
  1268. {
  1269. get { return _IsAllowEdit; }
  1270. set { SetProperty(ref _IsAllowEdit, value); }
  1271. }
  1272. private void LoginChange(Models.User user)
  1273. {
  1274. if (user.userPart == Enums.UserPart.Operator)
  1275. {
  1276. IsAllowEdit = false;
  1277. }
  1278. else
  1279. {
  1280. IsAllowEdit = true;
  1281. }
  1282. }
  1283. #region 系统参数
  1284. private Int16 _WorkMode = 0;
  1285. /// <summary>
  1286. /// 工作模式 =1 拍1打1 =2 拍1打所有
  1287. /// </summary>
  1288. public Int16 WorkMode
  1289. {
  1290. get { return _WorkMode; }
  1291. set { SetProperty(ref _WorkMode, value); }
  1292. }
  1293. private Int16 _PickPointNum = 0;
  1294. /// <summary>
  1295. /// 取料位置 =0 不需要取料 =1 需要取料
  1296. /// </summary>
  1297. public Int16 PickPointNum
  1298. {
  1299. get { return _PickPointNum; }
  1300. set { SetProperty(ref _PickPointNum, value); }
  1301. }
  1302. private Int16 _DowmCameraNum = 0;
  1303. /// <summary>
  1304. /// 下相机拍照次数
  1305. /// </summary>
  1306. public Int16 DowmCameraNum
  1307. {
  1308. get => _DowmCameraNum;
  1309. set => SetProperty(ref _DowmCameraNum, value);
  1310. }
  1311. private Int16 _UpCameraPickNum = 0;
  1312. /// <summary>
  1313. /// 上相机取料拍照次数
  1314. /// </summary>
  1315. public Int16 UpCameraPickNum
  1316. {
  1317. get => _UpCameraPickNum;
  1318. set => SetProperty(ref _UpCameraPickNum, value);
  1319. }
  1320. private Int16 _UpCameraPutNum = 0;
  1321. /// <summary>
  1322. /// 上相机放料拍照次数
  1323. /// </summary>
  1324. public Int16 UpCameraPutNum
  1325. {
  1326. get => _UpCameraPutNum;
  1327. set => SetProperty(ref _UpCameraPutNum, value);
  1328. }
  1329. private Int16 _UPCameracheckNum = 0;
  1330. /// <summary>
  1331. /// 上相机锁附/组装次数
  1332. /// </summary>
  1333. public Int16 UPCameracheckNum
  1334. {
  1335. get => _UPCameracheckNum;
  1336. set => SetProperty(ref _UPCameracheckNum, value);
  1337. }
  1338. private Int16 _FixedCameraPickNum = 0;
  1339. /// <summary>
  1340. /// 取料固定位置拍产品相机数量
  1341. /// </summary>
  1342. public Int16 FixedCameraPickNum
  1343. {
  1344. get => _FixedCameraPickNum;
  1345. set => SetProperty(ref _FixedCameraPickNum, value);
  1346. }
  1347. private Int16 _FixedCameraPutNum = 0;
  1348. /// <summary>
  1349. /// 锁附/组装固定位置拍产品相机数量
  1350. /// </summary>
  1351. public Int16 FixedCameraPutNum
  1352. {
  1353. get => _FixedCameraPutNum;
  1354. set => SetProperty(ref _FixedCameraPutNum, value);
  1355. }
  1356. private Int16 _FixedCameracheckNum = 0;
  1357. /// <summary>
  1358. /// 固定相机检测锁附/组装次数
  1359. /// </summary>
  1360. public Int16 FixedCameracheckNum
  1361. {
  1362. get => _FixedCameracheckNum;
  1363. set => SetProperty(ref _FixedCameracheckNum, value);
  1364. }
  1365. private Int16 _ScrewTeachNum = 0;
  1366. /// <summary>
  1367. /// 披头矫正的模式,1:初始化时矫正,2:每个产品矫正一次,3:每颗螺丝矫正一次
  1368. /// </summary>
  1369. public Int16 ScrewTeachNum
  1370. {
  1371. get => _ScrewTeachNum;
  1372. set => SetProperty(ref _ScrewTeachNum, value);
  1373. }
  1374. private ObservableCollection<PressureSensorSettings> _PressureSensorConfig;
  1375. /// <summary>
  1376. /// 压力传感器配置列表
  1377. /// </summary>
  1378. public ObservableCollection<PressureSensorSettings> PressureSensorConfig
  1379. {
  1380. get { return _PressureSensorConfig; }
  1381. set { SetProperty(ref _PressureSensorConfig, value); }
  1382. }
  1383. async void ExecuteSaveSystemParametersCommand()
  1384. {
  1385. var view = new ShowMessage($"AAS", Lang.是否保存系统参数);
  1386. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  1387. if (result != null && result is bool && (bool)result)
  1388. {
  1389. try
  1390. {
  1391. FileHelper.WriteJsonFile(DownPressureConfig, FilePath.PressureParamterConfigPath);
  1392. ExecuteSaveCardReaderConfigCommand();
  1393. var sysConfig = _configService.GetSystemConfiguration();
  1394. sysConfig.WorkMode = WorkMode;
  1395. sysConfig.PickPointNum = PickPointNum;
  1396. sysConfig.DowmCameraNum = DowmCameraNum;
  1397. sysConfig.UpCameraPickNum = UpCameraPickNum;
  1398. sysConfig.UpCameraPutNum = UpCameraPutNum;
  1399. sysConfig.UPCameracheckNum = UPCameracheckNum;
  1400. sysConfig.FixedCameraPickNum = FixedCameraPickNum;
  1401. sysConfig.FixedCameraPutNum = FixedCameraPutNum;
  1402. sysConfig.FixedCameracheckNum = FixedCameracheckNum;
  1403. sysConfig.ScrewTeachNum= ScrewTeachNum;
  1404. sysConfig.PressureSensorConfig = PressureSensorConfig;
  1405. _configService.SaveSystemConfiguration(sysConfig);
  1406. await management.WriteSystemParameterToPlcAsync(sysConfig);
  1407. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  1408. }
  1409. catch (Exception ex)
  1410. {
  1411. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg =ex.Message, Duration = 1 });
  1412. }
  1413. }
  1414. }
  1415. /// <summary>
  1416. /// 选择压力曲线保存路径
  1417. /// </summary>
  1418. /// <param name="parameter"></param>
  1419. void ExecuteBrowsePressureCurvePathCommand(object parameter)
  1420. {
  1421. if (parameter is PressureSensorSettings sensorSettings)
  1422. {
  1423. var dialog = new System.Windows.Forms.FolderBrowserDialog();
  1424. System.Windows.Forms.DialogResult result = dialog.ShowDialog();
  1425. if (result == System.Windows.Forms.DialogResult.OK)
  1426. {
  1427. sensorSettings.PressureCurvePath = dialog.SelectedPath;
  1428. }
  1429. }
  1430. }
  1431. /// <summary>
  1432. /// 保存读卡器配置
  1433. /// </summary>
  1434. void ExecuteSaveCardReaderConfigCommand()
  1435. {
  1436. try
  1437. {
  1438. _configService.SaveCardReaderConfig(CardReaderConfig);
  1439. }
  1440. catch (Exception ex)
  1441. {
  1442. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1443. }
  1444. }
  1445. #endregion
  1446. #region 设备信息
  1447. /// <summary>
  1448. /// 保存设备信息
  1449. /// </summary>
  1450. void ExecuteSaveDeviceInfoCommand()
  1451. {
  1452. try
  1453. {
  1454. if (DeviceInfo != null)
  1455. {
  1456. _configService.SaveDeviceInfo(DeviceInfo);
  1457. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  1458. }
  1459. }
  1460. catch (Exception ex)
  1461. {
  1462. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1463. }
  1464. }
  1465. #endregion
  1466. }
  1467. }