package com.gkhy.testFourierSpecialGasMonitor.schedule; import com.gkhy.testFourierSpecialGasMonitor.commons.enums.ResultCode; import com.gkhy.testFourierSpecialGasMonitor.commons.exception.BusinessException; import com.gkhy.testFourierSpecialGasMonitor.entity.DeviceExceptionLog; import com.gkhy.testFourierSpecialGasMonitor.entity.GasConcentration; import com.gkhy.testFourierSpecialGasMonitor.entity.GasFlux; import com.gkhy.testFourierSpecialGasMonitor.enums.HeartbeatExecEnum; import com.gkhy.testFourierSpecialGasMonitor.service.DeviceExceptionLogService; import com.gkhy.testFourierSpecialGasMonitor.service.GasConcentrationService; import com.gkhy.testFourierSpecialGasMonitor.service.GasFluxService; import com.gkhy.testFourierSpecialGasMonitor.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.text.MessageFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; /** * @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()); private static final String deviceExcLogFormat = "【设备预警提示】{0} 硬件设备离线。"; private static final DateTimeFormatter deviceExcLogFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss"); @Scheduled(cron = "0 0/10 * * * ?") @Async(value = "SocketTaskExecutor") public void gasConcentrationStatus() { GasConcentration gasConcentration = gasConcentrationService.getLastData(); if (gasConcentration != null){ LocalDateTime lastReceiveTime = gasConcentration.getDataReceivingTime().plusMinutes(5); if (LocalDateTime.now().compareTo(lastReceiveTime) > 0){ try { heartbeatExcWebsocketServer.sendInfo(HeartbeatExecEnum.GAS_CONCENTRATION.getStatus()+"",null); //logger.info(HeartbeatExecEnum.GAS_CONCENTRATION.getDesc()); DeviceExceptionLog deviceExceptionLog = new DeviceExceptionLog(); LocalDateTime now = LocalDateTime.now(); String content = MessageFormat.format(deviceExcLogFormat,deviceExcLogFormatter.format(now)); deviceExceptionLog.setTime(now); deviceExceptionLog.setContent(content); 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(); LocalDateTime now = LocalDateTime.now(); String content = MessageFormat.format(deviceExcLogFormat,deviceExcLogFormatter.format(now)); deviceExceptionLog.setTime(now); deviceExceptionLog.setContent(content); 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(),"设备异常消息推送失败"); } } } } }