UIViewModelBase.cs 25 KB

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