fuliqi
2024-09-10 2a2885f45160b4048a27e75d5be03ba9232c7363
ycl-server/src/main/java/com/ycl/thread/OnlineCheckThread.java
New file
@@ -0,0 +1,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;
    }
}