|
@@ -0,0 +1,303 @@
|
|
|
|
|
+package com.usky.cdi.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
|
+import com.usky.cdi.service.config.mqtt.MqttGateway;
|
|
|
|
|
+import com.usky.cdi.service.enums.EnvMonitorMqttTopic;
|
|
|
|
|
+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.time.LocalDateTime;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author fyc
|
|
|
|
|
+ * @email yuchuan.fu@chinausky.com
|
|
|
|
|
+ * @date 2025/11/20
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Service
|
|
|
|
|
+@ConditionalOnProperty(prefix = "mqtt", value = {"enabled"}, havingValue = "true")
|
|
|
|
|
+public class IotDataTransferService {
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired(required = false)
|
|
|
|
|
+ private MqttGateway mqttGateway;
|
|
|
|
|
+
|
|
|
|
|
+ private final SnowflakeIdGenerator idGenerator;
|
|
|
|
|
+
|
|
|
|
|
+ public IotDataTransferService() {
|
|
|
|
|
+ // 使用默认的workerId和datacenterId,实际项目中可以从配置读取
|
|
|
|
|
+ this.idGenerator = new SnowflakeIdGenerator(1L, 1L);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取当前时间
|
|
|
|
|
+ */
|
|
|
|
|
+ private LocalDateTime getCurrentTime() {
|
|
|
|
|
+ return LocalDateTime.now();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 生成数据包ID
|
|
|
|
|
+ */
|
|
|
|
|
+ private Long generateDataPacketID() {
|
|
|
|
|
+ return idGenerator.nextPacketId();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送水浸状态
|
|
|
|
|
+ * Topic: iotInfo/flooded
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param vo 水浸状态
|
|
|
|
|
+ * @return 是否发送成功
|
|
|
|
|
+ */
|
|
|
|
|
+ public boolean sendWaterLeak(WaterLeakVO 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 = EnvMonitorMqttTopic.WATER_LEAK.getTopic();
|
|
|
|
|
+
|
|
|
|
|
+ log.info("发送水浸状态信息,Topic: {}, Data: {}", topic, json);
|
|
|
|
|
+ mqttGateway.sendToMqtt(topic, json);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("发送水浸状态信息失败", e);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送温度
|
|
|
|
|
+ * Topic: iotInfo/temp
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param vo 温度信息
|
|
|
|
|
+ * @return 是否发送成功
|
|
|
|
|
+ */
|
|
|
|
|
+ public boolean sendTemp(TempVO 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 = EnvMonitorMqttTopic.TEMP.getTopic();
|
|
|
|
|
+
|
|
|
|
|
+ log.info("发送温度信息,Topic: {}, Data: {}", topic, json);
|
|
|
|
|
+ mqttGateway.sendToMqtt(topic, json);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("发送温度信息失败", e);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送湿度
|
|
|
|
|
+ * Topic: iotInfo/rh
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param vo 湿度信息
|
|
|
|
|
+ * @return 是否发送成功
|
|
|
|
|
+ */
|
|
|
|
|
+ public boolean batchSendHumidity(HumidityVO 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 = EnvMonitorMqttTopic.HUMIDITY.getTopic();
|
|
|
|
|
+ log.info("发送湿度信息,Topic: {}, Data: {}", topic, json);
|
|
|
|
|
+ mqttGateway.sendToMqtt(topic, json);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("发送湿度信息失败", e);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送氧气浓度信息
|
|
|
|
|
+ * Topic: iotInfo/o2
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param vo 氧气浓度
|
|
|
|
|
+ * @return 是否发送成功
|
|
|
|
|
+ */
|
|
|
|
|
+ public boolean sendOxygen(OxygenVO 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 = EnvMonitorMqttTopic.OXYGEN.getTopic();
|
|
|
|
|
+ log.info("发送氧气浓度信息,Topic: {}, Data: {}", topic, json);
|
|
|
|
|
+ mqttGateway.sendToMqtt(topic, json);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("发送氧气浓度信息失败", e);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送二氧化碳浓度信息
|
|
|
|
|
+ * Topic: iotInfo/co2
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param vo 二氧化碳浓度
|
|
|
|
|
+ * @return 是否发送成功
|
|
|
|
|
+ */
|
|
|
|
|
+ public boolean sendCarbonDioxide(Co2VO 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 = EnvMonitorMqttTopic.CO2.getTopic();
|
|
|
|
|
+ log.info("发送二氧化碳浓度信息,Topic: {}, Data: {}", topic, json);
|
|
|
|
|
+
|
|
|
|
|
+ mqttGateway.sendToMqtt(topic, json);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("发送二氧化碳浓度信息失败", e);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送一氧化碳浓度信息
|
|
|
|
|
+ * Topic: iotInfo/co
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param vo 一氧化碳浓度
|
|
|
|
|
+ * @return 是否发送成功
|
|
|
|
|
+ */
|
|
|
|
|
+ public boolean sendCarbonMonoxide(CoVO 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 = EnvMonitorMqttTopic.CO.getTopic();
|
|
|
|
|
+ log.info("发送一氧化碳浓度信息,Topic: {}, Data: {}", topic, json);
|
|
|
|
|
+ mqttGateway.sendToMqtt(topic, json);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("发送一氧化碳浓度信息失败", e);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送人员闯入情况
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param vo 人员闯入
|
|
|
|
|
+ * @return 是否发送成功
|
|
|
|
|
+ **/
|
|
|
|
|
+ public boolean sendPersonPresence(PersonPresenceVO 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 = EnvMonitorMqttTopic.PERSON_PRESENCE.getTopic();
|
|
|
|
|
+ log.info("发送人员闯入情况,Topic: {}, Data: {}", topic, json);
|
|
|
|
|
+ mqttGateway.sendToMqtt(topic, json);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("发送人员闯入情况失败", e);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送人防用电负荷情况
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param vo 防人电用负荷
|
|
|
|
|
+ * @return 是否发送成功
|
|
|
|
|
+ **/
|
|
|
|
|
+ public boolean sendElectricityLoad(ElectricityLoadVO 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 = EnvMonitorMqttTopic.ELECTRICITY_LOAD.getTopic();
|
|
|
|
|
+ log.info("发送人防用电负荷情况,Topic: {}, Data: {}", topic, json);
|
|
|
|
|
+ mqttGateway.sendToMqtt(topic, json);
|
|
|
|
|
+ return true;
|
|
|
|
|
+
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("发送人防用电负荷情况失败", e);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+}
|