AgcDeviateController.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package com.gyee.runeconomy.controller.agc;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUtil;
  4. import com.alibaba.fastjson.JSON;
  5. import com.alibaba.fastjson.TypeReference;
  6. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  7. import com.gyee.common.model.PointData;
  8. import com.gyee.runeconomy.config.GyeeConfig;
  9. import com.gyee.runeconomy.init.CacheContext;
  10. import com.gyee.runeconomy.model.StatusTime;
  11. import com.gyee.runeconomy.model.auto.PointInfo;
  12. import com.gyee.runeconomy.model.auto.ProBasicEquipment;
  13. import com.gyee.runeconomy.model.auto.ProBasicEquipmentPoint;
  14. import com.gyee.runeconomy.service.agc.AgcDeviateService;
  15. import com.gyee.runeconomy.service.auto.IPointInfoService;
  16. import com.gyee.runeconomy.util.realtimesource.IEdosUtil;
  17. import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
  18. import org.springframework.data.redis.core.StringRedisTemplate;
  19. import org.springframework.web.bind.annotation.GetMapping;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.RequestParam;
  22. import org.springframework.web.bind.annotation.RestController;
  23. import javax.annotation.Resource;
  24. import java.util.*;
  25. import java.util.concurrent.ConcurrentHashMap;
  26. import java.util.stream.Collectors;
  27. /**
  28. * 获取各个场站AGC偏差分析数据
  29. */
  30. @RestController
  31. @RequestMapping("/agc")
  32. public class AgcDeviateController {
  33. public static Map<String, ConcurrentHashMap<Double, Double>> fitcoef = new ConcurrentHashMap<>();
  34. @Resource
  35. private AgcDeviateService agcDeviateService;
  36. @Resource
  37. private GyeeConfig gyeeConfig;
  38. @Resource
  39. private StringRedisTemplate stringRedisTemplate;
  40. @Resource
  41. private IEdosUtil edosUtil;
  42. @Resource
  43. private IPointInfoService pointInfoService;
  44. /**
  45. * 获取偏差信息
  46. *
  47. * @param startTs 开始时间
  48. * @param endTs 结束时间
  49. * @param id 场站id
  50. * @param interval 时间间隔
  51. * @return
  52. */
  53. @GetMapping("/deviate")
  54. public Map<String, AgcDeviateTag> getData(@RequestParam(value = "startTs") long startTs,
  55. @RequestParam(value = "endTs") long endTs,
  56. @RequestParam(value = "id") String id,
  57. @RequestParam(value = "interval", defaultValue = "60", required = false) int interval) {
  58. List<AgcDeviateTag> ls = new ArrayList<>();
  59. if (gyeeConfig.isOffLine()) {
  60. ls = agcDeviateService.getAgcDeviateTags(id, startTs, endTs, interval);
  61. } else {
  62. ls = agcDeviateService.getAgcDeviateTags(id, startTs, endTs, interval);
  63. }
  64. return ls.stream().collect(Collectors.toMap(AgcDeviateTag::getName, f -> f));
  65. }
  66. /**
  67. * 获取配置
  68. *
  69. * @return
  70. */
  71. @GetMapping("/config")
  72. public Object getAgcConifg() {
  73. return agcDeviateService.getConfig();
  74. }
  75. @GetMapping("/windturbine/curve")
  76. public Map<String, List<SpeedPowerAnalysis>> getWindturbineData(@RequestParam(value = "startTs") long startTs,
  77. @RequestParam(value = "endTs") long endTs,
  78. @RequestParam(value = "windturbineId") String windturbineId,
  79. @RequestParam(value = "interval", defaultValue = "60", required = false) int interval) throws Exception {
  80. //切换数据库
  81. LettuceConnectionFactory factory = (LettuceConnectionFactory) stringRedisTemplate.getConnectionFactory();
  82. int database = factory.getDatabase();
  83. factory.setDatabase(5);
  84. stringRedisTemplate.setConnectionFactory(factory);
  85. factory.afterPropertiesSet();
  86. factory.resetConnection();
  87. DateTime date = DateUtil.date(endTs * 1000);
  88. Map<String, List<SpeedPowerAnalysis>> map = new HashMap<>();
  89. List<SpeedPowerAnalysis> ls = new ArrayList<>();
  90. String[] wtIds = windturbineId.split(",");
  91. List<String> wtIdsLs = Arrays.asList(wtIds);
  92. for (String wtId : wtIdsLs) {
  93. SpeedPowerAnalysis spa = new SpeedPowerAnalysis();
  94. List<ProBasicEquipmentPoint> proBasicEquipmentPoints = CacheContext.pointNewMap.get(wtId);
  95. List<ProBasicEquipmentPoint> speedPoint = proBasicEquipmentPoints.stream().filter(p -> p.getUniformCode().equals("AI066")).collect(Collectors.toList());
  96. List<PointData> historyspeedDatas = edosUtil.getHistoryDatasSnap(speedPoint.get(0).getNemCode(), startTs, endTs, interval);
  97. List<Double> speedDatas = historyspeedDatas.stream().map(PointData::getPointValueInDouble).collect(Collectors.toList());
  98. spa.setWindSpeed(speedDatas);
  99. List<ProBasicEquipmentPoint> powerPoint = proBasicEquipmentPoints.stream().filter(p -> p.getUniformCode().equals("AI114")).collect(Collectors.toList());
  100. List<PointData> historyPowerDatas = edosUtil.getHistoryDatasSnap(powerPoint.get(0).getNemCode(), startTs, endTs, interval);
  101. List<Double> powerData = historyPowerDatas.stream().map(PointData::getPointValueInDouble).collect(Collectors.toList());
  102. spa.setActuatedPower(powerData);
  103. String powerCurveMonth = "glqxnh:" + date.month() + ":";
  104. Set<String> keys = stringRedisTemplate.keys(powerCurveMonth + wtId);
  105. Iterator<String> iterator = keys.iterator(); // 获取迭代器
  106. String firstKey = iterator.hasNext() ? iterator.next() : null;
  107. String yc = stringRedisTemplate.opsForValue().get(firstKey);
  108. ConcurrentHashMap<Double, Double> wtyc = JSON.parseObject(yc, new TypeReference<ConcurrentHashMap<Double, Double>>() {
  109. }.getType());
  110. fitcoef.put(firstKey.replaceFirst(powerCurveMonth, ""), wtyc);
  111. List<Double> llgl = speedDatas.stream().map(a -> fitcoef.get(wtId).get(a)).collect(Collectors.toList());
  112. spa.setTheoreticalPower(llgl);
  113. ls.add(spa);
  114. map.put(wtId, ls);
  115. }
  116. //关闭数据库切换
  117. factory.setDatabase(database);
  118. factory.afterPropertiesSet();
  119. factory.resetConnection();
  120. return map;
  121. }
  122. @GetMapping("/windtur")
  123. public Map<String, List<StatusTime>> getTestData(@RequestParam(value = "startTs") long startTs,
  124. @RequestParam(value = "endTs") long endTs,
  125. @RequestParam(value = "uniformcode") String uniformcode) throws Exception {
  126. Map<String, List<StatusTime>> map = new HashMap<>();
  127. QueryWrapper<PointInfo> qw = new QueryWrapper<>();
  128. qw.lambda().eq(PointInfo::getUniformCode, uniformcode);
  129. List<PointInfo> list = pointInfoService.list(qw);
  130. for (PointInfo ls : list) {
  131. List<StatusTime> ll = new ArrayList<>();
  132. List<PointData> historyDatasRaw = edosUtil.getHistoryDatasRaw(ls.getPointKey(), startTs, endTs);
  133. for (PointData pointData : historyDatasRaw) {
  134. StatusTime st = new StatusTime();
  135. st.setName(ls.getName());
  136. Date da=new Date(pointData.getPointTime());
  137. st.setTime(DateUtil.formatDateTime(da));
  138. String getstatus = getstatus((int) pointData.getPointValueInDouble());
  139. st.setStatus(getstatus);
  140. ll.add(st);
  141. }
  142. List<String> collect = CacheContext.wtls.stream().filter(w -> w.getId().equals(ls.getTurbineId())).map(ProBasicEquipment::getNemCode).collect(Collectors.toList());
  143. map.put(collect.get(0), ll);
  144. }
  145. List<String> sortedKeys = new ArrayList<>(map.keySet());
  146. Collections.sort(sortedKeys);
  147. Map<String, List<StatusTime>> sortedMap = new LinkedHashMap<>();
  148. for (String key : sortedKeys) {
  149. sortedMap.put(key, map.get(key));
  150. }
  151. return sortedMap;
  152. }
  153. private String getstatus(int number) {
  154. String statusText = "";
  155. switch (number) {
  156. case 0:
  157. statusText = "待机";
  158. break;
  159. case 1:
  160. statusText = "停机";
  161. break;
  162. case 2:
  163. statusText = "并网";
  164. break;
  165. case 4:
  166. statusText = "故障";
  167. break;
  168. case 6:
  169. statusText = "检修";
  170. break;
  171. case 8:
  172. statusText = "限电";
  173. break;
  174. case 12:
  175. statusText = "离线";
  176. break;
  177. default:
  178. statusText = "未知";
  179. break;
  180. }
  181. return statusText;
  182. }
  183. }