MatrixPopupLine.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Windows.Forms.DataVisualization.Charting;
  11. using System.Threading;
  12. using WisdomClient;
  13. using IntelligentControlForsx.Service;
  14. using WisdomClient.data;
  15. using log4net;
  16. using IntelligentControlForsx.Model;
  17. using IntelligentControlForsx.Common;
  18. namespace IntelligentControlForsx.MyControls
  19. {
  20. public partial class MatrixPopupLine : UserControl
  21. {
  22. private ILog logger = LogManager.GetLogger("AppInfoLog");
  23. public MatrixPopupLine()
  24. {
  25. InitializeComponent();
  26. }
  27. private void MatrixPopupLine_Load(object sender, EventArgs e)
  28. {
  29. //定义图表大小尺寸
  30. //chart1.Width = Width - 100;
  31. //chart1.Height = Height - 100;
  32. //X轴设置
  33. //chart1.ChartAreas[0].AxisX.Title = "时间";
  34. chart1.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Near;
  35. chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = true;//不显示竖着的分割线
  36. /************************************************************************/
  37. /* 本文重点讲解时间格式的设置
  38. * 如果想显示原点第一个时间坐标,需要设置最小时间,时间间隔类型,时间间隔值等三个参数*/
  39. /************************************************************************/
  40. chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm"; //X轴显示的时间格式,HH为大写时是24小时制,hh小写时是12小时制
  41. chart1.ChartAreas[0].AxisX.Minimum = DateTime.Parse("00:00:00").ToOADate();
  42. chart1.ChartAreas[0].AxisX.Maximum = DateTime.Parse("23:45:00").ToOADate();
  43. chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Seconds;//如果是时间类型的数据,间隔方式可以是秒、分、时
  44. chart1.ChartAreas[0].AxisX.Interval = DateTime.Parse("01:00:00").Second;//间隔为15分钟
  45. //Y轴设置
  46. //chart1.ChartAreas[0].AxisY.Title = "功率";
  47. //chart1.ChartAreas[0].AxisY.TitleAlignment = StringAlignment.Center;
  48. chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = true;//显示横着的分割线
  49. //chart1.ChartAreas[0].AxisY.Minimum = 0;
  50. //chart1.ChartAreas[0].AxisY.Maximum = 600;
  51. //chart1.ChartAreas[0].AxisY.Interval = 5;
  52. //Series绘制
  53. chart1.Series[0].LegendText = "预测功率";
  54. chart1.Series[0].ChartType = SeriesChartType.Spline;
  55. chart1.Series[0].XValueType = ChartValueType.DateTime;
  56. //chart1.Series[0].IsValueShownAsLabel = true;//显示数据点的值
  57. chart1.Series[0].MarkerStyle = MarkerStyle.None;
  58. chart1.Series[1].LegendText = "AGC有功设定";
  59. chart1.Series[1].ChartType = SeriesChartType.Line;
  60. chart1.Series[1].XValueType = ChartValueType.DateTime;
  61. //chart1.Series[1].IsValueShownAsLabel = true;//显示数据点的值
  62. chart1.Series[1].MarkerStyle = MarkerStyle.None;
  63. chart1.Series[2].LegendText = "实时功率";
  64. chart1.Series[2].ChartType = SeriesChartType.Line;
  65. chart1.Series[2].XValueType = ChartValueType.DateTime;
  66. //chart1.Series[2].IsValueShownAsLabel = true;//显示数据点的值
  67. chart1.Series[2].MarkerStyle = MarkerStyle.None;
  68. chart1.Series[3].LegendText = "理论功率";
  69. chart1.Series[3].ChartType = SeriesChartType.Line;
  70. chart1.Series[3].XValueType = ChartValueType.DateTime;
  71. //chart1.Series[3].IsValueShownAsLabel = true;//显示数据点的值
  72. chart1.Series[3].MarkerStyle = MarkerStyle.None;
  73. }
  74. private string stationId;
  75. public string StationId
  76. {
  77. get
  78. {
  79. return stationId;
  80. }
  81. set
  82. {
  83. stationId = value;
  84. Thread ayscThread = new Thread(LoadData);
  85. ayscThread.IsBackground = true;
  86. ayscThread.Start();
  87. }
  88. }
  89. // private DateTime lastUpdateTime = DateTime.Parse
  90. public void BuildChart(DateTime[] dts, Double[] speeds, Double[] powers)
  91. {
  92. chart1.Series[0].Points.Clear();
  93. chart1.Series[1].Points.Clear();
  94. if (dts.Length == speeds.Length && dts.Length == powers.Length)
  95. {
  96. for (int i = 0; i < dts.Length; i++)
  97. {
  98. this.chart1.Series[0].Points.AddXY(dts[i], speeds[i]);
  99. this.chart1.Series[1].Points.AddXY(dts[i], powers[i]);
  100. }
  101. }
  102. chart1.Invalidate();
  103. }
  104. private void chart1_GetToolTipText(object sender, System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs e)
  105. {
  106. //if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint)
  107. //{
  108. // int i = e.HitTestResult.PointIndex;
  109. // //DataPoint dp = e.HitTestResult.Series.Points[i];
  110. // DataPoint dp1 = this.chart1.Series[0].Points[i];
  111. // DataPoint dp2 = this.chart1.Series[1].Points[i];
  112. // //分别显示x轴和y轴的数值,其中{1:F3},表示显示的是float类型,精确到小数点后3位。
  113. // e.Text = string.Format("时间:{0};\n风速:{1:F2};\n功率:{2:F2};", dp1.XValue, dp1.YValues[0], dp2.YValues[0]);
  114. //}
  115. }
  116. public void DeActive()
  117. {
  118. timer1.Stop();
  119. }
  120. public void Active()
  121. {
  122. Thread ayscThread = new Thread(LoadData);
  123. ayscThread.IsBackground = true;
  124. ayscThread.Start();
  125. timer1.Start();
  126. }
  127. #region 异步加载数据
  128. private void timer1_Tick(object sender, EventArgs e)
  129. {
  130. if (isLoadding) return;
  131. Thread ayscThread = new Thread(LoadData);
  132. ayscThread.IsBackground = true;
  133. ayscThread.Start();
  134. //LoadData();
  135. }
  136. private bool isLoadding = false;
  137. private void LoadData()
  138. {
  139. if (isLoadding)
  140. return;
  141. isLoadding = true;
  142. try
  143. {
  144. UpdateCurve();
  145. }
  146. catch (Exception ex)
  147. {
  148. logger.Info("读取实时数据失败!ex=" + ex.Message);
  149. }
  150. finally
  151. {
  152. isLoadding = false;
  153. }
  154. }
  155. private void UpdateCurve()
  156. {
  157. var dict = CacheService.Instance.DictPowerCurveModel;
  158. if (dict == null)
  159. return;
  160. var pcm = dict[StationId];
  161. Console.WriteLine("==================当前曲线刷新场站为" + StationId);
  162. if (pcm == null)
  163. return;
  164. if (pcm.LastUpdateTime < DateTime.Today)
  165. {
  166. //跨天清空缓存数据
  167. pcm.LastUpdateTime = DateTime.Today;
  168. pcm.FactPowerData.Clear();
  169. pcm.PredictPowerData.Clear();
  170. //pcm.CDQPredictPowerData;
  171. pcm.TheoryPowerData.Clear();
  172. pcm.AgcPowerData.Clear();
  173. pcm.WindSpeedData.Clear();
  174. }
  175. DateTime now = DateTime.Now;
  176. long startTs = CommonMethod.ConvertDateTimeInt(pcm.LastUpdateTime);
  177. long endTs = CommonMethod.ConvertDateTimeInt(now);
  178. var lstFactPowerData = RestfulClient.findHistorySnapByTagName(pcm.FactPowerPoint, startTs, endTs, 20);
  179. pcm.FactPowerData.AddRange(lstFactPowerData);
  180. if (StationId != "SBQ_FDC")
  181. {
  182. var lstAgcPowerData = RestfulClient.findHistorySnapByTagName(pcm.AgcPowerPoint, startTs, endTs, 20);
  183. pcm.AgcPowerData.AddRange(lstAgcPowerData);
  184. }
  185. else
  186. {
  187. var lstAgcPowerData1 = RestfulClient.findHistorySnapByTagName("SBQDQ.NX_GD_SBQF_DQ_P1_L1_001_AI0818", startTs, endTs, 20);
  188. var lstAgcPowerData2 = RestfulClient.findHistorySnapByTagName("SBQDQ.NX_GD_SBQF_DQ_P1_L1_001_AI0827", startTs, endTs, 20);
  189. List<TsData> list = new List<TsData>();
  190. for (int i = 0; i < lstAgcPowerData1.Count; i++)
  191. {
  192. if (lstAgcPowerData1.Count == lstAgcPowerData2.Count)
  193. {
  194. TsData t = new TsData();
  195. t.ts = lstAgcPowerData1[i].ts;
  196. t.doubleValue = lstAgcPowerData1[i].doubleValue + lstAgcPowerData2[i].doubleValue;
  197. list.Add(t);
  198. }
  199. }
  200. pcm.AgcPowerData.AddRange(list);
  201. }
  202. var lstTheoryPowerData = RestfulClient.findHistorySnapByTagName(pcm.TheoryPowerPoint, startTs, endTs, 20);
  203. pcm.TheoryPowerData.AddRange(lstTheoryPowerData);
  204. var lstPredictPowerData = RestfulClient.findHistorySnapByTagName(pcm.PredictPowerPoint, startTs, endTs, 20);
  205. pcm.PredictPowerData.AddRange(lstPredictPowerData);
  206. pcm.LastUpdateTime = now;
  207. var dict2 = CacheService.Instance.MatrixCDQPredictPointMap;
  208. if (dict2 != null)
  209. {
  210. var pmms = dict2.Values.Where(q => q.ThingType == PointType.Station && q.ThingId == StationId).ToArray();
  211. var points = pmms.Select(q => q.PointId).ToArray();
  212. Dictionary<string, TsData> dictResult = RestfulClient.findLatestByTagNames(points);
  213. if (dictResult != null && dictResult.Count > 0)
  214. {
  215. for (int i = 0; i < pcm.CDQPredictPowerData.Length; i++)
  216. {
  217. TsData td = new TsData();
  218. td.ts = CommonMethod.ConvertDateTimeInt(pcm.LastUpdateTime.AddMinutes(15 * i));
  219. td.doubleValue = 0;
  220. pcm.CDQPredictPowerData[i] = td;
  221. }
  222. foreach (string pid in dictResult.Keys)
  223. {
  224. string id = pid.Substring(pid.Length - 2, 2);
  225. int index = Convert.ToInt32(id);
  226. pcm.CDQPredictPowerData[index - 1].doubleValue = dictResult[pid].doubleValue == null ? 0 : dictResult[pid].doubleValue.Value;
  227. }
  228. }
  229. }
  230. DateTime endTime = DateTime.Now;
  231. TimeSpan sp = endTime - now;
  232. Console.WriteLine("==============================曲线数据访问时间:" + sp.TotalMilliseconds);
  233. lock (this)
  234. {
  235. this.BeginInvoke(
  236. (Action)delegate
  237. {
  238. try
  239. {
  240. DateTime sTime = DateTime.Now;
  241. this.chart1.Series[0].Points.Clear();
  242. foreach (var td in pcm.PredictPowerData)
  243. {
  244. DateTime dt = CommonMethod.ConvertIntDateTime(td.ts);
  245. this.chart1.Series[0].Points.AddXY(dt, td.doubleValue.Value);
  246. }
  247. foreach (var td in pcm.CDQPredictPowerData)
  248. {
  249. DateTime dt = CommonMethod.ConvertIntDateTime(td.ts);
  250. this.chart1.Series[0].Points.AddXY(dt, td.doubleValue.Value);
  251. }
  252. this.chart1.Series[1].Points.Clear();
  253. foreach (var td in pcm.AgcPowerData)
  254. {
  255. DateTime dt = CommonMethod.ConvertIntDateTime(td.ts);
  256. this.chart1.Series[1].Points.AddXY(dt, td.doubleValue.Value);
  257. }
  258. this.chart1.Series[2].Points.Clear();
  259. foreach (var td in pcm.FactPowerData)
  260. {
  261. DateTime dt = CommonMethod.ConvertIntDateTime(td.ts);
  262. this.chart1.Series[2].Points.AddXY(dt, td.doubleValue.Value);
  263. }
  264. this.chart1.Series[3].Points.Clear();
  265. foreach (var td in pcm.TheoryPowerData)
  266. {
  267. DateTime dt = CommonMethod.ConvertIntDateTime(td.ts);
  268. this.chart1.Series[3].Points.AddXY(dt, td.doubleValue.Value / 1000);
  269. }
  270. DateTime eTime = DateTime.Now;
  271. TimeSpan sp1 = eTime - sTime;
  272. Console.WriteLine("==============================曲线数据绑定时间:" + sp1.TotalMilliseconds);
  273. }
  274. catch (Exception ex)
  275. {
  276. logger.Error(ex);
  277. }
  278. });
  279. }
  280. }
  281. #endregion
  282. }
  283. }