HistoryAlarmWindow.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using Microsoft.Win32;
  2. using NEIntelligentControl2.Models.Alarm;
  3. using NEIntelligentControl2.Models.Messages;
  4. using NEIntelligentControl2.Models.Station;
  5. using NEIntelligentControl2.Service.Station;
  6. using NEIntelligentControl2.Service.WebSocket;
  7. using NEIntelligentControl2.Service.Windturbine;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Configuration;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Data;
  18. using System.Windows.Documents;
  19. using System.Windows.Input;
  20. using System.Windows.Media;
  21. using System.Windows.Media.Imaging;
  22. using System.Windows.Shapes;
  23. namespace NEIntelligentControl2.Windows.Alarm
  24. {
  25. /// <summary>
  26. /// 历史报警窗口
  27. /// </summary>
  28. public partial class HistoryAlarmWindow : Window
  29. {
  30. private StationManager _StationManager;
  31. private WEBHelper _WEBHelper;
  32. private InfoManager _InfoManager;
  33. private UrlManager _UrlManager;
  34. private string _Url;
  35. private StationInfo _CurrentStationInfo;// 场站
  36. private AlarmTypeItem _ClassTypeInfo;// 级别
  37. private AlarmTypeItem _TypeInfo;// 类型
  38. private AlarmTypeItem _STypeInfo;// 子类型
  39. private List<AlarmInfo> _AlarmInfos;
  40. private AlarmTypeInfo _AlarmTypeInfo;
  41. private string _WindturbineId;
  42. public string KeyString { get; set; }
  43. private bool _IsWindturbineFilter;
  44. private bool _IsLoaded;
  45. private string _StartTime;
  46. private string _EndTime;
  47. private int _PageIndex;// 页索引
  48. private int _EachPageCount;// 每页条数
  49. private List<AlarmInfo> _CurrentAlarmInfos;
  50. public HistoryAlarmWindow(StationManager sm, WEBHelper web, InfoManager im, UrlManager um)
  51. {
  52. InitializeComponent();
  53. _StationManager = sm;
  54. _WEBHelper = web;
  55. _InfoManager = im;
  56. _UrlManager = um;
  57. _TBStartTime.Text = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
  58. _TBEndTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  59. Init();
  60. }
  61. private void Init()
  62. {
  63. _Url = _UrlManager.AlarmPath;
  64. var ls = _StationManager.GetStationInfos().Where(i => i.Type != Models.Station.StationType.Other).ToList();
  65. if (ls == null) return;
  66. _CBStation.ItemsSource = ls;
  67. _AlarmTypeInfo = _InfoManager.GetAlarmTypeInfo();
  68. _CBClass.ItemsSource = _AlarmTypeInfo.RankInfo;
  69. _CBType.ItemsSource = _AlarmTypeInfo.DeviceType;
  70. _IBMain.OnPagination = OnPagination;
  71. }
  72. private void ContentControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  73. {
  74. switch (((FrameworkElement)sender).Tag)
  75. {
  76. case "close":
  77. this.Close();
  78. break;
  79. case "maximized":
  80. Maximized();
  81. break;
  82. default: return;
  83. }
  84. }
  85. private void Maximized()
  86. {
  87. if (this.WindowState == WindowState.Maximized)
  88. {
  89. this.WindowState = WindowState.Normal;
  90. }
  91. else
  92. {
  93. this.WindowState = WindowState.Maximized;
  94. }
  95. }
  96. public static void ShowWindow()
  97. {
  98. HistoryAlarmWindow haw = App.ServiceProvider.GetService(typeof(HistoryAlarmWindow)) as HistoryAlarmWindow;
  99. haw.Owner = Application.Current.MainWindow;
  100. haw.ShowDialog();
  101. }
  102. private void InitTime()
  103. {
  104. DateTime.TryParse(_TBStartTime.Text, out DateTime ds);
  105. DateTime.TryParse(_TBEndTime.Text, out DateTime de);
  106. _StartTime = ds.ToString("yyyy-MM-dd HH:mm:ss");
  107. _EndTime = de.ToString("yyyy-MM-dd HH:mm:ss");
  108. _PageIndex = _IBMain.PageIndex;
  109. _EachPageCount = _IBMain.EachPageCount;
  110. }
  111. private void OnPagination(int arg1, int arg2)
  112. {
  113. InitTime();
  114. Task.Run(Search);
  115. }
  116. /// <summary>
  117. /// 场站
  118. /// </summary>
  119. private void _CBStation_SelectionChanged(object sender, SelectionChangedEventArgs e)
  120. {
  121. if (e.AddedItems.Count <= 0) return;
  122. var v = e.AddedItems[0] as StationInfo;
  123. if (v == null) return;
  124. if (_CurrentStationInfo != null && _CurrentStationInfo.Id == v.Id) return;
  125. _CurrentStationInfo = v;
  126. InitTime();
  127. Task.Run(Search);
  128. }
  129. /// <summary>
  130. /// 级别
  131. /// </summary>
  132. private void _CBClass_SelectionChanged(object sender, SelectionChangedEventArgs e)
  133. {
  134. if (e.AddedItems.Count <= 0) return;
  135. var v = e.AddedItems[0] as AlarmTypeItem;
  136. if (v == null) return;
  137. _ClassTypeInfo = v;
  138. _IsWindturbineFilter = false;
  139. InitTime();
  140. Task.Run(Search);
  141. }
  142. /// <summary>
  143. /// 类型
  144. /// </summary>
  145. private void _CBType_SelectionChanged(object sender, SelectionChangedEventArgs e)
  146. {
  147. if (e.AddedItems.Count <= 0) return;
  148. var v = e.AddedItems[0] as AlarmTypeItem;
  149. if (v == null) return;
  150. _TypeInfo = v;
  151. _CBSType.ItemsSource = v.SubItem;
  152. _CBSType.SelectedIndex = 0;
  153. _IsWindturbineFilter = false;
  154. InitTime();
  155. Task.Run(Search);
  156. }
  157. /// <summary>
  158. /// 子类型
  159. /// </summary>
  160. private void _CBSType_SelectionChanged(object sender, SelectionChangedEventArgs e)
  161. {
  162. if (e.AddedItems.Count <= 0) return;
  163. var v = e.AddedItems[0] as AlarmTypeItem;
  164. if (v == null) return;
  165. _STypeInfo = v;
  166. _IsWindturbineFilter = false;
  167. InitTime();
  168. Task.Run(Search);
  169. }
  170. /// <summary>
  171. /// 风机
  172. /// </summary>
  173. private void _CBWindturbine_SelectionChanged(object sender, SelectionChangedEventArgs e)
  174. {
  175. if (e.AddedItems.Count <= 0) return;
  176. var v = e.AddedItems[0] as string;
  177. if (v == null) return;
  178. _WindturbineId = v == "全部" ? "" : v;
  179. _IsWindturbineFilter = true;
  180. InitTime();
  181. Task.Run(Search);
  182. }
  183. private void Button_Click(object sender, RoutedEventArgs e)
  184. {
  185. switch (((Control)sender).Tag)
  186. {
  187. case "search":
  188. InitTime();
  189. Task.Run(Search);
  190. break;
  191. case "output":
  192. Output();
  193. break;
  194. default:return;
  195. }
  196. }
  197. /// <summary>
  198. /// 导出数据
  199. /// </summary>
  200. ///
  201. private void Output()
  202. {
  203. SaveFileDialog dialog = new SaveFileDialog
  204. {
  205. Title = "保存csv文件",
  206. Filter = "csv文件(*.csv) |*.csv |所有文件(*.*) |*.*",
  207. FilterIndex = 1
  208. };
  209. var v = dialog.ShowDialog();
  210. if (v != true) return;
  211. var path = dialog.FileName;
  212. OutputToFile(path);
  213. }
  214. private void OutputToFile(string path)
  215. {
  216. Task.Run(() =>
  217. {
  218. try
  219. {
  220. _IBMain.Inof("导出数据...");
  221. if (File.Exists(path))
  222. {
  223. File.Delete(path);
  224. }
  225. using (StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8))
  226. {
  227. sw.WriteLine("时间,设备,报警信息,级别,报警类型,停机类型,故障类型,故障原因");
  228. if (_CurrentAlarmInfos == null || _CurrentAlarmInfos.Count <= 0)
  229. {
  230. _IBMain.Success();
  231. return;
  232. }
  233. foreach (var v in _CurrentAlarmInfos)
  234. {
  235. sw.WriteLine(string.Format("{0},{1},{2},{3},{4},{5},{6},{7}", v.AlertTimeTimeString,
  236. v.ObjectName, v.AlertText, v.RankString, v.Category1String, "", "", ""));
  237. }
  238. }
  239. _IBMain.Success();
  240. Dispatcher.Invoke(() => MessageWindow.ShowMessage("数据导出成功!"));
  241. }
  242. catch (Exception ex)
  243. {
  244. _IBMain.Warning($"导出数据出现错误:{ex.Message}");
  245. }
  246. });
  247. }
  248. private void Search()
  249. {
  250. if (!_IsLoaded) return;
  251. var ur = $"{_Url}/alarm/history/page?pagenum={_PageIndex}&pagesize={_EachPageCount}" +
  252. $"&stationid={_CurrentStationInfo.Id}&starttime={_StartTime}&endtime={_EndTime}&windturbineid={(string.IsNullOrWhiteSpace(_WindturbineId) ? "" : _WindturbineId)}" +
  253. $"&rank={GetInfoString(_ClassTypeInfo)}&category1={GetInfoString(_TypeInfo)}&category2={GetInfoString(_STypeInfo)}&keyword={(string.IsNullOrWhiteSpace(KeyString) ? "" : KeyString)}";
  254. try
  255. {
  256. _IBMain.Inof("查询数据...");
  257. var vs = _WEBHelper.HttpGetJSON<AlarmPage>(ur);
  258. _AlarmInfos = vs.Records;
  259. ShowData();
  260. _IBMain.Success();
  261. Dispatcher.Invoke(() =>
  262. {
  263. _IBMain.PageCount = vs.Pages;
  264. });
  265. }
  266. catch (Exception ex)
  267. {
  268. _IBMain.Warning($"查询数据出现错误:{ex.Message}");
  269. }
  270. }
  271. /// <summary>
  272. /// 展示信息
  273. /// </summary>
  274. /// <exception cref="NotImplementedException"></exception>
  275. private void ShowData()
  276. {
  277. if (_AlarmInfos == null || _AlarmInfos.Count <= 0)
  278. {
  279. _AlarmInfos = new List<AlarmInfo>();
  280. }
  281. var ids = new HashSet<string>() { "全部" };
  282. foreach (var v in _AlarmInfos)
  283. {
  284. if (!string.IsNullOrWhiteSpace(v.WindturbineId))
  285. {
  286. ids.Add(v.WindturbineId);
  287. }
  288. }
  289. Dispatcher.Invoke(() =>
  290. {
  291. if (!_IsWindturbineFilter)
  292. {
  293. _CBWindturbine.ItemsSource = ids;
  294. }
  295. _DGMain.ItemsSource = _AlarmInfos;
  296. _CurrentAlarmInfos = _AlarmInfos;
  297. });
  298. }
  299. private object GetInfoString(AlarmTypeItem ati)
  300. {
  301. if (ati == null) return "";
  302. return ati.Id == "all" ? "" : ati.Id;
  303. }
  304. private void _CBKeyString_LostFocus(object sender, RoutedEventArgs e)
  305. {
  306. KeyString = _CBKeyString.Text;
  307. }
  308. private void TextBox_KeyDown(object sender, KeyEventArgs e)
  309. {
  310. if (e.Key != Key.Enter) return;
  311. InitTime();
  312. Task.Run(Search);
  313. }
  314. private void Window_Loaded(object sender, RoutedEventArgs e)
  315. {
  316. _IsLoaded = true;
  317. InitTime();
  318. Task.Run(Search);
  319. }
  320. }
  321. }