SettingViewModel.cs 63 KB

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