FaViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. using Aitex.Core.UI.MVVM;
  2. using Aitex.Core.Util;
  3. using Aitex.Core.Utilities;
  4. using MECF.Framework.Common.DataCenter;
  5. using MECF.Framework.Common.Equipment;
  6. using MECF.Framework.Common.OperationCenter;
  7. using OpenSEMI.ClientBase;
  8. using System;
  9. using System.Collections.Concurrent;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Reflection;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Input;
  16. using Aitex.Core.RT.Log;
  17. using MessageBox = Xceed.Wpf.Toolkit.MessageBox;
  18. using PageSCValue = MECF.Framework.UI.Core.View.Common.PageSCValue;
  19. using Prism.Mvvm;
  20. namespace CyberX8_MainPages.ViewModels
  21. {
  22. public enum FACommunicationState
  23. {
  24. Disabled,
  25. Enabled,
  26. EnabledNotCommunicating,
  27. EnabledCommunicating,
  28. WaitCRA,
  29. WaitDelay,
  30. WaitCRFromHost,
  31. }
  32. public enum FAControlState
  33. {
  34. Unknown,
  35. EquipmentOffline,
  36. AttemptOnline,
  37. HostOffline,
  38. OnlineLocal,
  39. OnlineRemote,
  40. }
  41. public enum FAControlSubState
  42. {
  43. Local,
  44. Remote,
  45. }
  46. public enum FASpoolingState
  47. {
  48. Active = 1,
  49. Inactive
  50. }
  51. public class FACommandName
  52. {
  53. public const string FAEnable = "FAEnable";
  54. public const string FADisable = "FADisable";
  55. public const string FAOnline = "FAOnline";
  56. public const string FAOffline = "FAOffline";
  57. public const string FALocal = "FALocal";
  58. public const string FARemote = "FARemote";
  59. public const string FAEnableSpooling = "FAEnableSpooling";
  60. public const string FADisableSpooling = "FADisableSpooling";
  61. public const string FASendTerminalMessage = "FASendTerminalMessage";
  62. }
  63. public class FAViewModel : BindableBase
  64. {
  65. protected ConcurrentBag<string> _subscribedKeys = new ConcurrentBag<string>();
  66. protected Func<object, bool> _isSubscriptionAttribute;
  67. protected Func<MemberInfo, bool> _hasSubscriptionAttribute;
  68. private class DataName
  69. {
  70. public const string CommunicationStatus = "CommunicationStatus";
  71. public const string ControlStatus = "ControlStatus";
  72. public const string ControlSubStatus = "ControlSubStatus";
  73. public const string SpoolingState = "SpoolingState";
  74. public const string SpoolingActual = "SpoolingActual";
  75. public const string SpoolingTotal = "SpoolingTotal";
  76. public const string SpoolingFullTime = "SpoolingFullTime";
  77. public const string SpoolingStartTime = "SpoolingStartTime";
  78. public const string IsSpoolingEnable = "IsSpoolingEnable";
  79. //EFEM
  80. }
  81. public FAViewModel()
  82. {
  83. ConfigFeedback = new PageSCFA();
  84. ConfigSetPoint = new PageSCFA();
  85. SetConfigCommand = new DelegateCommand<object>(SetConfig);
  86. InvokeCommand = new DelegateCommand<object>(Invoke);
  87. Initialize();
  88. }
  89. protected void Initialize()
  90. {
  91. _isSubscriptionAttribute = attribute => attribute is SubscriptionAttribute;
  92. _hasSubscriptionAttribute = mi => mi.GetCustomAttributes(false).Any(_isSubscriptionAttribute);
  93. Parallel.ForEach(this.GetType().GetProperties().Where(_hasSubscriptionAttribute),
  94. property =>
  95. {
  96. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  97. string key = subscription.ModuleKey;
  98. if (!_subscribedKeys.Contains(key))
  99. _subscribedKeys.Add(key);
  100. });
  101. _timer = new PeriodicJob(200, this.OnTimer, "UIUpdaterThread - " + GetType().Name, true);
  102. ConfigSetPoint.Update(QueryDataClient.Instance.Service.PollConfig(ConfigSetPoint.GetKeys()));
  103. }
  104. #region subscrib
  105. [Subscription(DataName.IsSpoolingEnable, ModuleNameString.System)]
  106. public bool IsSpoolingEnable
  107. {
  108. get { return _isSpoolingEnable; }
  109. set
  110. {
  111. IsEnableSpoolingEnableButton = !value;
  112. IsEnableSpoolingDisableButton = value;
  113. _isSpoolingEnable = value;
  114. }
  115. }
  116. private bool _isSpoolingEnable = false;
  117. [Subscription("System.SpoolingState")]
  118. public string SpoolingState
  119. {
  120. get;
  121. set;
  122. }
  123. [Subscription(DataName.SpoolingActual, ModuleNameString.System)]
  124. public string SpoolingActual
  125. {
  126. get;
  127. set;
  128. }
  129. [Subscription(DataName.SpoolingTotal, ModuleNameString.System)]
  130. public string SpoolingTotal
  131. {
  132. get;
  133. set;
  134. }
  135. [Subscription(DataName.SpoolingFullTime, ModuleNameString.System)]
  136. public string SpoolingFullTime
  137. {
  138. get;
  139. set;
  140. }
  141. [Subscription(DataName.SpoolingStartTime, ModuleNameString.System)]
  142. public string SpoolingStartTime
  143. {
  144. get;
  145. set;
  146. }
  147. [Subscription(DataName.ControlStatus, ModuleNameString.System)]
  148. public string HostControlStatus
  149. {
  150. get { return _hostControlStatus; }
  151. set
  152. {
  153. IsEnableOnlineButton = FACommunicationState == FACommunicationState.EnabledCommunicating && (value == FAControlState.Unknown.ToString() || value == FAControlState.EquipmentOffline.ToString());
  154. IsEnableOfflineButton = FACommunicationState == FACommunicationState.EnabledCommunicating && (value == FAControlState.Unknown.ToString() || value != FAControlState.EquipmentOffline.ToString());
  155. IsEnableRemoteButton = FACommunicationState == FACommunicationState.EnabledCommunicating&&value==FAControlState.OnlineLocal.ToString();
  156. IsEnableLocalButton = FACommunicationState == FACommunicationState.EnabledCommunicating && value == FAControlState.OnlineRemote.ToString();
  157. _hostControlStatus = value;
  158. }
  159. }
  160. private string _hostControlStatus;
  161. [Subscription(DataName.CommunicationStatus, ModuleNameString.System)]
  162. public string HostCommunicationStatus
  163. {
  164. get { return _hostCommunicationStatus; }
  165. set
  166. {
  167. if(_hostCommunicationStatus == FACommunicationState.Disabled.ToString())
  168. {
  169. IsEnableEnableButton = true;
  170. IsEnableDisableButton = false;
  171. }
  172. else
  173. {
  174. IsEnableEnableButton = false;
  175. IsEnableDisableButton = true;
  176. }
  177. _hostCommunicationStatus = value;
  178. }
  179. }
  180. private string _hostCommunicationStatus;
  181. #endregion
  182. #region logic
  183. public FACommunicationState FACommunicationState
  184. {
  185. get
  186. {
  187. return string.IsNullOrEmpty(HostCommunicationStatus) ? FACommunicationState.Disabled
  188. : (FACommunicationState)Enum.Parse(typeof(FACommunicationState), HostCommunicationStatus);
  189. }
  190. }
  191. public FAControlState FAControlState
  192. {
  193. get
  194. {
  195. return string.IsNullOrEmpty(HostControlStatus) ? FAControlState.Unknown
  196. : (FAControlState)Enum.Parse(typeof(FAControlState), HostControlStatus);
  197. }
  198. }
  199. //Disabled,
  200. //Enabled,
  201. //EnabledNotCommunicating,
  202. //EnabledCommunicating,
  203. //WaitCRA,
  204. //WaitDelay,
  205. //WaitCRFromHost,
  206. public bool IsEnableEnableButton
  207. {
  208. get { return _isEnableEnableButton; }
  209. set { SetProperty(ref _isEnableEnableButton, value); }
  210. }
  211. private bool _isEnableEnableButton = false;
  212. public bool IsEnableDisableButton
  213. {
  214. get { return _isEnableDisableButton; }
  215. set { SetProperty(ref _isEnableDisableButton, value); }
  216. }
  217. private bool _isEnableDisableButton;
  218. //Unknown,
  219. //EquipmentOffline,
  220. //AttemptOnline,
  221. //HostOffline,
  222. //OnlineLocal,
  223. //OnlineRemote,
  224. public bool IsEnableOnlineButton
  225. {
  226. get { return _isEnableOnlineButton; }
  227. set { SetProperty(ref _isEnableOnlineButton, value); }
  228. }
  229. private bool _isEnableOnlineButton;
  230. public bool IsEnableOfflineButton
  231. {
  232. get { return _isEnableOfflineButton; }
  233. set { SetProperty(ref _isEnableOfflineButton, value); }
  234. }
  235. private bool _isEnableOfflineButton;
  236. public bool IsEnableLocalButton
  237. {
  238. get { return _isEnableLocalButton; }
  239. set { SetProperty(ref _isEnableLocalButton, value); }
  240. }
  241. private bool _isEnableLocalButton;
  242. public bool IsEnableRemoteButton
  243. {
  244. get { return _isEnableRemoteButton; }
  245. set { SetProperty(ref _isEnableRemoteButton,value); }
  246. }
  247. private bool _isEnableRemoteButton;
  248. public bool IsEnableSpoolingEnableButton
  249. {
  250. get
  251. {
  252. return _isEnableSpoolingEnableButton;//SpoolingState == FASpoolingState.Inactive.ToString();
  253. }
  254. set
  255. {
  256. SetProperty(ref _isEnableSpoolingEnableButton, value);
  257. }
  258. }
  259. private bool _isEnableSpoolingEnableButton;
  260. public bool IsEnableSpoolingDisableButton
  261. {
  262. get
  263. {
  264. return _isEnableSpoolingDisableButton;// SpoolingState == FASpoolingState.Active.ToString();
  265. }
  266. set
  267. {
  268. SetProperty(ref _isEnableSpoolingDisableButton, value);
  269. }
  270. }
  271. private bool _isEnableSpoolingDisableButton;
  272. #endregion
  273. [IgnorePropertyChange]
  274. public PageSCFA ConfigFeedback
  275. {
  276. get;
  277. set;
  278. }
  279. [IgnorePropertyChange]
  280. public PageSCFA ConfigSetPoint
  281. {
  282. get;
  283. set;
  284. }
  285. [IgnorePropertyChange]
  286. public ICommand SetConfigCommand
  287. {
  288. get;
  289. private set;
  290. }
  291. [IgnorePropertyChange]
  292. public ICommand InvokeCommand
  293. {
  294. get;
  295. private set;
  296. }
  297. PeriodicJob _timer;
  298. //protected ConcurrentBag<string> _subscribedKeys = new ConcurrentBag<string>();
  299. //protected Func<object, bool> _isSubscriptionAttribute;
  300. //protected Func<MemberInfo, bool> _hasSubscriptionAttribute;
  301. private void Invoke(object param)
  302. {
  303. InvokeClient.Instance.Service.DoOperation("FACommand", ((string)param).Split(',')[1]);
  304. }
  305. void SetConfig(object param)
  306. {
  307. object[] sc = (object[])param;
  308. InvokeClient.Instance.Service.DoOperation("System.SetConfig", sc[0].ToString(), sc[1].ToString());
  309. UpdateConfig();
  310. }
  311. public void UpdateConfig()
  312. {
  313. ConfigFeedback.Update(QueryDataClient.Instance.Service.PollConfig(ConfigFeedback.GetKeys()));
  314. this.RaisePropertyChanged("ConfigFeedback");
  315. }
  316. private bool OnTimer()
  317. {
  318. try
  319. {
  320. Poll();
  321. UpdateConfig();
  322. }
  323. catch
  324. {
  325. }
  326. return true;
  327. }
  328. private void Poll()
  329. {
  330. if (_subscribedKeys.Count > 0)
  331. {
  332. Dictionary<string, object> result = QueryDataClient.Instance.Service.PollData(_subscribedKeys);
  333. if (result == null)
  334. {
  335. return;
  336. }
  337. if (result.Count != _subscribedKeys.Count)
  338. {
  339. string unknowKeys = string.Empty;
  340. foreach (string key in _subscribedKeys)
  341. {
  342. if (!result.ContainsKey(key))
  343. {
  344. unknowKeys += key + "\r\n";
  345. }
  346. }
  347. //System.Diagnostics.Debug.Assert(false, unknowKeys);
  348. }
  349. UpdateValue(result);
  350. }
  351. }
  352. void UpdateValue(Dictionary<string, object> data)
  353. {
  354. if (data == null)
  355. return;
  356. UpdateSubscribe(data, this);
  357. }
  358. void UpdateSubscribe(Dictionary<string, object> data, object target, string module = null)
  359. {
  360. Parallel.ForEach(target.GetType().GetProperties().Where(_hasSubscriptionAttribute),
  361. property =>
  362. {
  363. PropertyInfo pi = (PropertyInfo)property;
  364. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  365. string key = subscription.ModuleKey;
  366. key = module == null ? key : string.Format("{0}.{1}", module, key);
  367. if (_subscribedKeys.Contains(key) && data.ContainsKey(key))
  368. {
  369. try
  370. {
  371. var convertedValue = Convert.ChangeType(data[key], pi.PropertyType);
  372. var originValue = Convert.ChangeType(pi.GetValue(target, null), pi.PropertyType);
  373. if (originValue != convertedValue)
  374. {
  375. pi.SetValue(target, convertedValue, null);
  376. RaisePropertyChanged(pi.Name);
  377. }
  378. }
  379. catch
  380. {
  381. }
  382. }
  383. });
  384. }
  385. }
  386. public class PageSCFA : PageSCValue
  387. {
  388. public string Fa_ConnectionMode { get; set; }
  389. public string FA_LocalIpAddress { get; set; }
  390. public int FA_LocalPortNumber { get; set; }
  391. public string FA_RemoteIpAddress { get; set; }
  392. public int FA_RemotePortNumber { get; set; }
  393. public int FA_T3Timeout { get; set; }
  394. public int FA_T5Timeout { get; set; }
  395. public int FA_T6Timeout { get; set; }
  396. public int FA_T7Timeout { get; set; }
  397. public int FA_T8Timeout { get; set; }
  398. public bool FA_EnableSpooling { get; set; }
  399. public int FA_LinkTestInterval { get; set; }
  400. public string FA_DefaultCommunicationState { get; set; }
  401. public string FA_DefaultControlState { get; set; }
  402. public string FA_DefaultControlSubState { get; set; }
  403. public string FA_DeviceId { get; set; }
  404. public int FA_SpoolingCapability { get; set; }
  405. public int FA_SpoolingMaxTransmit { get; set; }
  406. public string FA_SpoolingFullOverWrite { get; set; }
  407. public List<string> FA_SpoolingFullOverWriteLst
  408. {
  409. get { return new List<string>() { "True", "False" }; }
  410. }
  411. public List<string> FA_CommunicateStateLst
  412. {
  413. get { return new List<string>() {"Enabled","Disabled" };}
  414. }
  415. public List<string> FA_ControlStateLst
  416. {
  417. get { return new List<string>() { "Online", "Offline" }; }
  418. }
  419. public List<string> FA_ControlSubStateLst
  420. {
  421. get { return new List<string>() { "Local", "Remote" }; }
  422. }
  423. public PageSCFA()
  424. {
  425. UpdateKeys(typeof(PageSCFA).GetProperties());
  426. }
  427. }
  428. }