TopViewModel.cs 8.9 KB

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