UIViewModelBase.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 PMA { get; set; }
  150. public ModuleInfo PMB { get; set; }
  151. #region Wafer info for machine
  152. public WaferInfo PMAWafer { get; set; }
  153. public WaferInfo PMBWafer { get; set; }
  154. public WaferInfo Aligner1Wafer { get; set; }
  155. public WaferInfo Aligner2Wafer { get; set; }
  156. public WaferInfo Cooling1Wafer { get; set; }
  157. public WaferInfo Cooling2Wafer { get; set; }
  158. public WaferInfo EfemRobotWafer1 { get; set; }
  159. public WaferInfo EfemRobotWafer2 { get; set; }
  160. public Dictionary<string, float> ModuleTemperature { get; set; }
  161. #endregion Wafer info for machine
  162. public ICommand DeviceOperationCommand { get; protected set; }
  163. #endregion Property
  164. protected override void OnInitialize()
  165. {
  166. base.OnInitialize();
  167. PageEnabled = true;
  168. DeviceOperationCommand = new DelegateCommand<object>(DeviceOperation);
  169. ModuleTemperature = new Dictionary<string, float>();
  170. SubscribeKeys();
  171. }
  172. void DeviceOperation(object param)
  173. {
  174. InvokeClient.Instance.Service.DoOperation(RtOperation.DeviceOperation.ToString(), (object[])param);
  175. }
  176. protected void InitModules()
  177. {
  178. EFEM = ModuleManager.ModuleInfos["EfemRobot"];
  179. if (ModuleManager.ModuleInfos.ContainsKey("EfemRobot"))
  180. {
  181. EfemRobotWafer1 = ModuleManager.ModuleInfos["EfemRobot"].WaferManager.Wafers[0];
  182. EfemRobotWafer2 = ModuleManager.ModuleInfos["EfemRobot"].WaferManager.Wafers[1];
  183. }
  184. Aligner1 = ModuleManager.ModuleInfos["Aligner1"];
  185. if (ModuleManager.ModuleInfos.ContainsKey("Aligner1"))
  186. Aligner1Wafer = ModuleManager.ModuleInfos["Aligner1"].WaferManager.Wafers[0];
  187. Aligner2 = ModuleManager.ModuleInfos["Aligner2"];
  188. if (ModuleManager.ModuleInfos.ContainsKey("Aligner2"))
  189. Aligner2Wafer = ModuleManager.ModuleInfos["Aligner2"].WaferManager.Wafers[0];
  190. Cooling1 = ModuleManager.ModuleInfos["Cooling1"];
  191. if (ModuleManager.ModuleInfos.ContainsKey("Cooling1"))
  192. Cooling1Wafer = ModuleManager.ModuleInfos["Cooling1"].WaferManager.Wafers[0];
  193. Cooling2 = ModuleManager.ModuleInfos["Cooling2"];
  194. if (ModuleManager.ModuleInfos.ContainsKey("Cooling2"))
  195. Cooling2Wafer = ModuleManager.ModuleInfos["Cooling2"].WaferManager.Wafers[0];
  196. FOUPA = ModuleManager.ModuleInfos["LP1"];
  197. FOUPB = ModuleManager.ModuleInfos["LP2"];
  198. if (ModuleManager.ModuleInfos.ContainsKey("PMA"))
  199. {
  200. PMA = ModuleManager.ModuleInfos["PMA"];
  201. PMAWafer = ModuleManager.ModuleInfos["PMA"].WaferManager.Wafers[0];
  202. }
  203. if (ModuleManager.ModuleInfos.ContainsKey("PMB"))
  204. {
  205. PMB = ModuleManager.ModuleInfos["PMB"];
  206. PMBWafer = ModuleManager.ModuleInfos["PMB"].WaferManager.Wafers[0];
  207. }
  208. }
  209. public string GetUnitStatusBackground(string status)
  210. {
  211. if (status != null)
  212. status = status.Trim().ToLower();
  213. switch (status)
  214. {
  215. case "error":
  216. return "red";
  217. case "idle":
  218. return "Transparent";
  219. case "init":
  220. return "Yellow";
  221. default:
  222. return "LawnGreen";
  223. }
  224. }
  225. /// <summary>
  226. /// support wafer transfer for slot
  227. /// </summary>
  228. public void OnWaferTransfer(DragDropEventArgs args)
  229. {
  230. try
  231. {
  232. float temp = 0.0f;
  233. if (ModuleTemperature.ContainsKey(args.TranferTo.ModuleID))
  234. {
  235. temp = ModuleTemperature[args.TranferTo.ModuleID];
  236. }
  237. WaferMoveManager.Instance.TransferWafer(args.TranferFrom, args.TranferTo, temp);
  238. }
  239. catch (Exception ex)
  240. {
  241. LOG.Write(ex);
  242. }
  243. }
  244. /// <summary>
  245. /// support context menu
  246. /// </summary>
  247. public void OnMouseUp(object sender, MouseButtonEventArgs e)
  248. {
  249. if (e.ChangedButton == MouseButton.Right)
  250. {
  251. Slot slot = sender as Slot;
  252. ContextMenu cm = ContextMenuManager.Instance.GetSlotMenus(slot);
  253. if (cm != null)
  254. {
  255. ((FrameworkElement)e.Source).ContextMenu = cm;
  256. }
  257. }
  258. }
  259. public UiViewModelBase()
  260. {
  261. _timer = new PeriodicJob(1000, this.OnTimer, "UIUpdaterThread - " + GetType().Name);
  262. _isSubscriptionAttribute = attribute => attribute is SubscriptionAttribute;
  263. _hasSubscriptionAttribute = mi => mi.GetCustomAttributes(false).Any(_isSubscriptionAttribute);
  264. _allDataItemsA = SystemConfigManager.Instance.GetMonitorDataList("A");
  265. _allDataItemsB = SystemConfigManager.Instance.GetMonitorDataList("B");
  266. }
  267. public virtual void SubscribeKeys()
  268. {
  269. SubscribeKeys(this);
  270. }
  271. //
  272. protected virtual bool OnTimer()
  273. {
  274. try
  275. {
  276. Poll();
  277. }
  278. catch (Exception ex)
  279. {
  280. LOG.Error(ex.Message);
  281. }
  282. return true;
  283. }
  284. public virtual void EnableTimer(bool enable)
  285. {
  286. if (enable) _timer.Start();
  287. else _timer.Pause();
  288. }
  289. protected virtual void Poll()
  290. {
  291. if (_subscribedKeys.Count > 0)
  292. {
  293. Dictionary<string, object> result = QueryDataClient.Instance.Service.PollData(_subscribedKeys);
  294. if (result == null)
  295. {
  296. LOG.Error("获取RT数据失败");
  297. return;
  298. }
  299. if (result.Count != _subscribedKeys.Count)
  300. {
  301. string unknowKeys = string.Empty;
  302. foreach (string key in _subscribedKeys)
  303. {
  304. if (!result.ContainsKey(key))
  305. {
  306. unknowKeys += key + "\r\n";
  307. }
  308. }
  309. //System.Diagnostics.Debug.Assert(false, unknowKeys);
  310. }
  311. InvokeBeforeUpdateProperty(result);
  312. UpdateValue(result);
  313. Application.Current.Dispatcher.Invoke(new Action(() =>
  314. {
  315. InvokePropertyChanged();
  316. InvokeAfterUpdateProperty(result);
  317. }));
  318. }
  319. }
  320. protected virtual void InvokePropertyChanged()
  321. {
  322. Refresh();
  323. }
  324. protected virtual void InvokeBeforeUpdateProperty(Dictionary<string, object> data)
  325. {
  326. }
  327. protected virtual void InvokeAfterUpdateProperty(Dictionary<string, object> data)
  328. {
  329. }
  330. public Dictionary<string, Tuple<string, string, bool>> GetDataElements(string culture, string type, string chamber)
  331. {
  332. Dictionary<string, Tuple<string, string, bool>> result = new Dictionary<string, Tuple<string, string, bool>>();
  333. Dictionary<string, Tuple<string, string, string, bool>> all = GetDataElements(type);
  334. foreach (var tuple in all)
  335. {
  336. if (!string.IsNullOrEmpty(chamber))
  337. {
  338. if (!tuple.Key.StartsWith($"{chamber}.") && !tuple.Key.StartsWith($"IO32.{chamber}."))
  339. continue;
  340. }
  341. result.Add(tuple.Key, Tuple.Create(tuple.Value.Item1, culture == CultureSupported.Chinese ? tuple.Value.Item3 : tuple.Value.Item2, tuple.Value.Item4));
  342. }
  343. return result;
  344. }
  345. public Dictionary<string, Tuple<string, string, string, bool>> GetDataElements(string type)
  346. {
  347. Dictionary<string, Tuple<string, string, string, bool>> dicItems = new Dictionary<string, Tuple<string, string, string, bool>>();
  348. if (type == "A")
  349. {
  350. foreach (var dataItem in _allDataItemsA)
  351. {
  352. dicItems[dataItem.Item1] = dataItem;
  353. }
  354. }
  355. else
  356. {
  357. foreach (var dataItem in _allDataItemsB)
  358. {
  359. dicItems[dataItem.Item1] = dataItem;
  360. }
  361. }
  362. return dicItems;
  363. }
  364. void UpdateGasItem(bool enable, string display, string id, string key, Dictionary<string, Tuple<string, string, string, bool>> dicItems)
  365. {
  366. if (enable && dicItems.ContainsKey(key))
  367. {
  368. dicItems[key] = Tuple.Create(key,
  369. dicItems[key].Item2.Replace(id, display),
  370. dicItems[key].Item3.Replace(id, display),
  371. dicItems[key].Item4);
  372. }
  373. else
  374. {
  375. dicItems.Remove(key);
  376. }
  377. }
  378. void UpdateValue(Dictionary<string, object> data)
  379. {
  380. if (data == null)
  381. return;
  382. UpdateSubscribe(data, this);
  383. var properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.GetCustomAttribute<SubscriptionModuleAttribute>() != null);
  384. foreach (var property in properties)
  385. {
  386. var moduleAttr = property.GetCustomAttribute<SubscriptionModuleAttribute>();
  387. UpdateSubscribe(data, property.GetValue(this), moduleAttr.Module);
  388. }
  389. }
  390. protected void Subscribe(string key)
  391. {
  392. if (!string.IsNullOrEmpty(key))
  393. {
  394. _subscribedKeys.Add(key);
  395. }
  396. }
  397. public void SubscribeKeys(UiViewModelBase target)
  398. {
  399. SubscribeKeys(target, "");
  400. }
  401. public void SubscribeKeys(UiViewModelBase target, string module)
  402. {
  403. Parallel.ForEach(target.GetType().GetProperties().Where(_hasSubscriptionAttribute),
  404. property =>
  405. {
  406. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  407. string key = subscription.ModuleKey;
  408. if (!string.IsNullOrEmpty(module))
  409. {
  410. key = $"{module}.{key}";
  411. subscription.SetModule(module);
  412. }
  413. if (!_subscribedKeys.Contains(key))
  414. _subscribedKeys.Add(key);
  415. });
  416. Parallel.ForEach(target.GetType().GetFields().Where(_hasSubscriptionAttribute),
  417. method =>
  418. {
  419. SubscriptionAttribute subscription = method.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  420. string key = subscription.ModuleKey;
  421. if (!string.IsNullOrEmpty(module))
  422. {
  423. key = $"{module}.{key}";
  424. subscription.SetModule(module);
  425. }
  426. if (!_subscribedKeys.Contains(key))
  427. _subscribedKeys.Add(key);
  428. });
  429. }
  430. public virtual void UpdateSubscribe(Dictionary<string, object> data, object target, string module = null)
  431. {
  432. Parallel.ForEach(target.GetType().GetProperties().Where(_hasSubscriptionAttribute),
  433. property =>
  434. {
  435. PropertyInfo pi = (PropertyInfo)property;
  436. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  437. string key = subscription.ModuleKey;
  438. key = module == null ? key : string.Format("{0}.{1}", module, key);
  439. if (_subscribedKeys.Contains(key) && data.ContainsKey(key))
  440. {
  441. try
  442. {
  443. var convertedValue = Convert.ChangeType(data[key], pi.PropertyType);
  444. var originValue = Convert.ChangeType(pi.GetValue(target, null), pi.PropertyType);
  445. if (originValue != convertedValue)
  446. {
  447. pi.SetValue(target, convertedValue, null);
  448. }
  449. }
  450. catch (Exception ex)
  451. {
  452. LOG.Error("由RT返回的数据更新失败" + key, ex);
  453. }
  454. }
  455. });
  456. Parallel.ForEach(target.GetType().GetFields().Where(_hasSubscriptionAttribute),
  457. property =>
  458. {
  459. FieldInfo pi = (FieldInfo)property;
  460. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  461. string key = subscription.ModuleKey;
  462. if (_subscribedKeys.Contains(key) && data.ContainsKey(key))
  463. {
  464. try
  465. {
  466. var convertedValue = Convert.ChangeType(data[key], pi.FieldType);
  467. pi.SetValue(target, convertedValue);
  468. }
  469. catch (Exception ex)
  470. {
  471. LOG.Error("由RT返回的数据更新失败" + key, ex);
  472. }
  473. }
  474. });
  475. }
  476. protected override void OnActivate()
  477. {
  478. base.OnActivate();
  479. EnableTimer(true);
  480. }
  481. protected override void OnDeactivate(bool close)
  482. {
  483. base.OnDeactivate(close);
  484. EnableTimer(false);
  485. }
  486. }
  487. }