StateTimePage.xaml.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using Microsoft.Win32;
  2. using NEIntelligentControl2.Models;
  3. using NEIntelligentControl2.Models.Messages;
  4. using NEIntelligentControl2.Models.Pages;
  5. using NEIntelligentControl2.Models.Station;
  6. using NEIntelligentControl2.Models.Windturbine;
  7. using NEIntelligentControl2.Service.Station;
  8. using NEIntelligentControl2.Service.WebSocket;
  9. using NEIntelligentControl2.Views.Infos;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Runtime.InteropServices;
  15. using System.Security.Policy;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Data;
  21. using System.Windows.Documents;
  22. using System.Windows.Input;
  23. using System.Windows.Media;
  24. using System.Windows.Media.Imaging;
  25. using System.Windows.Navigation;
  26. using System.Windows.Shapes;
  27. namespace NEIntelligentControl2.Pages.Home
  28. {
  29. /// <summary>
  30. /// 状态时间页面
  31. /// </summary>
  32. public partial class StateTimePage : Page
  33. {
  34. private StationManager _StationManager;
  35. private TagManager _TagManager;
  36. private WEBHelper _WEBHelper;
  37. private UrlManager _UrlManager;
  38. private string _Url;
  39. private List<StationInfo> _StationInfos; // 场站信息
  40. private StationInfo _CurrentStationInfo;// 当前场站信息
  41. private Dictionary<string, List<StatusTimeInfo>> _CurrentData;
  42. public StateTimePage(WEBHelper web, TagManager tm, StationManager sm, UrlManager um)
  43. {
  44. InitializeComponent();
  45. _TagManager = tm;
  46. _StationManager = sm;
  47. _WEBHelper = web;
  48. _UrlManager = um;
  49. _Url = _UrlManager.ServicePath;
  50. Init();
  51. }
  52. private void Init()
  53. {
  54. var st = DateTime.Now.AddDays(-1);
  55. _tpstart.SelectedDateTime = st.AddSeconds(-st.Second);
  56. }
  57. private void Button_Click(object sender, RoutedEventArgs e)
  58. {
  59. switch (((Control)sender).Tag)
  60. {
  61. case "search":
  62. Search();
  63. break;
  64. case "output":
  65. Output();
  66. break;
  67. default: return;
  68. }
  69. }
  70. /// <summary>
  71. /// 导出
  72. /// </summary>
  73. private void Output()
  74. {
  75. SaveFileDialog dialog = new SaveFileDialog
  76. {
  77. Title = "保存csv文件",
  78. Filter = "csv文件(*.csv) |*.csv |所有文件(*.*) |*.*",
  79. FilterIndex = 1
  80. };
  81. var v = dialog.ShowDialog();
  82. if (v != true) return;
  83. var path = dialog.FileName;
  84. Task.Run(output);
  85. void output()
  86. {
  87. try
  88. {
  89. _IBMain.Inof("导出数据...");
  90. if (File.Exists(path))
  91. {
  92. File.Delete(path);
  93. }
  94. using (StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8))
  95. {
  96. sw.WriteLine("风机,状态,开始时间,结束时间,持续时间(小时)");
  97. foreach (var vv in _CurrentData)
  98. {
  99. foreach(var dt in vv.Value)
  100. {
  101. sw.WriteLine(string.Format("{0},{1},{2},{3},{4}", dt.Code, dt.StatusName, dt.StartTime, dt.EndTime, dt.Duration));
  102. }
  103. }
  104. }
  105. _IBMain.Success();
  106. Dispatcher.Invoke(() => MessageWindow.ShowMessage("数据导出成功!"));
  107. }
  108. catch (Exception ex)
  109. {
  110. _IBMain.Warning($"导出数据出现错误:{ex.Message}");
  111. }
  112. }
  113. }
  114. /// <summary>
  115. /// 查询
  116. /// </summary>
  117. private void Search()
  118. {
  119. if (_CurrentStationInfo == null) return;
  120. _lb.Items.Clear();
  121. var ur = $"{_Url}/api/windturbine/status-station?startTs={_tpstart.SelectedDateTime.GetTimeSpan()}&endTs={_tpend.SelectedDateTime.GetTimeSpan()}&stationId={this._CurrentStationInfo.Id}";
  122. Task.Run(search);
  123. void search()
  124. {
  125. try
  126. {
  127. _IBMain.Inof("查询数据...");
  128. var sts = _WEBHelper.HttpGetJSON<Dictionary<string, List<StatusTimeInfo>>>(ur);
  129. _CurrentData = sts;
  130. Dispatcher.Invoke(() =>
  131. {
  132. foreach (var v in sts)
  133. {
  134. StateInfo si = new StateInfo() { Width = _lb.ActualWidth - 20, ItemsTitle = v.Key, ItemsSource = v.Value };
  135. _lb.Items.Add(si);
  136. }
  137. });
  138. _IBMain.Success();
  139. }
  140. catch (Exception ex)
  141. {
  142. _IBMain.Warning($"查询数据出现错误:{ex.Message}");
  143. }
  144. }
  145. }
  146. private void Page_Loaded(object sender, RoutedEventArgs e)
  147. {
  148. try
  149. {
  150. if (_StationInfos == null || _StationInfos.Count <= 0)
  151. {
  152. _StationInfos = _StationManager.GetStationInfos().Where(s => s.Type == StationType.Wind).ToList();
  153. }
  154. }
  155. catch (Exception ex)
  156. {
  157. Console.WriteLine("读取场站信息出错!", ex);
  158. }
  159. _TagManager.TagSwitched = TagSwitched;
  160. _TagManager.TagChanged?.Invoke(_StationInfos);
  161. }
  162. /// <summary>
  163. /// 标签切换
  164. /// </summary>
  165. private void TagSwitched(StationInfo si)
  166. {
  167. if (_CurrentStationInfo != null && (si == null || si.Id == _CurrentStationInfo.Id)) return;
  168. _CurrentStationInfo = si;
  169. Search();
  170. }
  171. }
  172. }