订阅-通知是视图库接口最核心的业务,也是大华,华为和友商使用最频繁的一个功能。
上下级概念
订阅通知过程本质是数据转移,例如A想通过视图库获得B的机动车数据,那么A就是上级、B就是下级;假如A想通过B间接获得C的机动车数据,那么A就是B的上级,B是C的上级,这个过程为跨级订阅/通知。
订阅-通知
订阅-通知流程见图2
tep1:订阅者(上级)向被订阅者(下级)发送HTTP POST请求/VIID/Subscribes。
Step2:被订阅者(下级)将订阅成功与否的响应消息返回给订阅者(上级)。
订阅成功后,被订阅者(下级)如果有订阅信息,便会进行通知任务。
Step3:被订阅者(下级)向订阅者(上级)发送HTTP POST请求/VIID/SubscribeNotifications。
Step4:订阅者(上级)返回响应消息。
Step5:被订阅者(下级)接收到Step4订阅者(上级)正确返回结果才会再重复Step3,4的操作,如此循环。
要搞清楚订阅的参数含义
@AuthorizationInterceptor.UnAuthRequest
@RequestMapping("/subscribes")
public Result subscribe(
// @RequestBody SubscribeReq subscribeReq
) {
log.info("订阅接口---------->");
// 订阅统一标识码 = 公安机关机构代码 + 子类型编码(03-订阅)+ 时间编码(YYYYMMDDhhmmss) + 流水序号(00001)
String time = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now());
//123456789012
String subscribeID = "360302000000" + "03" + time + "00001";
String title = "订阅车牌识别消息";
String subscribeDetail = "13"; // 订阅类别:视频卡口目录 7--视频卡口目录 12-人脸信息 13-车辆信息
Integer ResourceClass=4;
String resourceURI ="360xxxxxxx"; // 订阅资源路径 这个参数很重要 对方提供
String applicantName = "xxxxx"; // 申请人
String applicantOrg = "xxxxxx公司"; // 申请单位
String beginTime ="20210326150000"; //DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
String endTime ="20210331230000"; //DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now().plusMinutes(5l));
String receiveAddr = "http://36.36.1.70:1314/VIID/SubscribeNotifications";
String ResultImageDeclare="01";
String ResultFeatureDeclare="-1";
Integer operateType = 0;
SubscribeRequestObject subscribeRequestObject = new SubscribeRequestObject();
SubscribeRequestObject.SubscribeListObject subscribeListObject = new SubscribeRequestObject.SubscribeListObject();
subscribeRequestObject.setSubscribeListObject(subscribeListObject);
List<SubscribeRequestObject.Subscribe> subscribeList = new ArrayList<>();
SubscribeRequestObject.Subscribe subscribe = new SubscribeRequestObject.Subscribe();
subscribe.setSubscribeID(subscribeID);
subscribe.setTitle(title);
subscribe.setSubscribeDetail(subscribeDetail);
subscribe.setResourceURI("36030220195032160250");
subscribe.setApplicantName(applicantName);
subscribe.setApplicantOrg(applicantOrg);
subscribe.setBeginTime(beginTime);
subscribe.setEndTime(endTime);
subscribe.setReceiveAddr(receiveAddr);
subscribe.setOperateType(operateType);
subscribeList.add(subscribe);
subscribeListObject.setSubscribeObject(subscribeList);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/json;charset=utf-8"));
headers.set("User-Identify","36030220195032160999");
HttpEntity<String> httpEntity = new HttpEntity<>(JSONUtil.toJsonStr(subscribeRequestObject), headers);
// log.info("消息体:{}", JSONUtil.toJsonStr(subscribeRequestObject));
// 请求执行
ResponseEntity<String> responseEntity = restTemplate.exchange(subscribeUrl, HttpMethod.POST, httpEntity, String.class);
log.info("头部消息"+responseEntity.getHeaders());
if (org.apache.http.HttpStatus.SC_OK == responseEntity.getStatusCode().value()) {
log.info("响应体"+responseEntity.getBody());
log.info(Result.success()+"订阅成功");
return Result.success();
}
log.info(responseEntity.getBody());
log.info(Result.failed()+"订阅失败");
return Result.failed("订阅失败");
}
成功后,会接收到对方推送过来的数据