|
|
@@ -7,7 +7,7 @@ import com.usky.cdi.domain.DmpDevice;
|
|
|
import com.usky.cdi.domain.DmpProduct;
|
|
|
import com.usky.cdi.mapper.DmpDeviceMapper;
|
|
|
import com.usky.cdi.mapper.DmpProductMapper;
|
|
|
-//import com.usky.cdi.service.config.mqtt.MqttGateway;
|
|
|
+import com.usky.cdi.service.config.mqtt.MqttBaseConfig;
|
|
|
import com.usky.cdi.service.config.mqtt.MqttOutConfig;
|
|
|
import com.usky.cdi.service.enums.EnvMonitorMqttTopic;
|
|
|
import com.usky.cdi.service.util.DeviceDataQuery;
|
|
|
@@ -15,14 +15,18 @@ import com.usky.cdi.service.util.SnowflakeIdGenerator;
|
|
|
import com.usky.cdi.service.vo.IotDataTransferVO;
|
|
|
import com.usky.cdi.service.vo.base.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.context.ConfigurableApplicationContext;
|
|
|
+import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.context.ContextLoader;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
import java.time.Instant;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.ZoneId;
|
|
|
@@ -39,12 +43,16 @@ import java.util.stream.Collectors;
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
-@ConditionalOnProperty(prefix = "mqtt", value = {"enabled"}, havingValue = "true")
|
|
|
public class IotDataTransferService {
|
|
|
|
|
|
- @Resource
|
|
|
private MqttOutConfig.MqttGateway mqttGateway;
|
|
|
|
|
|
+ // MQTT连接相关配置
|
|
|
+ private static final String MQTT_URL = "ssl://114.80.201.143:8883";
|
|
|
+ private static final String MQTT_TOPIC = "iotInfo/+";
|
|
|
+ private static final int KEEP_ALIVE_INTERVAL = 60;
|
|
|
+ private static final int COMPLETION_TIMEOUT = 5000;
|
|
|
+
|
|
|
private SnowflakeIdGenerator idGenerator;
|
|
|
|
|
|
@Autowired
|
|
|
@@ -178,8 +186,20 @@ public class IotDataTransferService {
|
|
|
List<JSONObject> deviceData = deviceDataQuery.getDeviceData(transferVO);
|
|
|
Integer totalDevices = transferVO.getDevices().size();
|
|
|
|
|
|
+ // 统计各类型设备数据数量
|
|
|
+ int tempCount = 0, humidityCount = 0, coCount = 0, o2Count = 0, co2Count = 0;
|
|
|
+ for (JSONObject dataItem : deviceData) {
|
|
|
+ if (dataItem.containsKey("wd") && dataItem.getFloat("wd") != null) tempCount++;
|
|
|
+ if (dataItem.containsKey("sd") && dataItem.getFloat("sd") != null) humidityCount++;
|
|
|
+ if (dataItem.containsKey("co") && dataItem.getFloat("co") != null) coCount++;
|
|
|
+ if (dataItem.containsKey("o2") && dataItem.getFloat("o2") != null) o2Count++;
|
|
|
+ if (dataItem.containsKey("co2") && dataItem.getFloat("co2") != null) co2Count++;
|
|
|
+ }
|
|
|
+
|
|
|
log.info("开始推送温湿度及气体浓度数据,设备类型:{},设备数量:{},获取到的数据条数:{}",
|
|
|
deviceType, totalDevices, deviceData.size());
|
|
|
+ log.info("各类型设备数据数量:温度{}条,湿度{}条,一氧化碳{}条,氧气{}条,二氧化碳{}条",
|
|
|
+ tempCount, humidityCount, coCount, o2Count, co2Count);
|
|
|
|
|
|
if (deviceData.isEmpty()) {
|
|
|
log.warn("没有获取到空气质量数据!设备类型:{}", deviceType);
|
|
|
@@ -387,7 +407,14 @@ public class IotDataTransferService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 提取公共发送方法,减少代码冗余
|
|
|
+ /**
|
|
|
+ * 推送温度信息(701)
|
|
|
+ *
|
|
|
+ * @param deviceDataItem 设备数据
|
|
|
+ * @param deviceId 设备ID
|
|
|
+ * @param dataEndTime 数据结束时间
|
|
|
+ * @param engineeringID 工程ID
|
|
|
+ **/
|
|
|
private void sendTempData(Integer deviceId, LocalDateTime dataEndTime, JSONObject deviceDataItem, Long engineeringID) {
|
|
|
Float value = deviceDataItem.getFloat("wd");
|
|
|
if (value == null) {
|
|
|
@@ -404,6 +431,14 @@ public class IotDataTransferService {
|
|
|
sendMqttMessage(EnvMonitorMqttTopic.TEMP, tempVO, "温度信息");
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 推送湿度信息(702)
|
|
|
+ *
|
|
|
+ * @param deviceDataItem 设备数据
|
|
|
+ * @param deviceId 设备ID
|
|
|
+ * @param dataEndTime 数据结束时间
|
|
|
+ * @param engineeringID 工程ID
|
|
|
+ **/
|
|
|
private void sendHumidityData(Integer deviceId, LocalDateTime dataEndTime, JSONObject deviceDataItem, Long engineeringID) {
|
|
|
Float value = deviceDataItem.getFloat("sd");
|
|
|
if (value == null) {
|
|
|
@@ -420,6 +455,14 @@ public class IotDataTransferService {
|
|
|
sendMqttMessage(EnvMonitorMqttTopic.HUMIDITY, humidityVO, "湿度信息");
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 推送氧气浓度信息(705)
|
|
|
+ *
|
|
|
+ * @param deviceDataItem 设备数据
|
|
|
+ * @param deviceId 设备ID
|
|
|
+ * @param dataEndTime 数据结束时间
|
|
|
+ * @param engineeringID 工程ID
|
|
|
+ **/
|
|
|
private void sendOxygenData(Integer deviceId, LocalDateTime dataEndTime, JSONObject deviceDataItem, Long engineeringID) {
|
|
|
Float value = deviceDataItem.getFloat("o2");
|
|
|
if (value == null) {
|
|
|
@@ -436,6 +479,14 @@ public class IotDataTransferService {
|
|
|
sendMqttMessage(EnvMonitorMqttTopic.OXYGEN, oxygenVO, "氧气浓度信息");
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 推送一氧化碳浓度信息(706)
|
|
|
+ *
|
|
|
+ * @param deviceDataItem 设备数据
|
|
|
+ * @param deviceId 设备ID
|
|
|
+ * @param dataEndTime 数据结束时间
|
|
|
+ * @param engineeringID 工程ID
|
|
|
+ **/
|
|
|
private void sendCoData(Integer deviceId, LocalDateTime dataEndTime, JSONObject deviceDataItem, Long engineeringID) {
|
|
|
Float value = deviceDataItem.getFloat("co");
|
|
|
if (value == null) {
|
|
|
@@ -452,6 +503,14 @@ public class IotDataTransferService {
|
|
|
sendMqttMessage(EnvMonitorMqttTopic.CO, coVO, "一氧化碳浓度信息");
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 推送二氧化碳浓度信息(707)
|
|
|
+ *
|
|
|
+ * @param deviceDataItem 设备数据
|
|
|
+ * @param deviceId 设备ID
|
|
|
+ * @param dataEndTime 数据结束时间
|
|
|
+ * @param engineeringID 工程ID
|
|
|
+ **/
|
|
|
private void sendCo2Data(Integer deviceId, LocalDateTime dataEndTime, JSONObject deviceDataItem, Long engineeringID) {
|
|
|
Float value = deviceDataItem.getFloat("co2");
|
|
|
if (value == null) {
|
|
|
@@ -468,15 +527,233 @@ public class IotDataTransferService {
|
|
|
sendMqttMessage(EnvMonitorMqttTopic.CO2, co2VO, "二氧化碳浓度信息");
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 发送倾斜数据(712)
|
|
|
+ *
|
|
|
+ * @return 推送结果,包含成功数和失败数
|
|
|
+ **/
|
|
|
+ public Map<String, Integer> sendTiltData(IotDataTransferVO transferVO) {
|
|
|
+ Map<String, Integer> result = new HashMap<>();
|
|
|
+ result.put("successCount", 0);
|
|
|
+ result.put("failureCount", 0);
|
|
|
+
|
|
|
+ if (!validateMqttGateway()) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ List<JSONObject> deviceData = deviceDataQuery.getDeviceData(transferVO);
|
|
|
+ Integer deviceType = transferVO.getDeviceType();
|
|
|
+ Integer totalDevices = transferVO.getDevices().size();
|
|
|
+
|
|
|
+ log.info("开始推送倾斜数据,设备类型:{},设备数量:{},获取到的数据条数:{}",
|
|
|
+ deviceType, totalDevices, deviceData.size());
|
|
|
+
|
|
|
+ if (deviceData.isEmpty()) {
|
|
|
+ log.warn("没有获取到倾斜数据!设备类型:{}", deviceType);
|
|
|
+ result.put("failureCount", totalDevices);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ Long engineeringId = transferVO.getEngineeringId();
|
|
|
+ for (JSONObject deviceDataItem : deviceData) {
|
|
|
+ LocalDateTime dataEndTime = parseDataTime(deviceDataItem);
|
|
|
+ if (dataEndTime == null) {
|
|
|
+ result.put("failureCount", result.get("failureCount") + 1);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer deviceId = deviceDataItem.getIntValue("device_id");
|
|
|
+ Integer value = deviceDataItem.getInteger("qx");
|
|
|
+ if (value == null) {
|
|
|
+ log.warn("设备{}的倾斜数据为空", deviceId);
|
|
|
+ result.put("failureCount", result.get("failureCount") + 1);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ TiltVO vo = new TiltVO();
|
|
|
+ vo.setDataPacketID(generateDataPacketID());
|
|
|
+ vo.setSensorID(deviceId);
|
|
|
+ vo.setEngineeringID(engineeringId);
|
|
|
+ vo.setPublishTime(getCurrentTime());
|
|
|
+ vo.setDataEndTime(dataEndTime);
|
|
|
+ vo.setSensorValue(value == 0 ? 0 : 1);
|
|
|
+
|
|
|
+ try {
|
|
|
+ sendMqttMessage(EnvMonitorMqttTopic.TILT, vo, "倾斜信息");
|
|
|
+ result.put("successCount", result.get("successCount") + 1);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("设备{}的倾斜数据推送失败:{}", deviceId, e.getMessage());
|
|
|
+ result.put("failureCount", result.get("failureCount") + 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("倾斜数据推送完成,设备类型:{},成功:{},失败:{}",
|
|
|
+ deviceType, result.get("successCount"), result.get("failureCount"));
|
|
|
+
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("倾斜数据推送发生异常", e);
|
|
|
+ result.put("failureCount", transferVO.getDevices().size());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送裂缝数据(713)
|
|
|
+ *
|
|
|
+ * @return 推送结果,包含成功数和失败数
|
|
|
+ **/
|
|
|
+ public Map<String, Integer> sendCrackData(IotDataTransferVO transferVO) {
|
|
|
+ Map<String, Integer> result = new HashMap<>();
|
|
|
+ result.put("successCount", 0);
|
|
|
+ result.put("failureCount", 0);
|
|
|
+
|
|
|
+ if (!validateMqttGateway()) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ List<JSONObject> deviceData = deviceDataQuery.getDeviceData(transferVO);
|
|
|
+ Integer deviceType = transferVO.getDeviceType();
|
|
|
+ Integer totalDevices = transferVO.getDevices().size();
|
|
|
+
|
|
|
+ log.info("开始推送裂缝数据,设备类型:{},设备数量:{},获取到的数据条数:{}",
|
|
|
+ deviceType, totalDevices, deviceData.size());
|
|
|
+
|
|
|
+ if (deviceData.isEmpty()) {
|
|
|
+ log.warn("没有获取到裂缝数据!设备类型:{}", deviceType);
|
|
|
+ result.put("failureCount", totalDevices);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ Long engineeringId = transferVO.getEngineeringId();
|
|
|
+ for (JSONObject deviceDataItem : deviceData) {
|
|
|
+ LocalDateTime dataEndTime = parseDataTime(deviceDataItem);
|
|
|
+ if (dataEndTime == null) {
|
|
|
+ result.put("failureCount", result.get("failureCount") + 1);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer deviceId = deviceDataItem.getIntValue("device_id");
|
|
|
+ Integer value = deviceDataItem.getInteger("cd");
|
|
|
+ if (value == null) {
|
|
|
+ log.warn("设备{}的裂缝数据为空", deviceId);
|
|
|
+ result.put("failureCount", result.get("failureCount") + 1);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ CrackVO vo = new CrackVO();
|
|
|
+ vo.setDataPacketID(generateDataPacketID());
|
|
|
+ vo.setSensorID(deviceId);
|
|
|
+ vo.setEngineeringID(engineeringId);
|
|
|
+ vo.setPublishTime(getCurrentTime());
|
|
|
+ vo.setDataEndTime(dataEndTime);
|
|
|
+ vo.setSensorValue(value == 0 ? 0 : 1);
|
|
|
+
|
|
|
+ try {
|
|
|
+ sendMqttMessage(EnvMonitorMqttTopic.CRACK, vo, "裂缝信息");
|
|
|
+ result.put("successCount", result.get("successCount") + 1);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("设备{}的裂缝数据推送失败:{}", deviceId, e.getMessage());
|
|
|
+ result.put("failureCount", result.get("failureCount") + 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("裂缝数据推送完成,设备类型:{},成功:{},失败:{}",
|
|
|
+ deviceType, result.get("successCount"), result.get("failureCount"));
|
|
|
+
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("裂缝数据推送发生异常", e);
|
|
|
+ result.put("failureCount", transferVO.getDevices().size());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送位移数据(714)
|
|
|
+ *
|
|
|
+ * @return 推送结果,包含成功数和失败数
|
|
|
+ **/
|
|
|
+ public Map<String, Integer> sendDeviationData(IotDataTransferVO transferVO) {
|
|
|
+ Map<String, Integer> result = new HashMap<>();
|
|
|
+ result.put("successCount", 0);
|
|
|
+ result.put("failureCount", 0);
|
|
|
+
|
|
|
+ if (!validateMqttGateway()) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ List<JSONObject> deviceData = deviceDataQuery.getDeviceData(transferVO);
|
|
|
+ Integer deviceType = transferVO.getDeviceType();
|
|
|
+ Integer totalDevices = transferVO.getDevices().size();
|
|
|
+
|
|
|
+ log.info("开始推送位移数据,设备类型:{},设备数量:{},获取到的数据条数:{}",
|
|
|
+ deviceType, totalDevices, deviceData.size());
|
|
|
+
|
|
|
+ if (deviceData.isEmpty()) {
|
|
|
+ log.warn("没有获取到位移数据!设备类型:{}", deviceType);
|
|
|
+ result.put("failureCount", totalDevices);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ Long engineeringId = transferVO.getEngineeringId();
|
|
|
+ for (JSONObject deviceDataItem : deviceData) {
|
|
|
+ LocalDateTime dataEndTime = parseDataTime(deviceDataItem);
|
|
|
+ if (dataEndTime == null) {
|
|
|
+ result.put("failureCount", result.get("failureCount") + 1);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer deviceId = deviceDataItem.getIntValue("device_id");
|
|
|
+ Integer value = deviceDataItem.getInteger("wy");
|
|
|
+ if (value == null) {
|
|
|
+ log.warn("设备{}的位移数据为空", deviceId);
|
|
|
+ result.put("failureCount", result.get("failureCount") + 1);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ DeviationVO vo = new DeviationVO();
|
|
|
+ vo.setDataPacketID(generateDataPacketID());
|
|
|
+ vo.setSensorID(deviceId);
|
|
|
+ vo.setEngineeringID(engineeringId);
|
|
|
+ vo.setPublishTime(getCurrentTime());
|
|
|
+ vo.setDataEndTime(dataEndTime);
|
|
|
+ vo.setSensorValue(value == 0 ? 0 : 1);
|
|
|
+
|
|
|
+ try {
|
|
|
+ sendMqttMessage(EnvMonitorMqttTopic.DEVIATION, vo, "位移信息");
|
|
|
+ result.put("successCount", result.get("successCount") + 1);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("设备{}的位移数据推送失败:{}", deviceId, e.getMessage());
|
|
|
+ result.put("failureCount", result.get("failureCount") + 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("位移数据推送完成,设备类型:{},成功:{},失败:{}",
|
|
|
+ deviceType, result.get("successCount"), result.get("failureCount"));
|
|
|
+
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("位移数据推送发生异常", e);
|
|
|
+ result.put("failureCount", transferVO.getDevices().size());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 同步设备数据
|
|
|
* @param tenantId 租户ID
|
|
|
* @param engineeringId 工程ID
|
|
|
+ * @param username MQTT用户名
|
|
|
+ * @param password MQTT密码
|
|
|
*/
|
|
|
- public void synchronizeDeviceData(Integer tenantId, Long engineeringId) {
|
|
|
+ public void synchronizeDeviceData(Integer tenantId, Long engineeringId, String username, String password) {
|
|
|
// 参数校验
|
|
|
- if (tenantId == null || engineeringId == null) {
|
|
|
- log.error("租户ID或工程ID不能为空");
|
|
|
+ if (engineeringId == null || username == null || password == null) {
|
|
|
+ log.error("工程ID、MQTT用户名或密码不能为空");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -488,7 +765,7 @@ public class IotDataTransferService {
|
|
|
}
|
|
|
|
|
|
// 查询设备列表
|
|
|
- List<DmpDevice> deviceList = getDeviceListByType(deviceTypeList, tenantId);
|
|
|
+ List<DmpDevice> deviceList = getDeviceListByType(deviceTypeList);
|
|
|
if (deviceList.isEmpty()) {
|
|
|
log.warn("租户{}不存在任何设备", tenantId);
|
|
|
return;
|
|
|
@@ -514,6 +791,9 @@ public class IotDataTransferService {
|
|
|
.collect(Collectors.groupingBy(DmpDevice::getProductCode,
|
|
|
Collectors.mapping(DmpDevice::getDeviceUuid, Collectors.toList())));
|
|
|
|
|
|
+ // 创建MQTT连接
|
|
|
+ createMqttConnection(username, password);
|
|
|
+
|
|
|
// 任务开始日志
|
|
|
Integer totalDevices = deviceList.size();
|
|
|
Integer totalProductTypes = codeDeviceUuidsMap.size();
|
|
|
@@ -552,6 +832,16 @@ public class IotDataTransferService {
|
|
|
case 704:
|
|
|
result = sendElectricityLoad(transferVO);
|
|
|
break;
|
|
|
+ case 712:
|
|
|
+ result = sendTiltData(transferVO);
|
|
|
+ break;
|
|
|
+ case 713:
|
|
|
+ result = sendCrackData(transferVO);
|
|
|
+ break;
|
|
|
+ case 714:
|
|
|
+ result = sendDeviationData(transferVO);
|
|
|
+ break;
|
|
|
+
|
|
|
default:
|
|
|
log.debug("不支持的设备类型:{}", deviceType);
|
|
|
continue;
|
|
|
@@ -577,7 +867,7 @@ public class IotDataTransferService {
|
|
|
private List<String> getDeviceTypeListByTenant(Integer tenantId) {
|
|
|
LambdaQueryWrapper<DmpProduct> productQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
productQueryWrapper.select(DmpProduct::getProductCode)
|
|
|
- .eq(DmpProduct::getTenantId, tenantId)
|
|
|
+ .eq(tenantId != null && tenantId > 0, DmpProduct::getTenantId, tenantId)
|
|
|
.eq(DmpProduct::getDeleteFlag, 0);
|
|
|
List<DmpProduct> productList = dmpProductMapper.selectList(productQueryWrapper);
|
|
|
return productList.stream()
|
|
|
@@ -591,7 +881,7 @@ public class IotDataTransferService {
|
|
|
* @param productCodeList 设备类型列表
|
|
|
* @return 设备列表
|
|
|
*/
|
|
|
- private List<DmpDevice> getDeviceListByType(List<String> productCodeList, Integer tenantId) {
|
|
|
+ private List<DmpDevice> getDeviceListByType(List<String> productCodeList) {
|
|
|
LambdaQueryWrapper<DmpDevice> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
queryWrapper.select(DmpDevice::getDeviceUuid, DmpDevice::getDeviceType, DmpDevice::getDeviceId, DmpDevice::getProductCode)
|
|
|
.in(DmpDevice::getProductCode, productCodeList)
|
|
|
@@ -601,6 +891,60 @@ public class IotDataTransferService {
|
|
|
return dmpDeviceMapper.selectList(queryWrapper);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 手动创建MQTT连接
|
|
|
+ * @param username MQTT用户名
|
|
|
+ * @param password MQTT密码
|
|
|
+ */
|
|
|
+ private void createMqttConnection(String username, String password) {
|
|
|
+ try {
|
|
|
+ // 创建MqttBaseConfig实例并设置参数
|
|
|
+ MqttBaseConfig mqttBaseConfig = new MqttBaseConfig();
|
|
|
+ // 手动设置所有MQTT配置参数
|
|
|
+ mqttBaseConfig.setUsername(username);
|
|
|
+ mqttBaseConfig.setPassword(password);
|
|
|
+ mqttBaseConfig.setHostUrl(MQTT_URL);
|
|
|
+ mqttBaseConfig.setMsgTopic(MQTT_TOPIC);
|
|
|
+ mqttBaseConfig.setKeepAliveInterval(KEEP_ALIVE_INTERVAL);
|
|
|
+ mqttBaseConfig.setCompletionTimeout(COMPLETION_TIMEOUT);
|
|
|
+
|
|
|
+ // 创建MqttOutConfig实例
|
|
|
+ MqttOutConfig mqttOutConfig = new MqttOutConfig();
|
|
|
+ mqttOutConfig.mqttBaseConfig = mqttBaseConfig;
|
|
|
+
|
|
|
+ // 使用Spring的ApplicationContext手动创建Bean
|
|
|
+ ApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
|
|
|
+ if (context == null) {
|
|
|
+ throw new IllegalStateException("ApplicationContext未找到,无法创建MQTT相关Bean");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 注册MqttBaseConfig为Bean
|
|
|
+ ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext) context).getBeanFactory();
|
|
|
+ beanFactory.registerSingleton("mqttBaseConfig", mqttBaseConfig);
|
|
|
+
|
|
|
+ // 创建并注册mqttClientFactory
|
|
|
+ MqttConnectOptions options = new MqttConnectOptions();
|
|
|
+ options.setServerURIs(new String[]{mqttBaseConfig.getHostUrl()});
|
|
|
+ options.setUserName(mqttBaseConfig.getUsername());
|
|
|
+ options.setPassword(mqttBaseConfig.getPassword().toCharArray());
|
|
|
+ options.setKeepAliveInterval(mqttBaseConfig.getKeepAliveInterval());
|
|
|
+
|
|
|
+ DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
|
|
|
+ factory.setConnectionOptions(options);
|
|
|
+ beanFactory.registerSingleton("mqttClientFactory", factory);
|
|
|
+
|
|
|
+ // 注册MqttOutConfig为Bean
|
|
|
+ beanFactory.registerSingleton("mqttOutConfig", mqttOutConfig);
|
|
|
+
|
|
|
+ // 获取mqttGateway
|
|
|
+ this.mqttGateway = context.getBean(MqttOutConfig.MqttGateway.class);
|
|
|
+ log.info("MQTT连接创建成功");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("创建MQTT连接失败: {}", e.getMessage(), e);
|
|
|
+ throw new RuntimeException("创建MQTT连接失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 验证MQTT网关是否初始化
|
|
|
* @return 是否初始化
|
|
|
@@ -639,4 +983,9 @@ public class IotDataTransferService {
|
|
|
// 不再记录每条数据的详情,只记录发送操作
|
|
|
mqttGateway.sendToMqtt(topic, json);
|
|
|
}
|
|
|
+
|
|
|
+ public void allData(Long engineeringId, String username, String password) {
|
|
|
+ Integer tenantId = 0;
|
|
|
+ synchronizeDeviceData(tenantId, engineeringId, username, password);
|
|
|
+ }
|
|
|
}
|