FAViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 MECF.Framework.UI.Client.ClientBase;
  18. using MessageBox = Xceed.Wpf.Toolkit.MessageBox;
  19. using SCValue = MECF.Framework.UI.Core.View.Common.SCValue;
  20. using MECF.Framework.UI.Core.View.Common;
  21. namespace MECF.Framework.UI.Client.CenterViews.Operations.FA
  22. {
  23. public enum FACommunicationState
  24. {
  25. Disabled,
  26. Enabled,
  27. EnabledNotCommunicating,
  28. EnabledCommunicating,
  29. WaitCRA,
  30. WaitDelay,
  31. WaitCRFromHost,
  32. }
  33. public enum FAControlState
  34. {
  35. Unknown,
  36. EquipmentOffline,
  37. AttemptOnline,
  38. HostOffline,
  39. OnlineLocal,
  40. OnlineRemote,
  41. }
  42. public enum FAControlSubState
  43. {
  44. Local,
  45. Remote,
  46. }
  47. public enum FASpoolingState
  48. {
  49. Active = 1,
  50. Inactive
  51. }
  52. public class FACommandName
  53. {
  54. public const string FAEnable = "FAEnable";
  55. public const string FADisable = "FADisable";
  56. public const string FAOnline = "FAOnline";
  57. public const string FAOffline = "FAOffline";
  58. public const string FALocal = "FALocal";
  59. public const string FARemote = "FARemote";
  60. public const string FAEnableSpooling = "FAEnableSpooling";
  61. public const string FADisableSpooling = "FADisableSpooling";
  62. public const string FASendTerminalMessage = "FASendTerminalMessage";
  63. }
  64. public class FAViewModel : UiViewModelBase
  65. {
  66. public bool IsPermission { get => this.Permission == 3; }
  67. private class DataName
  68. {
  69. public const string CommunicationStatus = "CommunicationStatus";
  70. public const string ControlStatus = "ControlStatus";
  71. public const string ControlSubStatus = "ControlSubStatus";
  72. public const string SpoolingState = "SpoolingState";
  73. public const string SpoolingActual = "SpoolingActual";
  74. public const string SpoolingTotal = "SpoolingTotal";
  75. public const string SpoolingFullTime = "SpoolingFullTime";
  76. public const string SpoolingStartTime = "SpoolingStartTime";
  77. public const string IsSpoolingEnable = "IsSpoolingEnable";
  78. //EFEM
  79. }
  80. public FAViewModel()
  81. {
  82. this.DisplayName = "FA";
  83. ConfigFeedback = new PageSCFA();
  84. ConfigSetPoint = new PageSCFA();
  85. SetConfigCommand = new DelegateCommand<object>(SetConfig);
  86. SetConfig2Command = new DelegateCommand<object>(SetConfig2);
  87. InitializeCommand = new DelegateCommand<object>(InitializeFa);
  88. InvokeCommand = new DelegateCommand<object>(Invoke);
  89. }
  90. protected override void OnInitialize()
  91. {
  92. base.OnInitialize();
  93. _isSubscriptionAttribute = attribute => attribute is SubscriptionAttribute;
  94. _hasSubscriptionAttribute = mi => mi.GetCustomAttributes(false).Any(_isSubscriptionAttribute);
  95. Parallel.ForEach(this.GetType().GetProperties().Where(_hasSubscriptionAttribute),
  96. property =>
  97. {
  98. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  99. string key = subscription.ModuleKey;
  100. if (!_subscribedKeys.Contains(key))
  101. _subscribedKeys.Add(key);
  102. });
  103. _timer = new PeriodicJob(1000, this.OnTimer, "UIUpdaterThread - " + GetType().Name, true);
  104. }
  105. #region subscrib
  106. [Subscription(DataName.IsSpoolingEnable, ModuleNameString.System)]
  107. public bool IsSpoolingEnable
  108. {
  109. get;
  110. set;
  111. }
  112. [Subscription("System.SpoolingState")]
  113. public string SpoolingState
  114. {
  115. get;
  116. set;
  117. }
  118. [Subscription(DataName.SpoolingActual, ModuleNameString.System)]
  119. public string SpoolingActual
  120. {
  121. get;
  122. set;
  123. }
  124. [Subscription(DataName.SpoolingTotal, ModuleNameString.System)]
  125. public string SpoolingTotal
  126. {
  127. get;
  128. set;
  129. }
  130. [Subscription(DataName.SpoolingFullTime, ModuleNameString.System)]
  131. public string SpoolingFullTime
  132. {
  133. get;
  134. set;
  135. }
  136. [Subscription(DataName.SpoolingStartTime, ModuleNameString.System)]
  137. public string SpoolingStartTime
  138. {
  139. get;
  140. set;
  141. }
  142. [Subscription(DataName.ControlStatus, ModuleNameString.System)]
  143. public string HostControlStatus
  144. {
  145. get;
  146. set;
  147. }
  148. [Subscription(DataName.CommunicationStatus, ModuleNameString.System)]
  149. public string HostCommunicationStatus
  150. {
  151. get;
  152. set;
  153. }
  154. #endregion
  155. #region logic
  156. public FACommunicationState FACommunicationState
  157. {
  158. get
  159. {
  160. return string.IsNullOrEmpty(HostCommunicationStatus) ? FACommunicationState.Disabled
  161. : (FACommunicationState)Enum.Parse(typeof(FACommunicationState), HostCommunicationStatus);
  162. }
  163. }
  164. public FAControlState FAControlState
  165. {
  166. get
  167. {
  168. return string.IsNullOrEmpty(HostControlStatus) ? FAControlState.Unknown
  169. : (FAControlState)Enum.Parse(typeof(FAControlState), HostControlStatus);
  170. }
  171. }
  172. //Disabled,
  173. //Enabled,
  174. //EnabledNotCommunicating,
  175. //EnabledCommunicating,
  176. //WaitCRA,
  177. //WaitDelay,
  178. //WaitCRFromHost,
  179. public bool IsEnableEnableButton
  180. {
  181. get { return FACommunicationState == FACommunicationState.Disabled; }
  182. }
  183. public bool IsEnableDisableButton
  184. {
  185. get { return FACommunicationState != FACommunicationState.Disabled; }
  186. }
  187. //Unknown,
  188. //EquipmentOffline,
  189. //AttemptOnline,
  190. //HostOffline,
  191. //OnlineLocal,
  192. //OnlineRemote,
  193. public bool IsEnableOnlineButton
  194. {
  195. get { return FACommunicationState == FACommunicationState.EnabledCommunicating && (FAControlState == FAControlState.Unknown || FAControlState == FAControlState.EquipmentOffline); }
  196. }
  197. public bool IsEnableOfflineButton
  198. {
  199. get { return FACommunicationState == FACommunicationState.EnabledCommunicating && (FAControlState == FAControlState.Unknown || FAControlState != FAControlState.EquipmentOffline); }
  200. }
  201. public bool IsEnableLocalButton
  202. {
  203. get { return FACommunicationState == FACommunicationState.EnabledCommunicating && (FAControlState == FAControlState.OnlineRemote); }
  204. }
  205. public bool IsEnableRemoteButton
  206. {
  207. get { return FACommunicationState == FACommunicationState.EnabledCommunicating && (FAControlState == FAControlState.OnlineLocal); }
  208. }
  209. public bool IsEnableSpoolingEnableButton
  210. {
  211. get
  212. {
  213. return !IsSpoolingEnable;//SpoolingState == FASpoolingState.Inactive.ToString();
  214. }
  215. }
  216. public bool IsEnableSpoolingDisableButton
  217. {
  218. get
  219. {
  220. return IsSpoolingEnable;// SpoolingState == FASpoolingState.Active.ToString();
  221. }
  222. }
  223. #endregion
  224. [IgnorePropertyChange]
  225. public PageSCFA ConfigFeedback
  226. {
  227. get;
  228. set;
  229. }
  230. [IgnorePropertyChange]
  231. public PageSCFA ConfigSetPoint
  232. {
  233. get;
  234. set;
  235. }
  236. [IgnorePropertyChange]
  237. public ICommand SetConfigCommand
  238. {
  239. get;
  240. private set;
  241. }
  242. [IgnorePropertyChange]
  243. public ICommand SetConfig2Command
  244. {
  245. get;
  246. private set;
  247. }
  248. [IgnorePropertyChange]
  249. public ICommand InitializeCommand
  250. {
  251. get;
  252. private set;
  253. }
  254. [IgnorePropertyChange]
  255. public ICommand InvokeCommand
  256. {
  257. get;
  258. private set;
  259. }
  260. PeriodicJob _timer;
  261. //protected ConcurrentBag<string> _subscribedKeys = new ConcurrentBag<string>();
  262. //protected Func<object, bool> _isSubscriptionAttribute;
  263. //protected Func<MemberInfo, bool> _hasSubscriptionAttribute;
  264. private void Invoke(object param)
  265. {
  266. InvokeClient.Instance.Service.DoOperation("FACommand", ((string)param).Split(',')[1]);
  267. }
  268. private void InitializeFa(object obj)
  269. {
  270. if (MessageBox.Show("Are you sure you want to re-initialize FA connection?",
  271. "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  272. {
  273. InvokeClient.Instance.Service.DoOperation("Fa.Initialize");
  274. }
  275. }
  276. void SetConfig(object param)
  277. {
  278. object[] sc = (object[])param;
  279. InvokeClient.Instance.Service.DoOperation("System.SetConfig", sc[0].ToString(), sc[1].ToString());
  280. UpdateConfig();
  281. }
  282. void SetConfig2(object param)
  283. {
  284. string id = (string)param;
  285. string key = id.Replace("Fa.", "Fa_");
  286. PropertyInfo[] property = typeof(PageSCFA).GetProperties();
  287. foreach (PropertyInfo prop in property)
  288. {
  289. if (prop.Name == key)
  290. {
  291. InvokeClient.Instance.Service.DoOperation("System.SetConfig", id, prop.GetValue(ConfigSetPoint, null).ToString());
  292. break;
  293. }
  294. }
  295. UpdateConfig();
  296. }
  297. public void UpdateConfig()
  298. {
  299. ConfigFeedback.Update(QueryDataClient.Instance.Service.PollConfig(ConfigFeedback.GetKeys()));
  300. NotifyOfPropertyChange("ConfigFeedback");
  301. ConfigSetPoint.Update(QueryDataClient.Instance.Service.PollConfig(ConfigSetPoint.GetKeys()));
  302. }
  303. bool OnTimer()
  304. {
  305. try
  306. {
  307. Poll();
  308. UpdateConfig();
  309. }
  310. catch (Exception ex)
  311. {
  312. LOG.Error(ex.Message);
  313. }
  314. return true;
  315. }
  316. void Poll()
  317. {
  318. if (_subscribedKeys.Count > 0)
  319. {
  320. Dictionary<string, object> result = QueryDataClient.Instance.Service.PollData(_subscribedKeys);
  321. if (result == null)
  322. {
  323. LOG.Error("获取RT数据失败");
  324. return;
  325. }
  326. if (result.Count != _subscribedKeys.Count)
  327. {
  328. string unknowKeys = string.Empty;
  329. foreach (string key in _subscribedKeys)
  330. {
  331. if (!result.ContainsKey(key))
  332. {
  333. unknowKeys += key + "\r\n";
  334. }
  335. }
  336. //System.Diagnostics.Debug.Assert(false, unknowKeys);
  337. }
  338. UpdateValue(result);
  339. }
  340. }
  341. void UpdateValue(Dictionary<string, object> data)
  342. {
  343. if (data == null)
  344. return;
  345. UpdateSubscribe(data, this);
  346. }
  347. void UpdateSubscribe(Dictionary<string, object> data, object target, string module = null)
  348. {
  349. Parallel.ForEach(target.GetType().GetProperties().Where(_hasSubscriptionAttribute),
  350. property =>
  351. {
  352. PropertyInfo pi = (PropertyInfo)property;
  353. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  354. string key = subscription.ModuleKey;
  355. key = module == null ? key : string.Format("{0}.{1}", module, key);
  356. if (_subscribedKeys.Contains(key) && data.ContainsKey(key))
  357. {
  358. try
  359. {
  360. var convertedValue = Convert.ChangeType(data[key], pi.PropertyType);
  361. var originValue = Convert.ChangeType(pi.GetValue(target, null), pi.PropertyType);
  362. if (originValue != convertedValue)
  363. {
  364. pi.SetValue(target, convertedValue, null);
  365. NotifyOfPropertyChange(pi.Name);
  366. }
  367. }
  368. catch (Exception ex)
  369. {
  370. LOG.Error("由RT返回的数据更新失败" + key, ex);
  371. }
  372. }
  373. });
  374. }
  375. //protected override void InvokeBeforeUpdateProperty(Dictionary<string, object> data)
  376. //{
  377. //}
  378. }
  379. public class PageSCFA : PageSCValue
  380. {
  381. public string Fa_ConnectionMode { get; set; }
  382. public string Fa_LocalIpAddress { get; set; }
  383. public int Fa_LocalPortNumber { get; set; }
  384. public string Fa_RemoteIpAddress { get; set; }
  385. public int Fa_RemotePortNumber { get; set; }
  386. public int Fa_T3Timeout { get; set; }
  387. public int Fa_T5Timeout { get; set; }
  388. public int Fa_T6Timeout { get; set; }
  389. public int Fa_T7Timeout { get; set; }
  390. public int Fa_T8Timeout { get; set; }
  391. public bool Fa_EnableSpooling { get; set; }
  392. public int Fa_LinkTestInterval { get; set; }
  393. public string Fa_DefaultCommunicationState { get; set; }
  394. public string Fa_DefaultControlState { get; set; }
  395. public string Fa_DefaultControlSubState { get; set; }
  396. public string Fa_DeviceId { get; set; }
  397. public PageSCFA()
  398. {
  399. UpdateKeys(typeof(PageSCFA).GetProperties());
  400. }
  401. }
  402. }