| | |
| | | package com.ycl.thread; |
| | | |
| | | import com.ycl.platform.domain.entity.TMonitor; |
| | | import com.ycl.platform.domain.entity.WorkOrder; |
| | | import com.ycl.platform.domain.result.SYS.TMonitorResult; |
| | | import com.ycl.platform.domain.vo.OnlineThreadVO; |
| | | import com.ycl.platform.domain.vo.UpdateOnlineVO; |
| | | import com.ycl.utils.CheckPointUtil; |
| | | import com.ycl.utils.http.SelfHttpUtil; |
| | | import constant.RedisConstant; |
| | | import enumeration.ErrorType; |
| | |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | 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.*; |
| | | import java.util.concurrent.Callable; |
| | | |
| | | /** |
| | |
| | | * @date:2024/9/10 11:43 |
| | | */ |
| | | @Slf4j |
| | | public class OnlineCheckThread implements Callable<OnlineThreadVO> { |
| | | public class OnlineCheckThread implements Callable<TMonitorResult> { |
| | | |
| | | private TMonitor monitor; |
| | | |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | private SelfHttpUtil selfHttpUtil; |
| | | |
| | | private TMonitorResult monitor; |
| | | private CheckPointUtil checkPointUtil; |
| | | private Integer times; |
| | | |
| | | public OnlineCheckThread(TMonitor monitor, RedisTemplate redisTemplate, SelfHttpUtil selfHttpUtil, Integer times) { |
| | | public OnlineCheckThread(TMonitorResult monitor, CheckPointUtil checkPointUtil,Integer times) { |
| | | this.monitor = monitor; |
| | | this.redisTemplate = redisTemplate; |
| | | this.selfHttpUtil = selfHttpUtil; |
| | | this.checkPointUtil = checkPointUtil; |
| | | this.times = times; |
| | | } |
| | | |
| | |
| | | this.times = times; |
| | | } |
| | | |
| | | public SelfHttpUtil getSelfHttpUtil() { |
| | | return selfHttpUtil; |
| | | } |
| | | |
| | | public void setSelfHttpUtil(SelfHttpUtil selfHttpUtil) { |
| | | this.selfHttpUtil = selfHttpUtil; |
| | | } |
| | | |
| | | public TMonitor getMonitor() { |
| | | public TMonitorResult getMonitor() { |
| | | return monitor; |
| | | } |
| | | |
| | | public void setMonitor(TMonitor monitor) { |
| | | public void setMonitor(TMonitorResult monitor) { |
| | | this.monitor = monitor; |
| | | } |
| | | |
| | | public RedisTemplate getRedisTemplate() { |
| | | return redisTemplate; |
| | | public CheckPointUtil getCheckPointUtil() { |
| | | return checkPointUtil; |
| | | } |
| | | |
| | | public void setRedisTemplate(RedisTemplate redisTemplate) { |
| | | this.redisTemplate = redisTemplate; |
| | | public void setCheckPointUtil(CheckPointUtil checkPointUtil) { |
| | | this.checkPointUtil = checkPointUtil; |
| | | } |
| | | |
| | | @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; |
| | | public TMonitorResult call() { |
| | | TMonitorResult result = checkPointUtil.check(monitor); |
| | | // 一天内监测到离线1次以上,生成工单 |
| | | if (result.getOffLineCount() >= times) { |
| | | WorkOrder workOrder = new WorkOrder(); |
| | | workOrder.setSerialNumber(result.getNo()); |
| | | List<String> errList = new ArrayList<>(); |
| | | errList.add(ErrorType.DEVICE_OFFLINE.getValue()); |
| | | workOrder.setErrorTypeList(errList); |
| | | workOrder.setStatus(WorkOrderStatusEnum.DISTRIBUTED); |
| | | result.setWorkOrder(workOrder); |
| | | } |
| | | 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; |
| | | return result; |
| | | } |
| | | |
| | | } |