| | |
| | | import com.ycl.platform.domain.result.OSDResult; |
| | | import com.ycl.platform.domain.result.UY.MonitorQualifyResult; |
| | | import com.ycl.platform.domain.result.UY.OsdCheckResult; |
| | | import com.ycl.platform.domain.vo.OnlineThreadVO; |
| | | import com.ycl.platform.mapper.TMonitorMapper; |
| | | import com.ycl.platform.service.UYErrorTypeCheckService; |
| | | import com.ycl.thread.OSDCheckThread; |
| | | import com.ycl.utils.DateUtils; |
| | | import com.ycl.utils.StringUtils; |
| | | import constant.ApiConstants; |
| | |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.ArrayBlockingQueue; |
| | | import java.util.concurrent.ExecutorService; |
| | | import java.util.concurrent.ThreadPoolExecutor; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.concurrent.*; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Slf4j |
| | | @Component("OsdTask") |
| | | public class OsdTask { |
| | | @Value("${HK.userName}") |
| | | public String HKUserName; |
| | | @Value("${HK.password}") |
| | | public String HKPassword; |
| | | @Value("${DH.userName}") |
| | | public String DHUserName; |
| | | @Value("${DH.password}") |
| | | public String DHPassword; |
| | | @Value("${YS.userName}") |
| | | public String YSUserName; |
| | | @Value("${YS.password}") |
| | | public String YSPassword; |
| | | @Autowired |
| | | private MongoTemplate mongoTemplate; |
| | | @Autowired |
| | |
| | | * 同步品牌到mysql |
| | | */ |
| | | //TODO:大华动态库linux换位置 |
| | | public void getOSD() { |
| | | public void getOSD() throws ExecutionException, InterruptedException { |
| | | log.info("开始获取OSD"); |
| | | List<OSDResult> osdResultList = new ArrayList<>(); |
| | | //查一机一档monitor |
| | | Query query = new Query(Criteria.where("mongoCreateTime").gte(DateUtils.getDayStart(new Date())).lt(DateUtils.getDayEnd(new Date()))); |
| | | List<MonitorQualifyResult> oneMachineFileResults = mongoTemplate.find(query, MonitorQualifyResult.class); |
| | |
| | | new ThreadPoolExecutor.CallerRunsPolicy() |
| | | ); |
| | | List<TMonitor> monitors = monitorMapper.selectByNumbers(serialNumbers); |
| | | //过滤ip |
| | | monitors = monitors.stream().filter(monitor -> !StringUtils.isEmpty(monitor.getIp()) && !"127.0.0.1".equals(monitor.getIp())).collect(Collectors.toList()); |
| | | // log.info("处理数据大小{}", monitors.size()); |
| | | List<Future<OSDResult>> futureList = new ArrayList<>(48); |
| | | List<OSDResult> osdResultList = new ArrayList<>(); |
| | | for (TMonitor monitor : monitors) { |
| | | executorService.submit(() -> { |
| | | if (DeviceType.HK.getType().equals(monitor.getDeviceType())) { |
| | | //海康 |
| | | OSDResult osd = HKApi.getOsdByIP(monitor.getIp(), HKUserName, HKPassword); |
| | | if (checkSuccess(osdResultList, monitor, osd)) { |
| | | log.info("海康调用成功" + osd); |
| | | return; |
| | | } |
| | | } else if (DeviceType.DH.getType().equals(monitor.getDeviceType())) { |
| | | //大华 |
| | | OSDResult osd = DHApi.getOsd(monitor.getIp(), DHUserName, DHPassword); |
| | | if (checkSuccess(osdResultList, monitor, osd)) { |
| | | log.info("大华调用成功" + osd); |
| | | return; |
| | | } |
| | | } else if (DeviceType.YS.getType().equals(monitor.getDeviceType())) { |
| | | //宇视 |
| | | OSDResult osd = YSApi.getOsd(monitor.getIp(), YSUserName, YSPassword); |
| | | if (checkSuccess(osdResultList, monitor, osd)) { |
| | | log.info("宇视调用成功" + osd); |
| | | return; |
| | | } |
| | | } |
| | | //未知品牌或者api调用失败,挨个执行所有api |
| | | OSDResult osd = tryAllApi(monitor); |
| | | if (osd != null) { |
| | | synchronized (osdResultList) { |
| | | osdResultList.add(osd); |
| | | } |
| | | } |
| | | }); |
| | | OSDCheckThread thread = new OSDCheckThread(monitor); |
| | | Future<OSDResult> future = executorService.submit(thread); |
| | | futureList.add(future); |
| | | } |
| | | //不再接收新任务 |
| | | for (Future<OSDResult> future : futureList) { |
| | | osdResultList.add(future.get()); |
| | | } |
| | | executorService.shutdown(); |
| | | try { |
| | | // 等待所有任务完成,最多等待10秒(可以根据需要调整) |
| | | if (!executorService.awaitTermination(10, TimeUnit.SECONDS)) { |
| | | // 如果超时,尝试停止当前正在执行的任务 |
| | | executorService.shutdownNow(); |
| | | } |
| | | } catch (InterruptedException e) { |
| | | // 当前线程在等待过程中被中断 |
| | | executorService.shutdownNow(); |
| | | } |
| | | |
| | | //结束api执行,开始校验 |
| | | List<OsdCheckResult> checkResults = new ArrayList<>(); |
| | | Map<String, TMonitor> monitorMap = monitors.stream().collect(Collectors.toMap(TMonitor::getSerialNumber, Function.identity())); |
| | |
| | | } |
| | | } |
| | | |
| | | private boolean checkSuccess(List<OSDResult> osdResultList, TMonitor monitor, OSDResult osd) { |
| | | if (osd != null) { |
| | | osd.setSerialNumber(monitor.getSerialNumber()); |
| | | synchronized (osdResultList) { |
| | | osdResultList.add(osd); |
| | | } |
| | | return true; |
| | | } else { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | private OSDResult tryAllApi(TMonitor monitor) { |
| | | //尝试海康的api |
| | | OSDResult hkosd = HKApi.getOsdByIP(monitor.getIp(), HKUserName, HKPassword); |
| | | if (hkosd != null) { |
| | | hkosd.setSerialNumber(monitor.getSerialNumber()); |
| | | log.info("海康调用成功" + hkosd); |
| | | return hkosd; |
| | | } |
| | | //尝试大华的api |
| | | OSDResult dhosd = DHApi.getOsd(monitor.getIp(), DHUserName, DHPassword); |
| | | if (dhosd != null) { |
| | | dhosd.setSerialNumber(monitor.getSerialNumber()); |
| | | log.info("大华调用成功" + dhosd); |
| | | return dhosd; |
| | | } |
| | | //宇视api |
| | | OSDResult ysosd = YSApi.getOsd(monitor.getIp(), YSUserName, YSPassword); |
| | | if (ysosd != null) { |
| | | ysosd.setSerialNumber(monitor.getSerialNumber()); |
| | | log.info("宇视调用成功" + ysosd); |
| | | return ysosd; |
| | | } |
| | | return null; |
| | | } |
| | | } |