UIViewModelBase.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. using Aitex.Core.Common.DeviceData;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.UI.MVVM;
  4. using Aitex.Core.Util;
  5. using Aitex.Core.Utilities;
  6. using Caliburn.Micro.Core;
  7. using MECF.Framework.Common.DataCenter;
  8. using MECF.Framework.Common.IOCore;
  9. using MECF.Framework.Common.Utilities;
  10. using OpenSEMI.Ctrlib.Controls;
  11. using System;
  12. using System.Collections.Concurrent;
  13. using System.Collections.Generic;
  14. using System.Collections.ObjectModel;
  15. using System.Linq;
  16. using System.Reflection;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Input;
  21. namespace MECF.Framework.UI.Client.ClientBase
  22. {
  23. public class ModuleUiViewModelBase : UiViewModelBase
  24. {
  25. public string SystemName { get; set; }
  26. public override void SubscribeKeys()
  27. {
  28. SubscribeKeys(this, SystemName);
  29. }
  30. public override void UpdateSubscribe(Dictionary<string, object> data, object target, string module = null)
  31. {
  32. Parallel.ForEach(target.GetType().GetProperties().Where(_hasSubscriptionAttribute),
  33. property =>
  34. {
  35. PropertyInfo pi = (PropertyInfo)property;
  36. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  37. string key = subscription.ModuleKey;
  38. key = module == null ? key : string.Format("{0}.{1}", module, key);
  39. if (_subscribedKeys.Contains(key) && data.ContainsKey(key))
  40. {
  41. try
  42. {
  43. var convertedValue = Convert.ChangeType(data[key], pi.PropertyType);
  44. var originValue = Convert.ChangeType(pi.GetValue(target, null), pi.PropertyType);
  45. if (originValue != convertedValue)
  46. {
  47. pi.SetValue(target, convertedValue, null);
  48. }
  49. }
  50. catch (Exception ex)
  51. {
  52. LOG.Error("由RT返回的数据更新失败" + key, ex);
  53. }
  54. }
  55. });
  56. Parallel.ForEach(target.GetType().GetFields().Where(_hasSubscriptionAttribute),
  57. property =>
  58. {
  59. FieldInfo pi = (FieldInfo)property;
  60. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  61. string key = module == null ? $"{SystemName}.{subscription.ModuleKey}" : string.Format("{0}.{1}", module, subscription.ModuleKey);
  62. if (_subscribedKeys.Contains(key) && data.ContainsKey(key))
  63. {
  64. try
  65. {
  66. var convertedValue = Convert.ChangeType(data[key], pi.FieldType);
  67. pi.SetValue(target, convertedValue);
  68. }
  69. catch (Exception ex)
  70. {
  71. LOG.Error("由RT返回的数据更新失败" + key, ex);
  72. }
  73. }
  74. });
  75. }
  76. protected override void InvokePropertyChanged()
  77. {
  78. PropertyInfo[] ps = this.GetType().GetProperties();
  79. foreach (PropertyInfo p in ps)
  80. {
  81. if (!p.GetCustomAttributes(false).Any(attribute => attribute is IgnorePropertyChangeAttribute))
  82. InvokePropertyChanged(p.Name);
  83. if (p.PropertyType == typeof(ICommand))
  84. {
  85. if (p.GetValue(this, null) is IDelegateCommand cmd)
  86. cmd.RaiseCanExecuteChanged();
  87. }
  88. }
  89. FieldInfo[] fi = this.GetType().GetFields();
  90. foreach (FieldInfo p in fi)
  91. {
  92. InvokePropertyChanged(p.Name);
  93. if (p.FieldType == typeof(ICommand))
  94. {
  95. DelegateCommand<string> cmd = p.GetValue(this) as DelegateCommand<string>;
  96. if (cmd != null)
  97. cmd.RaiseCanExecuteChanged();
  98. }
  99. }
  100. //Parallel.ForEach(this.GetType().GetProperties(), property => InvokePropertyChanged(property.Name));
  101. }
  102. }
  103. public class UiViewModelBase : BaseModel
  104. {
  105. PeriodicJob _timer;
  106. protected ConcurrentBag<string> _subscribedKeys = new ConcurrentBag<string>();
  107. protected Func<object, bool> _isSubscriptionAttribute;
  108. protected Func<MemberInfo, bool> _hasSubscriptionAttribute;
  109. public ICommand DeviceOperationCommand { get; private set; }
  110. public bool SetField<T>(ref T field, T value, string propertyName)
  111. {
  112. if (EqualityComparer<T>.Default.Equals(field, value)) return false;
  113. field = value;
  114. NotifyOfPropertyChange(propertyName);
  115. return true;
  116. }
  117. [Subscription("PM1.EnableMinics")]
  118. public bool EnableMinics { get; set; }
  119. public Dictionary<string, string> StockerToModuleDic
  120. {
  121. get
  122. {
  123. var moduleDic = new Dictionary<string, string>()
  124. {
  125. { "Stocker11", "Stocker1" },
  126. { "Stocker21", "Stocker2" },
  127. { "Stocker12", "Stocker3" },
  128. { "Stocker22", "Stocker4" },
  129. { "Stocker13", "Stocker5" },
  130. { "Stocker23", "Stocker6" },
  131. { "Stocker14", "Stocker7" },
  132. { "Stocker24", "Stocker8" },
  133. { "Stocker15", "Stocker9" },
  134. { "Stocker25", "Stocker10" },
  135. { "Stocker31", "Stocker11" },
  136. { "Stocker41", "Stocker12" },
  137. { "Stocker32", "Stocker13" },
  138. { "Stocker42", "Stocker14" },
  139. { "Stocker33", "Stocker15" },
  140. { "Stocker43", "Stocker16" },
  141. { "Stocker34", "Stocker17" },
  142. { "Stocker44", "Stocker18" },
  143. { "Stocker35", "Stocker19" },
  144. { "Stocker36", "Stocker20" },
  145. };
  146. return moduleDic;
  147. }
  148. }
  149. public Dictionary<string, string> ModuleToStockerDic
  150. {
  151. get
  152. {
  153. var moduleDic = new Dictionary<string, string>()
  154. {
  155. { "Stocker1", "Stocker11" },
  156. { "Stocker2", "Stocker21" },
  157. { "Stocker3", "Stocker12" },
  158. { "Stocker4", "Stocker22" },
  159. { "Stocker5", "Stocker13" },
  160. { "Stocker6", "Stocker23" },
  161. { "Stocker7", "Stocker14" },
  162. { "Stocker8", "Stocker24" },
  163. { "Stocker9", "Stocker15" },
  164. { "Stocker10", "Stocker25" },
  165. { "Stocker11", "Stocker31" },
  166. { "Stocker12", "Stocker41" },
  167. { "Stocker13", "Stocker32" },
  168. { "Stocker14", "Stocker42" },
  169. { "Stocker15", "Stocker33" },
  170. { "Stocker16", "Stocker43" },
  171. { "Stocker17", "Stocker34" },
  172. { "Stocker18", "Stocker44" },
  173. { "Stocker19", "Stocker35" },
  174. { "Stocker20", "Stocker36" },
  175. };
  176. return moduleDic;
  177. }
  178. }
  179. /// <summary>
  180. /// 是否在退出页面后,仍然在后台更新数据,默认不更新
  181. /// </summary>
  182. public bool ActiveUpdateData { get; set; }
  183. protected override void OnInitialize()
  184. {
  185. base.OnInitialize();
  186. DeviceOperationCommand = new DelegateCommand<object>(DeviceOperation);
  187. SubscribeKeys();
  188. }
  189. void DeviceOperation(object param)
  190. {
  191. //InvokeClient.Instance.Service.DoOperation(OperationName.DeviceOperation.ToString(), (object[])param);
  192. }
  193. public string GetUnitStatusBackground(string status)
  194. {
  195. return ModuleStatusBackground.GetStatusBackground(status);
  196. }
  197. public string GetUnitOnlineColor(bool isOnline)
  198. {
  199. return isOnline ? "LimeGreen" : "Black";
  200. }
  201. /// <summary>
  202. /// support wafer transfer for slot
  203. /// </summary>
  204. public virtual void OnWaferTransfer(DragDropEventArgs args)
  205. {
  206. try
  207. {
  208. WaferMoveManager.Instance.TransferWafer(args.TranferFrom, args.TranferTo);
  209. }
  210. catch (Exception ex)
  211. {
  212. LOG.Write(ex);
  213. }
  214. }
  215. /// <summary>
  216. /// support context menu
  217. /// </summary>
  218. public virtual void OnMouseUp(object sender, MouseButtonEventArgs e)
  219. {
  220. if (e.ChangedButton == MouseButton.Left)
  221. {
  222. //if (sender is Slot slot)
  223. //{
  224. // if (slot.WaferStatus == 0)
  225. // {
  226. // slot.WaferStatus = 1;
  227. // }
  228. // else if(slot.WaferStatus == 1)
  229. // {
  230. // slot.WaferStatus = 0;
  231. // }
  232. //}
  233. }
  234. else
  235. {
  236. if (e.ChangedButton != MouseButton.Right)
  237. return;
  238. if (sender is Slot slot)
  239. {
  240. ContextMenu cm = ContextMenuManager.Instance.GetSlotMenus(slot);
  241. if (cm != null)
  242. {
  243. ((FrameworkElement)e.Source).ContextMenu = cm;
  244. }
  245. return;
  246. }
  247. if (sender is CarrierContentControl carrier)
  248. {
  249. ContextMenu cm = ContextMenuManager.Instance.GetCarrierMenus(carrier);
  250. if (cm != null)
  251. {
  252. ((FrameworkElement)e.Source).ContextMenu = cm;
  253. }
  254. return;
  255. }
  256. }
  257. }
  258. public UiViewModelBase()
  259. {
  260. _timer = new PeriodicJob(1000, this.OnTimer, "UIUpdaterThread - " + GetType().Name);
  261. _isSubscriptionAttribute = attribute => attribute is SubscriptionAttribute;
  262. _hasSubscriptionAttribute = mi => mi.GetCustomAttributes(false).Any(_isSubscriptionAttribute);
  263. }
  264. //
  265. protected virtual bool OnTimer()
  266. {
  267. try
  268. {
  269. Poll();
  270. }
  271. catch (Exception ex)
  272. {
  273. LOG.Error(ex.Message);
  274. }
  275. return true;
  276. }
  277. public virtual void EnableTimer(bool enable)
  278. {
  279. if (enable) _timer.Start();
  280. else _timer.Pause();
  281. }
  282. protected virtual void Poll()
  283. {
  284. if (_subscribedKeys.Count > 0)
  285. {
  286. Dictionary<string, object> result = QueryDataClient.Instance.Service.PollData(_subscribedKeys);
  287. if (result == null)
  288. {
  289. LOG.Error("获取RT数据失败");
  290. return;
  291. }
  292. if (result.Count != _subscribedKeys.Count)
  293. {
  294. string unknowKeys = string.Empty;
  295. foreach (string key in _subscribedKeys)
  296. {
  297. if (!result.ContainsKey(key))
  298. {
  299. unknowKeys += key + "\r\n";
  300. }
  301. }
  302. //System.Diagnostics.Debug.Assert(false, unknowKeys);
  303. }
  304. InvokeBeforeUpdateProperty(result);
  305. UpdateValue(result);
  306. Application.Current?.Dispatcher.Invoke(new Action(() =>
  307. {
  308. InvokePropertyChanged();
  309. InvokeAfterUpdateProperty(result);
  310. }));
  311. }
  312. }
  313. public void InvokePropertyChanged(string propertyName)
  314. {
  315. NotifyOfPropertyChange(propertyName);
  316. }
  317. public void InvokeAllPropertyChanged()
  318. {
  319. PropertyInfo[] ps = this.GetType().GetProperties();
  320. foreach (PropertyInfo p in ps)
  321. {
  322. InvokePropertyChanged(p.Name);
  323. if (p.PropertyType == typeof(ICommand))
  324. {
  325. DelegateCommand<string> cmd = p.GetValue(this, null) as DelegateCommand<string>;
  326. if (cmd != null)
  327. cmd.RaiseCanExecuteChanged();
  328. }
  329. }
  330. }
  331. protected virtual void InvokePropertyChanged()
  332. {
  333. Refresh();
  334. }
  335. protected virtual void InvokeBeforeUpdateProperty(Dictionary<string, object> data)
  336. {
  337. }
  338. protected virtual void InvokeAfterUpdateProperty(Dictionary<string, object> data)
  339. {
  340. }
  341. protected void UpdateValue(Dictionary<string, object> data)
  342. {
  343. if (data == null)
  344. return;
  345. UpdateSubscribe(data, this);
  346. var properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.GetCustomAttribute<SubscriptionModuleAttribute>() != null);
  347. foreach (var property in properties)
  348. {
  349. var moduleAttr = property.GetCustomAttribute<SubscriptionModuleAttribute>();
  350. UpdateSubscribe(data, property.GetValue(this), moduleAttr.Module);
  351. }
  352. }
  353. public virtual void SubscribeKeys()
  354. {
  355. SubscribeKeys(this);
  356. }
  357. protected void Subscribe(string key)
  358. {
  359. if (!string.IsNullOrEmpty(key))
  360. {
  361. _subscribedKeys.Add(key);
  362. }
  363. }
  364. public void SubscribeKeys(UiViewModelBase target)
  365. {
  366. SubscribeKeys(target, "");
  367. }
  368. public void SubscribeKeys(UiViewModelBase target, string module)
  369. {
  370. Parallel.ForEach(target.GetType().GetProperties().Where(_hasSubscriptionAttribute),
  371. property =>
  372. {
  373. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  374. string key = subscription.ModuleKey;
  375. if (!string.IsNullOrEmpty(module))
  376. {
  377. key = $"{module}.{key}";
  378. subscription.SetModule(module);
  379. }
  380. if (!_subscribedKeys.Contains(key))
  381. _subscribedKeys.Add(key);
  382. });
  383. Parallel.ForEach(target.GetType().GetFields().Where(_hasSubscriptionAttribute),
  384. method =>
  385. {
  386. SubscriptionAttribute subscription = method.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  387. string key = subscription.ModuleKey;
  388. if (!string.IsNullOrEmpty(module))
  389. {
  390. key = $"{module}.{key}";
  391. subscription.SetModule(module);
  392. }
  393. if (!_subscribedKeys.Contains(key))
  394. _subscribedKeys.Add(key);
  395. });
  396. }
  397. public virtual void UpdateSubscribe(Dictionary<string, object> data, object target, string module = null)
  398. {
  399. Parallel.ForEach(target.GetType().GetProperties().Where(_hasSubscriptionAttribute),
  400. property =>
  401. {
  402. PropertyInfo pi = (PropertyInfo)property;
  403. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  404. string key = subscription.ModuleKey;
  405. key = module == null ? key : string.Format("{0}.{1}", module, key);
  406. if (_subscribedKeys.Contains(key) && data.ContainsKey(key))
  407. {
  408. try
  409. {
  410. var convertedValue = Convert.ChangeType(data[key], pi.PropertyType);
  411. var originValue = Convert.ChangeType(pi.GetValue(target, null), pi.PropertyType);
  412. if (originValue != convertedValue)
  413. {
  414. pi.SetValue(target, convertedValue, null);
  415. }
  416. }
  417. catch (Exception ex)
  418. {
  419. LOG.Error("由RT返回的数据更新失败" + key, ex);
  420. }
  421. }
  422. });
  423. Parallel.ForEach(target.GetType().GetFields().Where(_hasSubscriptionAttribute),
  424. property =>
  425. {
  426. FieldInfo pi = (FieldInfo)property;
  427. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  428. string key = subscription.ModuleKey;
  429. if (_subscribedKeys.Contains(key) && data.ContainsKey(key))
  430. {
  431. try
  432. {
  433. var convertedValue = Convert.ChangeType(data[key], pi.FieldType);
  434. pi.SetValue(target, convertedValue);
  435. }
  436. catch (Exception ex)
  437. {
  438. LOG.Error("由RT返回的数据更新失败" + key, ex);
  439. }
  440. }
  441. });
  442. }
  443. protected override void OnActivate()
  444. {
  445. base.OnActivate();
  446. EnableTimer(true);
  447. }
  448. protected override void OnDeactivate(bool close)
  449. {
  450. base.OnDeactivate(close);
  451. if (!ActiveUpdateData)
  452. {
  453. EnableTimer(false);
  454. }
  455. }
  456. }
  457. public class IODisplay : PropertyChangedBase
  458. {
  459. public string Key { get; set; }
  460. private object _value;
  461. public object Value
  462. {
  463. get { return _value; }
  464. set { _value = value; this.NotifyOfPropertyChange(nameof(Value)); }
  465. }
  466. private Visibility _Visiable=Visibility.Visible;
  467. public Visibility Visiable
  468. {
  469. get { return _Visiable; }
  470. set { _Visiable = value; this.NotifyOfPropertyChange(nameof(Visiable)); }
  471. }
  472. }
  473. public class UiWithIOViewModelBase : UiViewModelBase
  474. {
  475. private string _ModuleIndex;
  476. public string ModuleIndex
  477. {
  478. get { return _ModuleIndex; }
  479. set { _ModuleIndex = value; InitIO(); this.NotifyOfPropertyChange(nameof(ModuleIndex)); }
  480. }
  481. public string ModuleName { get; set; }
  482. public string PMModuleName
  483. {
  484. get {
  485. string targetModule = "";
  486. switch (ModuleName)
  487. {
  488. case "CRA":
  489. case "IRA":
  490. targetModule = ModuleName;
  491. break;
  492. case "PRA":
  493. targetModule = "PRA1";
  494. break;
  495. case "PRA1":
  496. case "PRA2":
  497. int index = int.Parse(ModuleIndex.Split('_')[1]) + 1;
  498. targetModule = $"PRA{index}";
  499. break;
  500. case "UNC":
  501. case "PUP":
  502. case "RCV":
  503. case "SND":
  504. case "RJC":
  505. targetModule = "Cassette" + ModuleIndex.Split('_')[1];
  506. break;
  507. default:
  508. targetModule = "PM" + ModuleIndex.Replace("-", "_");
  509. break;
  510. }
  511. return targetModule;
  512. }
  513. }
  514. public ObservableCollection<IODisplay> SignalsList { get; set; } = new ObservableCollection<IODisplay>();
  515. private string _strDI = "DIListString";
  516. public ObservableCollection<IODisplay> DIList { get; set; } = new ObservableCollection<IODisplay>();
  517. private string _strDO = "DOListString";
  518. public ObservableCollection<IODisplay> DOList { get; set; } = new ObservableCollection<IODisplay>();
  519. private string _strAI = "AIListString";
  520. public ObservableCollection<IODisplay> AIList { get; set; } = new ObservableCollection<IODisplay>();
  521. public override void UpdateSubscribe(Dictionary<string, object> data, object target, string module = null)
  522. {
  523. UpdateValue(SignalsList,data);
  524. UpdateValue(DIList, data);
  525. UpdateValue(DOList, data);
  526. UpdateValue(AIList, data);
  527. }
  528. private void UpdateValue(ObservableCollection<IODisplay> list, Dictionary<string, object> data)
  529. {
  530. foreach (var item in list)
  531. {
  532. if (data.ContainsKey($"{PMModuleName}.{item.Key}"))
  533. item.Value = data[$"{PMModuleName}.{item.Key}"];
  534. }
  535. }
  536. protected override void OnInitialize()
  537. {
  538. base.OnInitialize();
  539. InitIO();
  540. }
  541. public void InitIO()
  542. {
  543. InitSignals();
  544. InitDI();
  545. InitDO();
  546. InitAI();
  547. }
  548. protected virtual void InitSignals()
  549. {
  550. }
  551. private void InitDI()
  552. {
  553. DIList.Clear();
  554. string strKey = $"{PMModuleName}.{_strDI}";
  555. var strDIValue = QueryDataClient.Instance.Service.PollData(new string[] { strKey });
  556. if (strDIValue == null || strDIValue.Count <= 0) return;
  557. var strDIValues = strDIValue[strKey].ToString().Split(',');
  558. foreach (var item in strDIValues)
  559. {
  560. base.Subscribe($"{PMModuleName}.{item}");
  561. DIList.Add(new IODisplay() { Key = item });
  562. }
  563. }
  564. private void InitDO()
  565. {
  566. DOList.Clear();
  567. string strKey = $"{PMModuleName}.{_strDO}";
  568. var strDOValue = QueryDataClient.Instance.Service.PollData(new string[] { strKey });
  569. if (strDOValue == null || strDOValue.Count <= 0) return;
  570. var strDOValues = strDOValue[strKey].ToString().Split(',');
  571. foreach (var item in strDOValues)
  572. {
  573. base.Subscribe($"{PMModuleName}.{item}");
  574. DOList.Add(new IODisplay() { Key = item });
  575. }
  576. }
  577. private void InitAI()
  578. {
  579. AIList.Clear();
  580. string strKey = $"{PMModuleName}.{_strAI}";
  581. var strAIValue = QueryDataClient.Instance.Service.PollData(new string[] { strKey });
  582. if (strAIValue == null || strAIValue.Count <= 0) return;
  583. var strAIValues = strAIValue[strKey].ToString().Split(',');
  584. foreach (var item in strAIValues)
  585. {
  586. base.Subscribe($"{PMModuleName}.{item}");
  587. AIList.Add(new IODisplay() { Key = item });
  588. }
  589. }
  590. protected bool GetDIValue(string key)
  591. {
  592. bool bRet=false;
  593. Parallel.ForEach(DIList.Where(x => x.Key == key),
  594. property =>
  595. {
  596. bool.TryParse(property.Value?.ToString(), out bRet);
  597. });
  598. return bRet;
  599. }
  600. protected T GetSignalValue<T>(string key)
  601. {
  602. T ret = default(T);
  603. Parallel.ForEach(SignalsList.Where(x => x.Key == key),
  604. property=>
  605. {
  606. if (property.Value != null)
  607. ret = (T)(object)property.Value;
  608. });
  609. return ret;
  610. }
  611. }
  612. }