UIViewModelBase.cs 20 KB

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