AGCCard2.xaml.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using LiveCharts.Configurations;
  2. using NEIntelligentControl2.Models.AGC;
  3. using NEIntelligentControl2.Models.Datas;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Runtime.ConstrainedExecution;
  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 AGCCard2 : UserControl
  25. {
  26. // ------------依赖属性------------
  27. public static readonly DependencyProperty StatusProperty = DependencyProperty.Register("Status", typeof(double), typeof(AGCCard2));
  28. public static readonly DependencyProperty RestrictionValueProperty = DependencyProperty.Register("RestrictionValue", typeof(string), typeof(AGCCard2));
  29. public static readonly DependencyProperty CurveDeviationRateProperty = DependencyProperty.Register("CurveDeviationRate", typeof(string), typeof(AGCCard2));
  30. /// <summary>
  31. /// 状态
  32. /// </summary>
  33. public double Status { get => (double)GetValue(StatusProperty); set => SetValue(StatusProperty, value); }
  34. /// <summary>
  35. /// 限电值
  36. /// </summary>
  37. public string RestrictionValue { get => GetValue(RestrictionValueProperty) as string; set => SetValue(RestrictionValueProperty, value); }
  38. /// <summary>
  39. /// 偏差率
  40. /// </summary>
  41. public string CurveDeviationRate { get => GetValue(CurveDeviationRateProperty) as string; set => SetValue(CurveDeviationRateProperty, value); }
  42. private AGCInfo info;
  43. /// <summary>
  44. /// 信息
  45. /// </summary>
  46. public AGCInfo Info { get => info; set { info = value; InitInfo(); } }
  47. private ViewModel _ViewModel;
  48. /// <summary>
  49. /// 实发有功
  50. /// </summary>
  51. private TagInfo _ActualPower;
  52. private List<TsData> _PowerSetList;
  53. private List<TsData> _ActualPowerList;
  54. private List<TsData> _IdeaPowerList;
  55. private List<TsData> _StatusList;
  56. public AGCCard2()
  57. {
  58. InitializeComponent();
  59. _ViewModel = new ViewModel();
  60. _pv.Model = _ViewModel;
  61. }
  62. /// <summary>
  63. /// 初始化
  64. /// </summary>
  65. private void InitInfo()
  66. {
  67. if (info == null)
  68. {
  69. return;
  70. }
  71. _UGMain.Children.Clear();
  72. info.Status.ValueChanged = (v) => Status = v;
  73. foreach (var v in info.AiPoints)
  74. {
  75. AGCTag at = new AGCTag() { Info = v };
  76. _UGMain.Children.Add(at);
  77. }
  78. foreach (var v in info.DiPoints)
  79. {
  80. AGCTag at = new AGCTag() { Info = v, IsDiPoint = true };
  81. _UGDiPoint.Children.Add(at);
  82. }
  83. var vs = info.AiPoints.Where(ap => ap.Type == TagType.PowerSet || ap.Type == TagType.ActualPower || ap.Type == TagType.IdeaPower);
  84. //_PowerTags = vs;
  85. foreach (var v in vs)
  86. {
  87. v.HistoryValueChanged = HistoryValueChanged;
  88. }
  89. info.Status.HistoryValueChanged = HistoryValueChanged;
  90. var rv = info.AiPoints.Where(ap => ap.Type == TagType.PowerSet).FirstOrDefault();
  91. rv.ValueChanged += PowerSetChanged;
  92. _ActualPower = info.AiPoints.Where(ap => ap.Type == TagType.ActualPower).FirstOrDefault();
  93. }
  94. /// <summary>
  95. /// 有功设定值改变
  96. /// </summary>
  97. private void PowerSetChanged(double obj)
  98. {
  99. RestrictionValue = (1 - (obj / info.InstalledCapacity)).ToString("p");
  100. if (_ActualPower == null || obj == 0) return;
  101. double cdr = Math.Abs((obj - _ActualPower.Value) / obj);
  102. CurveDeviationRate = cdr.ToString("p");
  103. _c.Margin = new Thickness(_pv.ActualModel.PlotArea.Left, 0, 9, 0);
  104. _cdr.Foreground = cdr > 0.03 ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Color.FromRgb(0x31, 0xB9, 0xFB));
  105. }
  106. /// <summary>
  107. /// 历史值改变
  108. /// </summary>
  109. private void HistoryValueChanged(TagInfo arg1, List<TsData> arg2)
  110. {
  111. if (arg2 == null || arg2.Count <= 0) return;
  112. if (arg1.Type == TagType.Other)
  113. {
  114. _StatusList = arg2;
  115. return;
  116. }
  117. _ViewModel.SetValueNow(arg1, arg2);
  118. _pv.Model = null;
  119. _pv.Model = _ViewModel;
  120. if (arg1.Type == TagType.PowerSet)
  121. {
  122. _PowerSetList = arg2;
  123. return;
  124. }
  125. else if (arg1.Type == TagType.ActualPower)
  126. {
  127. _ActualPowerList = arg2;
  128. }
  129. else if (arg1.Type == TagType.IdeaPower)
  130. {
  131. _IdeaPowerList = arg2;
  132. }
  133. if (Status != 1) return;
  134. InitCurveDeviationRate();
  135. }
  136. private void InitCurveDeviationRate()
  137. {
  138. if (_PowerSetList == null || _ActualPowerList == null || _StatusList == null) return;
  139. List<TsData> ls = GetCurveDeviationRateList();
  140. InitCanvas(ls);
  141. }
  142. private void InitCanvas(List<TsData> ls)
  143. {
  144. _c.Children.Clear();
  145. var maxVal = ls.Max(x => x.Value);
  146. var minTime = ls.Min(x => x.Ts);
  147. var maxTime = ls.Max(x => x.Ts);
  148. var totalTime = maxTime - minTime;
  149. var awidth = _c.ActualWidth;
  150. var aheight = _c.ActualHeight;
  151. var width = awidth / ls.Count;
  152. var ct = Math.Min(ls.Count, _StatusList.Count);
  153. for (int i = 0; i < ct; ++i)
  154. {
  155. var v = ls[i];
  156. Rectangle r = new Rectangle();
  157. if (_StatusList[i]?.Value == 0)
  158. {
  159. r.Fill = new SolidColorBrush(Color.FromArgb(0x77, 0x77, 0x77, 0x77));
  160. }
  161. else
  162. {
  163. r.Fill = v.Value > 3 ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.WhiteSmoke);
  164. }
  165. r.Width = width;
  166. r.Height = v.Value * aheight / maxVal;
  167. var left = awidth * (v.Ts - minTime) / totalTime;
  168. Canvas.SetLeft(r, left);
  169. Canvas.SetBottom(r, 0);
  170. r.ToolTip = Math.Round(v.Value, 2);
  171. _c.Children.Add(r);
  172. }
  173. TextBlock tb = new TextBlock() { Text = Math.Round(maxVal, 2).ToString(), Foreground = new SolidColorBrush(Colors.WhiteSmoke) };
  174. Canvas.SetRight(tb, 2);
  175. Canvas.SetTop(tb, 2);
  176. _c.Children.Add(tb);
  177. }
  178. private List<TsData> GetCurveDeviationRateList()
  179. {
  180. List<TsData> ls = new List<TsData>();
  181. int count = Math.Min(_PowerSetList.Count, _ActualPowerList.Count);
  182. for (int i = 0; i < count; i++)
  183. {
  184. double ps = _PowerSetList[i].Value;
  185. double ap = _ActualPowerList[i].Value;
  186. double cdr = ps == 0 ? 0 : Math.Abs((ps - ap) / ps) * 100;
  187. TsData td = new TsData() { Ts = _ActualPowerList[i].Ts, Value = cdr };
  188. ls.Add(td);
  189. }
  190. return ls;
  191. }
  192. private void _pv_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  193. {
  194. if (e.ClickCount < 1) return;
  195. Windows.AGCLineWindow.ShowWindow(_ViewModel, info);
  196. }
  197. }
  198. }