|
@ -4,38 +4,35 @@ import com.qyft.gd.common.constant.WebSocketMessageType; |
|
|
import com.qyft.gd.device.model.bo.DeviceStatus; |
|
|
import com.qyft.gd.device.model.bo.DeviceStatus; |
|
|
import com.qyft.gd.device.service.DeviceStateService; |
|
|
import com.qyft.gd.device.service.DeviceStateService; |
|
|
import jakarta.annotation.PostConstruct; |
|
|
import jakarta.annotation.PostConstruct; |
|
|
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
import java.util.Timer; |
|
|
|
|
|
import java.util.TimerTask; |
|
|
|
|
|
|
|
|
import java.util.concurrent.Executors; |
|
|
|
|
|
import java.util.concurrent.ScheduledExecutorService; |
|
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
@Slf4j |
|
|
@Slf4j |
|
|
@Service |
|
|
@Service |
|
|
|
|
|
@RequiredArgsConstructor |
|
|
public class StatusService { |
|
|
public class StatusService { |
|
|
|
|
|
|
|
|
private final WebSocketService webSocketService; |
|
|
private final WebSocketService webSocketService; |
|
|
private final DeviceStateService deviceStateService; |
|
|
private final DeviceStateService deviceStateService; |
|
|
private Timer timer; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
public StatusService(WebSocketService webSocketService, DeviceStateService deviceStateService) { |
|
|
|
|
|
this.webSocketService = webSocketService; |
|
|
|
|
|
this.deviceStateService = deviceStateService; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@PostConstruct |
|
|
@PostConstruct |
|
|
public void init() { |
|
|
public void init() { |
|
|
timer = new Timer(); |
|
|
|
|
|
TimerTask task = new TimerTask() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void run() { |
|
|
|
|
|
|
|
|
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); |
|
|
|
|
|
executorService.scheduleAtFixedRate(() -> { |
|
|
|
|
|
try { |
|
|
DeviceStatus deviceStatus = deviceStateService.getDeviceStatus(); |
|
|
DeviceStatus deviceStatus = deviceStateService.getDeviceStatus(); |
|
|
webSocketService.pushMsg(WebSocketMessageType.STATUS, deviceStatus); |
|
|
webSocketService.pushMsg(WebSocketMessageType.STATUS, deviceStatus); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
if (!executorService.isShutdown()) { |
|
|
|
|
|
executorService.shutdown(); // 关闭 ScheduledExecutorService |
|
|
|
|
|
} |
|
|
|
|
|
log.error("定时推送设备状态异常", e); |
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
timer.schedule(task, 10, 100); |
|
|
|
|
|
|
|
|
}, 10, 100, TimeUnit.MILLISECONDS); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |