UIViewModelBase.cs 20 KB

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