|
|
@@ -1,26 +1,17 @@
|
|
|
package com.usky.cdi.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-// import com.alibaba.nacos.shaded.com.google.gson.Gson;
|
|
|
-import com.usky.cdi.service.config.mqtt.MqttOutConfig;
|
|
|
import com.usky.cdi.service.mqtt.MqttConnectionTool;
|
|
|
import com.usky.cdi.service.util.SnowflakeIdGenerator;
|
|
|
import com.usky.cdi.service.vo.alarm.AlarmMessageVO;
|
|
|
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 javax.annotation.Resource;
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
import java.time.LocalDateTime;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
|
|
|
/**
|
|
|
- * 告警类数据传输服务
|
|
|
- * 负责向市适配平台发送告警类数据
|
|
|
+ * 告警类数据传输服务:向市适配平台通过 MQTT 上报告警数据。
|
|
|
*
|
|
|
* @author han
|
|
|
* @date 2025/12/08
|
|
|
@@ -29,162 +20,56 @@ import java.util.HashMap;
|
|
|
@Service
|
|
|
public class AlarmDataTransferService {
|
|
|
|
|
|
- @Autowired
|
|
|
- private MqttConnectionTool mqttConnectionTool;
|
|
|
- @Resource
|
|
|
- private MqttOutConfig.MqttGateway mqttGateway;
|
|
|
+ private static final String MQTT_USERNAME = "3101100021";
|
|
|
+ private static final String MQTT_PASSWORD = "SIixzph1";
|
|
|
+ private static final String ALARM_TOPIC = "alarm/message";
|
|
|
+ private static final DateTimeFormatter PUBLISH_TIME_FORMAT =
|
|
|
+ DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
|
|
|
|
|
|
- private final SnowflakeIdGenerator idGenerator;
|
|
|
- private final SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
|
|
+ private final MqttConnectionTool mqttConnectionTool;
|
|
|
+ private final SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(3L, 3L);
|
|
|
|
|
|
- public AlarmDataTransferService() {
|
|
|
- // 使用默认的workerId和datacenterId,实际项目中可以从配置读取
|
|
|
- this.idGenerator = new SnowflakeIdGenerator(3L, 3L);
|
|
|
+ public AlarmDataTransferService(MqttConnectionTool mqttConnectionTool) {
|
|
|
+ this.mqttConnectionTool = mqttConnectionTool;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取当前时间字符串
|
|
|
- */
|
|
|
private String getCurrentTime() {
|
|
|
- return timeFormat.format(new Date());
|
|
|
+ return LocalDateTime.now().format(PUBLISH_TIME_FORMAT);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 生成数据包ID
|
|
|
- */
|
|
|
- private Long generateDataPacketID() {
|
|
|
+ private long generateDataPacketId() {
|
|
|
return idGenerator.nextPacketId();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 发送告警信息
|
|
|
- * Topic: base/floorPlane
|
|
|
+ * 上报告警消息至 MQTT(topic: alarm/message)。
|
|
|
*
|
|
|
- * @param vo 楼层平面图信息
|
|
|
- * @return 是否发送成功
|
|
|
+ * @return 是否已成功投递到底层网关(不代表敌方平台一定消费成功)
|
|
|
*/
|
|
|
- public boolean sendAlarmMessage(AlarmMessageVO vo) {
|
|
|
- try {
|
|
|
- if (vo.getDataPacketID() == null) {
|
|
|
- vo.setDataPacketID(generateDataPacketID());
|
|
|
- }
|
|
|
- if (vo.getPublishTime() == null) {
|
|
|
- vo.setPublishTime(LocalDateTime.now());
|
|
|
- }
|
|
|
-
|
|
|
-// HashMap<String, Object> map = new HashMap<>();
|
|
|
-// map.put("dataPacketID", vo.getDataPacketID());
|
|
|
-// map.put("engineeringID", vo.getEngineeringID());
|
|
|
-// map.put("floor", vo.getFloor());
|
|
|
-// map.put("floorFileID", vo.getFloorFileID());
|
|
|
-// map.put("floorFileName", vo.getFloorFileName());
|
|
|
-// map.put("floorFileSuffix", vo.getFloorFileSuffix());
|
|
|
-// map.put("filePixWidth", vo.getFilePixWidth());
|
|
|
-// map.put("filePixHeight", vo.getFilePixHeight());
|
|
|
-// map.put("floorFile", imageBytes);
|
|
|
-// map.put("publishTime", vo.getPublishTime());
|
|
|
-// Gson gson = new Gson();
|
|
|
- JSONObject jsonObject = (JSONObject) JSON.toJSON(vo);
|
|
|
- String json = jsonObject.toJSONString();
|
|
|
- System.out.println(json);
|
|
|
- MqttConnectionTool.MqttGateway IQeIRyXG = mqttConnectionTool.connectOrRefresh("3101070011", "5RqhJ7VG");
|
|
|
- String topic = "alarm/message";
|
|
|
- IQeIRyXG.sendToMqtt(topic, json);
|
|
|
-
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("发送告警信息失败,AlarmID: {}", vo.getAlarmID(), e);
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 发送告警信息
|
|
|
- * Topic: base/floorPlane
|
|
|
- *
|
|
|
- * @param vo 楼层平面图信息
|
|
|
- * @return 是否发送成功
|
|
|
- */
|
|
|
- public boolean sendAlarmMessage1(AlarmMessageVO vo) {
|
|
|
- MqttConnectionTool.MqttGateway IQeIRyXG = mqttConnectionTool.connectOrRefresh("3101070011", "5RqhJ7VG");
|
|
|
-
|
|
|
+ public boolean publishAlarm(AlarmMessageVO<?> vo) {
|
|
|
try {
|
|
|
- if (vo.getDataPacketID() == null) {
|
|
|
- vo.setDataPacketID(generateDataPacketID());
|
|
|
- }
|
|
|
- if (vo.getPublishTime() == null) {
|
|
|
- vo.setPublishTime(LocalDateTime.now());
|
|
|
- }
|
|
|
-
|
|
|
- HashMap<String, Object> map = new HashMap<>();
|
|
|
-// map.put("dataPacketID", vo.getDataPacketID());
|
|
|
-// map.put("engineeringID", vo.getEngineeringID());
|
|
|
-// map.put("floor", vo.getFloor());
|
|
|
-// map.put("floorFileID", vo.getFloorFileID());
|
|
|
-// map.put("floorFileName", vo.getFloorFileName());
|
|
|
-// map.put("floorFileSuffix", vo.getFloorFileSuffix());
|
|
|
-// map.put("filePixWidth", vo.getFilePixWidth());
|
|
|
-// map.put("filePixHeight", vo.getFilePixHeight());
|
|
|
-// map.put("floorFile", imageBytes);
|
|
|
-// map.put("publishTime", vo.getPublishTime());
|
|
|
-// Gson gson = new Gson();
|
|
|
- JSONObject jsonObject = (JSONObject) JSON.toJSON(vo);
|
|
|
- String json = jsonObject.toJSONString();
|
|
|
- System.out.println(json);
|
|
|
- String topic = "alarm/message";
|
|
|
- IQeIRyXG.sendToMqtt(topic, json);
|
|
|
-
|
|
|
+ fillDefaults(vo);
|
|
|
+ String json = JSON.toJSONString(vo);
|
|
|
+ log.info("告警 MQTT 载荷: {}", json);
|
|
|
+ MqttConnectionTool.MqttGateway gateway =
|
|
|
+ mqttConnectionTool.connectOrRefresh(MQTT_USERNAME, MQTT_PASSWORD);
|
|
|
+ gateway.sendToMqtt(ALARM_TOPIC, json);
|
|
|
return true;
|
|
|
} catch (Exception e) {
|
|
|
- log.error("发送告警信息失败,AlarmID: {}", vo.getAlarmID(), e);
|
|
|
+ log.error("发送告警信息失败, alarmID: {}, engineeringID: {}",
|
|
|
+ vo != null ? vo.getAlarmID() : null,
|
|
|
+ vo != null ? vo.getEngineeringID() : null,
|
|
|
+ e);
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public boolean sendAlarmMessage2(AlarmMessageVO vo) {
|
|
|
- try {
|
|
|
- if (vo.getDataPacketID() == null) {
|
|
|
- vo.setDataPacketID(generateDataPacketID());
|
|
|
- }
|
|
|
- if (vo.getPublishTime() == null) {
|
|
|
- vo.setPublishTime(LocalDateTime.now());
|
|
|
- }
|
|
|
-
|
|
|
- JSONObject jsonObject = (JSONObject) JSON.toJSON(vo);
|
|
|
- String json = jsonObject.toJSONString();
|
|
|
- System.out.println(json);
|
|
|
- MqttConnectionTool.MqttGateway IQeIRyXG = mqttConnectionTool.connectOrRefresh("3101070011", "5RqhJ7VG");
|
|
|
- String topic = "alarm/message";
|
|
|
- IQeIRyXG.sendToMqtt(topic, json);
|
|
|
-
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("发送告警信息失败,AlarmID: {}", vo.getAlarmID(), e);
|
|
|
- return false;
|
|
|
+ private void fillDefaults(AlarmMessageVO<?> vo) {
|
|
|
+ if (vo.getDataPacketID() == null) {
|
|
|
+ vo.setDataPacketID(generateDataPacketId());
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- public boolean sendEngineeringBase(AlarmMessageVO vo) {
|
|
|
- try {
|
|
|
- if (vo.getDataPacketID() == null) {
|
|
|
- vo.setDataPacketID(generateDataPacketID());
|
|
|
- }
|
|
|
- if (vo.getPublishTime() == null) {
|
|
|
- vo.setPublishTime(LocalDateTime.now());
|
|
|
- }
|
|
|
-
|
|
|
- MqttConnectionTool.MqttGateway IQeIRyXG = mqttConnectionTool.connectOrRefresh("3101070011", "5RqhJ7VG");
|
|
|
-
|
|
|
- JSONObject jsonObject = (JSONObject) JSON.toJSON(vo);
|
|
|
- String json = jsonObject.toJSONString();
|
|
|
- String topic = "alarm/message";
|
|
|
- System.out.println("推送的数据: " + json);
|
|
|
- IQeIRyXG.sendToMqtt(topic, json);
|
|
|
-
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("发送电流告警信息失败,EngineeringID: {}", vo.getEngineeringID(), e);
|
|
|
- return false;
|
|
|
+ if (vo.getPublishTime() == null) {
|
|
|
+ vo.setPublishTime(getCurrentTime());
|
|
|
}
|
|
|
}
|
|
|
}
|