AGCLineWindow.xaml.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using Microsoft.Win32;
  2. using NEIntelligentControl2.Models;
  3. using NEIntelligentControl2.Models.AGC;
  4. using NEIntelligentControl2.Models.Datas;
  5. using NEIntelligentControl2.Models.Messages;
  6. using NEIntelligentControl2.Service.WebSocket;
  7. using System;
  8. using System.Collections.Generic;
  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.Shapes;
  21. namespace NEIntelligentControl2.Windows
  22. {
  23. /// <summary>
  24. /// AGC曲线窗口
  25. /// </summary>
  26. public partial class AGCLineWindow : Window
  27. {
  28. private AGCInfo _AGCInfo;
  29. private ViewModel _ViewModel;
  30. private UrlManager _UrlManager;
  31. private WEBHelper _WEBHelper;
  32. private string _HistoyUrl;
  33. public AGCLineWindow(ViewModel vm, AGCInfo info)
  34. {
  35. InitializeComponent();
  36. _ViewModel = vm;
  37. _ViewModel.AxisX.IsZoomEnabled = true;
  38. _ViewModel.AxisX.IsPanEnabled = true;
  39. _AGCInfo = info;
  40. _UrlManager = App.ServiceProvider.GetService(typeof(UrlManager)) as UrlManager;
  41. _WEBHelper = App.ServiceProvider.GetService(typeof(WEBHelper)) as WEBHelper;
  42. _HistoyUrl = _UrlManager.DataServicePath;
  43. Init();
  44. }
  45. private void Init()
  46. {
  47. this.Title = _AGCInfo.Title;
  48. _pv.Model = _ViewModel;
  49. _ViewModel.AxisX.StringFormat = "MM-dd HH:mm";
  50. var dt = DateTime.Now;
  51. _tbStart.Text = dt.AddHours(-8).ToString("yyyy-MM-dd HH:mm:ss");
  52. _tbEnd.Text = dt.ToString("yyyy-MM-dd HH:mm:ss");
  53. }
  54. internal static void ShowWindow(ViewModel vm, AGCInfo info)
  55. {
  56. ViewModel nvm = new ViewModel(vm.ActPower, vm.PowerSet);
  57. AGCLineWindow alw = new AGCLineWindow(nvm, info);
  58. alw.Owner = Application.Current.MainWindow;
  59. alw.ShowDialog();
  60. }
  61. private void Button_Click(object sender, RoutedEventArgs e)
  62. {
  63. switch (((Control)sender).Tag)
  64. {
  65. case "search":// 搜索
  66. Search();
  67. break;
  68. case "output":// 导出
  69. Output();
  70. break;
  71. default: return;
  72. }
  73. }
  74. /// <summary>
  75. /// 导出
  76. /// </summary>
  77. private void Output()
  78. {
  79. SaveFileDialog sfd = new SaveFileDialog();
  80. sfd.Title = "导出AGC数据";
  81. sfd.Filter = "csv文件(.csv)|*.csv";
  82. var v = sfd.ShowDialog();
  83. if (v != true) return;
  84. var path = sfd.FileName;
  85. try
  86. {
  87. ExportToCSV(path);
  88. MessageWindow.ShowMessage("导出成功!");
  89. }
  90. catch
  91. {
  92. MessageWindow.ShowMessage("导出失败!");
  93. }
  94. }
  95. private void ExportToCSV(string filePathOne)
  96. {
  97. FileStream fs = System.IO.File.Create(filePathOne);
  98. StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
  99. StringBuilder row_txt = new StringBuilder();
  100. sw.WriteLine($"{this.Title}\n实发有功,有功设定限值,时间戳");
  101. for (int i = 0; i < _ViewModel.ActPower.Count; ++i)
  102. {
  103. DateTime dt = _ViewModel.ActPower[i].Ts.GetLongDateTime();
  104. row_txt.Append(_ViewModel.ActPower[i].Value).Append(",");
  105. if (i < _ViewModel.PowerSet.Count)
  106. {
  107. row_txt.Append(_ViewModel.PowerSet[i].Value).Append(",");
  108. }
  109. row_txt.Append(dt.ToString("yyyy-MM-dd HH:mm:ss")).Append("\n");
  110. }
  111. sw.WriteLine(row_txt.ToString());//写入更改
  112. sw.Flush(); //此处必须有此操作
  113. }
  114. /// <summary>
  115. /// 搜索
  116. /// </summary>
  117. private void Search()
  118. {
  119. bool bs = DateTime.TryParse(_tbStart.Text, out DateTime ds);
  120. bool be = DateTime.TryParse(_tbEnd.Text, out DateTime de);
  121. if (!(bs && be))
  122. {
  123. MessageWindow.ShowMessage("时间格式错误!");
  124. return;
  125. }
  126. Task.Run(initHistoryData);
  127. void initHistoryData()
  128. {
  129. var psTag = _AGCInfo.AiPoints.Where(v => v.Type == TagType.PowerSet).FirstOrDefault();
  130. var apTag = _AGCInfo.AiPoints.Where(v => v.Type == TagType.ActualPower).FirstOrDefault();
  131. try
  132. {
  133. var vps = _WEBHelper.HttpGetJSON<List<TsData>>($"{_HistoyUrl}/ts/history/snap?interval=60&tagName={psTag?.Tag}&startTs={ds.GetTimeSpan()}&endTs={de.GetTimeSpan()}");
  134. var vap = _WEBHelper.HttpGetJSON<List<TsData>>($"{_HistoyUrl}/ts/history/snap?interval=60&tagName={apTag?.Tag}&startTs={ds.GetTimeSpan()}&endTs={de.GetTimeSpan()}");
  135. if (vps != null)
  136. {
  137. _ViewModel.SetValue(psTag, vps);
  138. }
  139. if (vap != null)
  140. {
  141. _ViewModel.SetValue(apTag, vap);
  142. }
  143. Dispatcher.Invoke(() => {
  144. _pv.Model = null;
  145. _pv.Model = _ViewModel;
  146. });
  147. }
  148. catch (Exception ex)
  149. {
  150. Console.WriteLine(ex.ToString());
  151. MessageWindow.ShowMessage("数据请求出现错误:" + ex.Message);
  152. }
  153. }
  154. }
  155. }
  156. }