AGCCard.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using LiveCharts;
  2. using LiveCharts.Configurations;
  3. using NEIntelligentControl2.Models.AGC;
  4. using NEIntelligentControl2.Models.Datas;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. namespace NEIntelligentControl2.Views.AGC
  20. {
  21. /// <summary>
  22. /// AGC卡片
  23. /// </summary>
  24. public partial class AGCCard : UserControl
  25. {
  26. // ------------依赖属性------------
  27. public static readonly DependencyProperty StatusProperty = DependencyProperty.Register("Status", typeof(double), typeof(AGCCard));
  28. public static readonly DependencyProperty RestrictionValueProperty = DependencyProperty.Register("RestrictionValue", typeof(string), typeof(AGCCard));
  29. /// <summary>
  30. /// 状态
  31. /// </summary>
  32. public double Status { get => (double)GetValue(StatusProperty); set => SetValue(StatusProperty, value); }
  33. /// <summary>
  34. /// 限电值
  35. /// </summary>
  36. public string RestrictionValue { get => GetValue(RestrictionValueProperty) as string; set => SetValue(RestrictionValueProperty, value); }
  37. private AGCInfo info;
  38. /// <summary>
  39. /// 信息
  40. /// </summary>
  41. public AGCInfo Info { get => info; set { info = value; InitInfo(); } }
  42. private IEnumerable<TagInfo> _PowerTags;
  43. /// <summary>
  44. /// 有功设定数据(历史)
  45. /// </summary>
  46. private ChartValues<TsData> _CVPowerSet { get; set; } = new ChartValues<TsData>();
  47. /// <summary>
  48. /// 实发有功数据(历史)
  49. /// </summary>
  50. private ChartValues<TsData> _CVPower { get; set; } = new ChartValues<TsData>();
  51. private int _CountUpdate;
  52. public AGCCard()
  53. {
  54. InitializeComponent();
  55. _LSPowerSet.Values = _CVPowerSet;
  56. _LSPower.Values = _CVPower;
  57. _LSPower.Configuration = _LSPowerSet.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. }
  60. /// <summary>
  61. /// 初始化
  62. /// </summary>
  63. private void InitInfo()
  64. {
  65. if (info == null)
  66. {
  67. return;
  68. }
  69. _UGMain.Children.Clear();
  70. info.Status.ValueChanged = (v) => Status = v;
  71. foreach (var v in info.AiPoints)
  72. {
  73. AGCTag at = new AGCTag() { Info = v };
  74. _UGMain.Children.Add(at);
  75. }
  76. foreach (var v in info.DiPoints)
  77. {
  78. AGCTag at = new AGCTag() { Info = v, IsDiPoint = true };
  79. _UGDiPoint.Children.Add(at);
  80. }
  81. var vs = info.AiPoints.Where(ap => ap.Type == TagType.PowerSet || ap.Type == TagType.ActualPower);
  82. _PowerTags = vs;
  83. foreach (var v in vs)
  84. {
  85. v.HistoryValueChanged = HistoryValueChanged;
  86. }
  87. var rv = info.AiPoints.Where(ap => ap.Type == TagType.PowerSet).FirstOrDefault();
  88. rv.ValueChanged += PowerSetChanged;
  89. }
  90. /// <summary>
  91. /// 有功设定值改变
  92. /// </summary>
  93. private void PowerSetChanged(double obj)
  94. {
  95. RestrictionValue = (1 - (obj / info.InstalledCapacity)).ToString("p");
  96. UpdateHistoryData();
  97. }
  98. private void UpdateHistoryData()
  99. {
  100. ++_CountUpdate;
  101. if (_CountUpdate < 30) return;
  102. long ts = _PowerTags.Max(t => t.Ts);
  103. foreach (var v in _PowerTags)
  104. {
  105. var td = new TsData() { Ts = ts, Value = v.Value };
  106. if (v.Type == TagType.PowerSet)
  107. {
  108. AddData(_CVPowerSet, td);
  109. }
  110. else
  111. {
  112. AddData(_CVPower, td);
  113. }
  114. }
  115. _CountUpdate = 0;
  116. }
  117. private void AddData(ChartValues<TsData> cv, TsData td)
  118. {
  119. if (cv.Count > 0)
  120. {
  121. var v = cv[cv.Count - 1];
  122. if (v.Ts >= td.Ts) return;
  123. }
  124. cv.Add(td);
  125. RemoveData(_CVPowerSet, td.Ts);
  126. }
  127. private void RemoveData(ChartValues<TsData> cv, long ts)
  128. {
  129. List<TsData> list = new List<TsData>();
  130. var d = ts - 28800000;
  131. foreach (var v in cv)
  132. {
  133. if (v.Ts >= d) break;
  134. list.Add(v);
  135. }
  136. foreach(var v in list)
  137. {
  138. cv.Remove(v);
  139. }
  140. }
  141. /// <summary>
  142. /// 历史值改变
  143. /// </summary>
  144. private void HistoryValueChanged(TagInfo arg1, List<TsData> arg2)
  145. {
  146. if (arg2 == null || arg2.Count <= 0) return;
  147. if (arg1.Type == TagType.PowerSet)
  148. {
  149. UpdateHistoryData(_CVPowerSet, arg2);
  150. }
  151. else
  152. {
  153. UpdateHistoryData(_CVPower, arg2);
  154. }
  155. //_WLCMain.UpdateData(arg1, arg2);
  156. }
  157. private void UpdateHistoryData(ChartValues<TsData> cv, List<TsData> arg2)
  158. {
  159. cv.Clear();
  160. foreach (var v in arg2)
  161. {
  162. cv.Add(v);
  163. }
  164. }
  165. }
  166. }