UIViewModelBase.cs 24 KB

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