ParamPageViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. using AutoMapper;
  2. using HandyControl.Controls;
  3. using LogoForceTestApp.Core;
  4. using LogoForceTestApp.Core.Mvvm;
  5. using LogoForceTestApp.Modules.MainModule.Models;
  6. using LogoForceTestApp.Modules.MainModule.Views;
  7. using Newtonsoft.Json;
  8. using Prism.Commands;
  9. using Prism.Events;
  10. using Prism.Regions;
  11. using PropertyChanged;
  12. using Repository;
  13. using Repository.Entiies;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.IO;
  17. using System.Management;
  18. using System.Net.NetworkInformation;
  19. using System.Runtime.CompilerServices;
  20. using System.Security.Cryptography;
  21. using System.Threading.Tasks;
  22. using System.Windows.Forms;
  23. using Team.Utility;
  24. namespace LogoForceTestApp.Modules.MainModule.ViewModels
  25. {
  26. public class ParamPageViewModel: RegionViewModelBase
  27. {
  28. private readonly IMapper _mapper;
  29. private readonly IEventAggregator _eventAggregator;
  30. private readonly IWritableOptions<AppSettings> _appSettings;
  31. private readonly IRepository _repository;
  32. private string _csvPath;
  33. public string CSVPath
  34. {
  35. get=>_csvPath;
  36. set {
  37. SetProperty(ref _csvPath, value);
  38. SetChanged();
  39. }
  40. }
  41. private string _imageSavePath;
  42. public string ImageSavePath
  43. {
  44. get => _imageSavePath;
  45. set
  46. {
  47. SetProperty(ref _imageSavePath, value);
  48. SetChanged();
  49. }
  50. }
  51. private bool _selected;
  52. public bool Selected
  53. {
  54. get => _selected;
  55. set
  56. {
  57. SetProperty(ref _selected, value);
  58. SetChanged();
  59. }
  60. }
  61. public bool TraceDisable { get; set; }
  62. public bool UnUseOee { get; set; }
  63. //[OnChangedMethod("SetChanged")]
  64. public string OeeIp { get; set; }
  65. //[OnChangedMethod("SetChanged")]
  66. public int OeePort { get; set; }
  67. //[OnChangedMethod("SetChanged")]
  68. public int WaitPartDuration { get; set; } = 1;
  69. public string Site { get; set; }
  70. public int StopDuration { get; set; } = 5;
  71. public string Floor { get; set; }
  72. public string Product { get; set; }
  73. public string Line { get; set; }
  74. public string ProcessOee { get; set; }
  75. public string TerminalId { get; set; }
  76. public string MachineId { get; set; }
  77. public string MacAddr { get; set; }
  78. public List<string> AddressSource { get; set; }
  79. public string Status { get; set; }
  80. public string OpId { get; set; }
  81. public string IpAddr { get; set; }
  82. public ParamDto Param { get; set; }
  83. public string OeePath { get; set; }
  84. public DelegateCommand SaveTraceParamCommand { get; set; }
  85. public DelegateCommand SelectFolderCommand { get; set; }
  86. public DelegateCommand SelectImageFolderCommand { get; set; }
  87. public DelegateCommand SaveOtherCommand { get; set; }
  88. public DelegateCommand SaveOeeCommand { get; set; }
  89. public DelegateCommand SelectOeeFolderCommand { get; set; }
  90. public DelegateCommand DebugCommand { get; set; }
  91. public ParamPageViewModel(IMapper mapper,IEventAggregator eventAggregator,
  92. IWritableOptions<AppSettings> appSettings,IRepository repository,IRegionManager regionManager
  93. ):base(regionManager)
  94. {
  95. //AddressSource=new List<string>();
  96. AddressSource = GetMacAddress();
  97. _mapper = mapper;
  98. _eventAggregator = eventAggregator;
  99. _appSettings = appSettings;
  100. _repository = repository;
  101. CSVPath = _appSettings.Value.CSVPath;
  102. Selected = _appSettings.Value.UnUseUpload;
  103. TraceDisable = _appSettings.Value.TraceDisable;
  104. ImageSavePath = _appSettings.Value.ImageSavePath;
  105. Build = _appSettings.Value.Builds;
  106. Project=_appSettings.Value.Project;
  107. Process=_appSettings.Value.Process;
  108. AIM_UNIQUE=_appSettings.Value.AIM_UNIQUE;
  109. AimName = _appSettings.Value.AimName;
  110. INSPECTION_PROGRAM_NAME = _appSettings.Value.INSPECTION_PROGRAM_NAME;
  111. Part=_appSettings.Value.Part;
  112. SaveTraceParamCommand = new DelegateCommand(SaveTraceParam);
  113. SaveOtherCommand = new DelegateCommand(SaveOther);
  114. SaveOeeCommand = new DelegateCommand(SaveOeeConfig);
  115. SelectOeeFolderCommand = new DelegateCommand(SelectOeeFolder);
  116. DebugCommand = new DelegateCommand(GoOeeDebugPage);
  117. var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "param.json");
  118. if (File.Exists(filePath))
  119. {
  120. using var stream = new StreamReader(filePath);
  121. var oeejson= stream.ReadToEnd();
  122. var param = JsonConvert.DeserializeObject<Param>(oeejson);
  123. Param= _mapper.Map<ParamDto>(param);
  124. Param.PropertyChanged += Param_PropertyChanged;
  125. }
  126. else
  127. {
  128. Param = new ParamDto
  129. {
  130. station_id = "LkYC_BO7_2F_01_002_TRACE",
  131. fixture_id = "H-76HO-SMA40-2200-A-00003",
  132. cavity_id = "\"1/2/3/4\"",
  133. line_id = "B07-2F-01",
  134. station_vendor = "Team",
  135. test = "Logo 30N Non-Destructive Test",
  136. OpId= "C10088888"
  137. };
  138. Param.PropertyChanged += Param_PropertyChanged;
  139. }
  140. if (string.IsNullOrWhiteSpace(Param.OpId))
  141. {
  142. Param.OpId = "C10088888";
  143. }
  144. if (string.IsNullOrWhiteSpace(Param.DownLimit))
  145. {
  146. Param.DownLimit = "30";
  147. }
  148. if (string.IsNullOrWhiteSpace(Param.UpperLimit))
  149. {
  150. Param.UpperLimit = "60";
  151. }
  152. var oeePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "oee.json");
  153. if (File.Exists(oeePath))
  154. {
  155. var streamOee = new StreamReader(oeePath);
  156. var json = streamOee.ReadToEnd();
  157. var oeeConfig=JsonConvert.DeserializeObject<OeeConfig>(json);
  158. UnUseOee = oeeConfig.UnUse;
  159. OeeIp = oeeConfig.Ip;
  160. OeePort = oeeConfig.Port;
  161. WaitPartDuration = oeeConfig.WaitPartDuration;
  162. StopDuration = oeeConfig.StopDuration;
  163. Site = oeeConfig.Site;
  164. Floor = oeeConfig.Floor;
  165. Product= oeeConfig.Product;
  166. Line = oeeConfig.Line;
  167. ProcessOee = oeeConfig.Process;
  168. TerminalId = oeeConfig.TerminalId;
  169. MachineId = oeeConfig.MachineId;
  170. MacAddr = oeeConfig.Macaddr;
  171. IpAddr = oeeConfig.Ipaddr;
  172. OeePath = oeeConfig.OeePath;
  173. }
  174. SelectFolderCommand = new DelegateCommand(SelectFolder);
  175. SelectImageFolderCommand = new DelegateCommand(SelectImageFolder);
  176. }
  177. private void GoOeeDebugPage()
  178. {
  179. RegionManager.RequestNavigate(RegionNames.ContentRegion, nameof(OeeDebugPage));
  180. }
  181. private void SelectOeeFolder()
  182. {
  183. FolderBrowserDialog dilog = new()
  184. {
  185. Description = "请选择文件夹"
  186. };
  187. if (dilog.ShowDialog() is System.Windows.Forms.DialogResult.OK or System.Windows.Forms.DialogResult.Yes)
  188. {
  189. OeePath = dilog.SelectedPath;
  190. }
  191. }
  192. public static List<string> GetMacAddress()
  193. {
  194. List<string> macAddressList = new();
  195. string MoAddress = " ";
  196. ManagementClass mc = new("Win32_NetworkAdapterConfiguration");
  197. ManagementObjectCollection moc2 = mc.GetInstances();
  198. foreach (ManagementObject mo in moc2)
  199. {
  200. if ((bool)mo["IPEnabled"] == true)
  201. {
  202. MoAddress = mo["MacAddress"].ToString();
  203. MoAddress.Replace(":", "-");
  204. macAddressList.Add(MoAddress);
  205. }
  206. mo.Dispose();
  207. }
  208. // return MoAddress.ToString()
  209. return macAddressList;
  210. }
  211. public static List<string> GetIpAddress()
  212. {
  213. NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
  214. List<string> ipList = new();
  215. foreach (NetworkInterface adapter in nics)
  216. {
  217. if (adapter.OperationalStatus == OperationalStatus.Up)
  218. {
  219. foreach (UnicastIPAddressInformation ip in adapter.GetIPProperties().UnicastAddresses)
  220. {
  221. if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
  222. {
  223. ipList.Add(ip.Address.ToString());
  224. }
  225. }
  226. }
  227. }
  228. return ipList;
  229. }
  230. private void SaveOeeConfig()
  231. {
  232. if (!Directory.Exists(OeePath))
  233. {
  234. Growl.Error("保存路径不存在,请重新选择");
  235. return;
  236. }
  237. if (!GetMacAddress().Contains(MacAddr))
  238. {
  239. Growl.Error("mac地址不正确,请重新输入");
  240. return;
  241. }
  242. var oeeConfig = new OeeConfig
  243. {
  244. Ip = OeeIp,
  245. Port = OeePort,
  246. WaitPartDuration = WaitPartDuration,
  247. StopDuration = StopDuration,
  248. Site = Site,
  249. Floor = Floor,
  250. Product = Product,
  251. Line = Line,
  252. Vendor = "Team",
  253. Process = ProcessOee,
  254. TerminalId = TerminalId,
  255. MachineId = MachineId,
  256. Op_id = Param.OpId,
  257. Ipaddr = IpAddr,
  258. Macaddr = MacAddr,
  259. OeePath = OeePath,
  260. UnUse = UnUseOee
  261. };
  262. var config = JsonConvert.SerializeObject(oeeConfig, Formatting.Indented);
  263. var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
  264. "oee.json");
  265. using var stream = new StreamWriter(filePath);
  266. stream.Write(config);
  267. Growl.Info("保存配置完成");
  268. _eventAggregator.GetEvent<OeeConfigChangedPublish>().Publish(oeeConfig);
  269. return;
  270. }
  271. private bool _loaded;
  272. public override void OnNavigatedFrom(NavigationContext navigationContext)
  273. {
  274. _loaded=false;
  275. base.OnNavigatedFrom(navigationContext);
  276. }
  277. public override void OnNavigatedTo(NavigationContext navigationContext)
  278. {
  279. _loaded = true;
  280. base.OnNavigatedTo(navigationContext);
  281. }
  282. private void LoginSuccess(bool obj)
  283. {
  284. if (_loaded)
  285. {
  286. RegionManager.RequestNavigate(RegionNames.ContentRegion,nameof(LoginPage));
  287. }
  288. }
  289. private readonly List<string> _storageChangedList = new();
  290. private void Param_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  291. {
  292. _storageChangedList.Add(e.PropertyName);
  293. SetChanged(e.PropertyName);
  294. }
  295. private void SetChanged([CallerMemberName] string propertyName = null)
  296. {
  297. _repository.Add(new ParamLog
  298. {
  299. OperateAction = OperateAction.ChangeParam,
  300. Content = $"{propertyName}参数修改",
  301. Id=Guid.NewGuid(),
  302. Occured=DateTime.Now,
  303. OperateObject=propertyName,
  304. UserName=CurrentUser.CurrentUserName
  305. });
  306. }
  307. private async Task HasSaved(string propertyName)
  308. {
  309. await _repository.AddAsync(new ParamLog
  310. {
  311. OperateAction = OperateAction.ChangeParam,
  312. Content = $"{propertyName}参数修改并保存成功",
  313. Id = Guid.NewGuid(),
  314. Occured = DateTime.Now,
  315. OperateObject = propertyName,
  316. UserName = CurrentUser.CurrentUserName
  317. });
  318. }
  319. private void SelectFolder()
  320. {
  321. System.Windows.Forms.FolderBrowserDialog dilog = new System.Windows.Forms.FolderBrowserDialog
  322. {
  323. Description = "请选择文件夹"
  324. };
  325. if (dilog.ShowDialog() == System.Windows.Forms.DialogResult.OK || dilog.ShowDialog() == System.Windows.Forms.DialogResult.Yes)
  326. {
  327. CSVPath = dilog.SelectedPath;
  328. }
  329. }
  330. private void SelectImageFolder()
  331. {
  332. System.Windows.Forms.FolderBrowserDialog dilog = new System.Windows.Forms.FolderBrowserDialog
  333. {
  334. Description = "请选择文件夹"
  335. };
  336. if (dilog.ShowDialog() is System.Windows.Forms.DialogResult.OK or System.Windows.Forms.DialogResult.Yes)
  337. {
  338. ImageSavePath = dilog.SelectedPath;
  339. }
  340. }
  341. private void SaveOther()
  342. {
  343. _appSettings.Update(async c =>
  344. {
  345. if (c.CSVPath != CSVPath)
  346. {
  347. c.CSVPath = CSVPath;
  348. await HasSaved(nameof(CSVPath));
  349. }
  350. if (c.UnUseUpload != Selected)
  351. {
  352. c.UnUseUpload = Selected;
  353. await HasSaved(nameof(c.UnUseUpload));
  354. }
  355. if (c.ImageSavePath != ImageSavePath)
  356. {
  357. c.ImageSavePath = ImageSavePath;
  358. await HasSaved(nameof(c.ImageSavePath));
  359. }
  360. if (c.Process!=Process)
  361. {
  362. c.Process = Process;
  363. await HasSaved(nameof(c.Process));
  364. }
  365. if (c.AIM_UNIQUE != AIM_UNIQUE)
  366. {
  367. c.AIM_UNIQUE = AIM_UNIQUE;
  368. await HasSaved(nameof(c.AIM_UNIQUE));
  369. }
  370. if (c.AimName != AimName)
  371. {
  372. c.AimName = AimName;
  373. await HasSaved(nameof(c.AimName));
  374. }
  375. if (c.AIM_UNIQUE != AIM_UNIQUE)
  376. {
  377. c.AIM_UNIQUE = AIM_UNIQUE;
  378. await HasSaved(nameof(c.AIM_UNIQUE));
  379. }
  380. if (c.INSPECTION_PROGRAM_NAME != INSPECTION_PROGRAM_NAME)
  381. {
  382. c.INSPECTION_PROGRAM_NAME = INSPECTION_PROGRAM_NAME;
  383. await HasSaved(nameof(c.INSPECTION_PROGRAM_NAME));
  384. }
  385. if (c.Project != Project)
  386. {
  387. c.Project = Project;
  388. await HasSaved(nameof(c.Project));
  389. }
  390. if (c.Part != Part)
  391. {
  392. c.Part = Part;
  393. await HasSaved(nameof(c.Part));
  394. }
  395. if (c.Builds != Build)
  396. {
  397. c.Builds = Build;
  398. await HasSaved(nameof(c.Builds));
  399. }
  400. if (c.TraceDisable != TraceDisable)
  401. {
  402. c.TraceDisable = TraceDisable;
  403. await HasSaved(nameof(c.TraceDisable));
  404. }
  405. //if (c.ID != Build)
  406. //{
  407. // c.Builds = Build;
  408. // await HasSaved(nameof(c.Builds));
  409. //}
  410. });
  411. Growl.Info("保存参数成功");
  412. _eventAggregator.GetEvent<OtherParamEvent>().Publish(true);
  413. }
  414. private async void SaveTraceParam()
  415. {
  416. var param = _mapper.Map<Param>(Param);
  417. var json = JsonConvert.SerializeObject(param);
  418. var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "param.json");
  419. using var stream = new StreamWriter(filePath);
  420. await stream.WriteAsync(json);
  421. await stream.FlushAsync();
  422. Growl.Info("保存trace参数成功");
  423. foreach (var item in _storageChangedList)
  424. {
  425. await HasSaved(item);
  426. }
  427. _storageChangedList.Clear();
  428. _eventAggregator.GetEvent<SaveTraceParam>().Publish(param);
  429. }
  430. private string aimName;
  431. public string AimName
  432. {
  433. get => aimName;
  434. set
  435. {
  436. SetProperty(ref aimName, value);
  437. SetChanged();
  438. }
  439. }
  440. private string aIM_UNIQUE;
  441. public string AIM_UNIQUE
  442. {
  443. get => aIM_UNIQUE;
  444. set
  445. {
  446. SetProperty(ref aIM_UNIQUE, value);
  447. SetChanged();
  448. }
  449. }
  450. private string iNSPECTION_PROGRAM_NAME;
  451. public string INSPECTION_PROGRAM_NAME
  452. {
  453. get => iNSPECTION_PROGRAM_NAME;
  454. set {
  455. SetProperty(ref iNSPECTION_PROGRAM_NAME, value);
  456. SetChanged();
  457. }
  458. }
  459. private string project;
  460. public string Project {
  461. get => project;
  462. set
  463. {
  464. SetProperty(ref project, value);
  465. SetChanged();
  466. }
  467. }
  468. private string part;
  469. public string Part {
  470. get => part;
  471. set {
  472. SetProperty(ref part, value);
  473. SetChanged();
  474. }
  475. }
  476. private string process;
  477. public string Process
  478. {
  479. get => process;
  480. set
  481. {
  482. SetProperty(ref process, value);
  483. SetChanged();
  484. }
  485. }
  486. private string build;
  487. public string Build
  488. {
  489. get => build;
  490. set {
  491. SetProperty(ref build, value);
  492. SetChanged();
  493. }
  494. }
  495. }
  496. }