fuliqi
2024-09-11 a136c5fba98b6dea0e43a77c409ba8eddcf5b9b6
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.ycl.thread;
 
import com.ycl.platform.domain.entity.TMonitor;
import com.ycl.platform.domain.entity.WorkOrder;
import com.ycl.platform.domain.vo.OnlineThreadVO;
import com.ycl.platform.domain.vo.UpdateOnlineVO;
import com.ycl.utils.http.SelfHttpUtil;
import constant.RedisConstant;
import enumeration.ErrorType;
import enumeration.general.WorkOrderStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
 
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
 
/**
 * @author:xp
 * @date:2024/9/10 11:43
 */
@Slf4j
public class OnlineCheckThread implements Callable<OnlineThreadVO> {
 
    private TMonitor monitor;
 
    private RedisTemplate redisTemplate;
 
    private SelfHttpUtil selfHttpUtil;
 
    private Integer times;
 
    public OnlineCheckThread(TMonitor monitor, RedisTemplate redisTemplate, SelfHttpUtil selfHttpUtil, Integer times) {
        this.monitor = monitor;
        this.redisTemplate = redisTemplate;
        this.selfHttpUtil = selfHttpUtil;
        this.times = times;
    }
 
    public Integer getTimes() {
        return times;
    }
 
    public void setTimes(Integer times) {
        this.times = times;
    }
 
    public SelfHttpUtil getSelfHttpUtil() {
        return selfHttpUtil;
    }
 
    public void setSelfHttpUtil(SelfHttpUtil selfHttpUtil) {
        this.selfHttpUtil = selfHttpUtil;
    }
 
    public TMonitor getMonitor() {
        return monitor;
    }
 
    public void setMonitor(TMonitor monitor) {
        this.monitor = monitor;
    }
 
    public RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }
 
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
 
    @Override
    public OnlineThreadVO call() throws Exception {
        // 先检测能否访问该ip的网页
        ResponseEntity<Object> res = null;
        OnlineThreadVO vo = new OnlineThreadVO();
        vo.setIp(monitor.getIp());
        log.info("监测IP:" + monitor.getIp());
        String prefix = "http://";
        if ("127.0.0.1".equals(monitor.getIp())) {
            vo.setOnline(Boolean.FALSE);
            return vo;
        }
        try {
            res = selfHttpUtil.get(prefix + monitor.getIp(), null, null);
            vo.setOnline(Objects.nonNull(res) && HttpStatus.OK == res.getStatusCode());
        } catch (Exception e) {
            vo.setOnline(Boolean.FALSE);
        }
 
        // 如果http得到的不在线,那么再ping一下
        boolean reachable = false;
        if (!vo.getOnline()) {
            try {
                reachable = InetAddress.getByName(monitor.getIp()).isReachable(3000);
            } catch (IOException e) {
                e.printStackTrace();
            }
            vo.setOnline(reachable);
        }
        if (!vo.getOnline()) {
            Integer outLineTimes = (Integer) redisTemplate.opsForHash().get(RedisConstant.ONLINE_KEY, monitor.getIp());
            if (Objects.isNull(outLineTimes)) {
                outLineTimes = 1;
            } else {
                outLineTimes += 1;
            }
            redisTemplate.opsForHash().put(RedisConstant.ONLINE_KEY, monitor.getIp(), outLineTimes);
            // 一天内监测到离线1次以上,生成工单
            if (outLineTimes >= times) {
                WorkOrder workOrder = new WorkOrder();
                workOrder.setSerialNumber(monitor.getSerialNumber());
                List<String> errList = new ArrayList<>();
                errList.add(ErrorType.DEVICE_OFFLINE.getValue());
                workOrder.setErrorTypeList(errList);
                workOrder.setStatus(WorkOrderStatusEnum.DISTRIBUTED);
                vo.setWorkOrder(workOrder);
            }
        }
        return vo;
    }
 
}