huangzhen
2023-10-20 5c72b20bee11513c06cdfb30c52cd0a4016a7102
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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/1 * * * * ?") // 每秒执行一次
    @Async(value = "SocketTaskExecutor")
    public void gasConcentrationStatus() {
        GasConcentration gasConcentration = gasConcentrationService.getLastData();
        if (gasConcentration != null){
            LocalDateTime lastReceiveTime = gasConcentration.getDataReceivingTime().plusMinutes(1);
            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(),"设备异常消息推送失败");
                }
            }
        }
    }
}