SettingViewModel.cs 68 KB

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