HistoryDataWindow.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using LiveCharts;
  2. using LiveCharts.Configurations;
  3. using NEIntelligentControl2.Models;
  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.Configuration;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Shapes;
  22. namespace NEIntelligentControl2.Windows
  23. {
  24. /// <summary>
  25. /// 历史曲线
  26. /// </summary>
  27. public partial class HistoryDataWindow : Window
  28. {
  29. private string lineName;
  30. public string LineName { get => lineName; set { lineName = value; _LSLine.Title = value; } }
  31. public string LineTag { get; set; }
  32. public string UniformCode { get; set; }
  33. public double Multiplier { get; set; } = 1;
  34. public string ThingType { get; set; }
  35. public string ThingId { get; set; }
  36. private double startTime;
  37. public double StartTime { get => startTime; private set { startTime = value; _TBStart.Text = DateTime.Now.AddHours(-value).ToString("yyyy-MM-dd HH:mm:ss"); } }
  38. /// <summary>
  39. /// 有功设定数据(历史)
  40. /// </summary>
  41. private ChartValues<TsData> _CVLine { get; set; } = new ChartValues<TsData>();
  42. /// <summary>
  43. /// 数据路径
  44. /// </summary>
  45. private string _Url;
  46. private WEBHelper _WEBHelper;
  47. private UrlManager _UrlManager;
  48. private List<TsData> _CurrentDatas;
  49. private int _Interval = 5;// 快照间隔
  50. public HistoryDataWindow(WEBHelper web, UrlManager um)
  51. {
  52. InitializeComponent();
  53. _WEBHelper = web;
  54. _TBStart.Text = DateTime.Now.AddHours(-8).ToString("yyyy-MM-dd HH:mm:ss");
  55. _TBEnd.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  56. _LSLine.Values = _CVLine;
  57. _LSLine.Configuration = Mappers.Xy<TsData>().X(d => d == null ? 0 : d.Ts).Y(d => d == null ? 0 : Math.Round(d.Value, 2));
  58. _ChartAxisX.LabelFormatter = x => TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddMilliseconds(x).ToShortTimeString();
  59. _UrlManager = um;
  60. _Url = um.DataServicePath;
  61. }
  62. private void Button_Click(object sender, RoutedEventArgs e)
  63. {
  64. switch (((Control)sender).Tag)
  65. {
  66. case "output":
  67. Output();
  68. break;
  69. case "search":
  70. Search();
  71. break;
  72. default: return;
  73. }
  74. }
  75. private void Search()
  76. {
  77. bool b1 = DateTime.TryParse(_TBStart.Text, out DateTime ds);
  78. bool b2 = DateTime.TryParse(_TBEnd.Text, out DateTime de);
  79. if (!b1 || !b2)
  80. {
  81. MessageWindow.ShowMessage("时间格式错误!");
  82. return;
  83. }
  84. Task.Run(toSearchData);
  85. void toSearchData()
  86. {
  87. try
  88. {
  89. var dts = ds.GetTimeSpan();
  90. var dte = de.GetTimeSpan();
  91. var url = GetSearchUrl(dts, dte);
  92. List<TsData> vs = null;
  93. if (string.IsNullOrWhiteSpace(url)) return;
  94. vs = _WEBHelper.HttpGetJSON<List<TsData>>(url);
  95. if (Multiplier != 1)
  96. {
  97. foreach (var v in vs)
  98. {
  99. v.Value = v.Value * Multiplier;
  100. }
  101. }
  102. _CurrentDatas = vs;
  103. Dispatcher.Invoke(() =>
  104. {
  105. _CVLine.Clear();
  106. _CVLine.AddRange(vs);
  107. });
  108. }
  109. catch (Exception ex)
  110. {
  111. Console.WriteLine(ex.ToString());
  112. }
  113. }
  114. }
  115. private string GetSearchUrl(long dts, long dte)
  116. {
  117. if (_Interval > 0)
  118. {
  119. var url = $"{_Url}/ts/history/snap?interval={_Interval * 60}";
  120. if (UniformCode != null)
  121. {
  122. return $"{url}&thingType={ThingType}&thingId={ThingId}&uniformCode={UniformCode}&startTs={dts}&endTs={dte}";
  123. }
  124. else if (LineTag != null)
  125. {
  126. return $"{url}&tagName={LineTag}&startTs={dts}&endTs={dte}";
  127. }
  128. }
  129. if (UniformCode != null)
  130. {
  131. return $"{_Url}/ts/history/raw?&thingType={ThingType}&thingId={ThingId}&uniformCode={UniformCode}&startTs={dts}&endTs={dte}";
  132. }
  133. else if (LineTag != null)
  134. {
  135. return $"{_Url}/ts/history/raw?tagName={LineTag}&startTs={dts}&endTs={dte}";
  136. }
  137. return "";
  138. }
  139. private void Output()
  140. {
  141. string path = SelectPath();
  142. if (string.IsNullOrWhiteSpace(path)) return;
  143. try
  144. {
  145. string filePathOne = System.IO.Path.Combine(path, this.LineName + ".csv");
  146. ExportToCSV(filePathOne, _CurrentDatas);
  147. MessageWindow.ShowMessage("导出成功");
  148. }
  149. catch (Exception e)
  150. {
  151. MessageWindow.ShowMessage("导出失败:" + e.Message);
  152. }
  153. }
  154. private string SelectPath() //弹出一个选择目录的对话框
  155. {
  156. System.Windows.Forms.FolderBrowserDialog path = new System.Windows.Forms.FolderBrowserDialog();
  157. if (path.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  158. {
  159. return path.SelectedPath;
  160. }
  161. return null;
  162. }
  163. private void ExportToCSV(string filePathOne, IList<TsData> dataList) //filePath为保存到本地磁盘的位置
  164. {
  165. FileStream fs = System.IO.File.Create(filePathOne);
  166. StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
  167. string col_txt = "";
  168. string row_txt = "";
  169. col_txt = this.LineName + ",时间戳";
  170. sw.WriteLine(col_txt);//写入更改
  171. for (int i = 0; i < dataList.Count; i++)
  172. {
  173. row_txt = "";//容易漏写,造成数据的重复写入
  174. DateTime dt = ConvertIntDateTime(dataList[i].Ts);
  175. row_txt = row_txt + dataList[i].Value + "," + dt.ToString("yyyy-MM-dd HH:mm:ss");
  176. sw.WriteLine(row_txt);//写入更改
  177. }
  178. sw.Flush(); //此处必须有此操作
  179. }
  180. public static DateTime ConvertIntDateTime(long utc)
  181. {
  182. System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
  183. startTime = startTime.AddMilliseconds(utc);
  184. //startTime = startTime.AddHours(8);
  185. return startTime;
  186. }
  187. private void Window_Loaded(object sender, RoutedEventArgs e)
  188. {
  189. Search();
  190. }
  191. /// <summary>
  192. /// 显示历史曲线
  193. /// </summary>
  194. /// <param name="name">名称</param>
  195. /// <param name="tag">测点</param>
  196. /// <param name="multiplier">倍率</param>
  197. public static void ShowWindow(string name, string tag, double multiplier)
  198. {
  199. HistoryDataWindow hdw = App.ServiceProvider.GetService(typeof(HistoryDataWindow)) as HistoryDataWindow; ;
  200. hdw.Owner = Application.Current.MainWindow;
  201. hdw.LineTag = tag;
  202. hdw.LineName = name;
  203. hdw.Multiplier = multiplier;
  204. hdw.ShowDialog();
  205. }
  206. /// <summary>
  207. /// 显示历史曲线
  208. /// </summary>
  209. /// <param name="pointName">名称</param>
  210. /// <param name="uniformcode">统一识别码</param>
  211. /// <param name="thingType">设备类型</param>
  212. /// <param name="thingId">设备ID</param>
  213. /// <param name="ratio">倍率</param>
  214. internal static void ShowWindow(string pointName, string uniformcode, string thingType, string thingId, double ratio)
  215. {
  216. HistoryDataWindow hdw = App.ServiceProvider.GetService(typeof(HistoryDataWindow)) as HistoryDataWindow; ;
  217. hdw.Owner = Application.Current.MainWindow;
  218. hdw.ThingType = thingType;
  219. hdw.ThingId = thingId;
  220. hdw.UniformCode = uniformcode;
  221. hdw.LineName = pointName;
  222. hdw.Multiplier = ratio;
  223. hdw.ShowDialog();
  224. }
  225. /// <summary>
  226. /// 显示历史曲线
  227. /// </summary>
  228. /// <param name="name">名称</param>
  229. /// <param name="uniformcode">统一识别码</param>
  230. /// <param name="thingType">设备类型</param>
  231. /// <param name="thingId">设备ID</param>
  232. /// <param name="ratio">倍率</param>
  233. /// <param name="hour">开始时间</param>
  234. internal static void ShowWindow(string name, string uniformcode, string thingType, string thingId, double ratio, double hour)
  235. {
  236. HistoryDataWindow hdw = App.ServiceProvider.GetService(typeof(HistoryDataWindow)) as HistoryDataWindow; ;
  237. hdw.Owner = Application.Current.MainWindow;
  238. hdw.ThingType = thingType;
  239. hdw.ThingId = thingId;
  240. hdw.UniformCode = uniformcode;
  241. hdw.LineName = name;
  242. hdw.Multiplier = ratio;
  243. hdw.StartTime = hour;
  244. hdw.ShowDialog();
  245. }
  246. private void TextBox_KeyDown(object sender, KeyEventArgs e)
  247. {
  248. if (e.Key != Key.Enter) return;
  249. Search();
  250. }
  251. private void RadioButton_Click(object sender, RoutedEventArgs e)
  252. {
  253. int.TryParse(((RadioButton)sender).Tag as string, out int i);
  254. _Interval = i;
  255. Search();
  256. }
  257. }
  258. }