UIViewModelBase.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Input;
  10. using Aitex.Core.RT.Log;
  11. using Aitex.Core.UI.MVVM;
  12. using Aitex.Core.Util;
  13. using Aitex.Core.Utilities;
  14. using MECF.Framework.Common.DataCenter;
  15. using MECF.Framework.Common.OperationCenter;
  16. using OpenSEMI.ClientBase;
  17. using OpenSEMI.Ctrlib.Controls;
  18. using Virgo_D.UI.Config;
  19. using VirgoCommon;
  20. namespace VirgoUI.Client.Models.Sys
  21. {
  22. public class ModuleUiViewModelBase : UiViewModelBase
  23. {
  24. public string SystemName { get; set; }
  25. public override void SubscribeKeys()
  26. {
  27. SubscribeKeys(this, SystemName);
  28. }
  29. public override void UpdateSubscribe(Dictionary<string, object> data, object target, string module = null)
  30. {
  31. Parallel.ForEach(target.GetType().GetProperties().Where(_hasSubscriptionAttribute),
  32. property =>
  33. {
  34. PropertyInfo pi = (PropertyInfo)property;
  35. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  36. string key = module == null ? $"{SystemName}.{subscription.ModuleKey}" : string.Format("{0}.{1}", module, subscription.ModuleKey);
  37. if (_subscribedKeys.Contains(key) && data.ContainsKey(key))
  38. {
  39. try
  40. {
  41. var convertedValue = Convert.ChangeType(data[key], pi.PropertyType);
  42. var originValue = Convert.ChangeType(pi.GetValue(target, null), pi.PropertyType);
  43. if (originValue != convertedValue)
  44. {
  45. pi.SetValue(target, convertedValue, null);
  46. }
  47. }
  48. catch (Exception ex)
  49. {
  50. LOG.Error("由RT返回的数据更新失败" + key, ex);
  51. }
  52. }
  53. });
  54. Parallel.ForEach(target.GetType().GetFields().Where(_hasSubscriptionAttribute),
  55. property =>
  56. {
  57. FieldInfo pi = (FieldInfo)property;
  58. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  59. string key = module == null ? $"{SystemName}.{subscription.ModuleKey}" : string.Format("{0}.{1}", module, subscription.ModuleKey);
  60. if (_subscribedKeys.Contains(key) && data.ContainsKey(key))
  61. {
  62. try
  63. {
  64. var convertedValue = Convert.ChangeType(data[key], pi.FieldType);
  65. pi.SetValue(target, convertedValue);
  66. }
  67. catch (Exception ex)
  68. {
  69. LOG.Error("由RT返回的数据更新失败" + key, ex);
  70. }
  71. }
  72. });
  73. }
  74. public void InvokePropertyChanged(string propertyName)
  75. {
  76. NotifyOfPropertyChange(propertyName);
  77. }
  78. public void InvokeAllPropertyChanged()
  79. {
  80. PropertyInfo[] ps = this.GetType().GetProperties();
  81. foreach (PropertyInfo p in ps)
  82. {
  83. InvokePropertyChanged(p.Name);
  84. if (p.PropertyType == typeof(ICommand))
  85. {
  86. DelegateCommand<string> cmd = p.GetValue(this, null) as DelegateCommand<string>;
  87. if (cmd != null)
  88. cmd.RaiseCanExecuteChanged();
  89. }
  90. }
  91. FieldInfo[] fi = this.GetType().GetFields();
  92. foreach (FieldInfo p in fi)
  93. {
  94. InvokePropertyChanged(p.Name);
  95. if (p.FieldType == typeof(ICommand))
  96. {
  97. DelegateCommand<string> cmd = p.GetValue(this) as DelegateCommand<string>;
  98. if (cmd != null)
  99. cmd.RaiseCanExecuteChanged();
  100. }
  101. }
  102. //Parallel.ForEach(this.GetType().GetProperties(), property => InvokePropertyChanged(property.Name));
  103. }
  104. protected override void InvokePropertyChanged()
  105. {
  106. PropertyInfo[] ps = this.GetType().GetProperties();
  107. foreach (PropertyInfo p in ps)
  108. {
  109. if (!p.GetCustomAttributes(false).Any(attribute => attribute is IgnorePropertyChangeAttribute))
  110. InvokePropertyChanged(p.Name);
  111. if (p.PropertyType == typeof(ICommand))
  112. {
  113. if (p.GetValue(this, null) is IDelegateCommand cmd)
  114. cmd.RaiseCanExecuteChanged();
  115. }
  116. }
  117. FieldInfo[] fi = this.GetType().GetFields();
  118. foreach (FieldInfo p in fi)
  119. {
  120. InvokePropertyChanged(p.Name);
  121. if (p.FieldType == typeof(ICommand))
  122. {
  123. DelegateCommand<string> cmd = p.GetValue(this) as DelegateCommand<string>;
  124. if (cmd != null)
  125. cmd.RaiseCanExecuteChanged();
  126. }
  127. }
  128. //Parallel.ForEach(this.GetType().GetProperties(), property => InvokePropertyChanged(property.Name));
  129. }
  130. }
  131. public class UiViewModelBase : BaseModel
  132. {
  133. private PeriodicJob _timer;
  134. protected ConcurrentBag<string> _subscribedKeys = new ConcurrentBag<string>();
  135. protected Func<object, bool> _isSubscriptionAttribute;
  136. protected Func<MemberInfo, bool> _hasSubscriptionAttribute;
  137. [IgnorePropertyChange]
  138. public List<Tuple<string, string, string, bool>> _allDataItemsA = new List<Tuple<string, string, string, bool>>();
  139. [IgnorePropertyChange]
  140. public List<Tuple<string, string, string, bool>> _allDataItemsB = new List<Tuple<string, string, string, bool>>();
  141. #region Property
  142. public ModuleInfo FOUPA { get; set; }
  143. public ModuleInfo FOUPB { get; set; }
  144. public ModuleInfo EFEM { get; set; }
  145. public ModuleInfo Aligner1 { get; set; }
  146. public ModuleInfo Aligner2 { get; set; }
  147. public ModuleInfo Cooling1 { get; set; }
  148. public ModuleInfo Cooling2 { get; set; }
  149. public ModuleInfo Flipper { get; set; }
  150. public ModuleInfo Buffer { get; set; }
  151. public ModuleInfo PMA { get; set; }
  152. public ModuleInfo PMB { get; set; }
  153. #region Wafer info for machine
  154. public WaferInfo PMAWafer { get; set; }
  155. public WaferInfo PMBWafer { get; set; }
  156. public WaferInfo Aligner1Wafer { get; set; }
  157. public WaferInfo Aligner2Wafer { get; set; }
  158. public WaferInfo Cooling1Wafer { get; set; }
  159. public WaferInfo Cooling2Wafer { get; set; }
  160. public WaferInfo EfemRobotWafer1 { get; set; }
  161. public WaferInfo EfemRobotWafer2 { get; set; }
  162. public WaferInfo FlipperWafer { get; set; }
  163. public WaferInfo BufferWafer { get; set; }
  164. public bool IsBufferIntalled
  165. {
  166. get
  167. {
  168. return ModuleManager.ModulesID.Contains("Buffer");
  169. }
  170. }
  171. public bool IsFlipperInstalled
  172. {
  173. get
  174. {
  175. return ModuleManager.ModulesID.Contains("Flipper");
  176. }
  177. }
  178. public bool IsAligner1Installed
  179. {
  180. get
  181. {
  182. return ModuleManager.ModulesID.Contains("Aligner1");
  183. }
  184. }
  185. public bool IsAligner2Installed
  186. {
  187. get
  188. {
  189. return ModuleManager.ModulesID.Contains("Aligner2");
  190. }
  191. }
  192. public bool IsCooling1Installed
  193. {
  194. get
  195. {
  196. return ModuleManager.ModulesID.Contains("Cooling1");
  197. }
  198. }
  199. public bool IsCooling2Installed
  200. {
  201. get
  202. {
  203. return ModuleManager.ModulesID.Contains("Cooling2");
  204. }
  205. }
  206. public Dictionary<string, float> ModuleTemperature { get; set; }
  207. #endregion Wafer info for machine
  208. public ICommand DeviceOperationCommand { get; protected set; }
  209. #endregion Property
  210. protected override void OnInitialize()
  211. {
  212. base.OnInitialize();
  213. PageEnabled = true;
  214. DeviceOperationCommand = new DelegateCommand<object>(DeviceOperation);
  215. ModuleTemperature = new Dictionary<string, float>();
  216. SubscribeKeys();
  217. }
  218. void DeviceOperation(object param)
  219. {
  220. InvokeClient.Instance.Service.DoOperation(RtOperation.DeviceOperation.ToString(), (object[])param);
  221. }
  222. protected void InitModules()
  223. {
  224. EFEM = ModuleManager.ModuleInfos["EfemRobot"];
  225. if (ModuleManager.ModuleInfos.ContainsKey("EfemRobot"))
  226. {
  227. EfemRobotWafer1 = ModuleManager.ModuleInfos["EfemRobot"].WaferManager.Wafers[0];
  228. EfemRobotWafer2 = ModuleManager.ModuleInfos["EfemRobot"].WaferManager.Wafers[1];
  229. }
  230. if (ModuleManager.ModuleInfos.ContainsKey("Aligner1"))
  231. {
  232. Aligner1 = ModuleManager.ModuleInfos["Aligner1"];
  233. Aligner1Wafer = ModuleManager.ModuleInfos["Aligner1"].WaferManager.Wafers[0];
  234. }
  235. if (ModuleManager.ModuleInfos.ContainsKey("Aligner2"))
  236. {
  237. Aligner2 = ModuleManager.ModuleInfos["Aligner2"];
  238. Aligner2Wafer = ModuleManager.ModuleInfos["Aligner2"].WaferManager.Wafers[0];
  239. }
  240. if (ModuleManager.ModuleInfos.ContainsKey("Cooling1"))
  241. {
  242. Cooling1 = ModuleManager.ModuleInfos["Cooling1"];
  243. Cooling1Wafer = ModuleManager.ModuleInfos["Cooling1"].WaferManager.Wafers[0];
  244. }
  245. if (ModuleManager.ModuleInfos.ContainsKey("Cooling2"))
  246. {
  247. Cooling2 = ModuleManager.ModuleInfos["Cooling2"];
  248. Cooling2Wafer = ModuleManager.ModuleInfos["Cooling2"].WaferManager.Wafers[0];
  249. }
  250. if (ModuleManager.ModuleInfos.ContainsKey("Flipper"))
  251. {
  252. Flipper = ModuleManager.ModuleInfos["Flipper"];
  253. FlipperWafer = ModuleManager.ModuleInfos["Flipper"].WaferManager.Wafers[0];
  254. }
  255. if (ModuleManager.ModuleInfos.ContainsKey("Buffer"))
  256. {
  257. Buffer = ModuleManager.ModuleInfos["Buffer"];
  258. BufferWafer = ModuleManager.ModuleInfos["Buffer"].WaferManager.Wafers[0];
  259. }
  260. FOUPA = ModuleManager.ModuleInfos["LP1"];
  261. FOUPB = ModuleManager.ModuleInfos["LP2"];
  262. if (ModuleManager.ModuleInfos.ContainsKey("PMA"))
  263. {
  264. PMA = ModuleManager.ModuleInfos["PMA"];
  265. PMAWafer = ModuleManager.ModuleInfos["PMA"].WaferManager.Wafers[0];
  266. }
  267. if (ModuleManager.ModuleInfos.ContainsKey("PMB"))
  268. {
  269. PMB = ModuleManager.ModuleInfos["PMB"];
  270. PMBWafer = ModuleManager.ModuleInfos["PMB"].WaferManager.Wafers[0];
  271. }
  272. }
  273. public string GetUnitStatusBackground(string status)
  274. {
  275. if (status != null)
  276. status = status.Trim().ToLower();
  277. switch (status)
  278. {
  279. case "error":
  280. return "red";
  281. case "idle":
  282. return "Transparent";
  283. case "init":
  284. return "Yellow";
  285. default:
  286. return "LawnGreen";
  287. }
  288. }
  289. /// <summary>
  290. /// support wafer transfer for slot
  291. /// </summary>
  292. public void OnWaferTransfer(DragDropEventArgs args)
  293. {
  294. try
  295. {
  296. float temp = 0.0f;
  297. if (ModuleTemperature.ContainsKey(args.TranferTo.ModuleID))
  298. {
  299. temp = ModuleTemperature[args.TranferTo.ModuleID];
  300. }
  301. WaferMoveManager.Instance.TransferWafer(args.TranferFrom, args.TranferTo, temp);
  302. }
  303. catch (Exception ex)
  304. {
  305. LOG.Write(ex);
  306. }
  307. }
  308. /// <summary>
  309. /// support context menu
  310. /// </summary>
  311. public void OnMouseUp(object sender, MouseButtonEventArgs e)
  312. {
  313. if (e.ChangedButton == MouseButton.Right)
  314. {
  315. Slot slot = sender as Slot;
  316. ContextMenu cm = ContextMenuManager.Instance.GetSlotMenus(slot);
  317. if (cm != null)
  318. {
  319. ((FrameworkElement)e.Source).ContextMenu = cm;
  320. }
  321. }
  322. }
  323. public UiViewModelBase()
  324. {
  325. _timer = new PeriodicJob(1000, this.OnTimer, "UIUpdaterThread - " + GetType().Name);
  326. _isSubscriptionAttribute = attribute => attribute is SubscriptionAttribute;
  327. _hasSubscriptionAttribute = mi => mi.GetCustomAttributes(false).Any(_isSubscriptionAttribute);
  328. _allDataItemsA = SystemConfigManager.Instance.GetMonitorDataList("A");
  329. _allDataItemsB = SystemConfigManager.Instance.GetMonitorDataList("B");
  330. }
  331. public virtual void SubscribeKeys()
  332. {
  333. SubscribeKeys(this);
  334. }
  335. //
  336. protected virtual bool OnTimer()
  337. {
  338. try
  339. {
  340. Poll();
  341. }
  342. catch (Exception ex)
  343. {
  344. LOG.Error(ex.Message);
  345. }
  346. return true;
  347. }
  348. public virtual void EnableTimer(bool enable)
  349. {
  350. if (enable) _timer.Start();
  351. else _timer.Pause();
  352. }
  353. protected virtual void Poll()
  354. {
  355. if (_subscribedKeys.Count > 0)
  356. {
  357. Dictionary<string, object> result = QueryDataClient.Instance.Service.PollData(_subscribedKeys);
  358. if (result == null)
  359. {
  360. LOG.Error("获取RT数据失败");
  361. return;
  362. }
  363. if (result.Count != _subscribedKeys.Count)
  364. {
  365. string unknowKeys = string.Empty;
  366. foreach (string key in _subscribedKeys)
  367. {
  368. if (!result.ContainsKey(key))
  369. {
  370. unknowKeys += key + "\r\n";
  371. }
  372. }
  373. //System.Diagnostics.Debug.Assert(false, unknowKeys);
  374. }
  375. InvokeBeforeUpdateProperty(result);
  376. UpdateValue(result);
  377. Application.Current.Dispatcher.Invoke(new Action(() =>
  378. {
  379. InvokePropertyChanged();
  380. InvokeAfterUpdateProperty(result);
  381. }));
  382. }
  383. }
  384. protected virtual void InvokePropertyChanged()
  385. {
  386. Refresh();
  387. }
  388. protected virtual void InvokeBeforeUpdateProperty(Dictionary<string, object> data)
  389. {
  390. }
  391. protected virtual void InvokeAfterUpdateProperty(Dictionary<string, object> data)
  392. {
  393. }
  394. public Dictionary<string, Tuple<string, string, bool>> GetDataElements(string culture, string type, string chamber)
  395. {
  396. Dictionary<string, Tuple<string, string, bool>> result = new Dictionary<string, Tuple<string, string, bool>>();
  397. Dictionary<string, Tuple<string, string, string, bool>> all = GetDataElements(type);
  398. foreach (var tuple in all)
  399. {
  400. if (!string.IsNullOrEmpty(chamber))
  401. {
  402. if (!tuple.Key.StartsWith($"{chamber}.") && !tuple.Key.StartsWith($"IO32.{chamber}."))
  403. continue;
  404. }
  405. result.Add(tuple.Key, Tuple.Create(tuple.Value.Item1, culture == CultureSupported.Chinese ? tuple.Value.Item3 : tuple.Value.Item2, tuple.Value.Item4));
  406. }
  407. return result;
  408. }
  409. public Dictionary<string, Tuple<string, string, string, bool>> GetDataElements(string type)
  410. {
  411. Dictionary<string, Tuple<string, string, string, bool>> dicItems = new Dictionary<string, Tuple<string, string, string, bool>>();
  412. if (type == "A")
  413. {
  414. foreach (var dataItem in _allDataItemsA)
  415. {
  416. dicItems[dataItem.Item1] = dataItem;
  417. }
  418. }
  419. else
  420. {
  421. foreach (var dataItem in _allDataItemsB)
  422. {
  423. dicItems[dataItem.Item1] = dataItem;
  424. }
  425. }
  426. return dicItems;
  427. }
  428. void UpdateGasItem(bool enable, string display, string id, string key, Dictionary<string, Tuple<string, string, string, bool>> dicItems)
  429. {
  430. if (enable && dicItems.ContainsKey(key))
  431. {
  432. dicItems[key] = Tuple.Create(key,
  433. dicItems[key].Item2.Replace(id, display),
  434. dicItems[key].Item3.Replace(id, display),
  435. dicItems[key].Item4);
  436. }
  437. else
  438. {
  439. dicItems.Remove(key);
  440. }
  441. }
  442. void UpdateValue(Dictionary<string, object> data)
  443. {
  444. if (data == null)
  445. return;
  446. UpdateSubscribe(data, this);
  447. var properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.GetCustomAttribute<SubscriptionModuleAttribute>() != null);
  448. foreach (var property in properties)
  449. {
  450. var moduleAttr = property.GetCustomAttribute<SubscriptionModuleAttribute>();
  451. UpdateSubscribe(data, property.GetValue(this), moduleAttr.Module);
  452. }
  453. }
  454. protected void Subscribe(string key)
  455. {
  456. if (!string.IsNullOrEmpty(key))
  457. {
  458. _subscribedKeys.Add(key);
  459. }
  460. }
  461. public void SubscribeKeys(UiViewModelBase target)
  462. {
  463. SubscribeKeys(target, "");
  464. }
  465. public void SubscribeKeys(UiViewModelBase target, string module)
  466. {
  467. Parallel.ForEach(target.GetType().GetProperties().Where(_hasSubscriptionAttribute),
  468. property =>
  469. {
  470. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  471. string key = subscription.ModuleKey;
  472. if (!string.IsNullOrEmpty(module))
  473. {
  474. key = $"{module}.{key}";
  475. subscription.SetModule(module);
  476. }
  477. if (!_subscribedKeys.Contains(key))
  478. _subscribedKeys.Add(key);
  479. });
  480. Parallel.ForEach(target.GetType().GetFields().Where(_hasSubscriptionAttribute),
  481. method =>
  482. {
  483. SubscriptionAttribute subscription = method.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  484. string key = subscription.ModuleKey;
  485. if (!string.IsNullOrEmpty(module))
  486. {
  487. key = $"{module}.{key}";
  488. subscription.SetModule(module);
  489. }
  490. if (!_subscribedKeys.Contains(key))
  491. _subscribedKeys.Add(key);
  492. });
  493. }
  494. public virtual void UpdateSubscribe(Dictionary<string, object> data, object target, string module = null)
  495. {
  496. Parallel.ForEach(target.GetType().GetProperties().Where(_hasSubscriptionAttribute),
  497. property =>
  498. {
  499. PropertyInfo pi = (PropertyInfo)property;
  500. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  501. string key = subscription.ModuleKey;
  502. key = module == null ? key : string.Format("{0}.{1}", module, key);
  503. if (_subscribedKeys.Contains(key) && data.ContainsKey(key))
  504. {
  505. try
  506. {
  507. var convertedValue = Convert.ChangeType(data[key], pi.PropertyType);
  508. var originValue = Convert.ChangeType(pi.GetValue(target, null), pi.PropertyType);
  509. if (originValue != convertedValue)
  510. {
  511. pi.SetValue(target, convertedValue, null);
  512. }
  513. }
  514. catch (Exception ex)
  515. {
  516. LOG.Error("由RT返回的数据更新失败" + key, ex);
  517. }
  518. }
  519. });
  520. Parallel.ForEach(target.GetType().GetFields().Where(_hasSubscriptionAttribute),
  521. property =>
  522. {
  523. FieldInfo pi = (FieldInfo)property;
  524. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  525. string key = subscription.ModuleKey;
  526. if (_subscribedKeys.Contains(key) && data.ContainsKey(key))
  527. {
  528. try
  529. {
  530. var convertedValue = Convert.ChangeType(data[key], pi.FieldType);
  531. pi.SetValue(target, convertedValue);
  532. }
  533. catch (Exception ex)
  534. {
  535. LOG.Error("由RT返回的数据更新失败" + key, ex);
  536. }
  537. }
  538. });
  539. }
  540. protected override void OnActivate()
  541. {
  542. base.OnActivate();
  543. EnableTimer(true);
  544. }
  545. protected override void OnDeactivate(bool close)
  546. {
  547. base.OnDeactivate(close);
  548. EnableTimer(false);
  549. }
  550. }
  551. }