houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.iailab.module.watch;
 
import com.iailab.common.utils.DateUtils;
import com.iailab.module.data.api.IFeignDataApi;
import com.iailab.module.data.dto.FeignResultDTO;
import com.iailab.module.data.dto.FeignWritePointValueDTO;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * @author PanZhibao
 * @Description
 * @createTime 2024年06月05日
 */
@Slf4j
@Component
public class WatchDog {
 
    private long failure_time = 1000 * 10;
 
    @Resource
    private IFeignDataApi feignDataApi;
 
    public int writeWatchDogTime(String writePoint, String readPoint, String statusPoint, int lastHeat) {
        try {
            FeignWritePointValueDTO writeHeart = new FeignWritePointValueDTO();
            writeHeart.setPointCode(writePoint);
            writeHeart.setPointValue(DateUtils.format(new Date(), DateUtils.TIME2_PATTERN));
            FeignResultDTO writeHeartResult = feignDataApi.setPointValue(writeHeart);
            if (writeHeartResult.getCode() != 0) {
                log.info("写入心跳值失败");
                setStsStop(statusPoint);
                return lastHeat;
            }
            List<String> pointNos = new ArrayList<>();
            pointNos.add(readPoint);
            Map<String, Object> readHeatValue = feignDataApi.getCurrentValue(pointNos);
            Integer heartValue = new BigDecimal(readHeatValue.get(readPoint).toString()).intValue();
            log.info("heartValue=" + heartValue);
            if (lastHeat == heartValue) {
                log.info("心跳值无变化");
                setStsStop(statusPoint);
                return lastHeat;
            }
            lastHeat = heartValue;
            setStsNormal(statusPoint);
        } catch (Exception ex) {
            log.error("异常:控制器不能下发随机数;message:" + ex.getMessage());
            ex.printStackTrace();
            setStsStop(statusPoint);
 
        }
        return lastHeat;
    }
 
    public boolean isConnect(String pointNo) {
        List<String> pointNos = new ArrayList<>();
        pointNos.add(pointNo);
        Map<String, Object> pointValue = feignDataApi.getCurrentValue(pointNos);
        Integer value = new BigDecimal(pointValue.get(pointNo).toString()).intValue();
        return WatchConstant.TX_STS_NORMAL.equals(value);
    }
 
    public boolean isUse(String pointNo) {
        List<String> pointNos = new ArrayList<>();
        pointNos.add(pointNo);
        Map<String, Object> pointValue = feignDataApi.getCurrentValue(pointNos);
        log.info("isUse=" + pointValue.get(pointNo).toString());
        return WatchConstant.CONTROLLER_ON.equals(pointValue.get(pointNo).toString());
    }
 
    public void stopControllerSts(String controllerCode) {
        FeignWritePointValueDTO writeSts = new FeignWritePointValueDTO();
        writeSts.setPointCode(controllerCode);
        writeSts.setPointValue(WatchConstant.CONTROLLER_OFF);
        feignDataApi.setPointValue(writeSts);
    }
 
    public void startControllerSts(String controllerCode) {
        FeignWritePointValueDTO writeSts = new FeignWritePointValueDTO();
        writeSts.setPointCode(controllerCode);
        writeSts.setPointValue(WatchConstant.CONTROLLER_ON);
        feignDataApi.setPointValue(writeSts);
    }
 
    private void setStsStop(String statusPoint) {
        FeignWritePointValueDTO writeSts = new FeignWritePointValueDTO();
        writeSts.setPointCode(statusPoint);
        writeSts.setPointValue(WatchConstant.TX_STS_STOP);
        feignDataApi.setPointValue(writeSts);
    }
 
    private void setStsNormal(String statusPoint) {
        FeignWritePointValueDTO writeSts = new FeignWritePointValueDTO();
        writeSts.setPointCode(statusPoint);
        writeSts.setPointValue(WatchConstant.TX_STS_NORMAL);
        feignDataApi.setPointValue(writeSts);
    }
 
}