| | |
| | | import java.io.IOException; |
| | | import java.net.InetAddress; |
| | | import java.net.UnknownHostException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | |
| | | /** |
| | |
| | | |
| | | /** |
| | | * 监测点位在线工具类 |
| | | * |
| | | * @return |
| | | */ |
| | | public TMonitorResult check(TMonitorResult monitor) { |
| | | public TMonitorResult check(TMonitorResult monitor,Integer times) { |
| | | // TMonitorResult(deptId=202, ip=51.95.48.18, online=0, |
| | | // onlineStr=null, pingOnline=true, pingOnlineStr=null, |
| | | // checkCount=1, offLineCount=0, offLineTimeStr=null, |
| | | // monitorType=3, name=DX_R三中大门前路段左侧人行道2_全景, |
| | | // workOrder=null, pointId=null, createWorkOrder=null, dynamicColumnList=null), |
| | | |
| | | // 先检测能否访问该ip的网页 |
| | | ResponseEntity<Object> res = null; |
| | | log.info("监测IP:" + monitor.getIp()); |
| | | ResponseEntity<String> res = null; |
| | | String prefix = "http://"; |
| | | if ("127.0.0.1".equals(monitor.getIp())) { |
| | | monitor.setOnline(Boolean.FALSE); |
| | | monitor.setPingOnline(Boolean.FALSE); |
| | | log.error("ip有误"+monitor.getIp()); |
| | | return monitor; |
| | | } |
| | | try { |
| | | res = selfHttpUtil.get(prefix + monitor.getIp(), null, null); |
| | | monitor.setOnline(Objects.nonNull(res) && HttpStatus.OK == res.getStatusCode()); |
| | | monitor.setPingOnline(Objects.nonNull(res) && HttpStatus.OK == res.getStatusCode()); |
| | | } catch (Exception e) { |
| | | monitor.setOnline(Boolean.FALSE); |
| | | monitor.setPingOnline(Boolean.FALSE); |
| | | } |
| | | // 如果http得到的不在线,那么再ping一下 |
| | | boolean reachable = false; |
| | | Integer checkTimes = 1; |
| | | Integer offLineTimes = 0; |
| | | Map<String, Object> map = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstant.ONLINE_KEY, monitor.getIp()); |
| | | if (!CollectionUtils.isEmpty(map)) { |
| | | Integer continueOffTimes = 0; |
| | | List<String> offTimeList = new ArrayList<>(); |
| | | // 从Redis获取数据时进行类型安全的转换 |
| | | Map<String, Object> map = null; |
| | | Object mapObj = redisTemplate.opsForHash().get(RedisConstant.ONLINE_KEY, monitor.getNo()); |
| | | if (mapObj != null) { |
| | | map = (Map<String, Object>) mapObj; |
| | | checkTimes = (Integer) map.get("checkTimes") + 1; |
| | | offLineTimes = (Integer) map.get("offLineTimes"); |
| | | continueOffTimes = (Integer) map.get("continueOffTimes"); |
| | | Object offTimeListObj = map.get("offTimeList"); |
| | | if (offTimeListObj instanceof List) { |
| | | offTimeList = new ArrayList<>((List<String>) offTimeListObj); |
| | | } |
| | | } else { |
| | | map = new HashMap<>(); |
| | | } |
| | | if (!monitor.getOnline()) { |
| | | try { |
| | | reachable = InetAddress.getByName(monitor.getIp()).isReachable(3000); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | monitor.setOnline(reachable); |
| | | if (!monitor.getPingOnline()) { |
| | | reachable = checkPing(monitor, reachable); |
| | | monitor.setPingOnline(reachable); |
| | | } |
| | | if (!monitor.getOnline()) { |
| | | if (!monitor.getPingOnline()) { |
| | | offLineTimes++; |
| | | continueOffTimes++; |
| | | //记录离线时间 |
| | | Date now = new Date(); |
| | | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | offTimeList.add(dateFormat.format(now)); |
| | | monitor.setOffLineTimeStr(offTimeList); |
| | | //到达产生工单的阈值次数 |
| | | if (continueOffTimes>=times) { |
| | | //产生了工单才会存储离线时间,存储最近一次产生工单的这几个离线时间点 |
| | | monitor.setCreateWorkOrder(Boolean.TRUE); |
| | | //产生了一次工单则清除 |
| | | continueOffTimes = 0; |
| | | } |
| | | }else { |
| | | //如果在线了,清空连续离线次数,清空离线时间 |
| | | continueOffTimes = 0; |
| | | } |
| | | map.put("checkTimes", checkTimes); |
| | | map.put("offLineTimes", offLineTimes); |
| | | redisTemplate.opsForHash().put(RedisConstant.ONLINE_KEY, monitor.getIp(), map); |
| | | map.put("continueOffTimes", continueOffTimes); |
| | | map.put("offTimeList", offTimeList); |
| | | redisTemplate.opsForHash().put(RedisConstant.ONLINE_KEY, monitor.getNo(), map); |
| | | monitor.setCheckCount(checkTimes); |
| | | monitor.setOffLineCount(offLineTimes); |
| | | |
| | | return monitor; |
| | | } |
| | | |
| | | private boolean checkPing(TMonitorResult monitor, boolean reachable) { |
| | | try { |
| | | int[] sleepTimes = {5000, 15000, 30000}; |
| | | for (int sleepTime : sleepTimes) { |
| | | reachable = InetAddress.getByName(monitor.getIp()).isReachable(5000); |
| | | if (reachable) { |
| | | break; |
| | | } |
| | | Thread.sleep(sleepTime); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("Ping异常",e); |
| | | } |
| | | return reachable; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 监测点位在线 |
| | | * |
| | | * @return |
| | | */ |
| | | public CheckResult webCheck(CheckUtil checkUtil) { |
| | |
| | | } |
| | | boolean webReachable = false; |
| | | try { |
| | | ResponseEntity<Object> res = selfHttpUtil.get(prefix + checkUtil.getIp(), null, null); |
| | | ResponseEntity<String> res = selfHttpUtil.get(prefix + checkUtil.getIp(), null, null); |
| | | webReachable = Objects.nonNull(res) && HttpStatus.OK == res.getStatusCode(); |
| | | } catch (Exception e) { |
| | | log.info("检测web异常" + e.getMessage()); |
| | | webReachable = Boolean.FALSE; |
| | | } |
| | | // ping |
| | |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | String imgUrl = ""; |
| | | // 查出国标设备,就一条数据 |
| | | List<DeviceInfo> gbDevices = new LambdaQueryChainWrapper<>(deviceInfoMapper) |
| | | .orderByDesc(DeviceInfo::getUpdateTime) |
| | | .last("limit 1") |
| | | .list(); |
| | | if (! CollectionUtils.isEmpty(gbDevices)) { |
| | | try { |
| | | imgUrl = workOrderService.getFrameImgByDevice(gbDevices.get(0).getDeviceId(), checkUtil.getSerialNumber(), IdUtils.workOrderNO(new Date(), "99999")); |
| | | result.setImg(imgUrl); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | if (pingReachable || webReachable) { |
| | | // 查出国标设备,就一条数据 |
| | | List<DeviceInfo> gbDevices = new LambdaQueryChainWrapper<>(deviceInfoMapper) |
| | | .orderByDesc(DeviceInfo::getUpdateTime) |
| | | .last("limit 1") |
| | | .list(); |
| | | if (!CollectionUtils.isEmpty(gbDevices)) { |
| | | try { |
| | | imgUrl = workOrderService.getFrameImgByDevice(gbDevices.get(0).getDeviceId(), checkUtil.getSerialNumber(), IdUtils.workOrderNO(new Date(), "99999")); |
| | | result.setImg(imgUrl); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | String status = ""; |
| | | if (! webReachable) { |
| | | if (!webReachable) { |
| | | status += "设备web访问失败;"; |
| | | } else if (webReachable) { |
| | | status += "设备web访问正常;"; |
| | | } |
| | | if (! pingReachable) { |
| | | |
| | | if (!pingReachable) { |
| | | status += "设备ip未ping通;"; |
| | | } else if (pingReachable) { |
| | | status += "设备ipPing正常;"; |
| | | } |
| | | |
| | | if (StringUtils.isEmpty(imgUrl)) { |
| | | status += "未获取到图片"; |
| | | } else { |
| | | status += "获取图片正常"; |
| | | } |
| | | result.setStatus(status); |
| | | return result; |
| | |
| | | e.printStackTrace(); |
| | | } |
| | | String status = ""; |
| | | if (! pingReachable) { |
| | | if (!pingReachable) { |
| | | status += "ip未ping通;"; |
| | | } else { |
| | | status += "成功"; |
| | | } |
| | | result.setStatus(status); |
| | | return result; |