AgcDeviateService.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package com.gyee.runeconomy.service.agc;
  2. import cn.hutool.core.lang.TypeReference;
  3. import com.gyee.runeconomy.config.GyeeConfig;
  4. import com.gyee.runeconomy.controller.agc.AgcDeviateTag;
  5. import com.gyee.runeconomy.controller.agc.AiPoints;
  6. import com.gyee.runeconomy.controller.agc.FileService;
  7. import com.gyee.runeconomy.init.CacheContext;
  8. import com.gyee.runeconomy.util.realtimesource.feign.IDataAdapter;
  9. import com.gyee.runeconomy.util.realtimesource.feign.RemoteServiceBuilder;
  10. import com.gyee.runeconomy.util.realtimesource.feign.TsDoubleTsData;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.boot.system.ApplicationHome;
  13. import org.springframework.core.annotation.Order;
  14. import org.springframework.stereotype.Component;
  15. import javax.annotation.Resource;
  16. import java.io.File;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.List;
  20. import java.util.Optional;
  21. import java.util.stream.Collectors;
  22. /**
  23. * 读取数据库配置文件
  24. */
  25. @Order(0)
  26. @Component
  27. public class AgcDeviateService {
  28. /**
  29. * 是否是离线版本
  30. */
  31. private boolean isOffline;
  32. /**
  33. * 离线数据保存路径
  34. */
  35. private String filePathPower = "data\\power\\";
  36. public File jarF = null;
  37. {
  38. ApplicationHome h = new ApplicationHome(getClass());
  39. jarF = h.getSource();
  40. }
  41. /**
  42. * 文件读写
  43. */
  44. private FileService fileService;
  45. @Autowired
  46. private GyeeConfig config;
  47. @Resource
  48. private IDataAdapter iDataAdapter;
  49. @Autowired
  50. private RemoteServiceBuilder remoteService;
  51. /**
  52. * 获取agc曲线偏差分析需要的数据
  53. *
  54. * @param id 场站ID
  55. * @param startTs 开始时间戳
  56. * @param endTs 结束时间戳
  57. * @param interval 数据时间间隔
  58. * @return 分析数据
  59. */
  60. public List<AgcDeviateTag> getAgcDeviateTags(String id, long startTs, long endTs, int interval) {
  61. List<AgcDeviateTag> ladt = new ArrayList<>();
  62. List<AiPoints> laps = getAiPoints(id);
  63. if (laps == null) {
  64. return ladt;
  65. }
  66. for (AiPoints ap : laps) {
  67. AgcDeviateTag adt = new AgcDeviateTag();
  68. adt.setName(ap.getName());
  69. List<TsDoubleTsData> lpd = getPointData(ap, startTs, endTs, interval);
  70. adt.setValues(lpd);
  71. ladt.add(adt);
  72. }
  73. // 上限下限
  74. List<AgcDeviateTag> upperLowerLimits = getUpperLowerLimits(ladt, id);
  75. ladt.addAll(upperLowerLimits);
  76. return ladt;
  77. }
  78. private List<TsDoubleTsData> getPointData(AiPoints ap, long startTs, long endTs, int interval) {
  79. List<TsDoubleTsData> lpds = null;
  80. if (ap.getTag().contains(",")) {
  81. lpds = getMultiple(ap, startTs, endTs, interval);
  82. } else {
  83. if (ap.getTag().startsWith("NEM")) {
  84. lpds = remoteService.taos().getHistorytsSnap(ap.getTag(), startTs, endTs, interval);
  85. } else {
  86. lpds = remoteService.adapterfd().getHistorytsSnap(ap.getTag(), startTs, endTs, interval);
  87. }
  88. }
  89. if (ap.getMultiplier() != 1) {
  90. for (TsDoubleTsData pd : lpds) {
  91. pd.setDoubleValue(pd.getDoubleValue() * ap.getMultiplier());
  92. }
  93. }
  94. return lpds;
  95. }
  96. /**
  97. * 获取多个标签点数值
  98. */
  99. private List<TsDoubleTsData> getMultiple(AiPoints ap, long startTs, long endTs, int interval) {
  100. String[] tags = ap.getTag().split(",");
  101. boolean isFirst = true;
  102. List<TsDoubleTsData> lpd = new ArrayList<>();
  103. for (String tag : tags) {
  104. if (tag.equals("")) {
  105. continue;
  106. }
  107. List<TsDoubleTsData> vals = iDataAdapter.getHistorySnap(tag, startTs, endTs, interval);
  108. for (int i = 0; i < vals.size(); ++i) {
  109. if (isFirst) {
  110. lpd.addAll(vals);
  111. isFirst = false;
  112. break;
  113. } else {
  114. TsDoubleTsData pd = lpd.get(i);
  115. pd.setDoubleValue(vals.get(i).getDoubleValue() + pd.getDoubleValue());
  116. }
  117. }
  118. }
  119. return lpd;
  120. }
  121. /**
  122. * 获取上限下限
  123. */
  124. private List<AgcDeviateTag> getUpperLowerLimits(List<AgcDeviateTag> ladt, String id) {
  125. // 装机容量
  126. double capacity = getCapacity(id);
  127. // 偏差
  128. double deviation = capacity * 0.03;
  129. // 有功设定
  130. Optional<AgcDeviateTag> agcLimit = ladt.stream().filter(ad -> ad.getName().equals("有功设定限值")).findFirst();
  131. List<AgcDeviateTag> la = new ArrayList<>();
  132. if (!agcLimit.isPresent()) {
  133. return la;
  134. }
  135. AgcDeviateTag adtUper = new AgcDeviateTag();
  136. adtUper.setName("偏差上限");
  137. adtUper.setValues(new ArrayList<>());
  138. AgcDeviateTag adtLimt = new AgcDeviateTag();
  139. adtLimt.setName("偏差下限");
  140. adtLimt.setValues(new ArrayList<>());
  141. for (TsDoubleTsData pd : agcLimit.get().getValues()) {
  142. long ts = pd.getTs();
  143. TsDoubleTsData pdUper = new TsDoubleTsData();
  144. pdUper.setTs(ts);
  145. pdUper.setDoubleValue(pd.getDoubleValue() * 1.03);
  146. TsDoubleTsData pdLimt = new TsDoubleTsData();
  147. pdLimt.setTs(ts);
  148. pdLimt.setDoubleValue(pd.getDoubleValue() * 0.97);
  149. adtUper.getValues().add(pdUper);
  150. adtLimt.getValues().add(pdLimt);
  151. }
  152. la.add(adtUper);
  153. la.add(adtLimt);
  154. return la;
  155. }
  156. /**
  157. * 根据ID获取agc配置信息
  158. *
  159. * @param id 场站ID
  160. * @return 配置信息
  161. */
  162. private List<AiPoints> getAiPoints(String id) {
  163. if (!CacheContext.agcDeviateConfigMap.containsKey(id)) {
  164. return null;
  165. }
  166. AiPoints[] aiPoints = CacheContext.agcDeviateConfigMap.get(id).getAiPoints();
  167. return Arrays.stream(aiPoints).filter(ap -> ap.getName().contains("功") && !ap.getName().contains("预测")).collect(Collectors.toList());
  168. }
  169. private double getCapacity(String id) {
  170. if (!CacheContext.agcDeviateConfigMap.containsKey(id)) {
  171. return 0;
  172. }
  173. return CacheContext.agcDeviateConfigMap.get(id).getInstalledCapacity();
  174. }
  175. /**
  176. * 获取配置
  177. *
  178. * @return
  179. */
  180. public Object getConfig() {
  181. return CacheContext.agcDeviateConfigMap;
  182. }
  183. public List<AgcDeviateTag> getAgcDeviateTagsOffline(String id, long startTs, long endTs, int interval) {
  184. if (!CacheContext.files.containsKey(id)) {
  185. return new ArrayList<>();
  186. }
  187. List<String> ls = CacheContext.files.get(id);
  188. if (ls == null) {
  189. return new ArrayList<>();
  190. }
  191. int ran = (int) (Math.random() * ls.size());
  192. return fileService.getFromFile(ls.get(ran), new TypeReference<List<AgcDeviateTag>>() {
  193. });
  194. }
  195. }