TopViewModel.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using Aitex.Core.RT.Event;
  2. using Aitex.Core.WCF;
  3. using MECF.Framework.Common.DataCenter;
  4. using MECF.Framework.Common.OperationCenter;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Threading;
  16. namespace Venus_MainPages.ViewModels
  17. {
  18. internal class TopViewModel : BindableBase
  19. {
  20. #region 私有字段
  21. private string m_Title;
  22. private string m_SoftwareVersion;
  23. private List<string> m_RtDataKeys=new List<string>();
  24. private Dictionary<string, object> m_RtDataValues;
  25. private string ModuleName;
  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. private Queue<EventItem> alarmQuery = new Queue<EventItem>();//控制alarm log 在 top UI显示
  32. private int logMaxCount = 50;//log在ui最多显示数量
  33. #endregion
  34. #region 属性
  35. public string Title
  36. {
  37. get { return m_Title; }
  38. set { SetProperty(ref m_Title, value); }
  39. }
  40. public string SoftwareVersion
  41. {
  42. get { return m_SoftwareVersion; }
  43. set { SetProperty(ref m_SoftwareVersion, value); }
  44. }
  45. public Dictionary<string, object> RtDataValues
  46. {
  47. get { return m_RtDataValues; }
  48. set { SetProperty(ref m_RtDataValues, value); }
  49. }
  50. public ObservableCollection<EventItem> EventLogList
  51. {
  52. get { return m_EventLogList; }
  53. set { SetProperty(ref m_EventLogList, value); }
  54. }
  55. public EventItem CurrentEventItem
  56. {
  57. get { return m_CurrentEventItem; }
  58. set { SetProperty(ref m_CurrentEventItem, value); }
  59. }
  60. public int EventLogListSelectedIndex
  61. {
  62. get { return m_EventLogListSelectedIndex; }
  63. set { SetProperty(ref m_EventLogListSelectedIndex, value); }
  64. }
  65. public string CurrentEventItemValue
  66. {
  67. get { return m_CurrentEventItemValue; }
  68. set { SetProperty(ref m_CurrentEventItemValue, value); }
  69. }
  70. #endregion
  71. #region 命令
  72. private DelegateCommand _SwichLanguageCommand;
  73. public DelegateCommand SwichLanguageCommand =>
  74. _SwichLanguageCommand ?? (_SwichLanguageCommand = new DelegateCommand(OnSwitchLanguage));
  75. private DelegateCommand _ResetCommand;
  76. public DelegateCommand ResetCommand =>
  77. _ResetCommand ?? (_ResetCommand = new DelegateCommand(OnReset));
  78. private DelegateCommand _ClearCommand;
  79. public DelegateCommand ClearCommand =>
  80. _ClearCommand ?? (_ClearCommand = new DelegateCommand(OnClear));
  81. private DelegateCommand _BuzzerOffCommand;
  82. public DelegateCommand BuzzerOffCommand =>
  83. _BuzzerOffCommand ?? (_BuzzerOffCommand = new DelegateCommand(OnBuzzerOff));
  84. #endregion
  85. #region 构造函数
  86. public TopViewModel()
  87. {
  88. Title = "Venus";
  89. m_SoftwareVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
  90. ModuleName = "PMA";
  91. addDataKeys();
  92. DispatcherTimer timer = new DispatcherTimer();
  93. timer.Interval = TimeSpan.FromSeconds(1);
  94. timer.Tick += timer_Tick;
  95. timer.Start();
  96. EventClient.Instance.OnEvent += Instance_OnEvent;
  97. EventClient.Instance.Start();
  98. }
  99. #endregion
  100. #region 方法
  101. void timer_Tick(object sender, EventArgs e)
  102. {
  103. RtDataValues = QueryDataClient.Instance.Service.PollData(m_RtDataKeys);
  104. }
  105. private void OnSwitchLanguage()
  106. {
  107. List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();
  108. foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
  109. {
  110. if (dictionary.Source != null)
  111. {
  112. dictionaryList.Add(dictionary);
  113. }
  114. }
  115. string requestedCulture1 = @"/Venus_Themes;component/Languages/StringResources.en-US.xaml";
  116. string requestedCulture2 = @"/Venus_Themes;component/Languages/StringResources.zh-CN.xaml";
  117. ResourceDictionary resourceDictionary1 = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture1));
  118. ResourceDictionary resourceDictionary2 = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture2));
  119. if (dictionaryList.IndexOf(resourceDictionary1) < dictionaryList.IndexOf(resourceDictionary2))
  120. {
  121. Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary1);
  122. Application.Current.Resources.MergedDictionaries.Add(resourceDictionary1);
  123. }
  124. else
  125. {
  126. Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary2);
  127. Application.Current.Resources.MergedDictionaries.Add(resourceDictionary2);
  128. }
  129. }
  130. private void OnReset()
  131. {
  132. InvokeClient.Instance.Service.DoOperation("System.Reset");
  133. }
  134. private void addDataKeys()
  135. {
  136. m_RtDataKeys.Add($"{ModuleName}.FsmState");
  137. m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsRedLightOn");
  138. m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsYellowLightOn");
  139. m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsGreenLightOn");
  140. m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsBlueLightOn");
  141. m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsBuzzerOn");
  142. }
  143. private void Instance_OnEvent(EventItem eventItem)
  144. {
  145. switch (eventItem.Type)
  146. {
  147. case EventType.EventUI_Notify:
  148. Application.Current.Dispatcher.Invoke(delegate
  149. {
  150. EventLogList.Add(eventItem);
  151. if (EventLogList.Count > logMaxCount)
  152. {
  153. EventLogList.RemoveAt(0);
  154. }
  155. if (eventItem.Level == EventLevel.Alarm)
  156. {
  157. alarmQuery.Enqueue(eventItem);
  158. }
  159. if (alarmQuery.Count > 0)
  160. {
  161. CurrentEventItem = alarmQuery.First();
  162. }
  163. else
  164. {
  165. CurrentEventItem = eventItem;
  166. }
  167. EventLogListSelectedIndex = 0;
  168. });
  169. break;
  170. case EventType.Dialog_Nofity:
  171. //PopDialog(obj);
  172. break;
  173. case EventType.KickOut_Notify:
  174. break;
  175. case EventType.Sound_Notify:
  176. break;
  177. case EventType.UIMessage_Notify:
  178. //PopUIMessage(obj);
  179. break;
  180. }
  181. }
  182. private void OnClear()
  183. {
  184. if (alarmQuery.Count > 0)
  185. {
  186. alarmQuery.Dequeue();
  187. if (alarmQuery.Count > 0)
  188. {
  189. CurrentEventItem = alarmQuery.First();
  190. }
  191. else
  192. {
  193. CurrentEventItem = null;
  194. }
  195. }
  196. }
  197. private void OnBuzzerOff()
  198. {
  199. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.SignalTower.SwitchOffBuzzer");
  200. }
  201. #endregion
  202. }
  203. }