|
|
@ -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<String, Object> 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(); |
|
|
|
} |
|
|
|
} |