AGCManager.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using log4net;
  2. using NEIntelligentControl2.Models;
  3. using NEIntelligentControl2.Models.AGC;
  4. using NEIntelligentControl2.Models.Datas;
  5. using NEIntelligentControl2.Models.Messages;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Configuration;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace NEIntelligentControl2.Service.AGC
  13. {
  14. /// <summary>
  15. /// AGC信息管理
  16. /// </summary>
  17. public class AGCManager
  18. {
  19. private static readonly ILog log = LogManager.GetLogger("File");
  20. /// <summary>
  21. /// AGC 信息
  22. /// </summary>
  23. public List<AGCInfo> AGCInfos;
  24. /// <summary>
  25. /// 标签点
  26. /// </summary>
  27. private List<TagInfo> _TagInfos;
  28. /// <summary>
  29. /// 需要读取历史的标签点
  30. /// </summary>
  31. private List<TagInfo> _HistoryTagInfos;
  32. /// <summary>
  33. /// 所有标签点
  34. /// </summary>
  35. private string _Tags;
  36. /// <summary>
  37. /// 数据路径
  38. /// </summary>
  39. private string _Url;
  40. /// <summary>
  41. /// 历史数据url
  42. /// </summary>
  43. private string _HistoyUrl;
  44. /// <summary>
  45. /// 数据路径
  46. /// </summary>
  47. private string _DataUrl;
  48. /// <summary>
  49. /// 信息路径
  50. /// </summary>
  51. private string _UrlInfo;
  52. private WEBHelper _WEBHelper;
  53. public AGCManager(WEBHelper web)
  54. {
  55. _WEBHelper = web;
  56. InitUrl();
  57. Init();
  58. }
  59. private void InitUrl()
  60. {
  61. try
  62. {
  63. #if (DEBUG)
  64. _UrlInfo = ConfigurationManager.AppSettings["ServicePathDebug"];
  65. _Url = ConfigurationManager.AppSettings["DataServicePathDebug"];
  66. #else
  67. _UrlInfo = ConfigurationManager.AppSettings["ServicePath"];
  68. _Url = ConfigurationManager.AppSettings["DataServicePath"];
  69. #endif
  70. }
  71. catch (Exception ex)
  72. {
  73. Console.WriteLine("读取配置文件[ServicePath]或[DataServicePath]出错!", ex);
  74. }
  75. }
  76. /// <summary>
  77. /// 初始化
  78. /// </summary>
  79. private void Init()
  80. {
  81. try
  82. {
  83. _HistoryTagInfos = new List<TagInfo>();
  84. _TagInfos = new List<TagInfo>();
  85. AGCInfos = _WEBHelper.HttpGetJSON<List<AGCInfo>>($"{_UrlInfo}/api/station/agc_net");
  86. InitTags();
  87. }
  88. catch (Exception ex)
  89. {
  90. Console.WriteLine(ex.Message);
  91. }
  92. }
  93. /// <summary>
  94. /// 初始化标签点
  95. /// </summary>
  96. private void InitTags()
  97. {
  98. StringBuilder sb = new StringBuilder();
  99. foreach (var v in AGCInfos)
  100. {
  101. FindTags(sb, v.AiPoints);
  102. FindTags(sb, v.DiPoints);
  103. FindHistoryTags(v);
  104. _TagInfos.AddRange(v.AiPoints);
  105. _TagInfos.AddRange(v.DiPoints);
  106. _TagInfos.Add(v.Status);
  107. sb.Append(v.Status.Tag).Append(',');
  108. }
  109. _Tags = sb.ToString();
  110. }
  111. /// <summary>
  112. /// 枚举需要读取历史的标签点
  113. /// </summary>
  114. private void FindHistoryTags(AGCInfo ai)
  115. {
  116. _HistoryTagInfos.Add(ai.Status);
  117. var vs = ai.AiPoints.Where(p => (p.Type == TagType.PowerSet || p.Type == TagType.ActualPower || p.Type == TagType.IdeaPower) && !string.IsNullOrWhiteSpace(p.Tag));
  118. foreach (var v in vs)
  119. {
  120. _HistoryTagInfos.Add(v);
  121. }
  122. }
  123. /// <summary>
  124. /// 枚举标签
  125. /// </summary>
  126. /// <param name="sb"></param>
  127. /// <param name="points"></param>
  128. private void FindTags(StringBuilder sb, List<TagInfo> points)
  129. {
  130. if (points == null) return;
  131. foreach (var v in points)
  132. {
  133. if (v == null) continue;
  134. foreach (var vv in v.Tags)
  135. {
  136. if (string.IsNullOrWhiteSpace(vv)) continue;
  137. sb.Append(vv).Append(',');
  138. }
  139. }
  140. }
  141. /// <summary>
  142. /// 数据刷新
  143. /// </summary>
  144. public void RefreshData(System.Windows.FrameworkElement fe)
  145. {
  146. try
  147. {
  148. if (_DataUrl == null)
  149. {
  150. _DataUrl = $"{_Url}/ts/latest?keys={_Tags}";
  151. }
  152. var vs = _WEBHelper.HttpGetJSON<Dictionary<string, TsData>>(_DataUrl);
  153. fe.Dispatcher.Invoke(() =>
  154. {
  155. try
  156. {
  157. foreach (var v in _TagInfos)
  158. {
  159. v.UpdateData(vs);
  160. }
  161. }
  162. catch (Exception ex)
  163. {
  164. log.Error("曲线上面里" + ex.Message);
  165. }
  166. });
  167. }
  168. catch (Exception ex)
  169. {
  170. log.Error("曲线上面" + ex.Message+ _DataUrl);
  171. }
  172. }
  173. /// <summary>
  174. /// 历史数据刷新
  175. /// </summary>
  176. public void RefreshLineData(System.Windows.FrameworkElement fe)
  177. {
  178. try
  179. {
  180. if (_HistoyUrl == null)
  181. {
  182. _HistoyUrl = $"{_Url}/ts/history/snap?interval=60";
  183. }
  184. var dte = DateTime.Now.GetTimeSpan();
  185. var dts = DateTime.Now.AddHours(-8).GetTimeSpan();
  186. foreach (var v in _HistoryTagInfos)
  187. {
  188. if (v.Tag == null) continue;
  189. var vs = _WEBHelper.HttpGetJSON<List<TsData>>($"{_HistoyUrl}&tagName={v.Tag}&startTs={dts}&endTs={dte}");
  190. fe.Dispatcher.Invoke(() => {
  191. try {
  192. v.UpdateHistoryData(vs);
  193. }
  194. catch(Exception ex)
  195. {
  196. log.Error("曲线里" + ex.Message);
  197. }
  198. });
  199. }
  200. }
  201. catch (Exception ex)
  202. {
  203. log.Error("曲线" + ex.Message);
  204. }
  205. }
  206. }
  207. }