VppCapabilityEvalHelper.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package com.usky.vpp.util;
  2. import com.usky.vpp.domain.VppDevice;
  3. import com.usky.vpp.domain.VppResourcePoint;
  4. import com.usky.vpp.service.vo.CapabilityLoadPointVO;
  5. import java.math.BigDecimal;
  6. import java.math.RoundingMode;
  7. import java.time.LocalDate;
  8. import java.time.LocalDateTime;
  9. import java.time.LocalTime;
  10. import java.time.format.DateTimeFormatter;
  11. import java.util.ArrayList;
  12. import java.util.Arrays;
  13. import java.util.Collections;
  14. import java.util.HashSet;
  15. import java.util.List;
  16. import java.util.Map;
  17. import java.util.Set;
  18. import java.util.TreeMap;
  19. /**
  20. * 可调能力评估辅助(TSDB 聚合 + 模拟数据兜底)
  21. */
  22. public final class VppCapabilityEvalHelper {
  23. public static final String CATEGORY_TOTAL = "TOTAL";
  24. public static final String CATEGORY_LOAD = "LOAD";
  25. public static final String CATEGORY_STORAGE = "STORAGE";
  26. public static final String CATEGORY_GENERATION = "GENERATION";
  27. private static final Set<String> LOAD_TYPES = new HashSet<>(Arrays.asList("IND_LOAD", "COM_BLDG", "EVCS"));
  28. private static final Set<String> STORAGE_TYPES = Collections.singleton("ESS");
  29. private static final Set<String> GENERATION_TYPES = Collections.singleton("PV");
  30. private static final DateTimeFormatter TIME_FMT = DateTimeFormatter.ofPattern("HH:mm");
  31. private VppCapabilityEvalHelper() {
  32. }
  33. public static String categoryLabel(String category) {
  34. if (category == null) {
  35. return "未知";
  36. }
  37. switch (category) {
  38. case CATEGORY_TOTAL:
  39. return "总资源";
  40. case CATEGORY_LOAD:
  41. return "负荷类";
  42. case CATEGORY_STORAGE:
  43. return "储能类";
  44. case CATEGORY_GENERATION:
  45. return "发电类";
  46. default:
  47. return category;
  48. }
  49. }
  50. public static String resolveCategory(String resourceType) {
  51. if (resourceType == null) {
  52. return CATEGORY_LOAD;
  53. }
  54. String type = resourceType.toUpperCase();
  55. if (GENERATION_TYPES.contains(type)) {
  56. return CATEGORY_GENERATION;
  57. }
  58. if (STORAGE_TYPES.contains(type)) {
  59. return CATEGORY_STORAGE;
  60. }
  61. if (LOAD_TYPES.contains(type)) {
  62. return CATEGORY_LOAD;
  63. }
  64. return CATEGORY_LOAD;
  65. }
  66. public static String eventTypeLabel(Integer eventType) {
  67. if (eventType == null) {
  68. return "未知";
  69. }
  70. return eventType == 1 ? "削峰" : eventType == 2 ? "填谷" : "未知";
  71. }
  72. public static String responseTypeLabel(Integer responseType) {
  73. if (responseType == null) {
  74. return "未知";
  75. }
  76. switch (responseType) {
  77. case 1:
  78. return "日前";
  79. case 2:
  80. return "日内";
  81. case 3:
  82. return "秒级";
  83. default:
  84. return "未知";
  85. }
  86. }
  87. /**
  88. * 基于 TDengine 历史数据构建负荷曲线(每 10 分钟一个点)
  89. * <p>实际负荷取统计日实测;基线负荷取 7 日前同时段作为参考基线。</p>
  90. */
  91. public static List<CapabilityLoadPointVO> buildLoadCurveFromTsdb(
  92. Map<String, Map<String, TreeMap<LocalDateTime, BigDecimal>>> actualData,
  93. Map<String, Map<String, TreeMap<LocalDateTime, BigDecimal>>> baselineData,
  94. LocalDate date,
  95. List<String> powerMetrics) {
  96. List<CapabilityLoadPointVO> points = new ArrayList<>();
  97. for (int minute = 0; minute < 1440; minute += 10) {
  98. LocalTime time = LocalTime.of(minute / 60, minute % 60);
  99. LocalDateTime actualStart = date.atTime(time);
  100. LocalDateTime actualEnd = actualStart.plusMinutes(10).minusSeconds(1);
  101. LocalDateTime baselineStart = date.minusDays(7).atTime(time);
  102. LocalDateTime baselineEnd = baselineStart.plusMinutes(10).minusSeconds(1);
  103. BigDecimal actual = sumBucketPower(actualData, powerMetrics, actualStart, actualEnd);
  104. BigDecimal baseline = sumBucketPower(baselineData, powerMetrics, baselineStart, baselineEnd);
  105. if (baseline == null || baseline.compareTo(BigDecimal.ZERO) <= 0) {
  106. baseline = actual;
  107. }
  108. CapabilityLoadPointVO point = new CapabilityLoadPointVO();
  109. point.setTime(time.format(TIME_FMT));
  110. point.setBaselineLoadKw(scale(baseline));
  111. point.setActualLoadKw(scale(actual));
  112. points.add(point);
  113. }
  114. return points;
  115. }
  116. /**
  117. * 汇总设备最新上下调能力(TDengine 实时指标)
  118. */
  119. public static BigDecimal sumLatestMetric(Map<String, Map<String, BigDecimal>> latestData,
  120. List<String> metricCandidates) {
  121. if (latestData == null || latestData.isEmpty()) {
  122. return BigDecimal.ZERO;
  123. }
  124. BigDecimal total = BigDecimal.ZERO;
  125. for (Map<String, BigDecimal> metrics : latestData.values()) {
  126. BigDecimal value = pickMetric(metrics, metricCandidates);
  127. if (value != null) {
  128. total = total.add(value);
  129. }
  130. }
  131. return total.setScale(2, RoundingMode.HALF_UP);
  132. }
  133. public static boolean hasTsdbPowerData(Map<String, Map<String, TreeMap<LocalDateTime, BigDecimal>>> deviceMetricData,
  134. List<String> powerMetrics) {
  135. if (deviceMetricData == null || deviceMetricData.isEmpty()) {
  136. return false;
  137. }
  138. for (Map<String, TreeMap<LocalDateTime, BigDecimal>> metricMap : deviceMetricData.values()) {
  139. for (String metric : powerMetrics) {
  140. TreeMap<LocalDateTime, BigDecimal> series = metricMap.get(metric);
  141. if (series != null && !series.isEmpty()) {
  142. return true;
  143. }
  144. }
  145. }
  146. return false;
  147. }
  148. /**
  149. * 生成当日负荷曲线(每 10 分钟一个点,00:00 ~ 23:50)- 模拟兜底
  150. */
  151. public static List<CapabilityLoadPointVO> buildLoadCurve(List<VppResourcePoint> resources,
  152. List<VppDevice> devices,
  153. Long siteId,
  154. LocalDate date) {
  155. List<CapabilityLoadPointVO> points = new ArrayList<>();
  156. long seed = (siteId != null ? siteId : 0L) + date.toEpochDay();
  157. for (int minute = 0; minute < 1440; minute += 10) {
  158. LocalTime time = LocalTime.of(minute / 60, minute % 60);
  159. double hourFactor = timeFactor(time);
  160. BigDecimal baseline = aggregateLoad(resources, devices, seed, minute, hourFactor, 0);
  161. BigDecimal actual = aggregateLoad(resources, devices, seed, minute, hourFactor, 1);
  162. CapabilityLoadPointVO point = new CapabilityLoadPointVO();
  163. point.setTime(time.format(TIME_FMT));
  164. point.setBaselineLoadKw(baseline);
  165. point.setActualLoadKw(actual);
  166. points.add(point);
  167. }
  168. return points;
  169. }
  170. private static BigDecimal aggregateLoad(List<VppResourcePoint> resources,
  171. List<VppDevice> devices,
  172. long seed,
  173. int minuteOfDay,
  174. double hourFactor,
  175. int seriesSalt) {
  176. if (resources == null || resources.isEmpty()) {
  177. double base = 800 * hourFactor + pseudoRandom(seed, minuteOfDay, seriesSalt) * 120;
  178. return BigDecimal.valueOf(base).setScale(2, RoundingMode.HALF_UP);
  179. }
  180. BigDecimal total = BigDecimal.ZERO;
  181. for (VppResourcePoint resource : resources) {
  182. VppDevice device = findDevice(devices, resource.getDeviceId());
  183. if (device != null && device.getCommStatus() != null && device.getCommStatus() == 0) {
  184. continue;
  185. }
  186. BigDecimal cap = resource.getCapacityKw() != null ? resource.getCapacityKw() : BigDecimal.valueOf(100);
  187. double typeFactor = typeLoadFactor(resource.getResourceType());
  188. double noise = 0.85 + pseudoRandom(seed, resource.getId() != null ? resource.getId() : 0L, minuteOfDay, seriesSalt) * 0.3;
  189. double value = cap.doubleValue() * hourFactor * typeFactor * noise;
  190. if (CATEGORY_GENERATION.equals(resolveCategory(resource.getResourceType()))) {
  191. value *= 0.15;
  192. }
  193. total = total.add(BigDecimal.valueOf(value));
  194. }
  195. if (seriesSalt == 1) {
  196. total = total.multiply(BigDecimal.valueOf(1.02 + pseudoRandom(seed, 999, minuteOfDay, seriesSalt) * 0.08));
  197. }
  198. return total.setScale(2, RoundingMode.HALF_UP);
  199. }
  200. private static BigDecimal sumBucketPower(Map<String, Map<String, TreeMap<LocalDateTime, BigDecimal>>> deviceData,
  201. List<String> metricCandidates,
  202. LocalDateTime bucketStart,
  203. LocalDateTime bucketEnd) {
  204. if (deviceData == null || deviceData.isEmpty()) {
  205. return BigDecimal.ZERO;
  206. }
  207. BigDecimal total = BigDecimal.ZERO;
  208. for (Map<String, TreeMap<LocalDateTime, BigDecimal>> metricMap : deviceData.values()) {
  209. TreeMap<LocalDateTime, BigDecimal> series = pickSeries(metricMap, metricCandidates);
  210. if (series == null || series.isEmpty()) {
  211. continue;
  212. }
  213. BigDecimal avg = averageInRange(series, bucketStart, bucketEnd);
  214. if (avg != null) {
  215. total = total.add(avg);
  216. }
  217. }
  218. return total;
  219. }
  220. private static TreeMap<LocalDateTime, BigDecimal> pickSeries(Map<String, TreeMap<LocalDateTime, BigDecimal>> metricMap,
  221. List<String> metricCandidates) {
  222. if (metricMap == null || metricMap.isEmpty()) {
  223. return null;
  224. }
  225. for (String metric : metricCandidates) {
  226. TreeMap<LocalDateTime, BigDecimal> series = metricMap.get(metric);
  227. if (series != null && !series.isEmpty()) {
  228. return series;
  229. }
  230. }
  231. for (Map.Entry<String, TreeMap<LocalDateTime, BigDecimal>> entry : metricMap.entrySet()) {
  232. if (entry.getValue() != null && !entry.getValue().isEmpty()) {
  233. return entry.getValue();
  234. }
  235. }
  236. return null;
  237. }
  238. private static BigDecimal pickMetric(Map<String, BigDecimal> metrics, List<String> metricCandidates) {
  239. if (metrics == null || metrics.isEmpty()) {
  240. return null;
  241. }
  242. for (String metric : metricCandidates) {
  243. BigDecimal value = metrics.get(metric);
  244. if (value != null) {
  245. return value;
  246. }
  247. }
  248. return null;
  249. }
  250. private static BigDecimal averageInRange(TreeMap<LocalDateTime, BigDecimal> series,
  251. LocalDateTime startInclusive,
  252. LocalDateTime endInclusive) {
  253. BigDecimal sum = BigDecimal.ZERO;
  254. int count = 0;
  255. for (Map.Entry<LocalDateTime, BigDecimal> entry : series.subMap(startInclusive, true, endInclusive, true).entrySet()) {
  256. if (entry.getValue() != null) {
  257. sum = sum.add(entry.getValue());
  258. count++;
  259. }
  260. }
  261. if (count == 0) {
  262. Map.Entry<LocalDateTime, BigDecimal> floor = series.floorEntry(startInclusive);
  263. if (floor != null && floor.getValue() != null) {
  264. return floor.getValue();
  265. }
  266. return null;
  267. }
  268. return sum.divide(BigDecimal.valueOf(count), 4, RoundingMode.HALF_UP);
  269. }
  270. private static BigDecimal scale(BigDecimal value) {
  271. if (value == null) {
  272. return BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
  273. }
  274. return value.setScale(2, RoundingMode.HALF_UP);
  275. }
  276. private static VppDevice findDevice(List<VppDevice> devices, Long deviceId) {
  277. if (devices == null || deviceId == null) {
  278. return null;
  279. }
  280. return devices.stream().filter(d -> deviceId.equals(d.getId())).findFirst().orElse(null);
  281. }
  282. /**
  283. * 日内负荷形态:夜间低、早晚峰、午间次峰
  284. */
  285. private static double timeFactor(LocalTime time) {
  286. int hour = time.getHour();
  287. int minute = time.getMinute();
  288. double h = hour + minute / 60.0;
  289. if (h < 6) {
  290. return 0.45 + h / 6 * 0.15;
  291. }
  292. if (h < 9) {
  293. return 0.6 + (h - 6) / 3 * 0.35;
  294. }
  295. if (h < 12) {
  296. return 0.95 + (h - 9) / 3 * 0.05;
  297. }
  298. if (h < 14) {
  299. return 0.85;
  300. }
  301. if (h < 17) {
  302. return 0.9 + (h - 14) / 3 * 0.1;
  303. }
  304. if (h < 21) {
  305. return 1.0 - (h - 17) / 4 * 0.35;
  306. }
  307. return 0.5 - (h - 21) / 3 * 0.15;
  308. }
  309. private static double typeLoadFactor(String resourceType) {
  310. if (resourceType == null) {
  311. return 0.7;
  312. }
  313. switch (resourceType.toUpperCase()) {
  314. case "IND_LOAD":
  315. return 0.75;
  316. case "COM_BLDG":
  317. return 0.65;
  318. case "EVCS":
  319. return 0.55;
  320. case "ESS":
  321. return 0.4;
  322. case "PV":
  323. return 1.0;
  324. default:
  325. return 0.7;
  326. }
  327. }
  328. private static double pseudoRandom(long seed, long a, int b, int c) {
  329. long mixed = seed * 31L + a * 17L + b * 13L + c * 7L;
  330. return (mixed % 1000) / 1000.0;
  331. }
  332. private static double pseudoRandom(long seed, int minuteOfDay, int seriesSalt) {
  333. long mixed = seed * 31L + minuteOfDay * 17L + seriesSalt * 13L;
  334. return (mixed % 1000) / 1000.0;
  335. }
  336. }