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