TopViewModel.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 _SkipCommand;
  82. public DelegateCommand SkipCommand =>
  83. _SkipCommand ?? (_SkipCommand = new DelegateCommand(OnSkip));
  84. private DelegateCommand _BuzzerOffCommand;
  85. public DelegateCommand BuzzerOffCommand =>
  86. _BuzzerOffCommand ?? (_BuzzerOffCommand = new DelegateCommand(OnBuzzerOff));
  87. #endregion
  88. #region 构造函数
  89. public TopViewModel()
  90. {
  91. Title = "Venus";
  92. m_SoftwareVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
  93. ModuleName = "PMA";
  94. addDataKeys();
  95. DispatcherTimer timer = new DispatcherTimer();
  96. timer.Interval = TimeSpan.FromSeconds(1);
  97. timer.Tick += timer_Tick;
  98. timer.Start();
  99. EventClient.Instance.OnEvent += Instance_OnEvent;
  100. EventClient.Instance.Start();
  101. }
  102. #endregion
  103. #region 方法
  104. void timer_Tick(object sender, EventArgs e)
  105. {
  106. RtDataValues = QueryDataClient.Instance.Service.PollData(m_RtDataKeys);
  107. }
  108. private void OnSwitchLanguage()
  109. {
  110. List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();
  111. foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
  112. {
  113. if (dictionary.Source != null)
  114. {
  115. dictionaryList.Add(dictionary);
  116. }
  117. }
  118. string requestedCulture1 = @"/Venus_Themes;component/Languages/StringResources.en-US.xaml";
  119. string requestedCulture2 = @"/Venus_Themes;component/Languages/StringResources.zh-CN.xaml";
  120. ResourceDictionary resourceDictionary1 = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture1));
  121. ResourceDictionary resourceDictionary2 = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture2));
  122. if (dictionaryList.IndexOf(resourceDictionary1) < dictionaryList.IndexOf(resourceDictionary2))
  123. {
  124. Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary1);
  125. Application.Current.Resources.MergedDictionaries.Add(resourceDictionary1);
  126. }
  127. else
  128. {
  129. Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary2);
  130. Application.Current.Resources.MergedDictionaries.Add(resourceDictionary2);
  131. }
  132. }
  133. private void OnReset()
  134. {
  135. InvokeClient.Instance.Service.DoOperation("System.Reset");
  136. }
  137. private void addDataKeys()
  138. {
  139. m_RtDataKeys.Add($"{ModuleName}.FsmState");
  140. m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsRedLightOn");
  141. m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsYellowLightOn");
  142. m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsGreenLightOn");
  143. m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsBlueLightOn");
  144. m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsBuzzerOn");
  145. }
  146. private void Instance_OnEvent(EventItem eventItem)
  147. {
  148. switch (eventItem.Type)
  149. {
  150. case EventType.EventUI_Notify:
  151. Application.Current.Dispatcher.Invoke(delegate
  152. {
  153. EventLogList.Add(eventItem);
  154. if (EventLogList.Count > logMaxCount)
  155. {
  156. EventLogList.RemoveAt(0);
  157. }
  158. if (eventItem.Level == EventLevel.Alarm)
  159. {
  160. alarmQuery.Enqueue(eventItem);
  161. }
  162. if (alarmQuery.Count > 0)
  163. {
  164. CurrentEventItem = alarmQuery.First();
  165. }
  166. else
  167. {
  168. CurrentEventItem = eventItem;
  169. }
  170. EventLogListSelectedIndex = 0;
  171. });
  172. break;
  173. case EventType.Dialog_Nofity:
  174. //PopDialog(obj);
  175. break;
  176. case EventType.KickOut_Notify:
  177. break;
  178. case EventType.Sound_Notify:
  179. break;
  180. case EventType.UIMessage_Notify:
  181. //PopUIMessage(obj);
  182. break;
  183. }
  184. }
  185. private void OnClear()
  186. {
  187. CurrentEventItem = null;
  188. alarmQuery.Clear();
  189. EventLogList.Clear();
  190. }
  191. private void OnSkip()
  192. {
  193. if (alarmQuery.Count > 0)
  194. {
  195. alarmQuery.Dequeue();
  196. if (alarmQuery.Count > 0)
  197. {
  198. CurrentEventItem = alarmQuery.First();
  199. }
  200. else
  201. {
  202. CurrentEventItem = null;
  203. }
  204. }
  205. }
  206. private void OnBuzzerOff()
  207. {
  208. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.SignalTower.SwitchOffBuzzer");
  209. }
  210. #endregion
  211. }
  212. }