InverterInfoWindow.xaml.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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.WebSocket;
  7. using NEIntelligentControl2.Service.Windturbine;
  8. using NEIntelligentControl2.Views.Infos;
  9. using NEIntelligentControl2.Views.Matrix;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Configuration;
  13. using System.Linq;
  14. using System.Security.Policy;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows;
  18. using System.Windows.Controls;
  19. using System.Windows.Data;
  20. using System.Windows.Documents;
  21. using System.Windows.Input;
  22. using System.Windows.Media;
  23. using System.Windows.Media.Imaging;
  24. using System.Windows.Shapes;
  25. namespace NEIntelligentControl2.Windows.PV
  26. {
  27. /// <summary>
  28. /// 光伏详情页窗口
  29. /// </summary>
  30. public partial class InverterInfoWindow : Window
  31. {
  32. public static readonly DependencyProperty StatusProperty = DependencyProperty.Register("Status", typeof(PVState), typeof(InverterInfoWindow));
  33. public PVState Status { get => (PVState)GetValue(StatusProperty); set => SetValue(StatusProperty, value); }
  34. /// <summary>
  35. /// 矩阵块
  36. /// </summary>
  37. public PVBlock Block { get; set; }
  38. /// <summary>
  39. /// 名称
  40. /// </summary>
  41. public string PVName { get => $"{Info?.Name} {Block?.PVName}"; }
  42. public bool IsSUN2000 { get => Block?.Info.Model == "SUN2000"; }
  43. /// <summary>
  44. /// 信息
  45. /// </summary>
  46. private PVDetails Info { get; set; }
  47. /// <summary>
  48. /// 测点们
  49. /// </summary>
  50. private List<PVTagInfo> _TagInfos;
  51. /// <summary>
  52. /// 需要读取历史的测点们
  53. /// </summary>
  54. private List<PVTagInfo> _HistoryTagInfos;
  55. /// <summary>
  56. /// 所有UniformCode
  57. /// </summary>
  58. private string _Codes;
  59. /// <summary>
  60. /// 是否活跃
  61. /// </summary>
  62. private bool _IsActived;
  63. /// <summary>
  64. /// 数据请求url
  65. /// </summary>
  66. private string _Url = "";
  67. /// <summary>
  68. /// 温度数据(历史)
  69. /// </summary>
  70. private ChartValues<double> _CVTemperature { get; set; } = new ChartValues<double>();
  71. /// <summary>
  72. /// 功率数据(历史)
  73. /// </summary>
  74. private ChartValues<double> _CVPower { get; set; } = new ChartValues<double>();
  75. /// <summary>
  76. /// 光照数据(历史)
  77. /// </summary>
  78. private ChartValues<double> _CVSunshine { get; set; } = new ChartValues<double>();
  79. private WEBHelper _WEBHelper;
  80. private InfoManager _InfoService;
  81. private UrlManager _UrlManager;
  82. public InverterInfoWindow(WEBHelper web, InfoManager iss, UrlManager um)
  83. {
  84. InitializeComponent();
  85. _WEBHelper = web;
  86. _InfoService = iss;
  87. _LineChartTemperature.Values = _CVTemperature;
  88. _LineChartPower.Values = _CVPower;
  89. _LineChartSunshine.Values = _CVSunshine;
  90. _UrlManager = um;
  91. _Url = _UrlManager.DataServicePath;
  92. }
  93. private async void RefreshHistoryData()
  94. {
  95. while (_IsActived)
  96. {
  97. try
  98. {
  99. foreach (var v in _HistoryTagInfos)
  100. {
  101. DateTime de = DateTime.Now;
  102. DateTime ds = de.AddHours(-5);
  103. var url = $"{_Url}/ts/history/snap?thingType=windturbine&thingId={Block.Id}&uniformCode={v.Code}&startTs={ds.GetTimeSpan()}&endTs={de.GetTimeSpan()}&interval=321";
  104. var vs = _WEBHelper.HttpGetJSON<List<TsData>>(url);
  105. Dispatcher.Invoke(() =>
  106. {
  107. v.UpdateHistoryData(vs);
  108. });
  109. }
  110. }
  111. catch (Exception ex)
  112. {
  113. Console.WriteLine(ex.ToString());
  114. }
  115. await Task.Delay(7000);
  116. }
  117. }
  118. /// <summary>
  119. /// 刷新数据
  120. /// </summary>
  121. /// <exception cref="NotImplementedException"></exception>
  122. private async void RefreshData()
  123. {
  124. var url = $"{_Url}/ts/latest?thingType=windturbine&thingId={Block.Id}&uniformCodes={_Codes}";
  125. while (_IsActived)
  126. {
  127. try
  128. {
  129. var vs = _WEBHelper.HttpGetJSON<Dictionary<string, TsData>>(url);
  130. Dispatcher.Invoke(() =>
  131. {
  132. foreach (var v in _TagInfos)
  133. {
  134. v.UpdateValue(vs);
  135. }
  136. });
  137. }
  138. catch (Exception ex)
  139. {
  140. Console.WriteLine(ex.ToString());
  141. }
  142. await Task.Delay(2000);
  143. }
  144. }
  145. /// <summary>
  146. /// 初始化UniformCode
  147. /// </summary>
  148. /// <exception cref="NotImplementedException"></exception>
  149. private void InitCodes()
  150. {
  151. StringBuilder sb = new StringBuilder();
  152. foreach (var v in _TagInfos)
  153. {
  154. sb.Append(v.Code).Append(',');
  155. }
  156. _Codes = sb.ToString();
  157. }
  158. /// <summary>
  159. /// 初始化标签点信息
  160. /// </summary>
  161. private void InitTagInfos()
  162. {
  163. _TagInfos = new List<PVTagInfo>();
  164. var infos = _InfoService.GetPVDetails();
  165. if (infos == null) return;
  166. if (!infos.ContainsKey(Block.Info.Station)) return;
  167. Info = infos[Block.Info.Station];
  168. var vs = Info.CodeInfos?.Where(p => !p.IsIgnore);
  169. if (vs == null) return;
  170. foreach (var v in vs)
  171. {
  172. InverterPoint ip = new InverterPoint() { TagInfo = v, PVId = Block.Id };
  173. _UGSid.Children.Add(ip);
  174. _TagInfos.Add(v);
  175. }
  176. _HistoryTagInfos = Info.CodeInfos.Where(ci => ci.Type != PVTagType.Other).ToList();
  177. foreach (var v in _HistoryTagInfos)
  178. {
  179. v.HistoryValueChanged = HistoryValueChanged;
  180. }
  181. }
  182. /// <summary>
  183. /// 历史数据改变
  184. /// </summary>
  185. private void HistoryValueChanged(PVTagInfo pi, List<TsData> tds)
  186. {
  187. if (tds == null) return;
  188. AddAxisX(tds);
  189. switch (pi.Type)
  190. {
  191. case PVTagType.Power:
  192. SetCahrValues(_CVPower, tds);
  193. break;
  194. case PVTagType.Sunshine:
  195. SetCahrValues(_CVSunshine, tds);
  196. break;
  197. case PVTagType.Temperature:
  198. SetCahrValues(_CVTemperature, tds);
  199. break;
  200. }
  201. }
  202. /// <summary>
  203. /// 添加横坐标
  204. /// </summary>
  205. private void AddAxisX(List<TsData> val)
  206. {
  207. List<string> vs = new List<string>();
  208. DateTime dtStand = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
  209. foreach (var v in val)
  210. {
  211. DateTime dt = dtStand.AddMilliseconds(v.Ts);
  212. vs.Add(dt.ToShortTimeString());
  213. }
  214. if (vs.Count <= 0) return;
  215. _ChartAxisX.Labels = vs;
  216. }
  217. /// <summary>
  218. /// 设置图表数据
  219. /// </summary>
  220. private void SetCahrValues(ChartValues<double> cv, List<TsData> values)
  221. {
  222. if (values == null || values.Count <= 0)
  223. {
  224. if (cv.Count <= 0)
  225. {
  226. for (int i = 0; i < 56; ++i)
  227. {
  228. cv.Add(0);
  229. }
  230. }
  231. }
  232. else
  233. {
  234. cv.Clear();
  235. foreach (var v in values)
  236. {
  237. cv.Add(Math.Round(v.Value, 2));
  238. }
  239. Console.WriteLine("获得数据");
  240. }
  241. }
  242. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  243. {
  244. try
  245. {
  246. this.DragMove();
  247. }
  248. catch { }
  249. }
  250. private void Button_Click(object sender, RoutedEventArgs e)
  251. {
  252. _IsActived = false;
  253. this.Close();
  254. }
  255. private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  256. {
  257. if (e.ClickCount < 2 || !IsSUN2000) return;
  258. SUN2000InfoWindow.Show(this);
  259. }
  260. /// <summary>
  261. /// 显示光伏详情页
  262. /// </summary>
  263. internal static void Show(PVBlock ib)
  264. {
  265. InverterInfoWindow iiw = App.ServiceProvider.GetService(typeof(InverterInfoWindow)) as InverterInfoWindow;
  266. iiw.Block = ib;
  267. iiw.Owner = Application.Current.MainWindow;
  268. iiw.Status = ib.Status;
  269. iiw.ShowDialog();
  270. }
  271. private void Window_Loaded(object sender, RoutedEventArgs e)
  272. {
  273. InitTagInfos();
  274. InitCodes();
  275. _IsActived = true;
  276. Task.Run(RefreshData);
  277. Task.Run(RefreshHistoryData);
  278. }
  279. }
  280. }