HistoryFaultWindow.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 HistoryFaultWindow : 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<FaultInfo> _AlarmInfos;
  40. private List<FaultInfo> _CurrentFaultInfos;
  41. private AlarmTypeInfo _AlarmTypeInfo;
  42. private string _WindturbineId;
  43. public string KeyString { get; set; }
  44. private bool _IsWindturbineFilter;
  45. private bool _IsLoaded;
  46. private int _PageIndex;// 页索引
  47. private int _EachPageCount;// 每页条数
  48. private string _StartTime;
  49. private string _EndTime;
  50. public HistoryFaultWindow(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. HistoryFaultWindow hfw = App.ServiceProvider.GetService(typeof(HistoryFaultWindow)) as HistoryFaultWindow;
  99. hfw.Owner = Application.Current.MainWindow;
  100. hfw.ShowDialog();
  101. }
  102. private void OnPagination(int arg1, int arg2)
  103. {
  104. InitTime();
  105. Task.Run(Search);
  106. }
  107. /// <summary>
  108. /// 场站
  109. /// </summary>
  110. private void _CBStation_SelectionChanged(object sender, SelectionChangedEventArgs e)
  111. {
  112. if (e.AddedItems.Count <= 0) return;
  113. var v = e.AddedItems[0] as StationInfo;
  114. if (v == null) return;
  115. if (_CurrentStationInfo != null && _CurrentStationInfo.Id == v.Id) return;
  116. _CurrentStationInfo = v;
  117. _IsWindturbineFilter = false;
  118. InitTime();
  119. Task.Run(Search);
  120. }
  121. /// <summary>
  122. /// 类型
  123. /// </summary>
  124. private void _CBType_SelectionChanged(object sender, SelectionChangedEventArgs e)
  125. {
  126. if (e.AddedItems.Count <= 0) return;
  127. var v = e.AddedItems[0] as AlarmTypeItem;
  128. if (v == null) return;
  129. _TypeInfo = v;
  130. //_CBSType.ItemsSource = v.SubItem;
  131. //_CBSType.SelectedIndex = 0;
  132. _IsWindturbineFilter = false;
  133. InitTime();
  134. Task.Run(Search);
  135. }
  136. /// <summary>
  137. /// 风机
  138. /// </summary>
  139. private void _CBWindturbine_SelectionChanged(object sender, SelectionChangedEventArgs e)
  140. {
  141. if (e.AddedItems.Count <= 0) return;
  142. var v = e.AddedItems[0] as string;
  143. if (v == null) return;
  144. _WindturbineId = v;
  145. _IsWindturbineFilter = true;
  146. FilterWindturbine();
  147. }
  148. private void FilterWindturbine()
  149. {
  150. if (_AlarmInfos == null) return;
  151. if (string.IsNullOrWhiteSpace(_WindturbineId) || _WindturbineId == "全部")
  152. {
  153. _CurrentFaultInfos = _AlarmInfos;
  154. }
  155. else
  156. {
  157. _CurrentFaultInfos = _AlarmInfos.Where(f => f.ObjectId == _WindturbineId).ToList();
  158. }
  159. _DGMain.ItemsSource = _CurrentFaultInfos;
  160. }
  161. private void Button_Click(object sender, RoutedEventArgs e)
  162. {
  163. switch (((Control)sender).Tag)
  164. {
  165. case "search":
  166. InitTime();
  167. _IsWindturbineFilter = false;
  168. Task.Run(Search);
  169. break;
  170. case "output":
  171. Output();
  172. break;
  173. default: return;
  174. }
  175. }
  176. private void InitTime()
  177. {
  178. DateTime.TryParse(_TBStartTime.Text, out DateTime ds);
  179. DateTime.TryParse(_TBEndTime.Text, out DateTime de);
  180. _StartTime = ds.ToString("yyyy-MM-dd HH:mm:ss");
  181. _EndTime = de.ToString("yyyy-MM-dd HH:mm:ss");
  182. _PageIndex = _IBMain.PageIndex;
  183. _EachPageCount = _IBMain.EachPageCount;
  184. }
  185. private void Output()
  186. {
  187. SaveFileDialog dialog = new SaveFileDialog
  188. {
  189. Title = "保存csv文件",
  190. Filter = "csv文件(*.csv) |*.csv |所有文件(*.*) |*.*",
  191. FilterIndex = 1
  192. };
  193. var v = dialog.ShowDialog();
  194. if (v != true) return;
  195. var path = dialog.FileName;
  196. OutputToFile(path);
  197. }
  198. private void OutputToFile(string path)
  199. {
  200. Task.Run(() =>
  201. {
  202. try
  203. {
  204. _IBMain.Inof("导出数据...");
  205. if (File.Exists(path))
  206. {
  207. File.Delete(path);
  208. }
  209. using (StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8))
  210. {
  211. sw.WriteLine("时间,设备,报警信息,状态");
  212. if (_CurrentFaultInfos == null || _CurrentFaultInfos.Count <= 0)
  213. {
  214. _IBMain.Success();
  215. return;
  216. }
  217. foreach (var v in _CurrentFaultInfos)
  218. {
  219. sw.WriteLine(string.Format("{0},{1},{2},{3}", v.LatestUpdateTimeString,
  220. v.ObjectName, v.AlertText, v.MessageTypeString));
  221. }
  222. }
  223. _IBMain.Success();
  224. Dispatcher.Invoke(() => MessageWindow.ShowMessage("数据导出成功!"));
  225. }
  226. catch (Exception ex)
  227. {
  228. _IBMain.Warning($"导出数据出现错误:{ex.Message}");
  229. }
  230. });
  231. }
  232. private void Search()
  233. {
  234. if (!_IsLoaded) return;
  235. var ur = $"{_Url}/fault/history/list?pagenum={_PageIndex}&pagesize={_EachPageCount}&stationid={_CurrentStationInfo.Id}&starttime={_StartTime}&endtime={_EndTime}" +
  236. $"&category1={GetInfoString(_TypeInfo)}&keyword={(string.IsNullOrWhiteSpace(KeyString) ? "" : KeyString)}";
  237. try
  238. {
  239. _IBMain.Inof("查询数据...");
  240. var vs = _WEBHelper.HttpGetJSON<List<FaultInfo>>(ur);
  241. _AlarmInfos = vs;
  242. ShowData();
  243. if (vs != null && vs.Count > 0)
  244. {
  245. Dispatcher.Invoke(() =>
  246. {
  247. int.TryParse(vs[0].ProjectId, out int i);
  248. _IBMain.PageCount = (i / _EachPageCount) + 1;
  249. });
  250. }
  251. _IBMain.Success();
  252. }
  253. catch (Exception ex)
  254. {
  255. _IBMain.Warning($"查询数据出现错误:{ex.Message}");
  256. }
  257. }
  258. private object GetInfoString(AlarmTypeItem ati)
  259. {
  260. if (ati == null) return "";
  261. return ati.Id == "all" ? "" : ati.Id;
  262. }
  263. /// <summary>
  264. /// 展示信息
  265. /// </summary>
  266. /// <exception cref="NotImplementedException"></exception>
  267. private void ShowData()
  268. {
  269. if (_AlarmInfos == null || _AlarmInfos.Count <= 0)
  270. {
  271. _AlarmInfos = new List<FaultInfo>();
  272. }
  273. var ids = new HashSet<string>() { "全部" };
  274. foreach (var v in _AlarmInfos)
  275. {
  276. if (!string.IsNullOrWhiteSpace(v.WindturbineId))
  277. {
  278. ids.Add(v.WindturbineId);
  279. }
  280. }
  281. Dispatcher.Invoke(() =>
  282. {
  283. if (!_IsWindturbineFilter)
  284. {
  285. _CBWindturbine.ItemsSource = ids;
  286. }
  287. FilterWindturbine();
  288. });
  289. }
  290. private void _CBKeyString_LostFocus(object sender, RoutedEventArgs e)
  291. {
  292. KeyString = _CBKeyString.Text;
  293. }
  294. private void TextBox_KeyDown(object sender, KeyEventArgs e)
  295. {
  296. if (e.Key != Key.Enter) return;
  297. InitTime();
  298. Task.Run(Search);
  299. }
  300. private void Window_Loaded(object sender, RoutedEventArgs e)
  301. {
  302. _IsLoaded = true;
  303. InitTime();
  304. Task.Run(Search);
  305. }
  306. }
  307. }