2024-02-27 12:53:37 +08:00
|
|
|
package com.zhgd.xmgl.util;
|
|
|
|
|
|
2024-03-01 15:28:50 +08:00
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
2024-02-27 12:53:37 +08:00
|
|
|
import com.hikvision.artemis.sdk.Client;
|
|
|
|
|
import com.hikvision.artemis.sdk.Request;
|
|
|
|
|
import com.hikvision.artemis.sdk.Response;
|
|
|
|
|
import com.hikvision.artemis.sdk.constant.Constants;
|
|
|
|
|
import com.hikvision.artemis.sdk.enums.Method;
|
2024-03-01 15:28:50 +08:00
|
|
|
import com.zhgd.xmgl.modules.basicdata.entity.HikvisionRequestRetry;
|
|
|
|
|
import com.zhgd.xmgl.modules.basicdata.mapper.HikvisionRequestRetryMapper;
|
2024-02-27 12:53:37 +08:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2024-03-01 15:28:50 +08:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
2024-02-27 12:53:37 +08:00
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 海康接口
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
2024-03-01 15:28:50 +08:00
|
|
|
@Component
|
2024-02-27 12:53:37 +08:00
|
|
|
public class HikvisionUtil {
|
2024-03-01 15:28:50 +08:00
|
|
|
|
|
|
|
|
private static HikvisionRequestRetryMapper hikvisionRequestRetryMapper;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
public void setHikvisionRequestRetryMapper(HikvisionRequestRetryMapper hikvisionRequestRetryMapper) {
|
|
|
|
|
HikvisionUtil.hikvisionRequestRetryMapper = hikvisionRequestRetryMapper;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 12:53:37 +08:00
|
|
|
public static String doPost(String host, String path, String body, Map<String, String> querys, String appKey, String appSecret) {
|
|
|
|
|
log.info("HikvisionUtil#doPost.url:{}", host + path);
|
2024-03-05 09:37:46 +08:00
|
|
|
log.info("HikvisionUtil#doPost.body:{}", body);
|
2024-02-27 12:53:37 +08:00
|
|
|
String responseStr = null;
|
|
|
|
|
try {
|
|
|
|
|
Map<String, String> headers = new HashMap();
|
|
|
|
|
headers.put("Accept", "*/*");
|
|
|
|
|
headers.put("Content-Type", "application/json");
|
|
|
|
|
Request request = new Request(Method.POST_STRING, host, path, appKey, appSecret, Constants.DEFAULT_TIMEOUT);
|
|
|
|
|
request.setHeaders(headers);
|
|
|
|
|
request.setQuerys(querys);
|
|
|
|
|
request.setStringBody(body);
|
|
|
|
|
Response response = Client.execute(request);
|
|
|
|
|
responseStr = getResponseResult(response);
|
|
|
|
|
log.info("HikvisionUtil#doPost.getResponseResult:{}", responseStr);
|
|
|
|
|
} catch (Exception var10) {
|
|
|
|
|
var10.printStackTrace();
|
2024-03-01 15:28:50 +08:00
|
|
|
HikvisionRequestRetry entity = new HikvisionRequestRetry();
|
|
|
|
|
entity.setUrl(host + path);
|
|
|
|
|
entity.setHost(host);
|
|
|
|
|
entity.setPath(path);
|
|
|
|
|
entity.setMethod(2);
|
|
|
|
|
entity.setParam(JSON.toJSONString(querys));
|
|
|
|
|
entity.setBody(body);
|
|
|
|
|
entity.setLastResult(null);
|
|
|
|
|
entity.setRequestNum(1);
|
|
|
|
|
entity.setAppKey(appKey);
|
|
|
|
|
entity.setAppSecret(appSecret);
|
|
|
|
|
hikvisionRequestRetryMapper.insert(entity);
|
2024-02-27 12:53:37 +08:00
|
|
|
}
|
|
|
|
|
return responseStr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String getResponseResult(Response response) {
|
|
|
|
|
String responseStr = null;
|
|
|
|
|
int statusCode = response.getStatusCode();
|
|
|
|
|
if (!String.valueOf(statusCode).startsWith("2") && !String.valueOf(statusCode).startsWith("3")) {
|
|
|
|
|
//String msg = response.getErrorMessage();
|
|
|
|
|
responseStr = response.getBody();
|
|
|
|
|
} else {
|
|
|
|
|
responseStr = response.getBody();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return responseStr;
|
|
|
|
|
}
|
2024-03-01 15:28:50 +08:00
|
|
|
|
|
|
|
|
public static JSONObject getJSONObjectData(String rs) {
|
|
|
|
|
JSONObject rsJo = JSONObject.parseObject(rs);
|
|
|
|
|
Integer code = rsJo.getInteger("code");
|
|
|
|
|
if (code == 0) {
|
|
|
|
|
return rsJo.getJSONObject("data");
|
|
|
|
|
} else {
|
|
|
|
|
log.error("海康解析结果失败:{}", rs);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-27 12:53:37 +08:00
|
|
|
}
|