You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.3 KiB
38 lines
1.3 KiB
package com.qyft.ms.service;
|
|
|
|
import com.qyft.ms.common.constant.WebSocketMessageType;
|
|
import com.qyft.ms.device.model.bo.DeviceStatus;
|
|
import com.qyft.ms.device.service.DeviceStatusService;
|
|
import jakarta.annotation.PostConstruct;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.concurrent.Executors;
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
@Slf4j
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class StatusService {
|
|
|
|
private final WebSocketService webSocketService;
|
|
private final DeviceStatusService deviceStatusService;
|
|
|
|
// @PostConstruct
|
|
public void init() {
|
|
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
|
executorService.scheduleAtFixedRate(() -> {
|
|
try {
|
|
DeviceStatus deviceStatus = deviceStatusService.getDeviceStatus();
|
|
webSocketService.pushMsg(WebSocketMessageType.STATUS, deviceStatus);
|
|
} catch (Exception e) {
|
|
if (!executorService.isShutdown()) {
|
|
executorService.shutdown(); // 关闭 ScheduledExecutorService
|
|
}
|
|
log.error("定时推送设备状态异常", e);
|
|
}
|
|
}, 10, 100, TimeUnit.MILLISECONDS);
|
|
}
|
|
}
|