SettingViewModel.cs 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776
  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<object> _LightModelChangedCommand;
  405. public DelegateCommand<object> LightModelChangedCommand =>
  406. _LightModelChangedCommand ?? (_LightModelChangedCommand = new DelegateCommand<object>(ExecuteLightModelChangedCommand));
  407. private DelegateCommand _SaveCardReaderConfigCommand;
  408. public DelegateCommand SaveCardReaderConfigCommand =>
  409. _SaveCardReaderConfigCommand ?? (_SaveCardReaderConfigCommand = new DelegateCommand(ExecuteSaveCardReaderConfigCommand));
  410. private DelegateCommand<object> _BrowsePressureCurvePathCommand;
  411. public DelegateCommand<object> BrowsePressureCurvePathCommand =>
  412. _BrowsePressureCurvePathCommand ?? (_BrowsePressureCurvePathCommand = new DelegateCommand<object>(ExecuteBrowsePressureCurvePathCommand));
  413. #endregion
  414. #region 事件
  415. #endregion
  416. public SettingViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService,
  417. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, IPlcService plcService,
  418. IProductService productService, ISystemDatabaseService systemDatabaseService)
  419. {
  420. _regionManager = regionManager;
  421. _eventAggregator = ea;
  422. _container = container;
  423. _dialogService = dialogService;
  424. _configService = configService;
  425. _robotService = robotService;
  426. _cameraService = cameraService;
  427. _feederService = feederService;
  428. _plcService = plcService;
  429. _productService = productService;
  430. _systemDatabaseService = systemDatabaseService;
  431. management = _container.Resolve<Management>();
  432. LoadedCommand = new DelegateCommand(OnLoad);
  433. //订阅权限登录事件
  434. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  435. }
  436. #region 方法
  437. #region 机器人
  438. private void ExecuteAddRobotCommand()
  439. {
  440. _dialogService.ShowDialog("AddRobot", rst =>
  441. {
  442. //对话框关闭之后的回调函数,可以在这解析结果。
  443. ButtonResult result1 = rst.Result;
  444. if (result1 == ButtonResult.OK)
  445. {
  446. var param = rst.Parameters.GetValue<RobotInfo>("Robot");
  447. var robot = new RobotInfo()
  448. {
  449. Id = param.Id,
  450. RobotName = param.RobotName,
  451. ConnectType = param.ConnectType,
  452. RobotBrand = param.RobotBrand,
  453. IP = param.IP,
  454. Port = param.Port,
  455. Terminator = param.Terminator,
  456. };
  457. RobotList.Add(robot);
  458. _configService.AddOrUpdateRobot(robot);
  459. _robotService.CreateRobot(robot.Id, robot);
  460. //对所有产品也进行添加
  461. _productService.AddRobotPointToProduct(robot.Id, robot.RobotName);
  462. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.添加完成}", Duration = 1 });
  463. }
  464. });
  465. }
  466. void ExecuteRemoveCommand()
  467. {
  468. if (MessageBox.Show(Lang.是否移除机器人, $"AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  469. {
  470. // 找到要删除的机器人
  471. var robotToRemove = RobotList.FirstOrDefault(r => r.Id == SelectRobot.Id);
  472. if (robotToRemove != null)
  473. {
  474. RobotList.Remove(robotToRemove);
  475. _configService.RemoveRobot(robotToRemove.Id);
  476. _robotService.RemoveRobot(robotToRemove.Id);
  477. //对所有产品也进行移除
  478. _productService.RemoveRobotPointFromProduct(robotToRemove.Id);
  479. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.移除完成, Duration = 1 });
  480. }
  481. }
  482. }
  483. async void ExecuteSaveRobotsCommand()
  484. {
  485. var view = new ShowMessage($"AAS", Lang.是否保存机器人);
  486. //show the dialog
  487. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  488. if (result != null && result is bool)
  489. {
  490. if (((bool)result))
  491. {
  492. foreach (var item in RobotList)
  493. {
  494. var robot = _robotService.GetRobot(item.Id);
  495. if (robot != null)
  496. {
  497. //更新连接参数
  498. robot.SafeZ = item.SafeZ;
  499. }
  500. _configService.AddOrUpdateRobot(item);
  501. }
  502. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  503. }
  504. }
  505. }
  506. #endregion
  507. #region 相机
  508. /// <summary>
  509. /// 保存所有相机
  510. /// </summary>
  511. async void ExecuteSaveCamerasCommand()
  512. {
  513. var view = new ShowMessage($"AAS", Lang.是否保存相机配置);
  514. //show the dialog
  515. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  516. if (result != null && result is bool)
  517. {
  518. if (((bool)result))
  519. {
  520. //management.DeviceConfig.Cameras = DeviceConfig.Cameras;
  521. //management.SaveCamerasDeviceConfig();
  522. //management.DeviceConfig.Cameras = DeviceConfig.Cameras;
  523. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  524. }
  525. }
  526. }
  527. /// <summary>
  528. /// 移除相机
  529. /// </summary>
  530. void ExecuteRemoveCameraCommand()
  531. {
  532. if (MessageBox.Show(Lang.是否移除相机, $"AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  533. {
  534. // 找到要删除的相机
  535. var cameraToRemove = CameraList.FirstOrDefault(r => r.Id == SelectCamera.Id);
  536. if (cameraToRemove != null)
  537. {
  538. _configService.RemoveCamera(cameraToRemove.Id);
  539. _cameraService.RemoveCamera(cameraToRemove.Id);
  540. var root = _configService.GetAllCameras();
  541. CameraList = new ObservableCollection<CameraInfo>(root);
  542. foreach (var item in CameraList)
  543. {
  544. _cameraService.UpdateCameraNumber(item.Id, item.CameraNo);
  545. }
  546. //对所有产品也进行移除
  547. _productService.RemoveCameraFromProduct(cameraToRemove.Id);
  548. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  549. }
  550. }
  551. }
  552. /// <summary>
  553. /// 编辑相机
  554. /// </summary>
  555. void ExecuteEditCameraCommand()
  556. {
  557. if (SelectCamera == null) return;
  558. bool isSelectedCameraBrandSucced = false;
  559. CameraBrand cameraBrand = CameraBrand.HikRobot_MVS;
  560. IDialogParameters parameters1 = new DialogParameters();
  561. parameters1.Add("CameraBrand", SelectCamera.CameraBrand);
  562. _dialogService.ShowDialog("SelectCameraBrand", parameters1, rst =>
  563. {
  564. if (rst.Result == ButtonResult.OK)
  565. {
  566. isSelectedCameraBrandSucced = true;
  567. cameraBrand = rst.Parameters.GetValue<CameraBrand>("CameraBrand");
  568. }
  569. else
  570. {
  571. return;
  572. }
  573. });
  574. if (!isSelectedCameraBrandSucced) { return; }
  575. (bool isInstalled, string version) result = (false, "");
  576. string softwarename = "";
  577. switch (cameraBrand)
  578. {
  579. case CameraBrand.HikRobot_MVS:
  580. softwarename = "MVS";
  581. result = MvCamera.CheckSoftwareInstalled();
  582. break;
  583. case CameraBrand.Basler_Pylon:
  584. softwarename = "Pylon 7";
  585. result = BaslerCamera.CheckSoftwareInstalled();
  586. break;
  587. case CameraBrand.IRayple:
  588. softwarename = "MV Viewer";
  589. result = AHuaCamera.CheckSoftwareInstalled();
  590. break;
  591. case CameraBrand.Cognex:
  592. softwarename = "Cognex VisionPro";
  593. result = CognexCamera.CheckSoftwareInstalled();
  594. break;
  595. case CameraBrand.Galaxy:
  596. softwarename = "Galaxy Viewer";
  597. result = GalaxyCamera.CheckSoftwareInstalled();
  598. break;
  599. case CameraBrand.Opt:
  600. softwarename = "OPT Camera Viewer";
  601. result = OptCamera.CheckSoftwareInstalled();
  602. break;
  603. default:
  604. break;
  605. }
  606. if (!result.isInstalled)
  607. {
  608. if (string.IsNullOrEmpty(result.version))
  609. {
  610. //未安装
  611. MessageBox.Show(Lang.CameraSDKCheck_Message1.Replace("{name}", softwarename), "AAS", MessageBoxButton.OK, MessageBoxImage.Asterisk);
  612. }
  613. else
  614. {
  615. //版本较低
  616. MessageBox.Show(Lang.CameraSDKCheck_Message2.Replace("{name}", softwarename).Replace("{version}", result.version).Replace("\\n", Environment.NewLine), "AAS", MessageBoxButton.OK, MessageBoxImage.Asterisk);
  617. }
  618. return;
  619. }
  620. //编辑前的相机名称
  621. string oldCameraName = SelectCamera.CameraName;
  622. IDialogParameters parameters = new DialogParameters();
  623. parameters.Add("CameraBrand", cameraBrand);
  624. parameters.Add("Camera", SelectCamera.Copy());
  625. _dialogService.ShowDialog("EditCamera", parameters, rst =>
  626. {
  627. //对话框关闭之后的回调函数,可以在这解析结果。
  628. ButtonResult result1 = rst.Result;
  629. if (result1 == ButtonResult.OK)
  630. {
  631. var param = rst.Parameters.GetValue<CameraInfo>("Camera");
  632. SelectCamera.CameraName = param.CameraName;
  633. SelectCamera.SerialNumber = param.SerialNumber;
  634. SelectCamera.Model = param.Model;
  635. SelectCamera.ManufacturerName = param.ManufacturerName;
  636. SelectCamera.CameraType = param.CameraType;
  637. SelectCamera.CameraBrand = param.CameraBrand;
  638. SelectCamera.CameraIp = param.CameraIp;
  639. _configService.AddOrUpdateCamera(SelectCamera);
  640. if (oldCameraName != param.CameraName)
  641. {
  642. //对所有产品也进行修改
  643. _productService.UpdateCameraNameInProduct(SelectCamera.Id, SelectCamera.CameraName);
  644. }
  645. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  646. }
  647. });
  648. }
  649. /// <summary>
  650. /// 增加相机
  651. /// </summary>
  652. void ExecuteAddCameraCommand()
  653. {
  654. bool isSelectedCameraBrandSucced = false;
  655. CameraBrand cameraBrand = CameraBrand.HikRobot_MVS;
  656. _dialogService.ShowDialog("SelectCameraBrand", rst =>
  657. {
  658. if (rst.Result == ButtonResult.OK)
  659. {
  660. isSelectedCameraBrandSucced = true;
  661. cameraBrand = rst.Parameters.GetValue<CameraBrand>("CameraBrand");
  662. }
  663. else
  664. {
  665. return;
  666. }
  667. });
  668. if (!isSelectedCameraBrandSucced) { return; }
  669. ;
  670. (bool isInstalled, string version) result = (false, "");
  671. string softwarename = "";
  672. switch (cameraBrand)
  673. {
  674. case CameraBrand.HikRobot_MVS:
  675. softwarename = "MVS";
  676. result = MvCamera.CheckSoftwareInstalled();
  677. break;
  678. case CameraBrand.Basler_Pylon:
  679. softwarename = "Pylon 7";
  680. result = BaslerCamera.CheckSoftwareInstalled();
  681. break;
  682. case CameraBrand.IRayple:
  683. softwarename = "MV Viewer";
  684. result = AHuaCamera.CheckSoftwareInstalled();
  685. break;
  686. case CameraBrand.Cognex:
  687. softwarename = "Cognex VisionPro";
  688. result = CognexCamera.CheckSoftwareInstalled();
  689. break;
  690. case CameraBrand.Galaxy:
  691. softwarename = "Galaxy Viewer";
  692. result = GalaxyCamera.CheckSoftwareInstalled();
  693. break;
  694. case CameraBrand.Opt:
  695. softwarename = "OPT Camera Viewer";
  696. result = OptCamera.CheckSoftwareInstalled();
  697. break;
  698. default:
  699. break;
  700. }
  701. if (!result.isInstalled)
  702. {
  703. if (string.IsNullOrEmpty(result.version))
  704. {
  705. //未安装
  706. MessageBox.Show(Lang.CameraSDKCheck_Message1.Replace("{name}", softwarename), "AAS", MessageBoxButton.OK, MessageBoxImage.Asterisk);
  707. }
  708. else
  709. {
  710. //版本较低
  711. MessageBox.Show(Lang.CameraSDKCheck_Message2.Replace("{name}", softwarename).Replace("{version}", result.version).Replace("\\n", Environment.NewLine), "AAS", MessageBoxButton.OK, MessageBoxImage.Asterisk);
  712. }
  713. return;
  714. }
  715. IDialogParameters parameters = new DialogParameters();
  716. parameters.Add("CameraBrand", cameraBrand);
  717. _dialogService.ShowDialog("AddCamera", parameters, rst =>
  718. {
  719. //对话框关闭之后的回调函数,可以在这解析结果。
  720. ButtonResult result1 = rst.Result;
  721. if (result1 == ButtonResult.OK)
  722. {
  723. var param = rst.Parameters.GetValue<CameraInfo>("Camera");
  724. var camera = new CameraInfo()
  725. {
  726. Id = param.Id,
  727. CameraName = param.CameraName,
  728. SerialNumber = param.SerialNumber,
  729. Model = param.Model,
  730. ManufacturerName = param.ManufacturerName,
  731. CameraType = param.CameraType,
  732. CameraBrand = param.CameraBrand,
  733. CameraIp = param.CameraIp
  734. };
  735. CameraList.Add(camera);
  736. _configService.AddOrUpdateCamera(camera);
  737. _cameraService.CreateCamera(camera.Id, camera);
  738. //对所有产品也进行添加
  739. _productService.AddCameraToProduct(camera.Id, camera.CameraName);
  740. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  741. }
  742. });
  743. }
  744. #endregion
  745. #region Feeder
  746. void ExecuteAddFeederCommand()
  747. {
  748. _dialogService.ShowDialog("AddFeeder", rst =>
  749. {
  750. //对话框关闭之后的回调函数,可以在这解析结果。
  751. ButtonResult result1 = rst.Result;
  752. if (result1 == ButtonResult.OK)
  753. {
  754. var param = rst.Parameters.GetValue<FeederInfo>("Feeder");
  755. var feeder = new FeederInfo()
  756. {
  757. Id = param.Id,
  758. FeederName = param.FeederName,
  759. ConnectType = param.ConnectType,
  760. FeederBrand = param.FeederBrand,
  761. IP = param.IP,
  762. Port = param.Port
  763. };
  764. FeederList.Add(feeder);
  765. _configService.AddOrUpdateFeeder(feeder);
  766. _feederService.CreateFeeder(feeder.Id, feeder.FeederNo, feeder.FeederName, feeder.IP, feeder.Port, feeder.FeederBrand);
  767. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.添加完成, Duration = 1 });
  768. }
  769. });
  770. }
  771. void ExecuteRemoveFeederCommand()
  772. {
  773. if (MessageBox.Show(Lang.是否移除Feeder, " AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  774. {
  775. // 找到要删除的机器人
  776. var feederToRemove = FeederList.FirstOrDefault(r => r.Id == SelectFeeder.Id);
  777. if (feederToRemove != null)
  778. {
  779. _configService.RemoveFeeder(feederToRemove.Id);
  780. _feederService.RemoveFeeder(feederToRemove.Id);
  781. var root = _configService.GetAllFeeders();
  782. FeederList = new ObservableCollection<FeederInfo>(root);
  783. foreach (var item in FeederList)
  784. {
  785. _feederService.UpdateFeederNo(item.Id, item.FeederNo);
  786. }
  787. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.移除完成, Duration = 1 });
  788. }
  789. }
  790. }
  791. async void ExecuteSaveFeedersCommand()
  792. {
  793. var view = new ShowMessage($"AAS", Lang.是否保存Feeder);
  794. //show the dialog
  795. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  796. if (result != null && result is bool)
  797. {
  798. if (((bool)result))
  799. {
  800. foreach (var item in FeederList)
  801. {
  802. _configService.AddOrUpdateFeeder(item);
  803. }
  804. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  805. }
  806. }
  807. }
  808. #endregion
  809. #region PLC
  810. /// <summary>
  811. /// 保存PLC
  812. /// </summary>
  813. async void ExecuteSavePlcsCommand()
  814. {
  815. var view = new ShowMessage($"AAS", Lang.是否保存PLC);
  816. //show the dialog
  817. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  818. if (result != null && result is bool)
  819. {
  820. if (((bool)result))
  821. {
  822. foreach (var item in PlcList)
  823. {
  824. _configService.AddOrUpdatePlc(item);
  825. }
  826. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  827. }
  828. }
  829. }
  830. /// <summary>
  831. /// 移除PLC
  832. /// </summary>
  833. void ExecuteRemovePlcCommand()
  834. {
  835. if (MessageBox.Show(Lang.是否移除PLC, $"AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  836. {
  837. // 找到要删除的plc
  838. var plcToRemove = PlcList.FirstOrDefault(r => r.Id == SelectPlc.Id);
  839. if (plcToRemove != null)
  840. {
  841. _configService.RemovePlc(plcToRemove.Id);
  842. _plcService.RemovePlc(plcToRemove.Id);
  843. var root = _configService.GetAllPlcs();
  844. PlcList = new ObservableCollection<PlcInfo>(root);
  845. foreach (var item in PlcList)
  846. {
  847. _plcService.UpdatePlcNumber(item.Id, item.Index);
  848. }
  849. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.移除完成, Duration = 1 });
  850. }
  851. }
  852. }
  853. /// <summary>
  854. /// 增加PLC
  855. /// </summary>
  856. void ExecuteAddPlcCommand()
  857. {
  858. _dialogService.ShowDialog("AddPlc", rst =>
  859. {
  860. //对话框关闭之后的回调函数,可以在这解析结果。
  861. ButtonResult result1 = rst.Result;
  862. if (result1 == ButtonResult.OK)
  863. {
  864. var param = rst.Parameters.GetValue<PlcInfo>("PLC");
  865. var plc = new PlcInfo()
  866. {
  867. Id = param.Id,
  868. Name = param.Name,
  869. CommunicationType = param.CommunicationType,
  870. EndpointUrl = param.EndpointUrl,
  871. };
  872. PlcList.Add(plc);
  873. _configService.AddOrUpdatePlc(plc);
  874. _plcService.CreatePlc(plc.Id, plc);
  875. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.添加完成, Duration = 1 });
  876. }
  877. });
  878. }
  879. #endregion
  880. #region Light handlers
  881. void ExecuteAddLightControllerCommand()
  882. {
  883. // open dialog to add light controller (similar to robots)
  884. _dialogService.ShowDialog("AddLightController", rst =>
  885. {
  886. if (rst.Result == ButtonResult.OK)
  887. {
  888. var param = rst.Parameters.GetValue<LightControllerConfig>("LightController");
  889. // assign id and defaults if needed
  890. if (param.Id == 0)
  891. param.Id = (LightControllerList?.Count ?? 0) + 1;
  892. LightControllerList.Add(param);
  893. _configService.AddOrUpdateLightController(param);
  894. SelectLightController = param;
  895. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "添加完成", Duration = 1 });
  896. }
  897. });
  898. }
  899. void ExecuteRemoveLightControllerCommand()
  900. {
  901. if (SelectLightController == null) return;
  902. if (MessageBox.Show("是否移除光源控制器?", "AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  903. {
  904. _configService.RemoveLightController(SelectLightController.Id);
  905. LightControllerList.Remove(SelectLightController);
  906. SelectLightController = null;
  907. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "移除完成", Duration = 1 });
  908. }
  909. }
  910. async void ExecuteSaveLightControllersCommand()
  911. {
  912. var view = new ShowMessage($"AAS", "是否保存光源控制器配置?");
  913. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  914. if (result != null && result is bool && (bool)result)
  915. {
  916. foreach (var item in LightControllerList)
  917. {
  918. _configService.AddOrUpdateLightController(item);
  919. }
  920. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "完成", Duration = 1 });
  921. }
  922. }
  923. /// <summary>
  924. /// 光源型号改变
  925. /// </summary>
  926. void ExecuteLightModelChangedCommand(object obj)
  927. {
  928. try
  929. {
  930. if (obj == null)
  931. {
  932. return;
  933. }
  934. if (obj is LightControllerConfig lc)
  935. {
  936. lc.Init();
  937. }
  938. }
  939. catch (Exception)
  940. {
  941. }
  942. }
  943. #endregion
  944. #region 螺丝供料器
  945. /// <summary>
  946. /// 保存所有螺丝供料器
  947. /// </summary>
  948. async void ExecuteSaveScrewFeedersCommand()
  949. {
  950. var view = new ShowMessage($"AAS", "是否保存螺丝供料器?");
  951. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  952. if (result != null && result is bool && (bool)result)
  953. {
  954. if (ScrewFeederList != null)
  955. {
  956. foreach (var item in ScrewFeederList)
  957. {
  958. try
  959. {
  960. _configService.AddOrUpdateScrewFeeder(item);
  961. }
  962. catch (Exception ex)
  963. {
  964. // 记录或通知错误,但继续处理其它项
  965. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  966. }
  967. }
  968. }
  969. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  970. }
  971. }
  972. /// <summary>
  973. /// 移除螺丝供料器
  974. /// </summary>
  975. void ExecuteRemoveScrewFeederCommand()
  976. {
  977. if (SelectScrewFeeder == null) return;
  978. if (MessageBox.Show("是否移除螺丝供料器?", "AAS", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  979. {
  980. try
  981. {
  982. var toRemove = ScrewFeederList?.FirstOrDefault(s => s.Id == SelectScrewFeeder.Id);
  983. if (toRemove != null)
  984. {
  985. _configService.RemoveScrewFeeder(toRemove.Id);
  986. // 重新加载列表以保持序号/配置一致(与其他类似实现一致)
  987. var root = _configService.GetAllScrewFeeders();
  988. ScrewFeederList = new ObservableCollection<ScrewFeederInfo>(root);
  989. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "移除完成", Duration = 1 });
  990. }
  991. }
  992. catch (Exception ex)
  993. {
  994. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  995. }
  996. }
  997. }
  998. /// <summary>
  999. /// 添加螺丝供料器
  1000. /// </summary>
  1001. void ExecuteAddScrewFeederCommand()
  1002. {
  1003. _dialogService.ShowDialog("AddScrewFeeder", rst =>
  1004. {
  1005. ButtonResult result1 = rst.Result;
  1006. if (result1 == ButtonResult.OK)
  1007. {
  1008. var param = rst.Parameters.GetValue<ScrewFeederInfo>("ScrewFeeder");
  1009. if (param != null)
  1010. {
  1011. if (ScrewFeederList == null)
  1012. {
  1013. ScrewFeederList = new ObservableCollection<ScrewFeederInfo>();
  1014. }
  1015. try
  1016. {
  1017. param = _configService.AddOrUpdateScrewFeeder(param);
  1018. // 直接添加对话框返回的对象(也可以根据需要映射字段)
  1019. ScrewFeederList.Add(param);
  1020. }
  1021. catch (Exception ex)
  1022. {
  1023. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1024. }
  1025. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "添加完成", Duration = 1 });
  1026. }
  1027. }
  1028. });
  1029. }
  1030. #endregion
  1031. #region 通讯
  1032. async void ExecuteSaveCommunicateCommand(string parm)
  1033. {
  1034. if (parm == "TCPIP")
  1035. {
  1036. var view = new ShowMessage($"AAS", Lang.是否保存后台TCP通讯配置);
  1037. //show the dialog
  1038. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  1039. if (result != null && result is bool)
  1040. {
  1041. if (((bool)result))
  1042. {
  1043. _configService.SetBgTcp(BgTcp);
  1044. _configService.SaveBgTcp();
  1045. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  1046. }
  1047. }
  1048. }
  1049. else if (parm == "ModbusTCP")
  1050. {
  1051. var view = new ShowMessage($"AAS", Lang.是否保存后台ModbusTCP通讯配置);
  1052. //show the dialog
  1053. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  1054. if (result != null && result is bool)
  1055. {
  1056. if (((bool)result))
  1057. {
  1058. _configService.SetBgModbusTcp(BgModbus);
  1059. _configService.SaveBgModbusTcp();
  1060. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  1061. }
  1062. }
  1063. }
  1064. else
  1065. {
  1066. return;
  1067. }
  1068. }
  1069. #endregion
  1070. #region 语言
  1071. void ExecuteSelectLanguageCommand(string language)
  1072. {
  1073. if (string.Equals(language, "ChineseSimplified"))
  1074. {
  1075. CurrentLanguage = Enums.Language.ChineseSimplified;
  1076. _configService.SetCurrentLanguage(CurrentLanguage);
  1077. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.5 });
  1078. }
  1079. else if (string.Equals(language, "ChineseTraditional"))
  1080. {
  1081. CurrentLanguage = Enums.Language.ChineseTraditional;
  1082. _configService.SetCurrentLanguage(CurrentLanguage);
  1083. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.5 });
  1084. }
  1085. else if (string.Equals(language, "English"))
  1086. {
  1087. CurrentLanguage = Enums.Language.English;
  1088. _configService.SetCurrentLanguage(CurrentLanguage);
  1089. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.5 });
  1090. }
  1091. else if (string.Equals(language, "Japanese"))
  1092. {
  1093. CurrentLanguage = Enums.Language.Japanese;
  1094. _configService.SetCurrentLanguage(CurrentLanguage);
  1095. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.5 });
  1096. }
  1097. }
  1098. #endregion
  1099. private void OnLoad()
  1100. {
  1101. try
  1102. {
  1103. if (!File.Exists(FilePath.PressureParamterConfigPath))
  1104. {
  1105. List<PressureModel> models = new List<PressureModel>();
  1106. models.Add(new PressureModel());
  1107. models.Add(new PressureModel());
  1108. models.Add(new PressureModel());
  1109. FileHelper.WriteJsonFile(models, FilePath.PressureParamterConfigPath);
  1110. }
  1111. var pressureParam = FileHelper.ReadJsonFile<List<PressureModel>>(FilePath.PressureParamterConfigPath);
  1112. this.AssemblyPressureConfig = pressureParam[0];
  1113. this.DownPressureConfig = pressureParam[1];
  1114. }
  1115. catch (Exception)
  1116. {
  1117. }
  1118. }
  1119. private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
  1120. {
  1121. switch (e.PropertyName)
  1122. {
  1123. case "Title1":
  1124. Application.Current.Resources["Font.Size.Title1"] = Title1;
  1125. break;
  1126. case "Title2":
  1127. Application.Current.Resources["Font.Size.Title2"] = Title2;
  1128. break;
  1129. case "Title3":
  1130. Application.Current.Resources["Font.Size.Title3"] = Title3;
  1131. break;
  1132. case "Title4":
  1133. Application.Current.Resources["Font.Size.Title4"] = Title4;
  1134. break;
  1135. case "Body1":
  1136. Application.Current.Resources["Font.Size.Body1"] = Body1;
  1137. break;
  1138. case "Body2":
  1139. Application.Current.Resources["Font.Size.Body2"] = Body2;
  1140. break;
  1141. case "Body3":
  1142. Application.Current.Resources["Font.Size.Body3"] = Body3;
  1143. break;
  1144. case "Body4":
  1145. Application.Current.Resources["Font.Size.Body4"] = Body4;
  1146. break;
  1147. case "Captions1":
  1148. Application.Current.Resources["Font.Size.Captions1"] = Captions1;
  1149. break;
  1150. case "Captions2":
  1151. Application.Current.Resources["Font.Size.Captions2"] = Captions2;
  1152. break;
  1153. case "Captions3":
  1154. Application.Current.Resources["Font.Size.Captions3"] = Captions3;
  1155. break;
  1156. case "Captions4":
  1157. Application.Current.Resources["Font.Size.Captions4"] = Captions4;
  1158. break;
  1159. default:
  1160. break;
  1161. }
  1162. }
  1163. #endregion
  1164. #region 继承
  1165. /// <summary>
  1166. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  1167. /// </summary>
  1168. /// <param name="navigationContext"></param>
  1169. /// <param name="continuationCallback"></param>
  1170. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  1171. {
  1172. continuationCallback(true);
  1173. }
  1174. /// <summary>
  1175. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  1176. /// </summary>
  1177. /// <param name="navigationContext"></param>
  1178. /// <exception cref="NotImplementedException"></exception>
  1179. public async void OnNavigatedTo(NavigationContext navigationContext)
  1180. {
  1181. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  1182. {
  1183. IsAllowEdit = false;
  1184. }
  1185. else
  1186. {
  1187. IsAllowEdit = true;
  1188. }
  1189. if (RobotList == null)
  1190. {
  1191. RobotList = new ObservableCollection<RobotInfo>();
  1192. var robots = _configService.GetAllRobots();
  1193. foreach (var robot in robots)
  1194. {
  1195. RobotList.Add(robot);
  1196. }
  1197. }
  1198. if (CameraList == null)
  1199. {
  1200. CameraList = new ObservableCollection<CameraInfo>();
  1201. var cameras = _configService.GetAllCameras();
  1202. foreach (var camera in cameras)
  1203. {
  1204. CameraList.Add(camera);
  1205. }
  1206. }
  1207. if (FeederList == null)
  1208. {
  1209. FeederList = new ObservableCollection<FeederInfo>();
  1210. var feeders = _configService.GetAllFeeders();
  1211. foreach (var feeder in feeders)
  1212. {
  1213. FeederList.Add(feeder);
  1214. }
  1215. }
  1216. if (PlcList == null)
  1217. {
  1218. PlcList = new ObservableCollection<PlcInfo>();
  1219. var plcs = _configService.GetAllPlcs();
  1220. foreach (var plc in plcs)
  1221. {
  1222. PlcList.Add(plc);
  1223. }
  1224. }
  1225. // load light controllers
  1226. if (LightControllerList == null)
  1227. {
  1228. LightControllerList = new ObservableCollection<LightControllerConfig>();
  1229. var controllers = _configService.GetAllLightControllers();
  1230. foreach (var ctrl in controllers)
  1231. {
  1232. LightControllerList.Add(ctrl);
  1233. }
  1234. }
  1235. if (BgTcp == null)
  1236. {
  1237. BgTcp = _configService.GetBgTcp();
  1238. }
  1239. if (BgModbus == null)
  1240. {
  1241. BgModbus = _configService.GetBgModbusTcp();
  1242. }
  1243. if (DeviceInfoList == null || DeviceInfoList.Count == 0)
  1244. {
  1245. // load device info list (支持多设备)
  1246. var devices = _configService.GetAllDeviceInfos();
  1247. DeviceInfoList = new ObservableCollection<DeviceInfo>(devices);
  1248. // 默认选中第一个设备
  1249. if (DeviceInfoList.Count > 0)
  1250. {
  1251. SelectDeviceInfo = DeviceInfoList[0];
  1252. }
  1253. }
  1254. if (CardReaderConfig == null)
  1255. {
  1256. // load card reader config
  1257. CardReaderConfig = _configService.GetCardReaderConfig();
  1258. }
  1259. CurrentLanguage = _configService.GetSystemConfiguration().CurrentLanguage;
  1260. // load system parameters into UI
  1261. var sys = _configService.GetSystemConfiguration();
  1262. if (sys != null)
  1263. {
  1264. WorkMode = sys.WorkMode;
  1265. PickPointNum = sys.PickPointNum;
  1266. DowmCameraNum = sys.DowmCameraNum;
  1267. WorkMode = sys.WorkMode;
  1268. PickPointNum = sys.PickPointNum;
  1269. DowmCameraNum = sys.DowmCameraNum;
  1270. UpCameraPickNum = sys.UpCameraPickNum;
  1271. UpCameraPutNum = sys.UpCameraPutNum;
  1272. UPCameracheckNum = sys.UPCameracheckNum;
  1273. FixedCameraPickNum = sys.FixedCameraPickNum;
  1274. FixedCameraPutNum = sys.FixedCameraPutNum;
  1275. FixedCameracheckNum = sys.FixedCameracheckNum;
  1276. ScrewTeachNum = sys.ScrewTeachNum;
  1277. PressureSensorConfig = sys.PressureSensorConfig;
  1278. }
  1279. if (isFirstLoad)
  1280. {
  1281. Title1 = (double)Application.Current.Resources["Font.Size.Title1"];
  1282. Title2 = (double)Application.Current.Resources["Font.Size.Title2"];
  1283. Title3 = (double)Application.Current.Resources["Font.Size.Title3"];
  1284. Title4 = (double)Application.Current.Resources["Font.Size.Title4"];
  1285. Body1 = (double)Application.Current.Resources["Font.Size.Body1"];
  1286. Body2 = (double)Application.Current.Resources["Font.Size.Body2"];
  1287. Body3 = (double)Application.Current.Resources["Font.Size.Body3"];
  1288. Body4 = (double)Application.Current.Resources["Font.Size.Body4"];
  1289. Captions1 = (double)Application.Current.Resources["Font.Size.Captions1"];
  1290. Captions2 = (double)Application.Current.Resources["Font.Size.Captions2"];
  1291. Captions3 = (double)Application.Current.Resources["Font.Size.Captions3"];
  1292. Captions4 = (double)Application.Current.Resources["Font.Size.Captions4"];
  1293. this.PropertyChanged += OnPropertyChanged;
  1294. isFirstLoad = false;
  1295. }
  1296. }
  1297. /// <summary>
  1298. /// 是否允许导航到此视图模型。
  1299. /// </summary>
  1300. /// <param name="navigationContext"></param>
  1301. /// <returns></returns>
  1302. public bool IsNavigationTarget(NavigationContext navigationContext)
  1303. {
  1304. return true;
  1305. }
  1306. /// <summary>
  1307. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  1308. /// </summary>
  1309. /// <param name="navigationContext"></param>
  1310. public void OnNavigatedFrom(NavigationContext navigationContext)
  1311. {
  1312. }
  1313. #endregion
  1314. private bool _IsAllowEdit = false;
  1315. public bool IsAllowEdit
  1316. {
  1317. get { return _IsAllowEdit; }
  1318. set { SetProperty(ref _IsAllowEdit, value); }
  1319. }
  1320. private void LoginChange(Models.User user)
  1321. {
  1322. if (user.userPart == Enums.UserPart.Operator)
  1323. {
  1324. IsAllowEdit = false;
  1325. }
  1326. else
  1327. {
  1328. IsAllowEdit = true;
  1329. }
  1330. }
  1331. #region 系统参数
  1332. private Int16 _WorkMode = 0;
  1333. /// <summary>
  1334. /// 工作模式 =1 拍1打1 =2 拍1打所有
  1335. /// </summary>
  1336. public Int16 WorkMode
  1337. {
  1338. get { return _WorkMode; }
  1339. set { SetProperty(ref _WorkMode, value); }
  1340. }
  1341. private Int16 _PickPointNum = 0;
  1342. /// <summary>
  1343. /// 取料位置 =0 不需要取料 =1 需要取料
  1344. /// </summary>
  1345. public Int16 PickPointNum
  1346. {
  1347. get { return _PickPointNum; }
  1348. set { SetProperty(ref _PickPointNum, value); }
  1349. }
  1350. private Int16 _DowmCameraNum = 0;
  1351. /// <summary>
  1352. /// 下相机拍照次数
  1353. /// </summary>
  1354. public Int16 DowmCameraNum
  1355. {
  1356. get => _DowmCameraNum;
  1357. set => SetProperty(ref _DowmCameraNum, value);
  1358. }
  1359. private Int16 _UpCameraPickNum = 0;
  1360. /// <summary>
  1361. /// 上相机取料拍照次数
  1362. /// </summary>
  1363. public Int16 UpCameraPickNum
  1364. {
  1365. get => _UpCameraPickNum;
  1366. set => SetProperty(ref _UpCameraPickNum, value);
  1367. }
  1368. private Int16 _UpCameraPutNum = 0;
  1369. /// <summary>
  1370. /// 上相机放料拍照次数
  1371. /// </summary>
  1372. public Int16 UpCameraPutNum
  1373. {
  1374. get => _UpCameraPutNum;
  1375. set => SetProperty(ref _UpCameraPutNum, value);
  1376. }
  1377. private Int16 _UPCameracheckNum = 0;
  1378. /// <summary>
  1379. /// 上相机锁附/组装次数
  1380. /// </summary>
  1381. public Int16 UPCameracheckNum
  1382. {
  1383. get => _UPCameracheckNum;
  1384. set => SetProperty(ref _UPCameracheckNum, value);
  1385. }
  1386. private Int16 _FixedCameraPickNum = 0;
  1387. /// <summary>
  1388. /// 取料固定位置拍产品相机数量
  1389. /// </summary>
  1390. public Int16 FixedCameraPickNum
  1391. {
  1392. get => _FixedCameraPickNum;
  1393. set => SetProperty(ref _FixedCameraPickNum, value);
  1394. }
  1395. private Int16 _FixedCameraPutNum = 0;
  1396. /// <summary>
  1397. /// 锁附/组装固定位置拍产品相机数量
  1398. /// </summary>
  1399. public Int16 FixedCameraPutNum
  1400. {
  1401. get => _FixedCameraPutNum;
  1402. set => SetProperty(ref _FixedCameraPutNum, value);
  1403. }
  1404. private Int16 _FixedCameracheckNum = 0;
  1405. /// <summary>
  1406. /// 固定相机检测锁附/组装次数
  1407. /// </summary>
  1408. public Int16 FixedCameracheckNum
  1409. {
  1410. get => _FixedCameracheckNum;
  1411. set => SetProperty(ref _FixedCameracheckNum, value);
  1412. }
  1413. private Int16 _ScrewTeachNum = 0;
  1414. /// <summary>
  1415. /// 披头矫正的模式,1:初始化时矫正,2:每个产品矫正一次,3:每颗螺丝矫正一次
  1416. /// </summary>
  1417. public Int16 ScrewTeachNum
  1418. {
  1419. get => _ScrewTeachNum;
  1420. set => SetProperty(ref _ScrewTeachNum, value);
  1421. }
  1422. private ObservableCollection<PressureSensorSettings> _PressureSensorConfig;
  1423. /// <summary>
  1424. /// 压力传感器配置列表
  1425. /// </summary>
  1426. public ObservableCollection<PressureSensorSettings> PressureSensorConfig
  1427. {
  1428. get { return _PressureSensorConfig; }
  1429. set { SetProperty(ref _PressureSensorConfig, value); }
  1430. }
  1431. async void ExecuteSaveSystemParametersCommand()
  1432. {
  1433. var view = new ShowMessage($"AAS", "是否保存系统参数?");
  1434. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  1435. if (result != null && result is bool && (bool)result)
  1436. {
  1437. try
  1438. {
  1439. SavePressureConfig();
  1440. var sysConfig = _configService.GetSystemConfiguration();
  1441. sysConfig.WorkMode = WorkMode;
  1442. sysConfig.PickPointNum = PickPointNum;
  1443. sysConfig.DowmCameraNum = DowmCameraNum;
  1444. sysConfig.UpCameraPickNum = UpCameraPickNum;
  1445. sysConfig.UpCameraPutNum = UpCameraPutNum;
  1446. sysConfig.UPCameracheckNum = UPCameracheckNum;
  1447. sysConfig.FixedCameraPickNum = FixedCameraPickNum;
  1448. sysConfig.FixedCameraPutNum = FixedCameraPutNum;
  1449. sysConfig.FixedCameracheckNum = FixedCameracheckNum;
  1450. sysConfig.ScrewTeachNum = ScrewTeachNum;
  1451. sysConfig.PressureSensorConfig = PressureSensorConfig;
  1452. _configService.SaveSystemConfiguration(sysConfig);
  1453. await management.WriteSystemParameterToPlcAsync(sysConfig);
  1454. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  1455. }
  1456. catch (Exception ex)
  1457. {
  1458. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1459. }
  1460. }
  1461. }
  1462. /// <summary>
  1463. /// 选择压力曲线保存路径
  1464. /// </summary>
  1465. /// <param name="parameter"></param>
  1466. void ExecuteBrowsePressureCurvePathCommand(object parameter)
  1467. {
  1468. if (parameter is PressureSensorSettings sensorSettings)
  1469. {
  1470. var dialog = new System.Windows.Forms.FolderBrowserDialog();
  1471. System.Windows.Forms.DialogResult result = dialog.ShowDialog();
  1472. if (result == System.Windows.Forms.DialogResult.OK)
  1473. {
  1474. sensorSettings.PressureCurvePath = dialog.SelectedPath;
  1475. }
  1476. }
  1477. }
  1478. private void SavePressureConfig()
  1479. {
  1480. List<PressureModel> models = new List<PressureModel>();
  1481. models.Add(AssemblyPressureConfig);
  1482. models.Add(DownPressureConfig);
  1483. FileHelper.WriteJsonFile(models, FilePath.PressureParamterConfigPath);
  1484. }
  1485. /// <summary>
  1486. /// 保存读卡器配置
  1487. /// </summary>
  1488. void ExecuteSaveCardReaderConfigCommand()
  1489. {
  1490. try
  1491. {
  1492. _configService.SaveCardReaderConfig(CardReaderConfig);
  1493. }
  1494. catch (Exception ex)
  1495. {
  1496. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1497. }
  1498. }
  1499. #endregion
  1500. #region 设备信息
  1501. /// <summary>
  1502. /// 保存当前选中的设备信息
  1503. /// </summary>
  1504. void ExecuteSaveDeviceInfoCommand()
  1505. {
  1506. try
  1507. {
  1508. if (DeviceInfoList != null && DeviceInfoList.Count > 0)
  1509. {
  1510. // 保存所有设备信息
  1511. _configService.SaveAllDeviceInfos(DeviceInfoList);
  1512. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  1513. }
  1514. }
  1515. catch (Exception ex)
  1516. {
  1517. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1518. }
  1519. }
  1520. /// <summary>
  1521. /// 添加新设备
  1522. /// </summary>
  1523. void ExecuteAddDeviceInfoCommand()
  1524. {
  1525. try
  1526. {
  1527. var newDevice = _configService.AddDeviceInfo();
  1528. if (DeviceInfoList == null)
  1529. {
  1530. DeviceInfoList = new ObservableCollection<DeviceInfo>();
  1531. }
  1532. DeviceInfoList.Add(newDevice);
  1533. SelectDeviceInfo = newDevice;
  1534. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  1535. }
  1536. catch (Exception ex)
  1537. {
  1538. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1539. }
  1540. }
  1541. /// <summary>
  1542. /// 删除当前选中的设备
  1543. /// </summary>
  1544. void ExecuteRemoveDeviceInfoCommand()
  1545. {
  1546. try
  1547. {
  1548. if (SelectDeviceInfo != null && DeviceInfoList != null && DeviceInfoList.Count > 1)
  1549. {
  1550. var deviceNo = SelectDeviceInfo.DeviceNo;
  1551. if (_configService.RemoveDeviceInfo(deviceNo))
  1552. {
  1553. DeviceInfoList.Remove(SelectDeviceInfo);
  1554. SelectDeviceInfo = DeviceInfoList.FirstOrDefault();
  1555. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  1556. }
  1557. }
  1558. else
  1559. {
  1560. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "至少保留一个设备", Duration = 2 });
  1561. }
  1562. }
  1563. catch (Exception ex)
  1564. {
  1565. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1566. }
  1567. }
  1568. /// <summary>
  1569. /// 选择CSV存储路径
  1570. /// </summary>
  1571. void ExecuteSelectSaveImagePathCommand()
  1572. {
  1573. if (SelectDeviceInfo == null)
  1574. {
  1575. return;
  1576. }
  1577. using (System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog())
  1578. {
  1579. // Optional: set the initial directory
  1580. //dialog.SelectedPath = @"C:\";
  1581. // Show the folder browser dialog
  1582. System.Windows.Forms.DialogResult result = dialog.ShowDialog();
  1583. if (result == System.Windows.Forms.DialogResult.OK)
  1584. {
  1585. SelectDeviceInfo.SaveCsvPath = dialog.SelectedPath;
  1586. }
  1587. }
  1588. }
  1589. #endregion
  1590. }
  1591. }