AGCCard2.xaml.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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> _StatusList;
  55. public AGCCard2()
  56. {
  57. InitializeComponent();
  58. _ViewModel = new ViewModel();
  59. _pv.Model = _ViewModel;
  60. }
  61. /// <summary>
  62. /// 初始化
  63. /// </summary>
  64. private void InitInfo()
  65. {
  66. if (info == null)
  67. {
  68. return;
  69. }
  70. _UGMain.Children.Clear();
  71. info.Status.ValueChanged = (v) => Status = v;
  72. foreach (var v in info.AiPoints)
  73. {
  74. AGCTag at = new AGCTag() { Info = v };
  75. _UGMain.Children.Add(at);
  76. }
  77. foreach (var v in info.DiPoints)
  78. {
  79. AGCTag at = new AGCTag() { Info = v, IsDiPoint = true };
  80. _UGDiPoint.Children.Add(at);
  81. }
  82. var vs = info.AiPoints.Where(ap => ap.Type == TagType.PowerSet || ap.Type == TagType.ActualPower);
  83. //_PowerTags = vs;
  84. foreach (var v in vs)
  85. {
  86. v.HistoryValueChanged = HistoryValueChanged;
  87. }
  88. info.Status.HistoryValueChanged = HistoryValueChanged;
  89. var rv = info.AiPoints.Where(ap => ap.Type == TagType.PowerSet).FirstOrDefault();
  90. rv.ValueChanged += PowerSetChanged;
  91. _ActualPower = info.AiPoints.Where(ap => ap.Type == TagType.ActualPower).FirstOrDefault();
  92. }
  93. /// <summary>
  94. /// 有功设定值改变
  95. /// </summary>
  96. private void PowerSetChanged(double obj)
  97. {
  98. RestrictionValue = (1 - (obj / info.InstalledCapacity)).ToString("p");
  99. if (_ActualPower == null || obj == 0) return;
  100. double cdr = Math.Abs((obj - _ActualPower.Value) / obj);
  101. CurveDeviationRate = cdr.ToString("p");
  102. _c.Margin = new Thickness(_pv.ActualModel.PlotArea.Left, 0, 9, 0);
  103. _cdr.Foreground = cdr > 0.03 ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Color.FromRgb(0x31, 0xB9, 0xFB));
  104. }
  105. /// <summary>
  106. /// 历史值改变
  107. /// </summary>
  108. private void HistoryValueChanged(TagInfo arg1, List<TsData> arg2)
  109. {
  110. if (arg2 == null || arg2.Count <= 0) return;
  111. if (arg1.Type == TagType.Other)
  112. {
  113. _StatusList = arg2;
  114. return;
  115. }
  116. _ViewModel.SetValueNow(arg1, arg2);
  117. _pv.Model = null;
  118. _pv.Model = _ViewModel;
  119. if (arg1.Type == TagType.PowerSet)
  120. {
  121. _PowerSetList = arg2;
  122. return;
  123. }
  124. else
  125. {
  126. _ActualPowerList = arg2;
  127. }
  128. if (Status != 1) return;
  129. InitCurveDeviationRate();
  130. }
  131. private void InitCurveDeviationRate()
  132. {
  133. if (_PowerSetList == null || _ActualPowerList == null || _StatusList == null) return;
  134. List<TsData> ls = GetCurveDeviationRateList();
  135. InitCanvas(ls);
  136. }
  137. private void InitCanvas(List<TsData> ls)
  138. {
  139. _c.Children.Clear();
  140. var maxVal = ls.Max(x => x.Value);
  141. var minTime = ls.Min(x => x.Ts);
  142. var maxTime = ls.Max(x => x.Ts);
  143. var totalTime = maxTime - minTime;
  144. var awidth = _c.ActualWidth;
  145. var aheight = _c.ActualHeight;
  146. var width = awidth / ls.Count;
  147. var ct = Math.Min(ls.Count, _StatusList.Count);
  148. for (int i = 0; i < ct; ++i)
  149. {
  150. var v = ls[i];
  151. Rectangle r = new Rectangle();
  152. if (_StatusList[i]?.Value == 0)
  153. {
  154. r.Fill = new SolidColorBrush(Color.FromArgb(0x77, 0x77, 0x77, 0x77));
  155. }
  156. else
  157. {
  158. r.Fill = v.Value > 3 ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.WhiteSmoke);
  159. }
  160. r.Width = width;
  161. r.Height = v.Value * aheight / maxVal;
  162. var left = awidth * (v.Ts - minTime) / totalTime;
  163. Canvas.SetLeft(r, left);
  164. Canvas.SetBottom(r, 0);
  165. r.ToolTip = Math.Round(v.Value, 2);
  166. _c.Children.Add(r);
  167. }
  168. TextBlock tb = new TextBlock() { Text = Math.Round(maxVal, 2).ToString(), Foreground = new SolidColorBrush(Colors.WhiteSmoke) };
  169. Canvas.SetRight(tb, 2);
  170. Canvas.SetTop(tb, 2);
  171. _c.Children.Add(tb);
  172. }
  173. private List<TsData> GetCurveDeviationRateList()
  174. {
  175. List<TsData> ls = new List<TsData>();
  176. int count = Math.Min(_PowerSetList.Count, _ActualPowerList.Count);
  177. for (int i = 0; i < count; i++)
  178. {
  179. double ps = _PowerSetList[i].Value;
  180. double ap = _ActualPowerList[i].Value;
  181. double cdr = ps == 0 ? 0 : Math.Abs((ps - ap) / ps) * 100;
  182. TsData td = new TsData() { Ts = _ActualPowerList[i].Ts, Value = cdr };
  183. ls.Add(td);
  184. }
  185. return ls;
  186. }
  187. private void _pv_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  188. {
  189. if (e.ClickCount < 1) return;
  190. Windows.AGCLineWindow.ShowWindow(_ViewModel, info);
  191. }
  192. }
  193. }