XmlBuilder.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package com.usky.ems.util;
  2. import org.dom4j.Document;
  3. import org.dom4j.DocumentHelper;
  4. import org.dom4j.Element;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Date;
  7. import java.util.List;
  8. import java.util.Map;
  9. /**
  10. * XML报文构建工具类
  11. *
  12. * @author system
  13. * @since 2024-01-01
  14. */
  15. public class XmlBuilder {
  16. private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
  17. /**
  18. * 构建身份认证请求XML
  19. *
  20. * @param buildingId 建筑ID
  21. * @param gatewayId 网关ID
  22. * @return XML字符串
  23. */
  24. public static String buildAuthRequest(String buildingId, String gatewayId) {
  25. Document document = DocumentHelper.createDocument();
  26. Element root = document.addElement("root");
  27. // common节点
  28. Element common = root.addElement("common");
  29. common.addElement("building_id").setText(buildingId);
  30. common.addElement("gateway_id").setText(gatewayId);
  31. common.addElement("type").setText("id_validate");
  32. // id_validate节点
  33. Element idValidate = root.addElement("id_validate");
  34. idValidate.addAttribute("operation", "request");
  35. return document.asXML();
  36. }
  37. /**
  38. * 构建身份认证MD5请求XML
  39. *
  40. * @param buildingId 建筑ID
  41. * @param gatewayId 网关ID
  42. * @param md5 MD5值
  43. * @return XML字符串
  44. */
  45. public static String buildAuthMd5Request(String buildingId, String gatewayId, String md5) {
  46. Document document = DocumentHelper.createDocument();
  47. Element root = document.addElement("root");
  48. // common节点
  49. Element common = root.addElement("common");
  50. common.addElement("building_id").setText(buildingId);
  51. common.addElement("gateway_id").setText(gatewayId);
  52. common.addElement("type").setText("id_validate");
  53. // id_validate节点
  54. Element idValidate = root.addElement("id_validate");
  55. idValidate.addAttribute("operation", "md5");
  56. idValidate.addElement("md5").setText(md5);
  57. return document.asXML();
  58. }
  59. /**
  60. * 构建心跳请求XML
  61. *
  62. * @param buildingId 建筑ID
  63. * @param gatewayId 网关ID
  64. * @return XML字符串
  65. */
  66. public static String buildHeartbeatRequest(String buildingId, String gatewayId) {
  67. Document document = DocumentHelper.createDocument();
  68. Element root = document.addElement("root");
  69. // common节点
  70. Element common = root.addElement("common");
  71. common.addElement("building_id").setText(buildingId);
  72. common.addElement("gateway_id").setText(gatewayId);
  73. common.addElement("type").setText("heart_beat");
  74. // heart_beat节点
  75. Element heartBeat = root.addElement("heart_beat");
  76. heartBeat.addAttribute("operation", "notify");
  77. return document.asXML();
  78. }
  79. /**
  80. * 构建能耗数据上报XML
  81. *
  82. * @param buildingId 建筑ID
  83. * @param gatewayId 网关ID
  84. * @param time 时间(yyyyMMddHHmmss格式)
  85. * @param energyItems 分项数据
  86. * @param meters 仪表数据列表
  87. * @return XML字符串
  88. */
  89. public static String buildEnergyDataRequest(String buildingId, String gatewayId, String time,
  90. Map<String, Double> energyItems, List<Map<String, Object>> meters) {
  91. Document document = DocumentHelper.createDocument();
  92. Element root = document.addElement("root");
  93. // common节点
  94. Element common = root.addElement("common");
  95. common.addElement("building_id").setText(buildingId);
  96. common.addElement("gateway_id").setText(gatewayId);
  97. common.addElement("type").setText("energy_data");
  98. // data节点
  99. Element data = root.addElement("data");
  100. data.addAttribute("operation", "report");
  101. data.addElement("time").setText(time);
  102. // energy_items节点
  103. if (energyItems != null && !energyItems.isEmpty()) {
  104. Element energyItemsElement = data.addElement("energy_items");
  105. for (Map.Entry<String, Double> entry : energyItems.entrySet()) {
  106. Element item = energyItemsElement.addElement("energy_item");
  107. item.addAttribute("code", entry.getKey());
  108. item.setText(String.valueOf(entry.getValue()));
  109. }
  110. }
  111. // meters节点
  112. if (meters != null && !meters.isEmpty()) {
  113. Element metersElement = data.addElement("meters");
  114. metersElement.addAttribute("total", String.valueOf(meters.size()));
  115. for (Map<String, Object> meter : meters) {
  116. Element meterElement = metersElement.addElement("meter");
  117. meterElement.addAttribute("id", (String) meter.get("id"));
  118. meterElement.addAttribute("name", (String) meter.get("name"));
  119. // function节点
  120. @SuppressWarnings("unchecked")
  121. List<Map<String, String>> functions = (List<Map<String, String>>) meter.get("functions");
  122. if (functions != null) {
  123. for (Map<String, String> function : functions) {
  124. Element functionElement = meterElement.addElement("function");
  125. functionElement.addAttribute("id", function.get("id"));
  126. String error = function.get("error");
  127. if (error != null && !error.isEmpty()) {
  128. functionElement.addAttribute("error", error);
  129. }
  130. functionElement.setText(function.get("value"));
  131. }
  132. }
  133. }
  134. }
  135. return document.asXML();
  136. }
  137. /**
  138. * 格式化时间为yyyyMMddHHmmss格式
  139. *
  140. * @param date 日期
  141. * @return 格式化后的字符串
  142. */
  143. public static String formatTime(Date date) {
  144. return DATE_FORMAT.format(date);
  145. }
  146. /**
  147. * 格式化当前时间为yyyyMMddHHmmss格式
  148. *
  149. * @return 格式化后的字符串
  150. */
  151. public static String formatCurrentTime() {
  152. return formatTime(new Date());
  153. }
  154. }