This commit is contained in:
2026-06-17 10:07:59 +08:00
parent a68a670ecb
commit caf4e2c415

View File

@@ -55,7 +55,9 @@ public class DataToDataBaseHandler extends SimpleChannelInboundHandler<String> {
@Autowired
private WebsocketMesDispather websocketMesDispather;
private static final Long interval = 5000L;
private static final Long recordInterval = 5000L; // 5秒记录一次
// private static final Long pushInterval = 500L; // 0.5秒推送一次
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
@@ -78,30 +80,29 @@ public class DataToDataBaseHandler extends SimpleChannelInboundHandler<String> {
} else {
long lastRecordTime = recordDTO.getLastRecordTime();
histories = recordDTO.getHistories();
if (System.currentTimeMillis() - lastRecordTime >= interval) {
if (System.currentTimeMillis() - lastRecordTime >= recordInterval) {
record = true;
}
}
// 推送websocket实时消息给前端 todo mqtt推送
String deviceStatus = new String(bytes, 2, bytes.length - 4, StandardCharsets.UTF_8).trim();
String[] split = deviceStatus.split(",");
WebStatusMessageDTO dto = createWebDeviceStatusMessage(split);
try {
GlobalMemory.mqttClientUtil.publish(
String.format(MqttTopic.DEVICE_STATUS_TOPIC, deviceID),
JsonUtils.toJsonString(dto));
} catch (MqttException e) {
logger.error("发送实时状态数据错误: {}", e.getMessage());
}
List<NettyDevice> controlMasters = deviceSessionManager
.getAllSlaveControl(deviceID);
if (!CollectionUtils.isEmpty(controlMasters)) {
controlMasters.forEach(x -> {
websocketMesDispather.dispather(x.getConnectorId(), JsonUtils.toJsonString(dto));
});
}
if (record) {
// 推送websocket实时消息给前端 todo mqtt推送
String deviceStatus = new String(bytes, 2, bytes.length - 4, StandardCharsets.UTF_8).trim();
String[] split = deviceStatus.split(",");
WebStatusMessageDTO dto = createWebDeviceStatusMessage(split);
try {
GlobalMemory.mqttClientUtil.publish(
String.format(MqttTopic.DEVICE_STATUS_TOPIC, deviceID),
JsonUtils.toJsonString(dto));
} catch (MqttException e) {
logger.error("发送实时状态数据错误: {}", e.getMessage());
}
List<NettyDevice> controlMasters = deviceSessionManager
.getAllSlaveControl(deviceID);
if (!CollectionUtils.isEmpty(controlMasters)) {
controlMasters.forEach(x -> {
websocketMesDispather.dispather(x.getConnectorId(), JsonUtils.toJsonString(dto));
});
}
DeviceRunningStatusHistory deviceRunningStatus = getDeviceRunningStatusHistory(deviceID, split);
if (histories == null) {
histories = new ArrayList<>();