PageBoostStation.xaml.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using NEIntelligentControl2.Models.BoostStation;
  2. using NEIntelligentControl2.Models.Messages;
  3. using NEIntelligentControl2.Models.Pages;
  4. using NEIntelligentControl2.Models.Station;
  5. using NEIntelligentControl2.Service.WebSocket;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Configuration;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Data;
  16. using System.Windows.Documents;
  17. using System.Windows.Input;
  18. using System.Windows.Media;
  19. using System.Windows.Media.Imaging;
  20. using System.Windows.Navigation;
  21. using System.Windows.Shapes;
  22. namespace NEIntelligentControl2.Pages.BoostStation
  23. {
  24. /// <summary>
  25. /// 升压站页面
  26. /// </summary>
  27. public partial class PageBoostStation : Page
  28. {
  29. private Dictionary<string, PageStation> _Stations;
  30. private Dictionary<string, BoostStationInfo> _BoostStationInfos;
  31. /// <summary>
  32. /// 当前 选择的场站
  33. /// </summary>
  34. private string _CurrentStationId;
  35. /// <summary>
  36. /// 服务地址
  37. /// </summary>
  38. private string _Url;
  39. private WEBHelper _WEBHelper;
  40. private IPageAction _IPageAction;
  41. private TagManager _TagManager;
  42. private List<StationInfo> _StationInfos;
  43. private UrlManager _UrlManager;
  44. public PageBoostStation(WEBHelper web, IPageAction pa, TagManager tm, UrlManager um)
  45. {
  46. InitializeComponent();
  47. _WEBHelper = web;
  48. _IPageAction = pa;
  49. _TagManager = tm;
  50. _UrlManager = um;
  51. _Url = _UrlManager.ServicePath;
  52. InitBoostatationInfos();
  53. }
  54. private void InitBoostatationInfos()
  55. {
  56. _BoostStationInfos = new Dictionary<string, BoostStationInfo>();
  57. _Stations = new Dictionary<string, PageStation>();
  58. try
  59. {
  60. var infos = _WEBHelper.HttpGetJSON<Dictionary<string, BoostStationInfo>>($"{_Url}/api/station/boost_station/all");
  61. _BoostStationInfos = infos;
  62. var sis = from i in infos
  63. orderby i.Value.Index
  64. select new StationInfo()
  65. {
  66. Name = i.Value.TagName,
  67. Id = i.Value.Id
  68. };
  69. _StationInfos = sis.ToList();
  70. }
  71. catch (Exception ex)
  72. {
  73. Console.WriteLine($"升压站信息加载出现错误:{ex.Message}");
  74. }
  75. }
  76. /// <summary>
  77. /// 场站选择改变
  78. /// </summary>
  79. /// <param name="stationId">场站ID</param>
  80. public void StationChanged(string stationId)
  81. {
  82. if (_CurrentStationId == stationId) return;
  83. if (!_Stations.ContainsKey(stationId))
  84. {
  85. BoostStationInfo bsi = new BoostStationInfo() { Id = stationId };
  86. if (_BoostStationInfos.ContainsKey(stationId))
  87. {
  88. bsi = _BoostStationInfos[stationId];
  89. }
  90. Page sp = _IPageAction["BoostStation.PageStation"];
  91. (sp as PageStation).StationInfo = bsi;
  92. (sp as PageStation).SaveAction = SaveBoostStationInfos;
  93. _Stations.Add(stationId, sp as PageStation);
  94. }
  95. _CurrentStationId = stationId;
  96. _MainFrame.Navigate(_Stations[stationId]);
  97. if (!_MainFrame.NavigationService.CanGoBack) return;
  98. var en = _MainFrame.NavigationService.RemoveBackEntry();
  99. while (en != null)
  100. {
  101. en = _MainFrame.NavigationService.RemoveBackEntry();
  102. }
  103. }
  104. /// <summary>
  105. /// 保存升压站信息
  106. /// </summary>
  107. private void SaveBoostStationInfos(BoostStationInfo obj)
  108. {
  109. if (_BoostStationInfos.ContainsKey(obj.Id))
  110. {
  111. _BoostStationInfos[obj.Id] = obj;
  112. }
  113. else
  114. {
  115. _BoostStationInfos.Add(obj.Id, obj);
  116. }
  117. try
  118. {
  119. _WEBHelper.HttpPostBodyString($"{_Url}/api/station/boost_station", obj);
  120. }
  121. catch (Exception ex)
  122. {
  123. Console.WriteLine($"保存升压站信息出现错误:{ex.Message}");
  124. }
  125. try
  126. {
  127. string s = _WEBHelper.Serialize(_BoostStationInfos);
  128. File.WriteAllText(".\\BoostStationInfos.json", s);
  129. }
  130. catch { }
  131. }
  132. private void Page_Loaded(object sender, RoutedEventArgs e)
  133. {
  134. _TagManager.TagSwitched = TagSwitched;
  135. _TagManager.TagChanged?.Invoke(_StationInfos);
  136. _CurrentStationId = null;
  137. }
  138. private void TagSwitched(StationInfo obj)
  139. {
  140. StationChanged(obj.Id);
  141. }
  142. private void Page_Unloaded(object sender, RoutedEventArgs e)
  143. {
  144. _CurrentStationId = null;
  145. }
  146. }
  147. }