diff --git a/maibu-netty-server/src/main/java/com/maibu/netty/handler/DataToDataBaseHandler.java b/maibu-netty-server/src/main/java/com/maibu/netty/handler/DataToDataBaseHandler.java index 309ae71..44b8907 100644 --- a/maibu-netty-server/src/main/java/com/maibu/netty/handler/DataToDataBaseHandler.java +++ b/maibu-netty-server/src/main/java/com/maibu/netty/handler/DataToDataBaseHandler.java @@ -55,7 +55,9 @@ public class DataToDataBaseHandler extends SimpleChannelInboundHandler { @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 { } 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 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 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<>();