WindturbineHistoryAlarm.xaml.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using Microsoft.Win32;
  2. using NEIntelligentControl2.Models.Alarm;
  3. using NEIntelligentControl2.Models.Messages;
  4. using NEIntelligentControl2.Models.Windturbine;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Configuration;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using System.Windows.Navigation;
  20. using System.Windows.Shapes;
  21. namespace NEIntelligentControl2.Views.Alarm
  22. {
  23. /// <summary>
  24. /// 风机历史报警
  25. /// </summary>
  26. public partial class WindturbineHistoryAlarm : UserControl
  27. {
  28. private string _Url;
  29. private WEBHelper _WEBHelper;
  30. private List<AlarmInfo> _CurrentAlarmInfos;
  31. public WindturbineInfo WindturbineInfo { get; set; }
  32. /// <summary>
  33. /// 是否是预警
  34. /// </summary>
  35. public bool IsCustom { get; set; }
  36. public WindturbineHistoryAlarm()
  37. {
  38. InitializeComponent();
  39. _WEBHelper = App.ServiceProvider.GetService(typeof(WEBHelper)) as WEBHelper;
  40. _TBStartTime.Text = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
  41. _TBEndTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  42. try
  43. {
  44. #if (DEBUG)
  45. _Url = ConfigurationManager.AppSettings["AlarmPathDebug"];
  46. #else
  47. _Url = ConfigurationManager.AppSettings["AlarmPath"];
  48. #endif
  49. }
  50. catch (Exception ex)
  51. {
  52. Console.WriteLine("读取配置文件[ServicePath]出错!", ex);
  53. }
  54. }
  55. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  56. {
  57. Search();
  58. }
  59. private void Search()
  60. {
  61. DateTime.TryParse(_TBStartTime.Text, out DateTime ds);
  62. DateTime.TryParse(_TBEndTime.Text, out DateTime de);
  63. string start = ds.ToString("yyyy-MM-dd HH:mm:ss");
  64. string end = de.ToString("yyyy-MM-dd HH:mm:ss");
  65. Task.Run(() =>
  66. {
  67. _IBMain.Inof("查询数据...");
  68. var c1 = IsCustom ? "custom" : "windturbine";
  69. var ur = $"{_Url}/alarm/history/page?pagenum=1&windturbineid={WindturbineInfo.WindturbineId}&category1={c1}&pagesize=7000&stationid={WindturbineInfo.StationId}&starttime={start}&endtime={end}";
  70. try
  71. {
  72. var vs = _WEBHelper.HttpGetJSON<AlarmPage>(ur);
  73. if (vs == null || vs.Records == null)
  74. {
  75. vs.Records = new List<AlarmInfo>();
  76. }
  77. Dispatcher.Invoke(() =>
  78. {
  79. _DGMain.ItemsSource = vs.Records;
  80. });
  81. _CurrentAlarmInfos = vs.Records;
  82. _IBMain.Success();
  83. }
  84. catch (Exception e)
  85. {
  86. _IBMain.Warning($"查询数据出现错误:{e.Message}");
  87. }
  88. });
  89. }
  90. private void TextBox_KeyDown(object sender, KeyEventArgs e)
  91. {
  92. if (e.Key != Key.Enter) return;
  93. Search();
  94. }
  95. private void Button_Click(object sender, RoutedEventArgs e)
  96. {
  97. switch (((Control)sender).Tag)
  98. {
  99. case "output":
  100. Output();
  101. break;
  102. case "ok":
  103. Search();
  104. break;
  105. default:return;
  106. }
  107. }
  108. private void Output()
  109. {
  110. SaveFileDialog dialog = new SaveFileDialog
  111. {
  112. Title = "保存csv文件",
  113. Filter = "csv文件(*.csv) |*.csv |所有文件(*.*) |*.*",
  114. FilterIndex = 1
  115. };
  116. var v = dialog.ShowDialog();
  117. if (v != true) return;
  118. var path = dialog.FileName;
  119. OutputToFile(path);
  120. }
  121. private void OutputToFile(string path)
  122. {
  123. Task.Run(() =>
  124. {
  125. try
  126. {
  127. _IBMain.Inof("导出数据...");
  128. if (File.Exists(path))
  129. {
  130. File.Delete(path);
  131. }
  132. using (StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8))
  133. {
  134. sw.WriteLine("时间,设备,报警信息,级别,报警类型,停机类型,故障类型,故障原因");
  135. if (_CurrentAlarmInfos == null || _CurrentAlarmInfos.Count <= 0)
  136. {
  137. _IBMain.Success();
  138. return;
  139. }
  140. foreach (var v in _CurrentAlarmInfos)
  141. {
  142. sw.WriteLine(string.Format("{0},{1},{2},{3},{4},{5},{6},{7}", v.AlertTimeTimeString,
  143. v.ObjectName, v.AlertText, v.RankString, v.Category1String, "", "", ""));
  144. }
  145. }
  146. _IBMain.Success();
  147. Dispatcher.Invoke(() => MessageWindow.ShowMessage("数据导出成功!"));
  148. }
  149. catch (Exception ex)
  150. {
  151. _IBMain.Warning($"导出数据出现错误:{ex.Message}");
  152. }
  153. });
  154. }
  155. }
  156. }