VppUnMessageBuilder.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. package com.usky.vpp.util;
  2. import com.usky.vpp.config.VppUnProperties;
  3. import com.usky.vpp.domain.VppResourcePoint;
  4. import com.usky.vpp.domain.VppSite;
  5. import org.springframework.util.StringUtils;
  6. import java.math.BigDecimal;
  7. import java.time.LocalDateTime;
  8. import java.time.format.DateTimeFormatter;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.LinkedHashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.UUID;
  15. /**
  16. * 构造运管平台标准响应报文(联调 JSON 实例)
  17. */
  18. public final class VppUnMessageBuilder {
  19. private static final DateTimeFormatter CREATED_DATE_TIME =
  20. DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  21. private static final DateTimeFormatter MOMENT_DATE_TIME = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
  22. private VppUnMessageBuilder() {
  23. }
  24. public static Map<String, Object> buildTokenRequest(VppUnProperties properties) {
  25. return buildBaseRequest("TokenRequest", properties);
  26. }
  27. public static Map<String, Object> buildPollRequest(VppUnProperties properties) {
  28. return buildBaseRequest("Poll", properties);
  29. }
  30. public static Map<String, Object> buildCreateRegistrationRequest(VppUnProperties properties) {
  31. Map<String, Object> req = buildBaseRequest("CreateRegistrationRequest", properties);
  32. req.put("dnName", properties.getDnName());
  33. if (StringUtils.hasText(properties.getRegistrationId())) {
  34. req.put("registrationID", properties.getRegistrationId());
  35. }
  36. req.put("reportOnly", false);
  37. req.put("pullMode", true);
  38. req.put("signature", false);
  39. req.put("transport", "REST");
  40. req.put("transportAddress", properties.getTransportAddress());
  41. return req;
  42. }
  43. public static Map<String, Object> buildCreateOptRequest(String eventId, String optType,
  44. List<Map<String, Object>> list,
  45. VppUnProperties properties) {
  46. Map<String, Object> req = buildBaseRequest("CreateOptRequest", properties);
  47. req.put("eventID", eventId);
  48. req.put("optType", optType);
  49. req.put("createdDateTime", CREATED_DATE_TIME.format(LocalDateTime.now()));
  50. req.put("priceDownCoeff", properties.getPriceDownCoeff());
  51. if (list != null) {
  52. req.put("list", list);
  53. }
  54. return req;
  55. }
  56. private static final int CREATE_CQ_REQUEST_CODE = 6206;
  57. public static Map<String, Object> buildCreateCqRequest(String eventId, String optType,
  58. List<Map<String, Object>> list,
  59. VppUnProperties properties) {
  60. Map<String, Object> req = new LinkedHashMap<>();
  61. req.put("optType", optType != null ? optType : "optIn");
  62. req.put("eventID", eventId);
  63. req.put("code", CREATE_CQ_REQUEST_CODE);
  64. req.put("requestID", UUID.randomUUID().toString());
  65. req.put("root", "CreateCqRequest");
  66. req.put("createdDateTime", CREATED_DATE_TIME.format(LocalDateTime.now()));
  67. req.put("priceDownCoeff", properties.getPriceDownCoeff());
  68. req.put("description", "");
  69. req.put("dnID", properties.getDnId());
  70. req.put("list", list != null ? list : new ArrayList<>());
  71. req.put("version", 1);
  72. return req;
  73. }
  74. public static Map<String, Object> buildCreateReportResponse(Map<String, Object> request, VppUnProperties properties) {
  75. Map<String, Object> resp = new LinkedHashMap<>();
  76. if (request != null) {
  77. resp.put("requestID", request.get("requestID"));
  78. }
  79. resp.put("root", "CreateReportResponse");
  80. resp.put("version", 1);
  81. resp.put("code", 200);
  82. resp.put("description", "ok");
  83. if (resp.get("requestID") == null) {
  84. resp.put("requestID", UUID.randomUUID().toString());
  85. }
  86. resp.put("dnID", properties.getDnId());
  87. Map<String, Object> pendingReports = new LinkedHashMap<>();
  88. List<String> reportRequestIds = new ArrayList<>();
  89. reportRequestIds.add("MetaDataReport");
  90. pendingReports.put("reportRequestID", reportRequestIds);
  91. resp.put("pendingReports", pendingReports);
  92. return resp;
  93. }
  94. /**
  95. * 构造 RegisterReportRequest:虚拟电厂量测点(含负荷预测)+ 站点/资源点有功功率(kW)。
  96. */
  97. public static Map<String, Object> buildRegisterReportRequest(VppUnProperties properties,
  98. List<VppSite> sites,
  99. List<VppResourcePoint> resources,
  100. Map<Long, String> deviceUnResourceIdMap) {
  101. Map<String, Object> req = buildBaseRequest("RegisterReportRequest", properties);
  102. req.put("reportRequestID", "MetaDataReport");
  103. List<Map<String, Object>> reports = new ArrayList<>();
  104. String createdDateTime = CREATED_DATE_TIME.format(LocalDateTime.now());
  105. for (VppUnRegisterReportIndex.Entry entry : VppUnRegisterReportIndex.buildRegisterEntries(
  106. properties, sites, resources, deviceUnResourceIdMap)) {
  107. String resourceId = resolveRegisterResourceId(entry, properties, deviceUnResourceIdMap);
  108. reports.add(buildVppMetadataReportItem(entry.getRid(), entry.getMetric(), resourceId, createdDateTime));
  109. }
  110. req.put("report", reports);
  111. return req;
  112. }
  113. private static String resolveRegisterResourceId(VppUnRegisterReportIndex.Entry entry,
  114. VppUnProperties properties,
  115. Map<Long, String> deviceUnResourceIdMap) {
  116. if (entry.getScope() == VppUnRegisterReportIndex.Scope.SITE) {
  117. return entry.getSite().getUnResourceId();
  118. }
  119. if (entry.getScope() == VppUnRegisterReportIndex.Scope.RESOURCE) {
  120. return deviceUnResourceIdMap.get(entry.getResource().getDeviceId());
  121. }
  122. return properties.getDnId();
  123. }
  124. public static Map<String, Object> buildIntervalDataReportRequest(VppUnProperties properties,
  125. Map<String, Object> createReportRequest,
  126. List<Map<String, Object>> pointCurveData,
  127. LocalDateTime reportTime) {
  128. Map<String, Object> req = buildBaseRequest("IntervalDataReportRequest", properties);
  129. req.put("reportRequestID", resolveReportRequestIdFromPoll(createReportRequest, "IntervalDataReport"));
  130. LocalDateTime createdAt = reportTime != null ? reportTime : LocalDateTime.now();
  131. req.put("createdDateTime", MOMENT_DATE_TIME.format(createdAt));
  132. req.put("pointCurveData", pointCurveData != null ? pointCurveData : new ArrayList<>());
  133. return req;
  134. }
  135. public static Map<String, Object> buildIntervalCurveDataEntry(int rid, List<Map<String, Object>> values) {
  136. Map<String, Object> entry = new LinkedHashMap<>();
  137. entry.put("rID", rid);
  138. Map<String, Object> irregular = new LinkedHashMap<>();
  139. irregular.put("values", values != null ? values : new ArrayList<>());
  140. entry.put("irregular", irregular);
  141. return entry;
  142. }
  143. public static Map<String, Object> buildIntervalCurveValue(BigDecimal value,
  144. LocalDateTime timestamp,
  145. String quality) {
  146. Map<String, Object> point = new LinkedHashMap<>();
  147. point.put("value", value != null ? value : BigDecimal.ZERO);
  148. point.put("timestamp", MOMENT_DATE_TIME.format(timestamp != null ? timestamp : LocalDateTime.now()));
  149. point.put("quality", StringUtils.hasText(quality) ? quality : "good");
  150. return point;
  151. }
  152. public static Map<String, Object> buildMomentDataReportRequest(VppUnProperties properties,
  153. Map<String, Object> createReportRequest,
  154. List<Map<String, Object>> pointData,
  155. LocalDateTime reportTime) {
  156. Map<String, Object> req = buildBaseRequest("MomentDataReportRequest", properties);
  157. req.put("reportRequestID", resolveReportRequestIdFromPoll(createReportRequest, "MomentDataReport"));
  158. LocalDateTime createdAt = reportTime != null ? reportTime : LocalDateTime.now();
  159. req.put("createdDateTime", MOMENT_DATE_TIME.format(createdAt));
  160. req.put("pointData", pointData != null ? pointData : new ArrayList<>());
  161. return req;
  162. }
  163. public static Map<String, Object> buildMomentDataPoint(int rid,
  164. BigDecimal value,
  165. LocalDateTime timestamp,
  166. String quality) {
  167. Map<String, Object> point = new LinkedHashMap<>();
  168. point.put("rID", rid);
  169. point.put("value", value != null ? value : BigDecimal.ZERO);
  170. point.put("timestamp", MOMENT_DATE_TIME.format(timestamp != null ? timestamp : LocalDateTime.now()));
  171. point.put("quality", StringUtils.hasText(quality) ? quality : "good");
  172. return point;
  173. }
  174. public static Map<String, Object> buildLoadForecastReportRequest(Map<String, Object> createReportRequest,
  175. List<VppResourcePoint> resources,
  176. Map<Long, VppSite> siteMap,
  177. VppUnProperties properties) {
  178. Map<String, Object> req = buildBaseRequest("LoadForecastReportRequest", properties);
  179. req.put("reportRequestID", resolveReportRequestIdFromPoll(createReportRequest, "LoadForecastReport"));
  180. req.put("createdDateTime", CREATED_DATE_TIME.format(LocalDateTime.now()));
  181. req.put("pointData", buildPointData(resources, siteMap, properties));
  182. return req;
  183. }
  184. public static Map<String, Object> buildRegulateForecastReportRequest(Map<String, Object> createReportRequest,
  185. List<VppResourcePoint> resources,
  186. Map<Long, VppSite> siteMap,
  187. VppUnProperties properties) {
  188. Map<String, Object> req = buildBaseRequest("RegulateForecastReportRequest", properties);
  189. req.put("reportRequestID", resolveReportRequestIdFromPoll(createReportRequest, "RegulateForecastReport"));
  190. req.put("createdDateTime", CREATED_DATE_TIME.format(LocalDateTime.now()));
  191. req.put("pointData", buildPointData(resources, siteMap, properties));
  192. return req;
  193. }
  194. private static Map<String, Object> buildVppMetadataReportItem(int rid,
  195. VppUnRegisterMetricDefinitions.RegisterMetric metric,
  196. String resourceId,
  197. String createdDateTime) {
  198. Map<String, Object> report = new LinkedHashMap<>();
  199. report.put("createdDateTime", createdDateTime);
  200. Map<String, Object> description = new LinkedHashMap<>();
  201. description.put("rID", rid);
  202. Map<String, Object> metricBody = new LinkedHashMap<>();
  203. metricBody.put("metricName", metric.getMetricName());
  204. if (StringUtils.hasText(metric.getMultiplier())) {
  205. metricBody.put("multiplier", metric.getMultiplier());
  206. }
  207. metricBody.put("symbol", metric.getSymbol());
  208. description.put("metric", metricBody);
  209. Map<String, Object> dataSource = new LinkedHashMap<>();
  210. dataSource.put("resourceID", java.util.Collections.singletonList(resourceId));
  211. description.put("reportDataSource", dataSource);
  212. report.put("reportDescription", description);
  213. return report;
  214. }
  215. private static List<Map<String, Object>> buildPointData(List<VppResourcePoint> resources,
  216. Map<Long, VppSite> siteMap,
  217. VppUnProperties properties) {
  218. List<Map<String, Object>> pointData = new ArrayList<>();
  219. if (resources == null || resources.isEmpty()) {
  220. Map<String, Object> point = new LinkedHashMap<>();
  221. point.put("resourceID", properties.getDnId());
  222. point.put("value", "0");
  223. point.put("timestamp", CREATED_DATE_TIME.format(LocalDateTime.now()));
  224. pointData.add(point);
  225. return pointData;
  226. }
  227. String timestamp = CREATED_DATE_TIME.format(LocalDateTime.now());
  228. for (VppResourcePoint resource : resources) {
  229. Map<String, Object> point = new LinkedHashMap<>();
  230. VppSite site = siteMap != null ? siteMap.get(resource.getSiteId()) : null;
  231. point.put("resourceID", resolveUnResourceId(resource, site, properties));
  232. BigDecimal value = resource.getMaxUpKw() != null ? resource.getMaxUpKw() : BigDecimal.ZERO;
  233. point.put("value", value.stripTrailingZeros().toPlainString());
  234. point.put("timestamp", timestamp);
  235. pointData.add(point);
  236. }
  237. return pointData;
  238. }
  239. private static String resolveUnResourceId(VppResourcePoint resource, VppSite site, VppUnProperties properties) {
  240. if (site != null && StringUtils.hasText(site.getUnResourceId())) {
  241. return site.getUnResourceId();
  242. }
  243. if (StringUtils.hasText(resource.getResourceCode())) {
  244. return resource.getResourceCode();
  245. }
  246. return properties.getDnId();
  247. }
  248. @SuppressWarnings("unchecked")
  249. private static String resolveReportRequestIdFromPoll(Map<String, Object> createReportRequest, String fallback) {
  250. if (createReportRequest == null) {
  251. return fallback;
  252. }
  253. Object reportRequest = createReportRequest.get("reportRequest");
  254. if (reportRequest instanceof List && !((List<?>) reportRequest).isEmpty()) {
  255. Object first = ((List<?>) reportRequest).get(0);
  256. if (first instanceof Map) {
  257. String id = VppUnPayloadHelper.getString((Map<String, Object>) first,
  258. "reportRequestID", "reportRequestId");
  259. if (StringUtils.hasText(id)) {
  260. return id;
  261. }
  262. }
  263. }
  264. String id = VppUnPayloadHelper.getString(createReportRequest, "reportRequestID", "reportRequestId");
  265. return StringUtils.hasText(id) ? id : fallback;
  266. }
  267. public static Map<String, Object> buildCqListItem(String account, BigDecimal loadKw) {
  268. Map<String, Object> item = new LinkedHashMap<>();
  269. item.put("load", loadKw != null ? loadKw.stripTrailingZeros().toPlainString() : "0");
  270. item.put("account", account);
  271. return item;
  272. }
  273. public static Map<String, Object> buildOptListItem(String account, BigDecimal loadKw, String needZk) {
  274. Map<String, Object> item = new LinkedHashMap<>();
  275. item.put("account", account);
  276. item.put("load", loadKw != null ? loadKw.stripTrailingZeros().toPlainString() : "0");
  277. if (StringUtils.hasText(needZk)) {
  278. item.put("needzk", needZk);
  279. }
  280. return item;
  281. }
  282. private static Map<String, Object> buildBaseRequest(String root, VppUnProperties properties) {
  283. Map<String, Object> req = new LinkedHashMap<>();
  284. req.put("root", root);
  285. req.put("version", 1);
  286. req.put("requestID", UUID.randomUUID().toString());
  287. req.put("dnID", properties.getDnId());
  288. return req;
  289. }
  290. public static Map<String, Object> buildCreateOptResponse(Map<String, Object> request, VppUnProperties properties) {
  291. Map<String, Object> resp = copyRequestAsResponse(request, "CreateOptResponse");
  292. fillCommon(resp, properties);
  293. resp.put("code", 200);
  294. resp.put("description", "ok");
  295. return resp;
  296. }
  297. public static Map<String, Object> buildCreateCqResponse(Map<String, Object> request, VppUnProperties properties) {
  298. Map<String, Object> resp = copyRequestAsResponse(request, "CreateCqResponse");
  299. fillCommon(resp, properties);
  300. resp.put("code", 200);
  301. resp.put("description", "ok");
  302. return resp;
  303. }
  304. public static Map<String, Object> buildCreateEventResponse(String eventId, String requestId,
  305. String optType, VppUnProperties properties) {
  306. Map<String, Object> resp = new LinkedHashMap<>();
  307. resp.put("root", "CreateEventResponse");
  308. resp.put("version", 1);
  309. resp.put("code", 200);
  310. resp.put("description", "ok");
  311. resp.put("requestID", requestId != null ? requestId : UUID.randomUUID().toString());
  312. resp.put("dnID", properties.getDnId());
  313. Map<String, Object> eventResponse = new LinkedHashMap<>();
  314. eventResponse.put("optType", optType != null ? optType : "optIn");
  315. eventResponse.put("code", 200);
  316. eventResponse.put("description", "ok");
  317. eventResponse.put("requestID", UUID.randomUUID().toString());
  318. Map<String, Object> qualifiedEventId = new HashMap<>();
  319. qualifiedEventId.put("eventID", eventId);
  320. qualifiedEventId.put("modificationNumber", 0);
  321. eventResponse.put("qualifiedEventID", qualifiedEventId);
  322. List<Map<String, Object>> eventResponses = new ArrayList<>();
  323. eventResponses.add(eventResponse);
  324. resp.put("eventResponses", eventResponses);
  325. return resp;
  326. }
  327. private static Map<String, Object> copyRequestAsResponse(Map<String, Object> request, String root) {
  328. Map<String, Object> resp = new LinkedHashMap<>();
  329. if (request != null) {
  330. resp.putAll(request);
  331. }
  332. resp.put("root", root);
  333. resp.put("version", request != null && request.get("version") != null ? request.get("version") : 1);
  334. return resp;
  335. }
  336. private static void fillCommon(Map<String, Object> resp, VppUnProperties properties) {
  337. if (resp.get("requestID") == null) {
  338. resp.put("requestID", UUID.randomUUID().toString());
  339. }
  340. resp.put("dnID", properties.getDnId());
  341. }
  342. }