PageMatrix.xaml.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using log4net;
  2. using NEIntelligentControl2.Models.Matrix;
  3. using NEIntelligentControl2.Models.Messages;
  4. using NEIntelligentControl2.Models.Pages;
  5. using NEIntelligentControl2.Models.PV;
  6. using NEIntelligentControl2.Models.Station;
  7. using NEIntelligentControl2.Models.Windturbine;
  8. using NEIntelligentControl2.Service.WebSocket;
  9. using NEIntelligentControl2.Service.Station;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Configuration;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Data;
  19. using System.Windows.Documents;
  20. using System.Windows.Input;
  21. using System.Windows.Media;
  22. using System.Windows.Media.Imaging;
  23. using System.Windows.Navigation;
  24. using System.Windows.Shapes;
  25. namespace NEIntelligentControl2.Pages.Matrix
  26. {
  27. /// <summary>
  28. /// 矩阵页面
  29. /// </summary>
  30. public partial class PageMatrix : Page
  31. {
  32. private ILog log = LogManager.GetLogger("File");
  33. private MessageBridge _MessageBridge;// websocket
  34. private WindturbineInfoBridge _WindturbineInfoBridge;// 风机信息数据桥
  35. private PVInfoBridge _PVInfoBridge;// 光伏信息
  36. private Page _CurrentPage; // 当前页面
  37. private IPageAction _IPageAction; // 页面动作
  38. private Dictionary<string, Page> _Pages = new Dictionary<string, Page>();
  39. private TagManager _TagManager;
  40. private StationManager _StationManager;
  41. private List<StationInfo> _StationInfos; // 场站信息
  42. private StationInfo _CurrentStationInfo;// 当前场站信息
  43. public PageMatrix(MessageBridge msbridge, WEBHelper web, IPageAction action, TagManager tm, StationManager sm)
  44. {
  45. InitializeComponent();
  46. _MessageBridge = msbridge;
  47. _IPageAction = action;
  48. _TagManager = tm;
  49. _StationManager = sm;
  50. _WindturbineInfoBridge = new WindturbineInfoBridge() { Messaged = OnWindturbineMessage };
  51. _PVInfoBridge = new PVInfoBridge() { Messaged = OnPVMessage };
  52. }
  53. /// <summary>
  54. /// 光伏基本信息
  55. /// </summary>
  56. private void OnPVMessage(Dictionary<string, PVInfo> obj)
  57. {
  58. if (obj == null) return;
  59. (_CurrentPage as IStationInfo)?.UpdateData(obj);
  60. }
  61. /// <summary>
  62. /// 风机基本信息
  63. /// </summary>
  64. private void OnWindturbineMessage(Dictionary<string, WindturbineInfo> obj)
  65. {
  66. if (obj == null) return;
  67. (_CurrentPage as IStationInfo)?.UpdateData(obj);
  68. }
  69. private Page GetPage(StationInfo si)
  70. {
  71. if (_Pages.ContainsKey(si.Id))
  72. {
  73. return _Pages[si.Id];
  74. }
  75. Page page = null;
  76. if (si.Type == StationType.Other)
  77. {
  78. page = _IPageAction["Matrix.PageMatrixAll"];
  79. }
  80. else
  81. {
  82. page = _IPageAction["Matrix.PageMatrixStation"];
  83. }
  84. _Pages.Add(si.Id, page);
  85. return page;
  86. }
  87. /// <summary>
  88. /// 显示页面
  89. /// </summary>
  90. private void ShowPage(Page page)
  91. {
  92. _FrameMain.Navigate(page);
  93. if (!_FrameMain.NavigationService.CanGoBack) return;
  94. var en = _FrameMain.NavigationService.RemoveBackEntry();
  95. while (en != null)
  96. {
  97. en = _FrameMain.NavigationService.RemoveBackEntry();
  98. }
  99. }
  100. /// <summary>
  101. /// 切换注册消息
  102. /// </summary>
  103. private void RegisterMessage()
  104. {
  105. if (_CurrentStationInfo == null || _CurrentStationInfo.Id == "WIND_ALL" || _CurrentStationInfo.Type == StationType.Wind)
  106. {
  107. _MessageBridge.Register(_WindturbineInfoBridge);
  108. _MessageBridge.Unregister(_PVInfoBridge);
  109. }
  110. else
  111. {
  112. _MessageBridge.Register(_PVInfoBridge);
  113. _MessageBridge.Unregister(_WindturbineInfoBridge);
  114. }
  115. }
  116. private void Page_Unloaded(object sender, RoutedEventArgs e)
  117. {
  118. _MessageBridge.Unregister(_WindturbineInfoBridge);
  119. _MessageBridge.Unregister(_PVInfoBridge);
  120. }
  121. private void Page_Loaded(object sender, RoutedEventArgs e)
  122. {
  123. try
  124. {
  125. if (_StationInfos == null || _StationInfos.Count <= 0)
  126. {
  127. _StationInfos = _StationManager.GetStationInfos();
  128. }
  129. }
  130. catch (Exception ex)
  131. {
  132. log.Error("读取场站信息出错!", ex);
  133. }
  134. _TagManager.TagSwitched = TagSwitched;
  135. _TagManager.TagChanged?.Invoke(_StationInfos);
  136. RegisterMessage();
  137. }
  138. /// <summary>
  139. /// 切换页面
  140. /// </summary>
  141. /// <param name="si"></param>
  142. private void TagSwitched(StationInfo si)
  143. {
  144. if (_CurrentStationInfo != null && (si == null || si.Id == _CurrentStationInfo.Id)) return;
  145. _CurrentStationInfo = si;
  146. RegisterMessage();
  147. Page page = GetPage(si);
  148. _CurrentPage = page;
  149. (_CurrentPage as IStationInfo).StationInfo = si;
  150. si.Tag = _StationInfos;
  151. ShowPage(page);
  152. }
  153. }
  154. }