TopViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. using Aitex.Core.Common.DeviceData;
  2. using Aitex.Core.RT.Event;
  3. using Aitex.Core.WCF;
  4. using FabConnect.SecsGemInterface.GemModel;
  5. using MECF.Framework.Common.DataCenter;
  6. using MECF.Framework.Common.OperationCenter;
  7. using Prism.Commands;
  8. using Prism.Mvvm;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using System.Windows.Threading;
  18. using Venus_Core;
  19. using Venus_MainPages.Unity;
  20. using Venus_Themes.UserControls;
  21. namespace Venus_MainPages.ViewModels
  22. {
  23. internal class TopViewModel : BindableBase
  24. {
  25. #region 私有字段
  26. private string m_Title;
  27. private string m_SoftwareVersion;
  28. private List<string> m_RtDataKeys=new List<string>();
  29. private Dictionary<string, object> m_RtDataValues;
  30. //private string ModuleName;
  31. private ObservableCollection<EventItem> m_EventLogList = new ObservableCollection<EventItem>();
  32. private EventItem m_CurrentEventItem=new EventItem ();
  33. private string m_CurrentEventItemValue;
  34. private object _lockObj = new object();
  35. private int m_EventLogListSelectedIndex;
  36. private Queue<EventItem> alarmQuery = new Queue<EventItem>();//控制alarm log 在 top UI显示
  37. private int logMaxCount = 50;//log在ui最多显示数量
  38. private AITSignalTowerData m_SignalTowerData;
  39. private string m_HostCommunicationStatus;
  40. private string m_TimeTick;
  41. private JetChamber m_SelectedJetChamber = JetChamber.None;
  42. private bool m_PMAIsInstalled;
  43. private bool m_PMBIsInstalled;
  44. private bool m_PMCIsInstalled;
  45. private bool m_PMDIsInstalled;
  46. private bool m_LLAIsInstalled;
  47. private bool m_LLBIsInstalled;
  48. private bool m_TMIsInstalled;
  49. private bool m_EFEMIsInstalled;
  50. #endregion
  51. #region 属性
  52. public string Title
  53. {
  54. get { return m_Title; }
  55. set { SetProperty(ref m_Title, value); }
  56. }
  57. public string SoftwareVersion
  58. {
  59. get { return m_SoftwareVersion; }
  60. set { SetProperty(ref m_SoftwareVersion, value); }
  61. }
  62. public string TimeTick
  63. {
  64. get { return m_TimeTick; }
  65. set { SetProperty(ref m_TimeTick, value); }
  66. }
  67. public Dictionary<string, object> RtDataValues
  68. {
  69. get { return m_RtDataValues; }
  70. set { SetProperty(ref m_RtDataValues, value); }
  71. }
  72. public ObservableCollection<EventItem> EventLogList
  73. {
  74. get { return m_EventLogList; }
  75. set { SetProperty(ref m_EventLogList, value); }
  76. }
  77. public EventItem CurrentEventItem
  78. {
  79. get { return m_CurrentEventItem; }
  80. set { SetProperty(ref m_CurrentEventItem, value); }
  81. }
  82. public int EventLogListSelectedIndex
  83. {
  84. get { return m_EventLogListSelectedIndex; }
  85. set { SetProperty(ref m_EventLogListSelectedIndex, value); }
  86. }
  87. public string CurrentEventItemValue
  88. {
  89. get { return m_CurrentEventItemValue; }
  90. set { SetProperty(ref m_CurrentEventItemValue, value); }
  91. }
  92. public AITSignalTowerData SignalTowerData
  93. {
  94. get { return m_SignalTowerData; }
  95. set { SetProperty(ref m_SignalTowerData, value); }
  96. }
  97. public string HostCommunicationStatusBackground
  98. {
  99. get
  100. {
  101. switch (HostCommunicationStatus)
  102. {
  103. case "Disabled":
  104. return "Yellow";
  105. case "Enabled":
  106. case "EnabledNotCommunicating":
  107. case "WaitCRA":
  108. case "WaitDelay":
  109. case "WaitCRFromHost":
  110. return "Transparent";
  111. case "EnabledCommunicating":
  112. return "LawnGreen";
  113. default:
  114. return "Yellow";
  115. }
  116. }
  117. }
  118. public string m_HostBack;
  119. public string HostBack
  120. {
  121. get { return m_HostBack; }
  122. set { SetProperty(ref m_HostBack, value); }
  123. }
  124. public string HostCommunicationStatus
  125. {
  126. get { return m_HostCommunicationStatus; }
  127. set { SetProperty(ref m_HostCommunicationStatus, value); }
  128. }
  129. public bool IsEnableFAEnable
  130. {
  131. get
  132. {
  133. return HostCommunicationStatus == "Disabled";
  134. }
  135. }
  136. public bool _IsDisableEnable;
  137. public bool IsEnableEnable
  138. {
  139. get { return _IsDisableEnable; }
  140. set { SetProperty(ref _IsDisableEnable, value); }
  141. }
  142. public bool _IsEnableDisable;
  143. public bool IsEnableDisable
  144. {
  145. get { return _IsEnableDisable; }
  146. set { SetProperty(ref _IsEnableDisable, value); }
  147. }
  148. public bool IsDisableFAEnable
  149. {
  150. get
  151. {
  152. return HostCommunicationStatus != "Disabled";
  153. }
  154. }
  155. public CommunicationState FACommunicationState
  156. {
  157. get
  158. {
  159. return string.IsNullOrEmpty(HostCommunicationStatus) ? CommunicationState.Disabled
  160. : (CommunicationState)Enum.Parse(typeof(CommunicationState), HostCommunicationStatus);
  161. }
  162. }
  163. public bool PMAIsInstalled
  164. {
  165. get { return m_PMAIsInstalled; }
  166. set { SetProperty(ref m_PMAIsInstalled, value); }
  167. }
  168. public bool PMBIsInstalled
  169. {
  170. get { return m_PMBIsInstalled; }
  171. set { SetProperty(ref m_PMBIsInstalled, value); }
  172. }
  173. public bool PMCIsInstalled
  174. {
  175. get { return m_PMCIsInstalled; }
  176. set { SetProperty(ref m_PMCIsInstalled, value); }
  177. }
  178. public bool PMDIsInstalled
  179. {
  180. get { return m_PMDIsInstalled; }
  181. set { SetProperty(ref m_PMDIsInstalled, value); }
  182. }
  183. public bool LLAIsInstalled
  184. {
  185. get { return m_LLAIsInstalled; }
  186. set { SetProperty(ref m_LLAIsInstalled, value); }
  187. }
  188. public bool LLBIsInstalled
  189. {
  190. get { return m_LLBIsInstalled; }
  191. set { SetProperty(ref m_LLBIsInstalled, value); }
  192. }
  193. public bool TMIsInstalled
  194. {
  195. get { return m_TMIsInstalled; }
  196. set { SetProperty(ref m_TMIsInstalled, value); }
  197. }
  198. public bool EFEMIsInstalled
  199. {
  200. get { return m_EFEMIsInstalled; }
  201. set { SetProperty(ref m_EFEMIsInstalled, value); }
  202. }
  203. #endregion
  204. #region 命令
  205. private DelegateCommand _SwichLanguageCommand;
  206. public DelegateCommand SwichLanguageCommand =>
  207. _SwichLanguageCommand ?? (_SwichLanguageCommand = new DelegateCommand(OnSwitchLanguage));
  208. private DelegateCommand _ResetCommand;
  209. public DelegateCommand ResetCommand =>
  210. _ResetCommand ?? (_ResetCommand = new DelegateCommand(OnReset));
  211. private DelegateCommand _ClearCommand;
  212. public DelegateCommand ClearCommand =>
  213. _ClearCommand ?? (_ClearCommand = new DelegateCommand(OnClear));
  214. private DelegateCommand _SkipCommand;
  215. public DelegateCommand SkipCommand =>
  216. _SkipCommand ?? (_SkipCommand = new DelegateCommand(OnSkip));
  217. //private DelegateCommand _BuzzerOffCommand;
  218. //public DelegateCommand BuzzerOffCommand =>
  219. // _BuzzerOffCommand ?? (_BuzzerOffCommand = new DelegateCommand(OnBuzzerOff));
  220. private DelegateCommand _FADisableCommand;
  221. public DelegateCommand FADisableCommand =>
  222. _FADisableCommand ?? (_FADisableCommand = new DelegateCommand(FaDisable));
  223. private DelegateCommand _FAEnableCommand;
  224. public DelegateCommand FAEnableCommand =>
  225. _FAEnableCommand ?? (_FAEnableCommand = new DelegateCommand(FaEnable));
  226. #endregion
  227. #region 构造函数
  228. public TopViewModel()
  229. {
  230. JetChamber selectedChamber = (JetChamber)(Convert.ToInt32(QueryDataClient.Instance.Service.GetConfig("System.ChamberSelect")));
  231. if (selectedChamber == JetChamber.Venus)
  232. {
  233. Title = "Venus";
  234. }
  235. else
  236. {
  237. Title = "Kepler";
  238. }
  239. //Title = selectedChamber.ToString();
  240. m_SoftwareVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
  241. string allModules = QueryDataClient.Instance.Service.GetConfig($"System.InstalledModules").ToString();
  242. PMAIsInstalled = allModules.Contains("PMA");
  243. PMBIsInstalled = allModules.Contains("PMB");
  244. PMCIsInstalled = allModules.Contains("PMC");
  245. PMDIsInstalled = allModules.Contains("PMD");
  246. LLAIsInstalled = allModules.Contains("LLA");
  247. LLBIsInstalled = allModules.Contains("LLB");
  248. TMIsInstalled = allModules.Contains("TM");
  249. EFEMIsInstalled = allModules.Contains("EFEM");
  250. //ModuleName = "PMA";
  251. addDataKeys();
  252. DispatcherTimer timer = new DispatcherTimer();
  253. timer.Interval = TimeSpan.FromSeconds(1);
  254. timer.Tick += timer_Tick;
  255. timer.Start();
  256. EventClient.Instance.OnEvent += Instance_OnEvent;
  257. EventClient.Instance.Start();
  258. }
  259. #endregion
  260. #region 方法
  261. void timer_Tick(object sender, EventArgs e)
  262. {
  263. TimeTick = DateTime.Now.ToString();
  264. RtDataValues = QueryDataClient.Instance.Service.PollData(m_RtDataKeys);
  265. SignalTowerData = CommonFunction.GetValue<AITSignalTowerData>(RtDataValues, $"System.SignalTower.DeviceData");
  266. HostCommunicationStatus= CommonFunction.GetValue<String>(RtDataValues,"System.CommunicationStatus");
  267. IsEnableEnable = FACommunicationState == CommunicationState.Disabled;
  268. IsEnableDisable = FACommunicationState != CommunicationState.Disabled;
  269. HostBack = HostCommunicationStatusBackground;
  270. }
  271. private void OnSwitchLanguage()
  272. {
  273. List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();
  274. foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
  275. {
  276. if (dictionary.Source != null)
  277. {
  278. dictionaryList.Add(dictionary);
  279. }
  280. }
  281. string requestedCulture1 = @"/Venus_Themes;component/Languages/StringResources.en-US.xaml";
  282. string requestedCulture2 = @"/Venus_Themes;component/Languages/StringResources.zh-CN.xaml";
  283. ResourceDictionary resourceDictionary1 = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture1));
  284. ResourceDictionary resourceDictionary2 = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture2));
  285. if (dictionaryList.IndexOf(resourceDictionary1) < dictionaryList.IndexOf(resourceDictionary2))
  286. {
  287. Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary1);
  288. Application.Current.Resources.MergedDictionaries.Add(resourceDictionary1);
  289. }
  290. else
  291. {
  292. Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary2);
  293. Application.Current.Resources.MergedDictionaries.Add(resourceDictionary2);
  294. }
  295. }
  296. private void OnReset()
  297. {
  298. InvokeClient.Instance.Service.DoOperation("System.Reset");
  299. }
  300. private void addDataKeys()
  301. {
  302. if (PMAIsInstalled == true)
  303. {
  304. m_RtDataKeys.Add("PMA.FsmState");
  305. }
  306. if (PMBIsInstalled == true)
  307. {
  308. m_RtDataKeys.Add("PMB.FsmState");
  309. }
  310. if (PMCIsInstalled == true)
  311. {
  312. m_RtDataKeys.Add("PMC.FsmState");
  313. }
  314. if (PMDIsInstalled == true)
  315. {
  316. m_RtDataKeys.Add("PMD.FsmState");
  317. }
  318. if (TMIsInstalled == true)
  319. {
  320. m_RtDataKeys.Add("TM.FsmState");
  321. }
  322. if (LLAIsInstalled == true)
  323. {
  324. m_RtDataKeys.Add("LLA.FsmState");
  325. }
  326. if (LLBIsInstalled == true)
  327. {
  328. m_RtDataKeys.Add("LLB.FsmState");
  329. }
  330. if (EFEMIsInstalled == true)
  331. {
  332. m_RtDataKeys.Add("EFEM.FsmState");
  333. }
  334. m_RtDataKeys.Add("SYSTEM.FsmState");
  335. m_RtDataKeys.Add($"System.SignalTower.DeviceData");
  336. m_RtDataKeys.Add("System.CommunicationStatus");
  337. }
  338. private void Instance_OnEvent(EventItem eventItem)
  339. {
  340. GlobalEvents.Instance.OnEventItemRaiseChanged(eventItem);
  341. switch (eventItem.Type)
  342. {
  343. case EventType.EventUI_Notify:
  344. Application.Current.Dispatcher.Invoke(delegate
  345. {
  346. EventLogList.Insert(0,eventItem);
  347. if (EventLogList.Count > logMaxCount)
  348. {
  349. EventLogList.RemoveAt(EventLogList.Count-1);
  350. }
  351. if (eventItem.Level == EventLevel.Alarm)
  352. {
  353. alarmQuery.Enqueue(eventItem);
  354. }
  355. if (alarmQuery.Count > 0)
  356. {
  357. CurrentEventItem = alarmQuery.First();
  358. }
  359. else
  360. {
  361. CurrentEventItem = eventItem;
  362. }
  363. EventLogListSelectedIndex = 0;
  364. });
  365. break;
  366. case EventType.Dialog_Nofity:
  367. //PopDialog(obj);
  368. break;
  369. case EventType.KickOut_Notify:
  370. break;
  371. case EventType.Sound_Notify:
  372. break;
  373. case EventType.UIMessage_Notify:
  374. //PopUIMessage(obj);
  375. break;
  376. }
  377. }
  378. private void OnClear()
  379. {
  380. CurrentEventItem = null;
  381. alarmQuery.Clear();
  382. EventLogList.Clear();
  383. }
  384. private void OnSkip()
  385. {
  386. if (alarmQuery.Count > 0)
  387. {
  388. alarmQuery.Dequeue();
  389. if (alarmQuery.Count > 0)
  390. {
  391. CurrentEventItem = alarmQuery.First();
  392. }
  393. else
  394. {
  395. CurrentEventItem = null;
  396. }
  397. }
  398. }
  399. //private void OnBuzzerOff()
  400. //{
  401. // InvokeClient.Instance.Service.DoOperation($"{ModuleName}.SignalTower.SwitchOffBuzzer");
  402. //}
  403. public void FaDisable()
  404. {
  405. InvokeClient.Instance.Service.DoOperation($"System.FACommand", "FADisable");
  406. }
  407. public void FaEnable()
  408. {
  409. InvokeClient.Instance.Service.DoOperation($"System.FACommand", "FAEnable");
  410. }
  411. #endregion
  412. }
  413. }