SettingViewModel.cs 61 KB

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