From e4b4a7ac53ba20800234c8a02eca7273c8ad7d09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E5=87=A4=E5=90=89?= Date: Wed, 19 Mar 2025 19:48:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=AE=9A=E6=97=B6=E6=8E=A8=E9=80=81?= =?UTF-8?q?=E6=B9=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/common/constant/WebSocketMessageType.java | 5 ++ .../ms/app/device/spray/HumidityCollector.java | 57 ++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/main/java/com/qyft/ms/app/device/spray/HumidityCollector.java diff --git a/src/main/java/com/qyft/ms/app/common/constant/WebSocketMessageType.java b/src/main/java/com/qyft/ms/app/common/constant/WebSocketMessageType.java index bc63894..399f84d 100644 --- a/src/main/java/com/qyft/ms/app/common/constant/WebSocketMessageType.java +++ b/src/main/java/com/qyft/ms/app/common/constant/WebSocketMessageType.java @@ -15,6 +15,11 @@ public class WebSocketMessageType { public static final String SPRAY_POINT = "spray_point"; /** + * 湿度 + */ + public static final String HUMIDITY = "humidity"; + + /** * 系统急停按钮被按下 */ public static final String SYSTEM_E_STOP_PRESSED = "system_e_stop_pressed"; diff --git a/src/main/java/com/qyft/ms/app/device/spray/HumidityCollector.java b/src/main/java/com/qyft/ms/app/device/spray/HumidityCollector.java new file mode 100644 index 0000000..6192544 --- /dev/null +++ b/src/main/java/com/qyft/ms/app/device/spray/HumidityCollector.java @@ -0,0 +1,57 @@ +package com.qyft.ms.app.device.spray; + +import cn.hutool.json.JSONObject; +import com.qyft.ms.app.common.constant.WebSocketMessageType; +import com.qyft.ms.system.common.device.command.CommandFuture; +import com.qyft.ms.system.common.device.command.DeviceCommandGenerator; +import com.qyft.ms.system.model.bo.DeviceCommand; +import com.qyft.ms.system.service.WebSocketService; +import com.qyft.ms.system.service.device.DeviceCommandService; +import jakarta.annotation.PostConstruct; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.*; + +@Slf4j +@Component +@RequiredArgsConstructor +public class HumidityCollector { + private final WebSocketService webSocketService; + private final DeviceCommandService deviceCommandService; + + @PostConstruct + public void init() { + ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); + executorService.scheduleAtFixedRate(() -> { + try { + DeviceCommand humidityGetCmd = DeviceCommandGenerator.humidityGet(); // 生成获取湿度指令 + CommandFuture humidityGetCmdFuture = deviceCommandService.sendCommandNoFront(humidityGetCmd); + commandWait(humidityGetCmdFuture); + JSONObject humidityGetCmdFutureResult = humidityGetCmdFuture.getResponseResult(); + Double deviceHumidity = humidityGetCmdFutureResult.getDouble("humidity"); + Map map = new HashMap<>(); + map.put("humidity", deviceHumidity); + webSocketService.pushMsg(WebSocketMessageType.HUMIDITY, map); + } catch (Exception e) { + if (!executorService.isShutdown()) { + executorService.shutdown(); // 关闭 ScheduledExecutorService + } + log.error("定时推送设备状态异常", e); + } + }, 10, 1000, TimeUnit.MILLISECONDS); + } + + private void commandWait(CommandFuture... futures) throws ExecutionException, InterruptedException { + CompletableFuture[] responseFutures = Arrays.stream(futures) + .map(CommandFuture::getResponseFuture) + .toArray(CompletableFuture[]::new); + CompletableFuture.allOf(responseFutures) + .orTimeout(120, TimeUnit.SECONDS) + .get(); + } +}