SettingViewModel.cs 63 KB

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