TopViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 JetChamber m_SelectedJetChamber = JetChamber.None;
  41. private bool m_PMAIsInstalled;
  42. private bool m_PMBIsInstalled;
  43. private bool m_PMCIsInstalled;
  44. private bool m_PMDIsInstalled;
  45. #endregion
  46. #region 属性
  47. public string Title
  48. {
  49. get { return m_Title; }
  50. set { SetProperty(ref m_Title, value); }
  51. }
  52. public string SoftwareVersion
  53. {
  54. get { return m_SoftwareVersion; }
  55. set { SetProperty(ref m_SoftwareVersion, value); }
  56. }
  57. public Dictionary<string, object> RtDataValues
  58. {
  59. get { return m_RtDataValues; }
  60. set { SetProperty(ref m_RtDataValues, value); }
  61. }
  62. public ObservableCollection<EventItem> EventLogList
  63. {
  64. get { return m_EventLogList; }
  65. set { SetProperty(ref m_EventLogList, value); }
  66. }
  67. public EventItem CurrentEventItem
  68. {
  69. get { return m_CurrentEventItem; }
  70. set { SetProperty(ref m_CurrentEventItem, value); }
  71. }
  72. public int EventLogListSelectedIndex
  73. {
  74. get { return m_EventLogListSelectedIndex; }
  75. set { SetProperty(ref m_EventLogListSelectedIndex, value); }
  76. }
  77. public string CurrentEventItemValue
  78. {
  79. get { return m_CurrentEventItemValue; }
  80. set { SetProperty(ref m_CurrentEventItemValue, value); }
  81. }
  82. public AITSignalTowerData SignalTowerData
  83. {
  84. get { return m_SignalTowerData; }
  85. set { SetProperty(ref m_SignalTowerData, value); }
  86. }
  87. public string HostCommunicationStatusBackground
  88. {
  89. get
  90. {
  91. switch (HostCommunicationStatus)
  92. {
  93. case "Disabled":
  94. return "Yellow";
  95. case "Enabled":
  96. case "EnabledNotCommunicating":
  97. case "WaitCRA":
  98. case "WaitDelay":
  99. case "WaitCRFromHost":
  100. return "Transparent";
  101. case "EnabledCommunicating":
  102. return "LawnGreen";
  103. default:
  104. return "Yellow";
  105. }
  106. }
  107. }
  108. public string m_HostBack;
  109. public string HostBack
  110. {
  111. get { return m_HostBack; }
  112. set { SetProperty(ref m_HostBack, value); }
  113. }
  114. public string HostCommunicationStatus
  115. {
  116. get { return m_HostCommunicationStatus; }
  117. set { SetProperty(ref m_HostCommunicationStatus, value); }
  118. }
  119. public bool IsEnableFAEnable
  120. {
  121. get
  122. {
  123. return HostCommunicationStatus == "Disabled";
  124. }
  125. }
  126. public bool _IsDisableEnable;
  127. public bool IsEnableEnable
  128. {
  129. get { return _IsDisableEnable; }
  130. set { SetProperty(ref _IsDisableEnable, value); }
  131. }
  132. public bool _IsEnableDisable;
  133. public bool IsEnableDisable
  134. {
  135. get { return _IsEnableDisable; }
  136. set { SetProperty(ref _IsEnableDisable, value); }
  137. }
  138. public bool IsDisableFAEnable
  139. {
  140. get
  141. {
  142. return HostCommunicationStatus != "Disabled";
  143. }
  144. }
  145. public CommunicationState FACommunicationState
  146. {
  147. get
  148. {
  149. return string.IsNullOrEmpty(HostCommunicationStatus) ? CommunicationState.Disabled
  150. : (CommunicationState)Enum.Parse(typeof(CommunicationState), HostCommunicationStatus);
  151. }
  152. }
  153. public bool PMAIsInstalled
  154. {
  155. get { return m_PMAIsInstalled; }
  156. set { SetProperty(ref m_PMAIsInstalled, value); }
  157. }
  158. public bool PMBIsInstalled
  159. {
  160. get { return m_PMBIsInstalled; }
  161. set { SetProperty(ref m_PMBIsInstalled, value); }
  162. }
  163. public bool PMCIsInstalled
  164. {
  165. get { return m_PMCIsInstalled; }
  166. set { SetProperty(ref m_PMCIsInstalled, value); }
  167. }
  168. public bool PMDIsInstalled
  169. {
  170. get { return m_PMDIsInstalled; }
  171. set { SetProperty(ref m_PMDIsInstalled, value); }
  172. }
  173. #endregion
  174. #region 命令
  175. private DelegateCommand _SwichLanguageCommand;
  176. public DelegateCommand SwichLanguageCommand =>
  177. _SwichLanguageCommand ?? (_SwichLanguageCommand = new DelegateCommand(OnSwitchLanguage));
  178. private DelegateCommand _ResetCommand;
  179. public DelegateCommand ResetCommand =>
  180. _ResetCommand ?? (_ResetCommand = new DelegateCommand(OnReset));
  181. private DelegateCommand _ClearCommand;
  182. public DelegateCommand ClearCommand =>
  183. _ClearCommand ?? (_ClearCommand = new DelegateCommand(OnClear));
  184. private DelegateCommand _SkipCommand;
  185. public DelegateCommand SkipCommand =>
  186. _SkipCommand ?? (_SkipCommand = new DelegateCommand(OnSkip));
  187. private DelegateCommand _BuzzerOffCommand;
  188. public DelegateCommand BuzzerOffCommand =>
  189. _BuzzerOffCommand ?? (_BuzzerOffCommand = new DelegateCommand(OnBuzzerOff));
  190. private DelegateCommand _FADisableCommand;
  191. public DelegateCommand FADisableCommand =>
  192. _FADisableCommand ?? (_FADisableCommand = new DelegateCommand(FaDisable));
  193. private DelegateCommand _FAEnableCommand;
  194. public DelegateCommand FAEnableCommand =>
  195. _FAEnableCommand ?? (_FAEnableCommand = new DelegateCommand(FaEnable));
  196. #endregion
  197. #region 构造函数
  198. public TopViewModel()
  199. {
  200. JetChamber selectedChamber = (JetChamber)(Convert.ToInt32(QueryDataClient.Instance.Service.GetConfig("System.ChamberSelect")));
  201. if (selectedChamber == JetChamber.Venus)
  202. {
  203. Title = "Venus";
  204. }
  205. else
  206. {
  207. Title = "Kepler";
  208. }
  209. //Title = selectedChamber.ToString();
  210. m_SoftwareVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
  211. ModuleName = "PMA";
  212. addDataKeys();
  213. DispatcherTimer timer = new DispatcherTimer();
  214. timer.Interval = TimeSpan.FromSeconds(1);
  215. timer.Tick += timer_Tick;
  216. timer.Start();
  217. EventClient.Instance.OnEvent += Instance_OnEvent;
  218. EventClient.Instance.Start();
  219. string allModules = QueryDataClient.Instance.Service.GetConfig($"System.InstalledModules").ToString();
  220. PMAIsInstalled = allModules.Contains("PMA");
  221. PMBIsInstalled = allModules.Contains("PMB");
  222. PMCIsInstalled = allModules.Contains("PMC");
  223. PMDIsInstalled = allModules.Contains("PMD");
  224. }
  225. #endregion
  226. #region 方法
  227. void timer_Tick(object sender, EventArgs e)
  228. {
  229. RtDataValues = QueryDataClient.Instance.Service.PollData(m_RtDataKeys);
  230. SignalTowerData = CommonFunction.GetValue<AITSignalTowerData>(RtDataValues, $"System.SignalTower.DeviceData");
  231. HostCommunicationStatus= CommonFunction.GetValue<String>(RtDataValues,"System.CommunicationStatus");
  232. IsEnableEnable = FACommunicationState == CommunicationState.Disabled;
  233. IsEnableDisable = FACommunicationState != CommunicationState.Disabled;
  234. HostBack = HostCommunicationStatusBackground;
  235. }
  236. private void OnSwitchLanguage()
  237. {
  238. List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();
  239. foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
  240. {
  241. if (dictionary.Source != null)
  242. {
  243. dictionaryList.Add(dictionary);
  244. }
  245. }
  246. string requestedCulture1 = @"/Venus_Themes;component/Languages/StringResources.en-US.xaml";
  247. string requestedCulture2 = @"/Venus_Themes;component/Languages/StringResources.zh-CN.xaml";
  248. ResourceDictionary resourceDictionary1 = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture1));
  249. ResourceDictionary resourceDictionary2 = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture2));
  250. if (dictionaryList.IndexOf(resourceDictionary1) < dictionaryList.IndexOf(resourceDictionary2))
  251. {
  252. Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary1);
  253. Application.Current.Resources.MergedDictionaries.Add(resourceDictionary1);
  254. }
  255. else
  256. {
  257. Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary2);
  258. Application.Current.Resources.MergedDictionaries.Add(resourceDictionary2);
  259. }
  260. }
  261. private void OnReset()
  262. {
  263. InvokeClient.Instance.Service.DoOperation("System.Reset");
  264. }
  265. private void addDataKeys()
  266. {
  267. m_RtDataKeys.Add("PMA.FsmState");
  268. m_RtDataKeys.Add("PMB.FsmState");
  269. m_RtDataKeys.Add("PMC.FsmState");
  270. m_RtDataKeys.Add("PMD.FsmState");
  271. m_RtDataKeys.Add("TM.FsmState");
  272. m_RtDataKeys.Add("LLA.FsmState");
  273. m_RtDataKeys.Add("LLB.FsmState");
  274. m_RtDataKeys.Add("EFEM.FsmState");
  275. m_RtDataKeys.Add("SYSTEM.FsmState");
  276. m_RtDataKeys.Add("PMA.IsSlitDoorClosed");
  277. m_RtDataKeys.Add($"System.SignalTower.DeviceData");
  278. m_RtDataKeys.Add("System.CommunicationStatus");
  279. }
  280. private void Instance_OnEvent(EventItem eventItem)
  281. {
  282. GlobalEvents.Instance.OnEventItemRaiseChanged(eventItem);
  283. switch (eventItem.Type)
  284. {
  285. case EventType.EventUI_Notify:
  286. Application.Current.Dispatcher.Invoke(delegate
  287. {
  288. EventLogList.Insert(0,eventItem);
  289. if (EventLogList.Count > logMaxCount)
  290. {
  291. EventLogList.RemoveAt(EventLogList.Count-1);
  292. }
  293. if (eventItem.Level == EventLevel.Alarm)
  294. {
  295. alarmQuery.Enqueue(eventItem);
  296. }
  297. if (alarmQuery.Count > 0)
  298. {
  299. CurrentEventItem = alarmQuery.First();
  300. }
  301. else
  302. {
  303. CurrentEventItem = eventItem;
  304. }
  305. EventLogListSelectedIndex = 0;
  306. });
  307. break;
  308. case EventType.Dialog_Nofity:
  309. //PopDialog(obj);
  310. break;
  311. case EventType.KickOut_Notify:
  312. break;
  313. case EventType.Sound_Notify:
  314. break;
  315. case EventType.UIMessage_Notify:
  316. //PopUIMessage(obj);
  317. break;
  318. }
  319. }
  320. private void OnClear()
  321. {
  322. CurrentEventItem = null;
  323. alarmQuery.Clear();
  324. EventLogList.Clear();
  325. }
  326. private void OnSkip()
  327. {
  328. if (alarmQuery.Count > 0)
  329. {
  330. alarmQuery.Dequeue();
  331. if (alarmQuery.Count > 0)
  332. {
  333. CurrentEventItem = alarmQuery.First();
  334. }
  335. else
  336. {
  337. CurrentEventItem = null;
  338. }
  339. }
  340. }
  341. private void OnBuzzerOff()
  342. {
  343. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.SignalTower.SwitchOffBuzzer");
  344. }
  345. public void FaDisable()
  346. {
  347. InvokeClient.Instance.Service.DoOperation($"System.FACommand", "FADisable");
  348. }
  349. public void FaEnable()
  350. {
  351. InvokeClient.Instance.Service.DoOperation($"System.FACommand", "FAEnable");
  352. }
  353. #endregion
  354. }
  355. }