#update mqtt 上位机

This commit is contained in:
2026-08-07 08:45:43 +08:00
parent c3398c6c07
commit 3ba32b19ce
12 changed files with 403 additions and 80 deletions

View File

@@ -121,8 +121,9 @@ public class InitThread implements ApplicationRunner {
private void init() throws MqttException {
// 初始化InfluxDBUtil
GlobalMemory.influxDBUtil = new InfluxDBUtil(influxDBUrl, influxDBToken, influxDBOrg, influxDBBucket);
// 初始化MqttClientUtil
GlobalMemory.mqttClientUtil = new MqttClientUtil(mqttHostUrl, mqttClientId, mqttUsername, mqttPassword);
GlobalMemory.initMqttDeviceMonitor(mqttHostUrl, mqttClientId, mqttUsername, mqttPassword);
// 设备错误监听线程
deviceErrorMonitorExecutor.execute(() -> {
try {

View File

@@ -20,6 +20,7 @@ import org.eclipse.paho.client.mqttv3.MqttException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
@@ -63,6 +64,10 @@ public class DataToDataBaseHandler extends SimpleChannelInboundHandler<String> {
@Autowired
private DeviceRunParamMapper deviceRunParamMapper;
@Value("${mqtt.client-id}")
private String mqttClientId;
private static final Long recordInterval = 5000L; // 5秒记录一次
// private static final Long pushInterval = 500L; // 0.5秒推送一次
@@ -97,9 +102,11 @@ public class DataToDataBaseHandler extends SimpleChannelInboundHandler<String> {
String[] split = deviceStatus.split(",");
WebStatusMessageDTO dto = createWebDeviceStatusMessage(split);
try {
GlobalMemory.mqttClientUtil.publish(
String.format(MqttTopic.DEVICE_STATUS_TOPIC, deviceID),
GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId, String.format(MqttTopic.DEVICE_STATUS_TOPIC, deviceID),
JsonUtils.toJsonString(dto));
// GlobalMemory.mqttClientUtil.publish(
// String.format(MqttTopic.DEVICE_STATUS_TOPIC, deviceID),
// JsonUtils.toJsonString(dto));
} catch (MqttException e) {
logger.error("发送实时状态数据错误: {}", e.getMessage());
}

View File

@@ -15,6 +15,7 @@ import org.eclipse.paho.client.mqttv3.MqttException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
@@ -82,6 +83,9 @@ public class DeviceConnectHandler extends SimpleChannelInboundHandler<String> {
@Resource
private WebsocketMesDispather websocketMesDispather;
@Value("${mqtt.client-id}")
private String mqttClientId;
public DeviceConnectHandler(DeviceSessionManager sessionManager) {
this.sessionManager = sessionManager;
}
@@ -364,8 +368,12 @@ public class DeviceConnectHandler extends SimpleChannelInboundHandler<String> {
changeDTO.setTaskId(taskId);
try {
GlobalMemory.mqttClientUtil.publish(String.format(MqttTopic.DEVICE_TASK_STATUS_TOPIC, taskId),
GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId, String.format(MqttTopic.DEVICE_TASK_STATUS_TOPIC, taskId),
JsonUtils.toJsonString(changeDTO));
// GlobalMemory.mqttClientUtil.publish(String.format(MqttTopic.DEVICE_TASK_STATUS_TOPIC, taskId),
// JsonUtils.toJsonString(changeDTO));
} catch (MqttException e) {
logger.error("任务完成状态推送失败 task:{},device:{}error:{}", taskId, deviceId, e.getMessage());
}
@@ -392,8 +400,10 @@ public class DeviceConnectHandler extends SimpleChannelInboundHandler<String> {
reportDTO.setType(MesType.device_task_arrive_point);
try {
GlobalMemory.mqttClientUtil.publish(String.format(MqttTopic.DEVICE_TASK_ARRIVE_TOPIC, taskId),
GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId, String.format(MqttTopic.DEVICE_TASK_ARRIVE_TOPIC, taskId),
JsonUtils.toJsonString(reportDTO));
// GlobalMemory.mqttClientUtil.publish(String.format(MqttTopic.DEVICE_TASK_ARRIVE_TOPIC, taskId),
// JsonUtils.toJsonString(reportDTO));
} catch (MqttException e) {
logger.error("任务点位到达推送失败 task:{},device:{}error:{}", taskId, deviceId, e.getMessage());
}

View File

@@ -15,9 +15,11 @@ import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import com.maibu.memory.DeviceSessionManager;
import com.maibu.utils.json.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
@@ -69,6 +71,9 @@ public class DeviceThreadService {
@Autowired
private AlertPushService alertPushService;
@Value("${mqtt.client-id}")
private String mqttClientId;
public void deviceErrorMonitor() throws InterruptedException, IOException {
initStandard();
@@ -184,7 +189,8 @@ public class DeviceThreadService {
// }
// }
try {
GlobalMemory.mqttClientUtil.publish(String.format(MqttTopic.DEVICE_ERROR_PUSH_TOPIC, deviceId), dto);
GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId, String.format(MqttTopic.DEVICE_ERROR_PUSH_TOPIC, deviceId), dto);
// GlobalMemory.mqttClientUtil.publish(String.format(MqttTopic.DEVICE_ERROR_PUSH_TOPIC, deviceId), dto);
List<NettyDevice> controlMasters = sessionManager.getAllSlaveControl(deviceId);
if (!CollectionUtils.isEmpty(controlMasters)) {
controlMasters.forEach(x -> {