SUN2000InfoWindow.xaml.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using LiveCharts;
  2. using NEIntelligentControl2.Models;
  3. using NEIntelligentControl2.Models.Datas;
  4. using NEIntelligentControl2.Models.Messages;
  5. using NEIntelligentControl2.Models.PV;
  6. using NEIntelligentControl2.Service.Windturbine;
  7. using NEIntelligentControl2.Views.Infos;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Configuration;
  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.PV
  23. {
  24. /// <summary>
  25. /// SUN2000光伏机型的详细页面
  26. /// </summary>
  27. public partial class SUN2000InfoWindow : Window
  28. {
  29. public static readonly DependencyProperty IsForwardSortProperty = DependencyProperty.Register("IsForwardSort", typeof(bool), typeof(SUN2000InfoWindow));
  30. /// <summary>
  31. /// 是否活跃
  32. /// </summary>
  33. private bool _IsActived;
  34. /// <summary>
  35. /// 数据请求url
  36. /// </summary>
  37. private string _Url = "";
  38. /// <summary>
  39. /// 所有UniformCode
  40. /// </summary>
  41. private string _Codes = "";
  42. private List<SUN2000Info> _SUN2000Infos;
  43. /// <summary>
  44. /// 光伏逆变器ID
  45. /// </summary>
  46. public string PVId { get; set; }
  47. /// <summary>
  48. /// 是否正向排序
  49. /// </summary>
  50. public bool IsForwardSort { get => (bool)GetValue(IsForwardSortProperty); set => SetValue(IsForwardSortProperty, value); }
  51. private WEBHelper _WEBHelper;
  52. private InfoManager _InfoService;
  53. public SUN2000InfoWindow(WEBHelper web, InfoManager iss)
  54. {
  55. InitializeComponent();
  56. _WEBHelper = web;
  57. _InfoService = iss;
  58. Init();
  59. }
  60. private void Init()
  61. {
  62. _SUN2000Infos = _InfoService.GetSUN2000Infos();
  63. if (_SUN2000Infos == null)
  64. {
  65. return;
  66. }
  67. try
  68. {
  69. #if (DEBUG)
  70. _Url = ConfigurationManager.AppSettings["DataServicePathDebug"];
  71. #else
  72. _Url = ConfigurationManager.AppSettings["ServicePath"];
  73. #endif
  74. }
  75. catch (Exception ex)
  76. {
  77. Console.WriteLine("读取配置文件[DataServicePath]出错!", ex);
  78. }
  79. // 初始化UniformCode
  80. InitTags();
  81. _IsActived = true;
  82. Task.Factory.StartNew(RefreshData, TaskCreationOptions.LongRunning);
  83. Task.Run(InitHistoryData);
  84. }
  85. /// <summary>
  86. /// 初始化历史数据
  87. /// </summary>
  88. private void InitHistoryData()
  89. {
  90. DateTime de = DateTime.Now;
  91. DateTime ds = DateTime.Parse("06:00");
  92. List<List<TsData>> ls = new List<List<TsData>>();
  93. foreach (var v in _SUN2000Infos)
  94. {
  95. var url = $"{_Url}/ts/history/snap?thingType=windturbine&thingId={PVId}&uniformCode={v.I.Code}&startTs={ds.GetTimeSpan()}&endTs={de.GetTimeSpan()}&interval=60";
  96. var vs = _WEBHelper.HttpGetJSON<List<TsData>>(url);
  97. if (vs == null || vs.Count <= 0)
  98. {
  99. continue;
  100. }
  101. if (v.ΔI == 0)
  102. {
  103. v.ΔI = GetΔI(vs);
  104. }
  105. ls.Add(vs);
  106. }
  107. List<TsData> Δtss = GetDataSum(ls);
  108. AddAxisX(Δtss);
  109. SetCahrValues(Δtss);
  110. }
  111. /// <summary>
  112. /// 添加横坐标
  113. /// </summary>
  114. private void AddAxisX(List<TsData> val)
  115. {
  116. List<string> vs = new List<string>();
  117. DateTime dtStand = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
  118. foreach (var v in val)
  119. {
  120. DateTime dt = dtStand.AddMilliseconds(v.Ts);
  121. vs.Add(dt.ToShortTimeString());
  122. }
  123. if (vs.Count <= 0) return;
  124. Dispatcher.Invoke(() => _ChartAxisX.Labels = vs);
  125. }
  126. /// <summary>
  127. /// 设置图表数据
  128. /// </summary>
  129. private void SetCahrValues(List<TsData> values)
  130. {
  131. ChartValues<double> cv = new ChartValues<double>();
  132. foreach (var v in values)
  133. {
  134. cv.Add(v.Value);
  135. }
  136. Dispatcher.Invoke(() =>
  137. {
  138. _LineChartI.Values = cv;
  139. });
  140. }
  141. /// <summary>
  142. /// 获取历史值的和的变化值
  143. /// </summary>
  144. private List<TsData> GetDataSum(List<List<TsData>> vs)
  145. {
  146. List<TsData> ls = new List<TsData>();
  147. double oi = 0;
  148. for (int i = 0; i < vs[0].Count; ++i)
  149. {
  150. double d = 0;
  151. long ts = 0;
  152. foreach (var v in vs)
  153. {
  154. if (v.Count <= i)
  155. {
  156. continue;
  157. }
  158. d += v[i].Value;
  159. ts = v[i].Ts;
  160. }
  161. if (d == 0)
  162. {
  163. continue;
  164. }
  165. if (oi != 0)
  166. {
  167. ls.Add(new TsData() { Ts = ts, Value = Math.Abs(d - oi) });
  168. }
  169. oi = d;
  170. }
  171. return ls;
  172. }
  173. /// <summary>
  174. /// 从历史数据中获取电流变化值
  175. /// </summary>
  176. private double GetΔI(List<TsData> vs)
  177. {
  178. if (vs == null || vs.Count <= 0) return 0;
  179. double s = vs[vs.Count - 1].Value;
  180. for (int i = vs.Count - 2; i >= 0; --i)
  181. {
  182. if (vs[i].Value == s)
  183. {
  184. continue;
  185. }
  186. return Math.Abs(s - vs[i].Value);
  187. }
  188. return 0;
  189. }
  190. private void InitTags()
  191. {
  192. StringBuilder sb = new StringBuilder();
  193. foreach (var v in _SUN2000Infos)
  194. {
  195. sb.Append(v.I.Code).Append(',').Append(v.V.Code).Append(',');
  196. SUN2000InfoTag st = new SUN2000InfoTag() { Info = v };
  197. _SPMain.Children.Add(st);
  198. }
  199. _Codes = sb.ToString();
  200. }
  201. private async void RefreshData()
  202. {
  203. var url = $"{_Url}/ts/latest?thingType=windturbine&thingId={PVId}&uniformCodes={_Codes}";
  204. while (_IsActived)
  205. {
  206. try
  207. {
  208. var vs = _WEBHelper.HttpGetJSON<Dictionary<string, TsData>>(url);
  209. Dispatcher.Invoke(() =>
  210. {
  211. foreach (var v in _SUN2000Infos)
  212. {
  213. v.UpdateValue(vs);
  214. }
  215. });
  216. }
  217. catch (Exception ex)
  218. {
  219. Console.WriteLine(ex.ToString());
  220. }
  221. await Task.Delay(1000);
  222. }
  223. }
  224. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) => this.DragMove();
  225. private void Button_Click(object sender, RoutedEventArgs e)
  226. {
  227. _IsActived = false;
  228. this.Close();
  229. }
  230. internal static void Show(InverterInfoWindow iiw)
  231. {
  232. if (iiw == null || iiw.Block == null)
  233. {
  234. return;
  235. }
  236. SUN2000InfoWindow siw = App.ServiceProvider.GetService(typeof(SUN2000InfoWindow)) as SUN2000InfoWindow;
  237. siw.PVId = iiw.Block.Id;
  238. siw.Owner = iiw;
  239. siw.ShowDialog();
  240. }
  241. /// <summary>
  242. /// 排序
  243. /// </summary>
  244. private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  245. {
  246. List<SUN2000InfoTag> ls = _SPMain.Children.OfType<SUN2000InfoTag>().ToList();
  247. if ((((FrameworkElement)sender).Tag as string) == "p")
  248. {
  249. _SPP.Visibility = Visibility.Visible;
  250. _SPI.Visibility = Visibility.Collapsed;
  251. ls.Sort((x, y) =>
  252. {
  253. if (x.P == y.P) return 0;
  254. int i = 0;
  255. if (x.P > y.P)
  256. {
  257. i = 1;
  258. }
  259. else
  260. {
  261. i = -1;
  262. }
  263. return IsForwardSort ? i : -i;
  264. });
  265. }
  266. else
  267. {
  268. _SPI.Visibility = Visibility.Visible;
  269. _SPP.Visibility = Visibility.Collapsed;
  270. ls.Sort((x, y) =>
  271. {
  272. if (x.I == y.I) return 0;
  273. int i = 0;
  274. if (x.I > y.I)
  275. {
  276. i = 1;
  277. }
  278. else
  279. {
  280. i = -1;
  281. }
  282. return IsForwardSort ? i : -i;
  283. });
  284. }
  285. IsForwardSort = !IsForwardSort;
  286. foreach (var v in ls)
  287. {
  288. _SPMain.Children.Remove(v);
  289. _SPMain.Children.Add(v);
  290. }
  291. }
  292. }
  293. }