From cdb2bc5f871ff9ca5b80966aa08458a12373c4be Mon Sep 17 00:00:00 2001 From: wuweida <2918581207@qq.com> Date: Tue, 14 Jun 2022 16:37:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=98=BE=E7=A4=BA=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../monitormanage/entity/PeopleRecognize.java | 4 +- .../monitormanage/service/MonitorService.java | 892 +++++++++--------- 2 files changed, 449 insertions(+), 447 deletions(-) diff --git a/src/main/java/com/hisense/monitormanage/entity/PeopleRecognize.java b/src/main/java/com/hisense/monitormanage/entity/PeopleRecognize.java index b12a3b5..6f26234 100644 --- a/src/main/java/com/hisense/monitormanage/entity/PeopleRecognize.java +++ b/src/main/java/com/hisense/monitormanage/entity/PeopleRecognize.java @@ -16,6 +16,6 @@ public class PeopleRecognize { private String channelName; private String picUrl; private Integer personNum; - @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") - private Date pushTime; + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8") + private Date insertTime; } diff --git a/src/main/java/com/hisense/monitormanage/service/MonitorService.java b/src/main/java/com/hisense/monitormanage/service/MonitorService.java index f6c179c..3445184 100644 --- a/src/main/java/com/hisense/monitormanage/service/MonitorService.java +++ b/src/main/java/com/hisense/monitormanage/service/MonitorService.java @@ -208,6 +208,453 @@ public class MonitorService { } } + @Scheduled(cron = "0 0 8 1/1 * ?") + public void construction() { + + try { + + log.info("[monitor-capture]: start capture1"); + + List labels = cameraChannelMapper.selectLabelAndChannel("1009,1008"); + labels.forEach(label -> { + String channelCode = label.getChannelCode(); + try { + BufferedImage bufferedImage = capturecreenshot(channelCode); + String fileName = channelCode + ".jpg"; + ImageIO.write(bufferedImage, "jpg", new File(fileDir + "/images/" + fileName)); + + String picUrl = picHost + "/images/" + fileName; + + ChannelPicture picture = new ChannelPicture(); + picture.setChannelCode(channelCode); + picture.setPicUrl(picUrl); + /*picture.setInsertTime(new Date()); + channelPictureMapper.insert(picture);*/ + if (channelCode == null){ + channelPictureMapper.insert(picture); + }else { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("channel_code", channelCode); + picture.setInsertTime(new Date()); + channelPictureMapper.update(picture, queryWrapper); + } + }catch (Exception e){ + + log.info( "[monitor-capture] " +channelCode + " 截图图片失败 " + e.getMessage()); + } + }); + + log.info("[monitor-capture]: end capture1"); + }catch (Exception exception){ + exception.printStackTrace(); + } + } + + @Scheduled(cron = "0 0 8/1 * * ?") + public void scenicSpotAndBathing() { + + try { + + log.info("[monitor-capture]: start capture"); + + List labels = cameraChannelMapper.selectLabelAndChannel("1006,1004"); + labels.forEach(label -> { + String channelCode = label.getChannelCode(); + try { + BufferedImage bufferedImage = capturecreenshot(channelCode); + String fileName = channelCode + ".jpg"; + ImageIO.write(bufferedImage, "jpg", new File(fileDir + "/images/" + fileName)); + + String picUrl = picHost + "/images/" + fileName; + + ChannelPicture picture = new ChannelPicture(); + picture.setChannelCode(channelCode); + picture.setPicUrl(picUrl); + /*picture.setInsertTime(new Date()); + channelPictureMapper.insert(picture);*/ + if (channelCode == null){ + channelPictureMapper.insert(picture); + }else { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("channel_code", channelCode); + picture.setInsertTime(new Date()); + channelPictureMapper.update(picture, queryWrapper); + } + }catch (Exception e){ + + log.info( "[monitor-capture] " +channelCode + " 截图图片失败 " + e.getMessage()); + } + }); + + log.info("[monitor-capture]: end capture"); + }catch (Exception exception){ + exception.printStackTrace(); + } + } + + /** + * 海水浴场人群图片算法识别 + */ + public void bathingBeach () { + try { + + log.info("[monitor-capture]: start capture"); + + List labels = cameraChannelMapper.selectLabelAndChannel("1006"); + labels.forEach(label -> { + String channelCode = label.getChannelCode(); + try { + BufferedImage bufferedImage = capturecreenshot(channelCode); + String fileName = channelCode + ".jpg"; + ImageIO.write(bufferedImage, "jpg", new File(fileDir + "/images/" + fileName)); + String picUrl = picHost + "/images/" + fileName; + //人流识别 + String personNumUrl = "http://15.72.184.10:8003/cshi_rpc/v1.0/person"; + Map personImage = new HashMap<>(); + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + ImageIO.write(bufferedImage, "png", stream); + String imgBase64 = Base64Utils.encodeToString(stream.toByteArray()); + stream.flush(); + stream.close(); + personImage.put("img_base64", imgBase64); + ResponseEntity responseEntity = restTemplate.postForEntity(personNumUrl, personImage, HashMap.class); + HashMap body = responseEntity.getBody(); + if (body != null && (Integer) body.get("code") == 200){ + Map data = (Map) body.get("data"); + log.info(picUrl); + log.info("人流识别算法" + data); + } + }catch (Exception e){ + log.info( "[monitor-capture] " +channelCode + " 截图图片失败 " + e.getMessage()); + } + }); + log.info("[monitor-capture]: end capture"); + }catch (Exception exception){ + exception.printStackTrace(); + } + } + + /** + * 安保区域人群图片算法识别并保存到数据库 + */ + @Scheduled(cron = "0 0 8/2 * * ?") + public void peopleRecognize() { + try { + log.info("[monitor-capture]: start capture"); + + List labels = cameraChannelMapper.selectLabelAndChannel("1042"); + labels.forEach(label -> { + String channelCode = label.getChannelCode(); + try { + BufferedImage bufferedImage = capturecreenshot(channelCode); + String fileName = channelCode + ".jpg"; + ImageIO.write(bufferedImage, "jpg", new File(fileDir + "/images/" + fileName)); + String picUrl = picHost + "/images/" + fileName; + //人流识别 + String personNumUrl = "http://15.72.184.10:8003/cshi_rpc/v1.0/person"; + Map personImage = new HashMap<>(); + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + ImageIO.write(bufferedImage, "png", stream); + String imgBase64 = Base64Utils.encodeToString(stream.toByteArray()); + stream.flush(); + stream.close(); + personImage.put("img_base64", imgBase64); + ResponseEntity responseEntity = restTemplate.postForEntity(personNumUrl, personImage, HashMap.class); + HashMap body = responseEntity.getBody(); + if (body != null && (Integer) body.get("code") == 200){ + Map data = (Map) body.get("data"); + Integer personNum = (Integer) data.get("person_num"); + PeopleRecognize peopleRecognize = new PeopleRecognize(); + peopleRecognize.setChannelCode(channelCode); + peopleRecognize.setChannelName(label.getChannelName()); + peopleRecognize.setPicUrl(picUrl); + peopleRecognize.setPersonNum(personNum); + if (channelCode == null){ + peopleRecognizeMapper.insert(peopleRecognize); + }else { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("channel_code", channelCode); + peopleRecognize.setInsertTime(new Date()); + peopleRecognizeMapper.update(peopleRecognize, queryWrapper); + } + } + }catch (Exception e){ + log.info( "[monitor-capture] " +channelCode + " 截图图片失败 " + e.getMessage()); + } + }); + log.info("[monitor-capture]: end capture"); + }catch (Exception exception){ + exception.printStackTrace(); + } + } + + /** + * 景区人流量识别 + */ + @Scheduled(cron = "0 0 9 * * ?") + public void startScenic() { + try { + + log.info("[monitor-capture]: startScenic capture"); + + List cameras = scenicMapper.selectCamerasInScenic(); + cameras.forEach(camera -> { + String cameraCode = camera.getCode(); + try { + BufferedImage bufferedImage = capturecreenshot(cameraCode); + String fileName = cameraCode + ".jpg"; + ImageIO.write(bufferedImage, "jpg", new File(fileDir + "/images/" + fileName)); + + String picUrl = picHost + "/images/" + fileName; + camera.setPic(picUrl); + camera.setCaptureTime(new Date()); + cameraMapper.updateById(camera); + //人流识别 + String personNumUrl = "http://15.72.184.10:8003/cshi_rpc/v1.0/person"; + Map personImage = new HashMap<>(); + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + ImageIO.write(bufferedImage, "png", stream); + String imgBase64 = Base64Utils.encodeToString(stream.toByteArray()); + stream.flush(); + stream.close(); + personImage.put("img_base64", imgBase64); + ResponseEntity responseEntity = restTemplate.postForEntity(personNumUrl, personImage, HashMap.class); + HashMap body = responseEntity.getBody(); + if (body != null && (Integer) body.get("code") == 200){ + Map data = (Map) body.get("data"); + Integer personNum = (Integer) data.get("person_num"); + CameraScenic cameraScenic = new CameraScenic(); + cameraScenic.setCameraId(cameraCode); + cameraScenic.setPersonNum(personNum); + + CameraScenic cs = cameraScenicMapper.selectById(cameraCode); + if (cs == null){ + cameraScenicMapper.insert(cameraScenic); + }else { + cameraScenicMapper.updateById(cameraScenic); + } + + + } + } + catch (Exception e){ + + log.info( "[monitor-capture] " +cameraCode + " 人流识别失败 " + e.getMessage()); + } + }); + + log.info("[monitor-capture]: startScenic end capture"); + }catch (Exception exception){ + exception.printStackTrace(); + } + } + private BufferedImage capturecreenshot(String code) throws IOException { + String url = monitorDomain + "/videoService/realmonitor/uri?scheme=HLS&channelId=" + code; + + HttpHeaders headers = new HttpHeaders(); + headers.add("X-Subject-Token",token); + + ResponseEntity forEntity = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity(headers), HashMap.class ); + + HashMap body = forEntity.getBody(); + + String file = (String) body.get("url"); + + log.debug("开始抓流:" + file); + + FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(file); + + try { + + grabber.setOption("stimoout", "10*1000*1000"); + grabber.setOption("timeout", "10*1000*1000"); + grabber.start(); + + log.debug("grabber启动成功"); + + int frameIndex = 60; + Java2DFrameConverter converter = new Java2DFrameConverter(); + while (true){ + Frame frame = grabber.grabImage(); + if (frame != null) { + log.debug("grabber 跳帧:" + frameIndex); + if (frameIndex-- > 0) continue; + BufferedImage bufferedImage = converter.getBufferedImage(frame); + return bufferedImage; + } + } + + }catch (Exception e){ + throw e; + }finally { + grabber.stop(); + grabber.release(); + } + + } + + /** + * 获取路径返回给前端 + * @param code + * @return + * @throws IOException + */ + public String fileCode(String code)throws IOException{ + String url = monitorDomain + "/videoService/realmonitor/uri?scheme=HLS&channelId=" + code; + + HttpHeaders headers = new HttpHeaders(); + headers.add("X-Subject-Token",token); + + System.out.println(token); + + ResponseEntity forEntity = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity(headers), HashMap.class ); + + HashMap body = forEntity.getBody(); + + String file = (String) body.get("url"); + + + return file; + + } + + static public List cameras = new LinkedList(); + + /** + * 调用接口获取摄像头和组织 + * @param parentId + * @param path + * @throws IOException + * @throws InterruptedException + */ + public void videoService(String parentId, String path) throws IOException, InterruptedException { + + String url = monitorDomain + "/videoService/devicesManager/deviceTree?nodeType=1&typeCode=01;0;ALL;ALL&id=" + parentId; + + HttpHeaders headers = new HttpHeaders(); + + headers.add("X-Subject-Token",token); + + ResponseEntity forEntity = null; + for (int i = 0; i < 10; i++) { + try { + forEntity = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity(headers), List.class ); + break; + }catch (Exception e){ + log.error("重试次数:{} url:{}",i,url); + log.error(e); + Thread.sleep(5*1000); + } + } + + if (forEntity == null) return; + List results = forEntity.getBody(); + +// log.debug("response {}", JSONObject.toJSONString(results)); + String sep = "->"; + + for (Map result : results) { + Boolean isParent = (Boolean) result.get("isParent"); + + if (isParent){ + CameraOrganization cameraOrganization = JSONObject.parseObject(JSONObject.toJSONString(result), CameraOrganization.class); + + cameraOrgenMapper.insert(cameraOrganization); + + videoService((String) result.get("id"),path + sep + result.get("name")); + }else { + if (result.get("channelId") != null){ + CameraChannel cameraChannel = JSONObject.parseObject(JSONObject.toJSONString(result), CameraChannel.class); + + String id = (String)result.get("orgCode"); + + if (path != null && path.startsWith(sep)) { + path = path.substring(sep.length()); + } + cameraChannel.setNodeName(path); + + cameraChannel.setParentId(id); + + cameraChannelMapper.insert(cameraChannel); + + } + + } + } + } + + //查询视频点播巡检结果 + @Scheduled(cron="0 5 6 * * ?") + public Result listChannelPlayStates(){ + //获取当前时间和前一天的UTC时间 + Map condition = new HashMap<>(); + ZonedDateTime endUTC = ZonedDateTime.now(ZoneOffset.UTC); + ZonedDateTime startUTC = endUTC.minusDays(60); + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'"); + condition.put("checkStatus",0); + condition.put("startTime",dateTimeFormatter.format(startUTC)); + condition.put("endTime",dateTimeFormatter.format(endUTC)); + //组装查询条件,经过验证pageSize设置过大也没效果,最大512 + Map map = new HashMap<>(); + map.put("condition",condition); + map.put("page",1); + map.put("pageSize",500);//最多一次512条 + //查询路径和请求头信息 + String url = monitorDomain + "/nms/api/channel/play/list"; + HttpHeaders headers = new HttpHeaders(); + headers.add("X-Subject-Token",token); + + //查询数据的页数 + int pageCount = 0; + //查询结果 + List list = new ArrayList<>(); + + HttpEntity> request = new HttpEntity<>(map,headers); + ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, JSONObject.class ); + JSONObject entityBody = responseEntity.getBody(); + if (entityBody.get("i18n") != null && entityBody.get("i18n").toString() == "error"){ + log.info("{}程序异常{}",entityBody.get("message").toString()); + } + List results = (List) entityBody.get("results"); + if(!results.isEmpty()){ + list.addAll(results); + pageCount = entityBody.getIntValue("totalCount")/500 + 1; + } + for(int i = 2;i<=pageCount;i++){ + map.put("page",i); + HttpEntity> request2 = new HttpEntity<>(map,headers); + ResponseEntity responseEntity2 = restTemplate.exchange(url, HttpMethod.POST, request2, JSONObject.class ); + JSONObject entityBody2 = responseEntity2.getBody(); + List results2 = (List) entityBody2.get("results"); + if(!results2.isEmpty()){ + list.addAll(results); + } + } +// List list1 = JSONObject.parseArray(JSONObject.toJSONString(jsonArray),CameraCheck.class); +// Map collect = list.stream().collect(Collectors.groupingBy( +// CameraCheck::getChannelCode, +// Collectors.collectingAndThen( +// Collectors.reducing((c1, c2) -> c1.getCheckTime().compareTo(c2.getCheckTime()) > 0 ? c1 : c2), Optional::get) +// ) +// ); +// + //通过set对channel_code去重 + Set set = new HashSet<>(); + + for(Map m:list){ + set.add(m.get("channelCode").toString()); + } + List lists = new ArrayList<>(set.size()); + lists.addAll(set); + //根据channel_code对t_camera_channel表的status字段进行更新(0) + List> listMap = Lists.partition(lists,100); + for(List ll:listMap){ + cameraChannelMapper.updateCameraStatus(ll); + } + log.info("{} 查询视频点播巡检成功,发现异常数量:{}",LocalDateTime.now(),set.size()); + return Result.success(); + } + /** * 特定摊点8:20抓图 */ @@ -544,451 +991,6 @@ public class MonitorService { exception.printStackTrace(); } } - @Scheduled(cron = "0 0 8 1/1 * ?") - public void construction() { - - try { - - log.info("[monitor-capture]: start capture1"); - - List labels = cameraChannelMapper.selectLabelAndChannel("1009,1008"); - labels.forEach(label -> { - String channelCode = label.getChannelCode(); - try { - BufferedImage bufferedImage = capturecreenshot(channelCode); - String fileName = channelCode + ".jpg"; - ImageIO.write(bufferedImage, "jpg", new File(fileDir + "/images/" + fileName)); - - String picUrl = picHost + "/images/" + fileName; - - ChannelPicture picture = new ChannelPicture(); - picture.setChannelCode(channelCode); - picture.setPicUrl(picUrl); - /*picture.setInsertTime(new Date()); - channelPictureMapper.insert(picture);*/ - if (channelCode == null){ - channelPictureMapper.insert(picture); - }else { - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("channel_code", channelCode); - picture.setInsertTime(new Date()); - channelPictureMapper.update(picture, queryWrapper); - } - }catch (Exception e){ - - log.info( "[monitor-capture] " +channelCode + " 截图图片失败 " + e.getMessage()); - } - }); - - log.info("[monitor-capture]: end capture1"); - }catch (Exception exception){ - exception.printStackTrace(); - } - } - - @Scheduled(cron = "0 0 8/1 * * ?") - public void scenicSpotAndBathing() { - - try { - - log.info("[monitor-capture]: start capture"); - - List labels = cameraChannelMapper.selectLabelAndChannel("1006,1004"); - labels.forEach(label -> { - String channelCode = label.getChannelCode(); - try { - BufferedImage bufferedImage = capturecreenshot(channelCode); - String fileName = channelCode + ".jpg"; - ImageIO.write(bufferedImage, "jpg", new File(fileDir + "/images/" + fileName)); - - String picUrl = picHost + "/images/" + fileName; - - ChannelPicture picture = new ChannelPicture(); - picture.setChannelCode(channelCode); - picture.setPicUrl(picUrl); - /*picture.setInsertTime(new Date()); - channelPictureMapper.insert(picture);*/ - if (channelCode == null){ - channelPictureMapper.insert(picture); - }else { - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("channel_code", channelCode); - picture.setInsertTime(new Date()); - channelPictureMapper.update(picture, queryWrapper); - } - }catch (Exception e){ - - log.info( "[monitor-capture] " +channelCode + " 截图图片失败 " + e.getMessage()); - } - }); - - log.info("[monitor-capture]: end capture"); - }catch (Exception exception){ - exception.printStackTrace(); - } - } - - /** - * 海水浴场人群图片算法识别 - */ - public void bathingBeach () { - try { - - log.info("[monitor-capture]: start capture"); - - List labels = cameraChannelMapper.selectLabelAndChannel("1006"); - labels.forEach(label -> { - String channelCode = label.getChannelCode(); - try { - BufferedImage bufferedImage = capturecreenshot(channelCode); - String fileName = channelCode + ".jpg"; - ImageIO.write(bufferedImage, "jpg", new File(fileDir + "/images/" + fileName)); - String picUrl = picHost + "/images/" + fileName; - //人流识别 - String personNumUrl = "http://15.72.184.10:8003/cshi_rpc/v1.0/person"; - Map personImage = new HashMap<>(); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - ImageIO.write(bufferedImage, "png", stream); - String imgBase64 = Base64Utils.encodeToString(stream.toByteArray()); - stream.flush(); - stream.close(); - personImage.put("img_base64", imgBase64); - ResponseEntity responseEntity = restTemplate.postForEntity(personNumUrl, personImage, HashMap.class); - HashMap body = responseEntity.getBody(); - if (body != null && (Integer) body.get("code") == 200){ - Map data = (Map) body.get("data"); - log.info(picUrl); - log.info("人流识别算法" + data); - } - }catch (Exception e){ - log.info( "[monitor-capture] " +channelCode + " 截图图片失败 " + e.getMessage()); - } - }); - log.info("[monitor-capture]: end capture"); - }catch (Exception exception){ - exception.printStackTrace(); - } - } - - /** - * 安保区域人群图片算法识别并保存到数据库 - */ - @Scheduled(cron = "0 0 8/2 * * ?") - public void peopleRecognize() { - try { - log.info("[monitor-capture]: start capture"); - - List labels = cameraChannelMapper.selectLabelAndChannel("1042"); - labels.forEach(label -> { - String channelCode = label.getChannelCode(); - try { - BufferedImage bufferedImage = capturecreenshot(channelCode); - String fileName = channelCode + ".jpg"; - ImageIO.write(bufferedImage, "jpg", new File(fileDir + "/images/" + fileName)); - String picUrl = picHost + "/images/" + fileName; - //人流识别 - String personNumUrl = "http://15.72.184.10:8003/cshi_rpc/v1.0/person"; - Map personImage = new HashMap<>(); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - ImageIO.write(bufferedImage, "png", stream); - String imgBase64 = Base64Utils.encodeToString(stream.toByteArray()); - stream.flush(); - stream.close(); - personImage.put("img_base64", imgBase64); - ResponseEntity responseEntity = restTemplate.postForEntity(personNumUrl, personImage, HashMap.class); - HashMap body = responseEntity.getBody(); - if (body != null && (Integer) body.get("code") == 200){ - Map data = (Map) body.get("data"); - Integer personNum = (Integer) data.get("person_num"); - PeopleRecognize peopleRecognize = new PeopleRecognize(); - peopleRecognize.setChannelCode(channelCode); - peopleRecognize.setChannelName(label.getChannelName()); - peopleRecognize.setPicUrl(picUrl); - peopleRecognize.setPersonNum(personNum); - peopleRecognize.setPushTime(new Date()); - PeopleRecognize id = peopleRecognizeMapper.selectById(channelCode); - if (id == null){ - peopleRecognizeMapper.insert(peopleRecognize); - }else { - peopleRecognizeMapper.updateById(peopleRecognize); - } - } - }catch (Exception e){ - log.info( "[monitor-capture] " +channelCode + " 截图图片失败 " + e.getMessage()); - } - }); - log.info("[monitor-capture]: end capture"); - }catch (Exception exception){ - exception.printStackTrace(); - } - } - - /** - * 景区人流量识别 - */ - @Scheduled(cron = "0 0 9 * * ?") - public void startScenic() { - try { - - log.info("[monitor-capture]: startScenic capture"); - - List cameras = scenicMapper.selectCamerasInScenic(); - cameras.forEach(camera -> { - String cameraCode = camera.getCode(); - try { - BufferedImage bufferedImage = capturecreenshot(cameraCode); - String fileName = cameraCode + ".jpg"; - ImageIO.write(bufferedImage, "jpg", new File(fileDir + "/images/" + fileName)); - - String picUrl = picHost + "/images/" + fileName; - camera.setPic(picUrl); - camera.setCaptureTime(new Date()); - cameraMapper.updateById(camera); - //人流识别 - String personNumUrl = "http://15.72.184.10:8003/cshi_rpc/v1.0/person"; - Map personImage = new HashMap<>(); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - ImageIO.write(bufferedImage, "png", stream); - String imgBase64 = Base64Utils.encodeToString(stream.toByteArray()); - stream.flush(); - stream.close(); - personImage.put("img_base64", imgBase64); - ResponseEntity responseEntity = restTemplate.postForEntity(personNumUrl, personImage, HashMap.class); - HashMap body = responseEntity.getBody(); - if (body != null && (Integer) body.get("code") == 200){ - Map data = (Map) body.get("data"); - Integer personNum = (Integer) data.get("person_num"); - CameraScenic cameraScenic = new CameraScenic(); - cameraScenic.setCameraId(cameraCode); - cameraScenic.setPersonNum(personNum); - - CameraScenic cs = cameraScenicMapper.selectById(cameraCode); - if (cs == null){ - cameraScenicMapper.insert(cameraScenic); - }else { - cameraScenicMapper.updateById(cameraScenic); - } - - - } - } - catch (Exception e){ - - log.info( "[monitor-capture] " +cameraCode + " 人流识别失败 " + e.getMessage()); - } - }); - - log.info("[monitor-capture]: startScenic end capture"); - }catch (Exception exception){ - exception.printStackTrace(); - } - } - private BufferedImage capturecreenshot(String code) throws IOException { - String url = monitorDomain + "/videoService/realmonitor/uri?scheme=HLS&channelId=" + code; - - HttpHeaders headers = new HttpHeaders(); - headers.add("X-Subject-Token",token); - - ResponseEntity forEntity = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity(headers), HashMap.class ); - - HashMap body = forEntity.getBody(); - - String file = (String) body.get("url"); - - log.debug("开始抓流:" + file); - - FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(file); - - try { - - grabber.setOption("stimoout", "10*1000*1000"); - grabber.setOption("timeout", "10*1000*1000"); - grabber.start(); - - log.debug("grabber启动成功"); - - int frameIndex = 60; - Java2DFrameConverter converter = new Java2DFrameConverter(); - while (true){ - Frame frame = grabber.grabImage(); - if (frame != null) { - log.debug("grabber 跳帧:" + frameIndex); - if (frameIndex-- > 0) continue; - BufferedImage bufferedImage = converter.getBufferedImage(frame); - return bufferedImage; - } - } - - }catch (Exception e){ - throw e; - }finally { - grabber.stop(); - grabber.release(); - } - - } - - /** - * 获取路径返回给前端 - * @param code - * @return - * @throws IOException - */ - public String fileCode(String code)throws IOException{ - String url = monitorDomain + "/videoService/realmonitor/uri?scheme=HLS&channelId=" + code; - - HttpHeaders headers = new HttpHeaders(); - headers.add("X-Subject-Token",token); - - System.out.println(token); - - ResponseEntity forEntity = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity(headers), HashMap.class ); - - HashMap body = forEntity.getBody(); - - String file = (String) body.get("url"); - - - return file; - - } - - static public List cameras = new LinkedList(); - - /** - * 调用接口获取摄像头和组织 - * @param parentId - * @param path - * @throws IOException - * @throws InterruptedException - */ - public void videoService(String parentId, String path) throws IOException, InterruptedException { - - String url = monitorDomain + "/videoService/devicesManager/deviceTree?nodeType=1&typeCode=01;0;ALL;ALL&id=" + parentId; - - HttpHeaders headers = new HttpHeaders(); - - headers.add("X-Subject-Token",token); - - ResponseEntity forEntity = null; - for (int i = 0; i < 10; i++) { - try { - forEntity = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity(headers), List.class ); - break; - }catch (Exception e){ - log.error("重试次数:{} url:{}",i,url); - log.error(e); - Thread.sleep(5*1000); - } - } - - if (forEntity == null) return; - List results = forEntity.getBody(); - -// log.debug("response {}", JSONObject.toJSONString(results)); - String sep = "->"; - - for (Map result : results) { - Boolean isParent = (Boolean) result.get("isParent"); - - if (isParent){ - CameraOrganization cameraOrganization = JSONObject.parseObject(JSONObject.toJSONString(result), CameraOrganization.class); - - cameraOrgenMapper.insert(cameraOrganization); - - videoService((String) result.get("id"),path + sep + result.get("name")); - }else { - if (result.get("channelId") != null){ - CameraChannel cameraChannel = JSONObject.parseObject(JSONObject.toJSONString(result), CameraChannel.class); - - String id = (String)result.get("orgCode"); - - if (path != null && path.startsWith(sep)) { - path = path.substring(sep.length()); - } - cameraChannel.setNodeName(path); - - cameraChannel.setParentId(id); - - cameraChannelMapper.insert(cameraChannel); - - } - - } - } - } - - //查询视频点播巡检结果 - @Scheduled(cron="0 5 6 * * ?") - public Result listChannelPlayStates(){ - //获取当前时间和前一天的UTC时间 - Map condition = new HashMap<>(); - ZonedDateTime endUTC = ZonedDateTime.now(ZoneOffset.UTC); - ZonedDateTime startUTC = endUTC.minusDays(60); - DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'"); - condition.put("checkStatus",0); - condition.put("startTime",dateTimeFormatter.format(startUTC)); - condition.put("endTime",dateTimeFormatter.format(endUTC)); - //组装查询条件,经过验证pageSize设置过大也没效果,最大512 - Map map = new HashMap<>(); - map.put("condition",condition); - map.put("page",1); - map.put("pageSize",500);//最多一次512条 - //查询路径和请求头信息 - String url = monitorDomain + "/nms/api/channel/play/list"; - HttpHeaders headers = new HttpHeaders(); - headers.add("X-Subject-Token",token); - - //查询数据的页数 - int pageCount = 0; - //查询结果 - List list = new ArrayList<>(); - - HttpEntity> request = new HttpEntity<>(map,headers); - ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, JSONObject.class ); - JSONObject entityBody = responseEntity.getBody(); - if (entityBody.get("i18n") != null && entityBody.get("i18n").toString() == "error"){ - log.info("{}程序异常{}",entityBody.get("message").toString()); - } - List results = (List) entityBody.get("results"); - if(!results.isEmpty()){ - list.addAll(results); - pageCount = entityBody.getIntValue("totalCount")/500 + 1; - } - for(int i = 2;i<=pageCount;i++){ - map.put("page",i); - HttpEntity> request2 = new HttpEntity<>(map,headers); - ResponseEntity responseEntity2 = restTemplate.exchange(url, HttpMethod.POST, request2, JSONObject.class ); - JSONObject entityBody2 = responseEntity2.getBody(); - List results2 = (List) entityBody2.get("results"); - if(!results2.isEmpty()){ - list.addAll(results); - } - } -// List list1 = JSONObject.parseArray(JSONObject.toJSONString(jsonArray),CameraCheck.class); -// Map collect = list.stream().collect(Collectors.groupingBy( -// CameraCheck::getChannelCode, -// Collectors.collectingAndThen( -// Collectors.reducing((c1, c2) -> c1.getCheckTime().compareTo(c2.getCheckTime()) > 0 ? c1 : c2), Optional::get) -// ) -// ); -// - //通过set对channel_code去重 - Set set = new HashSet<>(); - - for(Map m:list){ - set.add(m.get("channelCode").toString()); - } - List lists = new ArrayList<>(set.size()); - lists.addAll(set); - //根据channel_code对t_camera_channel表的status字段进行更新(0) - List> listMap = Lists.partition(lists,100); - for(List ll:listMap){ - cameraChannelMapper.updateCameraStatus(ll); - } - log.info("{} 查询视频点播巡检成功,发现异常数量:{}",LocalDateTime.now(),set.size()); - return Result.success(); - } }