TopViewModel.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using Aitex.Core.RT.Event;
  2. using Aitex.Core.WCF;
  3. using MECF.Framework.Common.DataCenter;
  4. using Prism.Commands;
  5. using Prism.Mvvm;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Threading;
  15. namespace Venus_MainPages.ViewModels
  16. {
  17. internal class TopViewModel : BindableBase
  18. {
  19. #region 私有字段
  20. private string m_Title;
  21. private string m_SoftwareVersion;
  22. private List<string> m_RtDataKeys=new List<string>();
  23. private Dictionary<string, object> m_RtDataValues;
  24. private string ModuleName;
  25. //public ObservableCollection<EventItem> m_WarnEventLogList=new ObservableCollection<EventItem> ();
  26. private ObservableCollection<EventItem> m_EventLogList = new ObservableCollection<EventItem>();
  27. private EventItem m_CurrentEventItem=new EventItem ();
  28. private string m_CurrentEventItemValue;
  29. private object _lockObj = new object();
  30. private int m_EventLogListSelectedIndex;
  31. #endregion
  32. #region 属性
  33. public string Title
  34. {
  35. get { return m_Title; }
  36. set { SetProperty(ref m_Title, value); }
  37. }
  38. public string SoftwareVersion
  39. {
  40. get { return m_SoftwareVersion; }
  41. set { SetProperty(ref m_SoftwareVersion, value); }
  42. }
  43. public Dictionary<string, object> RtDataValues
  44. {
  45. get { return m_RtDataValues; }
  46. set { SetProperty(ref m_RtDataValues, value); }
  47. }
  48. //public ObservableCollection<EventItem> WarnEventLogList
  49. //{
  50. // get { return m_WarnEventLogList; }
  51. // set { SetProperty(ref m_WarnEventLogList, value); }
  52. //}
  53. public ObservableCollection<EventItem> EventLogList
  54. {
  55. get { return m_EventLogList; }
  56. set { SetProperty(ref m_EventLogList, value); }
  57. }
  58. public EventItem CurrentEventItem
  59. {
  60. get { return m_CurrentEventItem; }
  61. set { SetProperty(ref m_CurrentEventItem, value); }
  62. }
  63. public int EventLogListSelectedIndex
  64. {
  65. get { return m_EventLogListSelectedIndex; }
  66. set { SetProperty(ref m_EventLogListSelectedIndex, value); }
  67. }
  68. public string CurrentEventItemValue
  69. {
  70. get { return m_CurrentEventItemValue; }
  71. set { SetProperty(ref m_CurrentEventItemValue, value); }
  72. }
  73. #endregion
  74. #region 命令
  75. private DelegateCommand _SwichLanguageCommand;
  76. public DelegateCommand SwichLanguageCommand =>
  77. _SwichLanguageCommand ?? (_SwichLanguageCommand = new DelegateCommand(OnSwitchLanguage));
  78. #endregion
  79. #region 构造函数
  80. public TopViewModel()
  81. {
  82. Title = "Venus";
  83. m_SoftwareVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
  84. ModuleName = "PMA";
  85. addDataKeys();
  86. DispatcherTimer timer = new DispatcherTimer();
  87. timer.Interval = TimeSpan.FromSeconds(1);
  88. timer.Tick += timer_Tick;
  89. timer.Start();
  90. EventClient.Instance.OnEvent += Instance_OnEvent;
  91. EventClient.Instance.Start();
  92. }
  93. #endregion
  94. #region 方法
  95. void timer_Tick(object sender, EventArgs e)
  96. {
  97. RtDataValues = QueryDataClient.Instance.Service.PollData(m_RtDataKeys);
  98. }
  99. private void OnSwitchLanguage()
  100. {
  101. List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();
  102. foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
  103. {
  104. if (dictionary.Source != null)
  105. {
  106. dictionaryList.Add(dictionary);
  107. }
  108. }
  109. string requestedCulture1 = @"/Venus_Themes;component/Languages/StringResources.en-US.xaml";
  110. string requestedCulture2 = @"/Venus_Themes;component/Languages/StringResources.zh-CN.xaml";
  111. ResourceDictionary resourceDictionary1 = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture1));
  112. ResourceDictionary resourceDictionary2 = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture2));
  113. if (dictionaryList.IndexOf(resourceDictionary1) < dictionaryList.IndexOf(resourceDictionary2))
  114. {
  115. Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary1);
  116. Application.Current.Resources.MergedDictionaries.Add(resourceDictionary1);
  117. }
  118. else
  119. {
  120. Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary2);
  121. Application.Current.Resources.MergedDictionaries.Add(resourceDictionary2);
  122. }
  123. }
  124. private void addDataKeys()
  125. {
  126. m_RtDataKeys.Add($"{ModuleName}.FsmState");
  127. m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsRedLightOn");
  128. m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsYellowLightOn");
  129. m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsGreenLightOn");
  130. m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsBlueLightOn");
  131. m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsBuzzerOn");
  132. }
  133. private void Instance_OnEvent(EventItem eventItem)
  134. {
  135. //lock (_lockObj)
  136. //{
  137. // ThreadPool.QueueUserWorkItem(delegate
  138. // {
  139. // CurrentEventItem = eventItem;
  140. // });
  141. //}
  142. Application.Current.Dispatcher.Invoke(delegate
  143. {
  144. EventLogList.Add(eventItem);
  145. CurrentEventItem = eventItem;
  146. EventLogListSelectedIndex = EventLogList.Count - 1;
  147. //CurrentEventItemValue = eventItem.Description;
  148. });
  149. }
  150. #endregion
  151. }
  152. }