1、首先是登陆注册保活
2、定义消息推送
3、订阅通知相关接口
由于网上关于这方面的文档太烂,里面遇到很多坑,专门弄一篇避免大家走弯路
首先登陆,这里查看我的其他帖子
定义消息推送
@GetMapping("/pushDrvice")
public synchronized void fetchikvisionList() throws IOException {
log.info("推送hk设备列表----->");
File file=new File(DEVICE_HK_PATH);
log.info("设备列表目录"+DEVICE_HK_PATH);
JSONObject json = new JSONObject();
JSONObject subscribeNotificationListObject = new JSONObject();
JSONArray subscribeNotificationObjectArray = new JSONArray();
JSONObject subscribeNotificationObject = new JSONObject();
subscribeNotificationObject.put("NotificationID", "361103000050042022072016385017819");
subscribeNotificationObject.put("Title", "订阅设备目录消息");
subscribeNotificationObject.put("SubscribeID", "371400000000032023061016173911218");
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
subscribeNotificationObject.put("TriggerTime", sdf.format(new Date()));
subscribeNotificationObject.put("InfoIDs", "DeviceList");
StringBuilder result = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
String s = null;
List<ElectronichkFenceDevice> list=new ArrayList<>();
//JSONObject DeviceList = new JSONObject();
//JSONObject deviceList = new JSONObject();
JSONArray deviceList = new JSONArray();
while((s = br.readLine())!=null){//使用readLine方法,一次读一行
JSONObject apeObject = new JSONObject();
String[] str=s.split(",");
apeObject.put("Name", str[0]);
apeObject.put("Port", 80);
apeObject.put("Password", "p@ssword");
apeObject.put("Model", "1716");
apeObject.put("ApeID", str[1]);
apeObject.put("MonitorAreaDesc", "监控区域说明");
apeObject.put("IPAddr", str[2]);
apeObject.put("IPV6Addr", "");
apeObject.put("Longitude", str[3]);
apeObject.put("Latitude", str[4]);
apeObject.put("PlaceCode", "371425");
apeObject.put("OrgCode", "371425000000");
apeObject.put("CapDirection", 1);
apeObject.put("MonitorDirect", "1");
apeObject.put("IsOnline", "1");
apeObject.put("OwnerApsID", "");
apeObject.put("UserId", "Administrator");
apeObject.put("Place", "xxxxx县");
apeObject.put("FunctionType", "2");
deviceList.put(apeObject);
}
br.close();
JSONObject deviceList1 = new JSONObject();
deviceList1.put("APEObject", deviceList);
subscribeNotificationObject.put("DeviceList", deviceList1);
subscribeNotificationObjectArray.put(subscribeNotificationObject);
subscribeNotificationListObject.put("SubscribeNotificationObject", subscribeNotificationObjectArray);
json.put("SubscribeNotificationListObject", subscribeNotificationListObject);
JSONObject finalObject = new JSONObject();
finalObject.put("SubscribeNotificationListObject", subscribeNotificationListObject);
//log.info("设备列表长度"+list);
//log.info("json字符串"+finalObject.toString());
electronicFenceService.batchAddElectronickFenceDevice(finalObject.toString());
}
public void batchAddElectronickFenceDevice(String jsonArray) {
try {
Map<String, String> header = new HashMap<>();
header.put("User-Identify", deviceId);
header.put("Content-Type", "application/json;charset=UTF-8");
log.info(jsonArray.toString());
Response response = HttpUtil.postJson(url + "/VIID/SubscribeNotifications",jsonArray, header);
// 需再次授权
if (response.code() == 401) {
RegisterUtil.register();
response = HttpUtil.postJson(url + "/VIID/SubscribeNotifications",jsonArray, header);
}
if (!response.isSuccessful()) {
throw new IllegalStateException("invoke electronic fence api failed. response: " + response.body().string());
}
} catch (IOException e) {
throw new IllegalStateException("invoke electronic fence api failed.", e);
}
}
}
定义消息通知,这里根据自己的业务类型定义
@RequestMapping(value = "/Subscribes", method = {RequestMethod.PUT,RequestMethod.POST})
public String Subscribes(@RequestBody JSONObject json, HttpServletRequest request, HttpServletResponse response) throws IOException {
log.info("订阅响应"+json);
// 创建JSONObject和JSONArray对象来构建JSON结构
JSONObject responseStatusObject = new JSONObject();
responseStatusObject.put("RequestURL", "http://10.54.50.187:1314/VIID/Subscribes");
responseStatusObject.put("StatusCode", 0);
responseStatusObject.put("StatusString", "正常");
responseStatusObject.put("Id", "37140020225036153919");
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
responseStatusObject.put("LocalTime", sdf.format(new Date()));
JSONArray responseStatusArray = new JSONArray();
responseStatusArray.add(responseStatusObject);
JSONObject responseStatusListObject = new JSONObject();
responseStatusListObject.put("ResponseStatusObject", responseStatusArray);
JSONObject finalObject = new JSONObject();
finalObject.put("ResponseStatusListObject", responseStatusListObject);
// 将最终的JSON对象转换为字符串
String s=finalObject.toJSONString();
// 打印结果
System.out.println(finalObject.toJSONString());
return s;
}
推送设备消息体
{
"SubscribeNotificationListObject": {
"SubscribeNotificationObject": [{
"NotificationID": "361103000050042022072016385017819",
"Title": "订阅设备目录消息",
"SubscribeID": "371400000000032023061016173911218",
"TriggerTime": "20230610164000",
"InfoIDs": "DeviceList",
"DeviceList": {
"APEObject": [{
"Name": "摄像头",
"Port": 80,
"Password": "p@ssword",
"Model": "1716",
"ApeID": "37142521001325200001",
"MonitorAreaDesc": "监控区域说明",
"IPAddr": "192.168.8.100",
"IPV6Addr": "",
"Longitude": 116.768308,
"Latitude": 36.812146,
"PlaceCode": "371425",
"OrgCode": "371425000000",
"CapDirection": 1,
"MonitorDirect": "1",
"IsOnline": "1",
"OwnerApsID": "",
"UserId": "Administrator",
"Place": "xxxxxx县"
}]
}
}]
}
}
推送人脸消息体
{
"SubscribeNotificationListObject": {
"SubscribeNotificationObject": [{
"NotificationID": "361103000050042022072016385011119",
"InfoIDs": "faceListObject",
"Title": "订阅人脸识别消息",
"SubscribeID": "371400000000032023061016173911218",
"faceListObject": {
"faceObject": [{
"sourceID": "66000000001190123456022017120110101000007",
"leftTopY": 1,
"leftTopX": 1,
"rightBtmX": 1,
"isSuspectedTerrorist": 0,
"faceID": "660000000011901234560220171201101010000070600001",
"rightBtmY": 1,
"isForeigner": 0,
"deviceID": "37142521001325200001",
"isVictim": 0,
"infoKind": 0,
"isDetainees": 0,
"isCriminalInvolved": 0,
"subImageList": {
"subImageInfoObject": [{
"imageID": "66000000001190123456022017120110101000007",
"shotTime": "20230610173607",
"eventSort": 2,
"storagePath": "http://10.54.50.187:10081/2/1032/20230608/1032_20230608235853_376680_background.jpg",
"type": "11",
"deviceID": "37142521001325200001",
"width": 358,
"fileFormat": "Jpeg",
"height": 441
},{
"imageID": "66000000001190123456022017120110101000007",
"shotTime": "20230610173607",
"eventSort": 2,
"storagePath": "http://10.54.50.187:10081/2/1032/20230608/1032_20230608235853_376680_background.jpg",
"type": "14",
"deviceID": "37142521001325200001",
"width": 358,
"fileFormat": "Jpeg",
"height": 441
}
]
},
"isSuspiciousPerson": 0
}]
},
"TriggerTime": "20230610164000"
}]
}
}
接收订阅请求消息体
{
"SubscribeObject": [{
"SubscribeID": "371400000000032023061016173911218",
"Title": "设备订阅",
"SubscribeDetail": "3",
"ResourceURI": "37140020225036153111",
"ApplicantName": "admin",
"ApplicantOrg": "d1",
"BeginTime": "20230610161150",
"EndTime": "20991231235959",
"ReceiveAddr": "http://56.176.123.8:8120/VIID/SubscribeNotifications",
"ReportInterval": 3,
"Reason": "订阅设备信息",
"OperateType": 0,
"SubscribeStatus": 0,
"ResourceClass": 4,
"ResultImageDeclare": "-1",
"ResultFeatureDeclare": 1
}]
}
其中
FaceId长度固定是48位,其中前41位是sourceId。FaceObject中sourceId应与SubImageInfoObject中type=14(底图)中的ImageId一致
souceId = 设备id(20位) + “02” (2位) + time(14位) + 序号位(5位)
faceId=sourceId + “06” + “00001”;// 子类型编码 06-人脸
SubImageInfoObject对象列表,里边放两个对象,分别是大图和小图,大图和小图对象里的Type字段分别是14和11