package com.gkhy.fourierSpecialGasMonitor.schedule; import com.gkhy.fourierSpecialGasMonitor.commons.enums.ResultCode; import com.gkhy.fourierSpecialGasMonitor.commons.exception.BusinessException; import com.gkhy.fourierSpecialGasMonitor.entity.DeviceExceptionLog; import com.gkhy.fourierSpecialGasMonitor.entity.GasConcentration; import com.gkhy.fourierSpecialGasMonitor.entity.GasFlux; import com.gkhy.fourierSpecialGasMonitor.enums.HeartbeatExecEnum; import com.gkhy.fourierSpecialGasMonitor.service.DeviceExceptionLogService; import com.gkhy.fourierSpecialGasMonitor.service.GasConcentrationService; import com.gkhy.fourierSpecialGasMonitor.service.GasFluxService; import com.gkhy.fourierSpecialGasMonitor.websocket.HeartbeatExcWebsocketServer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.io.IOException; import java.time.LocalDateTime; /** * @author Mr.huang * @decription * @date 2023/8/8 10:49 */ @Component public class HeartbeatSchedule { @Autowired private GasConcentrationService gasConcentrationService; @Autowired private GasFluxService gasFluxService; @Autowired private DeviceExceptionLogService deviceExceptionLogService; @Autowired private HeartbeatExcWebsocketServer heartbeatExcWebsocketServer; private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Scheduled(cron = "0/30 * * * * ?") @Async(value = "SocketTaskExecutor") public void gasConcentrationStatus() { GasConcentration gasConcentration = gasConcentrationService.getLastData(); if (gasConcentration != null){ LocalDateTime lastReceiveTime = gasConcentration.getDataReceivingTime().plusMinutes(2); if (LocalDateTime.now().compareTo(lastReceiveTime) > 0){ try { heartbeatExcWebsocketServer.sendInfo(HeartbeatExecEnum.GAS_CONCENTRATION.getStatus()+"",null); logger.info(HeartbeatExecEnum.GAS_CONCENTRATION.getDesc()); DeviceExceptionLog deviceExceptionLog = new DeviceExceptionLog(); deviceExceptionLog.setTime(LocalDateTime.now()); deviceExceptionLog.setExecDesc(HeartbeatExecEnum.GAS_CONCENTRATION.getDesc()); DeviceExceptionLog save = deviceExceptionLogService.save(deviceExceptionLog); if (save == null) throw new BusinessException(this.getClass(), ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(),"设备异常日志保存失败"); } catch (IOException e) { throw new BusinessException(this.getClass(), ResultCode.SYSTEM_ERROR_WEBSOCKET_SEND_INFO_FAIL.getCode(),"设备异常消息推送失败"); } } } } @Scheduled(cron = "0 0/30 * * * ?") // 每30分钟执行一次 @Async(value = "SocketTaskExecutor") public void gasFluxStatus() { GasFlux gasFlux = gasFluxService.getLastData(); if (gasFlux != null){ LocalDateTime lastReceiveTime = gasFlux.getDataReceivingTime().plusMinutes(30); if (LocalDateTime.now().compareTo(lastReceiveTime) > 0){ try { heartbeatExcWebsocketServer.sendInfo(HeartbeatExecEnum.GAS_FLUX.getStatus()+"",null); logger.info(HeartbeatExecEnum.GAS_FLUX.getDesc()); DeviceExceptionLog deviceExceptionLog = new DeviceExceptionLog(); deviceExceptionLog.setTime(LocalDateTime.now()); deviceExceptionLog.setExecDesc(HeartbeatExecEnum.GAS_FLUX.getDesc()); DeviceExceptionLog save = deviceExceptionLogService.save(deviceExceptionLog); if (save == null) throw new BusinessException(this.getClass(), ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(),"设备异常日志保存失败"); } catch (IOException e) { throw new BusinessException(this.getClass(), ResultCode.SYSTEM_ERROR_WEBSOCKET_SEND_INFO_FAIL.getCode(),"设备异常消息推送失败"); } } } } }