|
@@ -0,0 +1,245 @@
|
|
|
|
|
+package com.usky.cdi.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.usky.cdi.service.config.mqtt.MqttGateway;
|
|
|
|
|
+import com.usky.cdi.service.util.SnowflakeIdGenerator;
|
|
|
|
|
+import com.usky.cdi.service.vo.base.*;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 基础类数据传输服务
|
|
|
|
|
+ * 负责向市适配平台发送基础类数据
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author han
|
|
|
|
|
+ * @date 2025/03/20
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Service
|
|
|
|
|
+@ConditionalOnProperty(prefix = "mqtt", value = {"enabled"}, havingValue = "true")
|
|
|
|
|
+public class BaseDataTransferService {
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired(required = false)
|
|
|
|
|
+ private MqttGateway mqttGateway;
|
|
|
|
|
+
|
|
|
|
|
+ private final SnowflakeIdGenerator idGenerator;
|
|
|
|
|
+ private final SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
|
|
|
|
+
|
|
|
|
|
+ public BaseDataTransferService() {
|
|
|
|
|
+ // 使用默认的workerId和datacenterId,实际项目中可以从配置读取
|
|
|
|
|
+ this.idGenerator = new SnowflakeIdGenerator(1L, 1L);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取当前时间字符串
|
|
|
|
|
+ */
|
|
|
|
|
+ private String getCurrentTime() {
|
|
|
|
|
+ return timeFormat.format(new Date());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 生成数据包ID
|
|
|
|
|
+ */
|
|
|
|
|
+ private Long generateDataPacketID() {
|
|
|
|
|
+ return idGenerator.nextPacketId();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送人防工程基础信息
|
|
|
|
|
+ * Topic: base/engineering
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param vo 人防工程基础信息
|
|
|
|
|
+ * @return 是否发送成功
|
|
|
|
|
+ */
|
|
|
|
|
+ public boolean sendEngineeringBase(EngineeringBaseVO vo) {
|
|
|
|
|
+ if (mqttGateway == null) {
|
|
|
|
|
+ log.warn("MQTT Gateway未初始化,无法发送消息");
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (vo.getDataPacketID() == null) {
|
|
|
|
|
+ vo.setDataPacketID(generateDataPacketID());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (vo.getPublishTime() == null) {
|
|
|
|
|
+ vo.setPublishTime(getCurrentTime());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String json = JSON.toJSONString(vo);
|
|
|
|
|
+ String topic = "base/engineering";
|
|
|
|
|
+
|
|
|
|
|
+ log.info("发送人防工程基础信息,Topic: {}, Data: {}", topic, json);
|
|
|
|
|
+ mqttGateway.sendToMqtt(topic, json);
|
|
|
|
|
+
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("发送人防工程基础信息失败", e);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送防护单元基础信息
|
|
|
|
|
+ * Topic: base/protectiveUnit
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param vo 防护单元基础信息
|
|
|
|
|
+ * @return 是否发送成功
|
|
|
|
|
+ */
|
|
|
|
|
+ public boolean sendProtectiveUnit(ProtectiveUnitVO vo) {
|
|
|
|
|
+ if (mqttGateway == null) {
|
|
|
|
|
+ log.warn("MQTT Gateway未初始化,无法发送消息");
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (vo.getDataPacketID() == null) {
|
|
|
|
|
+ vo.setDataPacketID(generateDataPacketID());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (vo.getPublishTime() == null) {
|
|
|
|
|
+ vo.setPublishTime(getCurrentTime());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String json = JSON.toJSONString(vo);
|
|
|
|
|
+ String topic = "base/protectiveUnit";
|
|
|
|
|
+
|
|
|
|
|
+ log.info("发送防护单元基础信息,Topic: {}, Data: {}", topic, json);
|
|
|
|
|
+ mqttGateway.sendToMqtt(topic, json);
|
|
|
|
|
+
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("发送防护单元基础信息失败", e);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送楼层平面图信息
|
|
|
|
|
+ * Topic: base/floorPlane
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param vo 楼层平面图信息
|
|
|
|
|
+ * @return 是否发送成功
|
|
|
|
|
+ */
|
|
|
|
|
+ public boolean sendFloorPlane(FloorPlaneVO vo) {
|
|
|
|
|
+ if (mqttGateway == null) {
|
|
|
|
|
+ log.warn("MQTT Gateway未初始化,无法发送消息");
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (vo.getDataPacketID() == null) {
|
|
|
|
|
+ vo.setDataPacketID(generateDataPacketID());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (vo.getPublishTime() == null) {
|
|
|
|
|
+ vo.setPublishTime(getCurrentTime());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 检查文件大小(不超过5MB)
|
|
|
|
|
+ if (vo.getFloorFile() != null && vo.getFloorFile().length > 5 * 1024 * 1024) {
|
|
|
|
|
+ log.error("楼层平面图文件大小超过5MB限制,FileID: {}", vo.getFloorFileID());
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 将字节数组转换为Base64编码
|
|
|
|
|
+ JSONObject jsonObject = (JSONObject) JSON.toJSON(vo);
|
|
|
|
|
+ if (vo.getFloorFile() != null) {
|
|
|
|
|
+ // 使用Base64编码传输二进制数据
|
|
|
|
|
+ String base64File = java.util.Base64.getEncoder().encodeToString(vo.getFloorFile());
|
|
|
|
|
+ jsonObject.put("floorFile", base64File);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String json = jsonObject.toJSONString();
|
|
|
|
|
+ String topic = "base/floorPlane";
|
|
|
|
|
+
|
|
|
|
|
+ log.info("发送楼层平面图信息,Topic: {}, FileID: {}, FileSize: {} bytes",
|
|
|
|
|
+ topic, vo.getFloorFileID(),
|
|
|
|
|
+ vo.getFloorFile() != null ? vo.getFloorFile().length : 0);
|
|
|
|
|
+ mqttGateway.sendToMqtt(topic, json);
|
|
|
|
|
+
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("发送楼层平面图信息失败,FileID: {}", vo.getFloorFileID(), e);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送智能监管物联设施信息
|
|
|
|
|
+ * Topic: base/sensorInfo
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param vo 智能监管物联设施信息
|
|
|
|
|
+ * @return 是否发送成功
|
|
|
|
|
+ */
|
|
|
|
|
+ public boolean sendSensorInfo(SensorInfoVO vo) {
|
|
|
|
|
+ if (mqttGateway == null) {
|
|
|
|
|
+ log.warn("MQTT Gateway未初始化,无法发送消息");
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (vo.getDataPacketID() == null) {
|
|
|
|
|
+ vo.setDataPacketID(generateDataPacketID());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (vo.getPublishTime() == null) {
|
|
|
|
|
+ vo.setPublishTime(getCurrentTime());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String json = JSON.toJSONString(vo);
|
|
|
|
|
+ String topic = "base/sensorInfo";
|
|
|
|
|
+
|
|
|
|
|
+ log.info("发送智能监管物联设施信息,Topic: {}, SensorID: {}", topic, vo.getSensorID());
|
|
|
|
|
+ mqttGateway.sendToMqtt(topic, json);
|
|
|
|
|
+
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("发送智能监管物联设施信息失败", e);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 批量发送防护单元基础信息
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param units 防护单元列表
|
|
|
|
|
+ * @return 成功发送的数量
|
|
|
|
|
+ */
|
|
|
|
|
+ public int batchSendProtectiveUnits(java.util.List<ProtectiveUnitVO> units) {
|
|
|
|
|
+ if (units == null || units.isEmpty()) {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ int successCount = 0;
|
|
|
|
|
+ for (ProtectiveUnitVO unit : units) {
|
|
|
|
|
+ if (sendProtectiveUnit(unit)) {
|
|
|
|
|
+ successCount++;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ log.info("批量发送防护单元基础信息,总数: {}, 成功: {}", units.size(), successCount);
|
|
|
|
|
+ return successCount;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 批量发送智能监管物联设施信息
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param sensors 物联设施列表
|
|
|
|
|
+ * @return 成功发送的数量
|
|
|
|
|
+ */
|
|
|
|
|
+ public int batchSendSensorInfos(java.util.List<SensorInfoVO> sensors) {
|
|
|
|
|
+ if (sensors == null || sensors.isEmpty()) {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ int successCount = 0;
|
|
|
|
|
+ for (SensorInfoVO sensor : sensors) {
|
|
|
|
|
+ if (sendSensorInfo(sensor)) {
|
|
|
|
|
+ successCount++;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ log.info("批量发送智能监管物联设施信息,总数: {}, 成功: {}", sensors.size(), successCount);
|
|
|
|
|
+ return successCount;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|