文中登录 退出登录 长连接和海康hCNetSDK等接口 见文章 初始SDK和登录
/**
* 删除人脸
IotCommDataResult 自定义类 收集结果
*/
@Override
public List<IotCommDataResult> deleteFace(IotCameraParam camera, Collection<Long> userIds) {
//登录设备
int lUserID = login_V40(camera.getIp(), new Short(String.valueOf(camera.getPort())), camera.getUsername(), camera.getPassword());
if (lUserID == -1) {
log.warn("登录失败,错误码为 :{}", hCNetSDK.NET_DVR_GetLastError());
return userIds.stream().map(id -> IotCommDataResult.FAIL_RESULT(id)).collect(Collectors.toList());
}
if (CollUtil.isEmpty(userIds)) {
return Collections.emptyList();
}
List<Long> userIdList = new ArrayList<>(userIds);
List<IotCommDataResult> commDataResults = new ArrayList<>();
String deleteUserUrl = "PUT /ISAPI/AccessControl/UserInfoDetail/Delete?format=json";
String getDeleteProcessUrl = "GET /ISAPI/AccessControl/UserInfoDetail/DeleteProcess?format=json";
// 删除可以通过组装数据 进行批量删除。我这边采用的是循环删除
for (int i = 0; i < userIdList.size(); i++) {
//删除用户信息
Boolean aBoolean = this.delUserFace(userIdList.get(i), deleteUserUrl, lUserID);
if (!aBoolean) {
commDataResults.add(IotCommDataResult.FAIL_RESULT(userIdList.get(i)));
continue;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
log.warn("配置等待异常 userid: {}", userIdList.get(i));
}
String deleteResult = this.delUserFaceRespon(getDeleteProcessUrl, lUserID);
if (StrUtil.isEmpty(deleteResult)) {
commDataResults.add(IotCommDataResult.FAIL_RESULT(userIdList.get(i)));
continue;
}
JSONObject jsonObjectRespon = JSONUtil.parseObj(deleteResult);
JSONObject jsonObjectData = jsonObjectRespon.getJSONObject("UserInfoDetailDeleteProcess");
String process = jsonObjectData.getStr("status");
log.info("process :{}", process);
if ("processing".equals(process)) {
log.info("正在删除");
int frequency = 0;
process = this.analysisDelData(getDeleteProcessUrl, deleteResult, frequency, lUserID);
}
if ("success".equals(process)) {
log.info("删除成功");
commDataResults.add(IotCommDataResult.COMMON_SUCCESS_RESULT(userIdList.get(i), "删除成功"));
} else if ("failed".equals(process)) {
log.info("删除失败");
commDataResults.add(IotCommDataResult.COMMON_FAIL_RESULT(userIdList.get(i), "删除失败"));
}
}
//退出登录
logout(lUserID);
return commDataResults;
}
/**
* 解析删除数据
*/
private String analysisDelData(String getDeleteProcessUrl, String deleteResult, int frequency, int lUserID) {
JSONObject jsonObjectRespon = JSONUtil.parseObj(deleteResult);
JSONObject jsonObjectData = jsonObjectRespon.getJSONObject("UserInfoDetailDeleteProcess");
String process = jsonObjectData.getStr("status");
log.info("process :{}", process);
if ("processing".equals(process)) {
log.info("正在删除");
if (frequency >= 3) {
return "failed";
}
frequency = frequency + 1;
try {
Thread.sleep(200);
String result = delUserFaceRespon(getDeleteProcessUrl, lUserID);
this.analysisDelData(getDeleteProcessUrl, result, frequency, lUserID);
} catch (InterruptedException e) {
log.warn("休眠异常 ", e);
}
}
if ("success".equals(process)) {
log.info("删除成功");
return process;
} else if ("failed".equals(process)) {
log.info("删除失败");
return process;
}
return null;
}
/**
* 执行删除操作
* userId
*/
private Boolean delUserFace(Long userId, String deleteUserUrl, int lUserID) {
JSONObject jsonData = new JSONObject();
JSONObject userInfoDetail = new JSONObject();
JSONArray employeeNoList = new JSONArray();
userInfoDetail.set("mode", "byEmployeeNo"); //通过用户编号删除
JSONObject jsonObject = new JSONObject();
jsonObject.set("employeeNo", String.valueOf(userId));
employeeNoList.put(jsonObject);
userInfoDetail.set("EmployeeNoList", employeeNoList);// 组装成集合 多个employeeNo
jsonData.set("UserInfoDetail", userInfoDetail);
String toJsonData = JSONUtil.toJsonStr(jsonData);
NET_DVR_XML_CONFIG_INPUT struXMLInput = new NET_DVR_XML_CONFIG_INPUT();
struXMLInput.read();
BYTE_ARRAY stringRequest = new BYTE_ARRAY(1024);
stringRequest.read();
//输入ISAPI协议命令
System.arraycopy(deleteUserUrl.getBytes(), 0, stringRequest.byValue, 0, deleteUserUrl.length());
stringRequest.write();
struXMLInput.dwSize = struXMLInput.size();
struXMLInput.lpRequestUrl = stringRequest.getPointer();
struXMLInput.dwRequestUrlLen = deleteUserUrl.length();
BYTE_ARRAY ptrInBuffer = new BYTE_ARRAY(toJsonData.length());
ptrInBuffer.read();
System.arraycopy(toJsonData.getBytes(), 0, ptrInBuffer.byValue, 0, toJsonData.length());
ptrInBuffer.write();
struXMLInput.lpInBuffer = ptrInBuffer.getPointer();
struXMLInput.dwInBufferSize = toJsonData.length();
struXMLInput.write();
BYTE_ARRAY stringXMLOut = new BYTE_ARRAY(8 * 1024);
stringXMLOut.read();
BYTE_ARRAY struXMLStatus = new BYTE_ARRAY(1024);
struXMLStatus.read();
NET_DVR_XML_CONFIG_OUTPUT struXMLOutput = new NET_DVR_XML_CONFIG_OUTPUT();
struXMLOutput.read();
struXMLOutput.dwSize = struXMLOutput.size();
struXMLOutput.lpOutBuffer = stringXMLOut.getPointer();
struXMLOutput.dwOutBufferSize = stringXMLOut.size();
struXMLOutput.lpStatusBuffer = struXMLStatus.getPointer();
struXMLOutput.dwStatusSize = struXMLStatus.size();
struXMLOutput.write();
boolean stdxmlConfig = hCNetSDK.NET_DVR_STDXMLConfig(lUserID, struXMLInput, struXMLOutput);
if (!stdxmlConfig) {
log.warn("NET_DVR_STDXMLConfig失败,错误号:{}", hCNetSDK.NET_DVR_GetLastError());
}
return stdxmlConfig;
}
/**
* 获取删除结果
*/
private String delUserFaceRespon(String getDeleteProcessUrl, int lUserID) {
NET_DVR_XML_CONFIG_INPUT struXMLInput = new NET_DVR_XML_CONFIG_INPUT();
struXMLInput.read();
BYTE_ARRAY stringRequest = new BYTE_ARRAY(1024);
stringRequest.read();
//输入ISAPI协议命令
System.arraycopy(getDeleteProcessUrl.getBytes(), 0, stringRequest.byValue, 0, getDeleteProcessUrl.length());
stringRequest.write();
struXMLInput.dwSize = struXMLInput.size();
struXMLInput.lpRequestUrl = stringRequest.getPointer();
struXMLInput.dwRequestUrlLen = getDeleteProcessUrl.length();
struXMLInput.lpInBuffer = null;
struXMLInput.dwInBufferSize = 0;
struXMLInput.write();
BYTE_ARRAY stringXMLOut = new BYTE_ARRAY(8 * 1024);
stringXMLOut.read();
BYTE_ARRAY struXMLStatus = new BYTE_ARRAY(1024);
struXMLStatus.read();
NET_DVR_XML_CONFIG_OUTPUT struXMLOutput = new NET_DVR_XML_CONFIG_OUTPUT();
struXMLOutput.read();
struXMLOutput.dwSize = struXMLOutput.size();
struXMLOutput.lpOutBuffer = stringXMLOut.getPointer();
struXMLOutput.dwOutBufferSize = stringXMLOut.size();
struXMLOutput.lpStatusBuffer = struXMLStatus.getPointer();
struXMLOutput.dwStatusSize = struXMLStatus.size();
struXMLOutput.write();
if (!hCNetSDK.NET_DVR_STDXMLConfig(lUserID, struXMLInput, struXMLOutput)) {
int iErr = hCNetSDK.NET_DVR_GetLastError();
log.warn("NET_DVR_STDXMLConfig失败,错误号 :{} ,url:{}", iErr, getDeleteProcessUrl);
return null;
} else {
stringXMLOut.read();
log.info("输出文本大小:{}", struXMLOutput.dwReturnedXMLSize);
//打印输出XML文本
String strOutXML = new String(stringXMLOut.byValue).trim();
log.info("delUserFaceRespon strOutXML:{}", strOutXML);
struXMLStatus.read();
String strStatus = new String(struXMLStatus.byValue).trim();
log.info("delUserFaceRespon strStatus:{}", strStatus);
return strOutXML;
}
}