2024-02-27 12:53:37 +08:00
|
|
|
|
package com.zhgd.xmgl.util;
|
|
|
|
|
|
|
2024-04-10 13:36:06 +08:00
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
2024-03-05 15:16:10 +08:00
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
2024-03-01 15:28:50 +08:00
|
|
|
|
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-05-21 20:43:59 +08:00
|
|
|
|
import com.zhgd.jeecg.common.execption.OpenAlertException;
|
2024-02-27 12:53:37 +08:00
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2024-03-01 15:28:50 +08:00
|
|
|
|
import org.springframework.stereotype.Component;
|
2024-02-27 12:53:37 +08:00
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
import java.util.Map;
|
2024-04-09 17:00:12 +08:00
|
|
|
|
import java.util.Objects;
|
2024-02-27 12:53:37 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 海康接口
|
|
|
|
|
|
*/
|
|
|
|
|
|
@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
|
|
|
|
|
2024-04-16 21:19:56 +08:00
|
|
|
|
public static String doPost(String host, String path, String body, Map<String, String> querys, String appKey, String appSecret) throws Exception {
|
2024-05-11 01:58:58 +08:00
|
|
|
|
log.info("调用海康接口.url:{}", host + path);
|
|
|
|
|
|
log.info("调用海康接口.body:{}", body);
|
2024-04-16 21:19:56 +08:00
|
|
|
|
Map<String, String> headers = new HashMap();
|
|
|
|
|
|
headers.put("Accept", "*/*");
|
|
|
|
|
|
headers.put("Content-Type", "application/json");
|
2024-04-29 21:14:46 +08:00
|
|
|
|
Request request = new Request(Method.POST_STRING, host, path, appKey, appSecret, Constants.DEFAULT_TIMEOUT * 30);
|
2024-04-16 21:19:56 +08:00
|
|
|
|
request.setHeaders(headers);
|
|
|
|
|
|
request.setQuerys(querys);
|
|
|
|
|
|
request.setStringBody(body);
|
|
|
|
|
|
Response response = Client.execute(request);
|
|
|
|
|
|
String responseStr = getResponseResult(response);
|
2024-05-11 01:58:58 +08:00
|
|
|
|
log.info("调用海康接口.getResponseResult:{}", responseStr);
|
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")) {
|
|
|
|
|
|
responseStr = response.getBody();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
responseStr = response.getBody();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return responseStr;
|
|
|
|
|
|
}
|
2024-03-01 15:28:50 +08:00
|
|
|
|
|
2024-04-02 18:26:22 +08:00
|
|
|
|
/**
|
2024-04-16 21:19:56 +08:00
|
|
|
|
* 成功后,获取data结果
|
2024-04-02 18:26:22 +08:00
|
|
|
|
*
|
|
|
|
|
|
* @param rs
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
2024-03-01 15:28:50 +08:00
|
|
|
|
public static JSONObject getJSONObjectData(String rs) {
|
|
|
|
|
|
JSONObject rsJo = JSONObject.parseObject(rs);
|
2024-04-09 17:00:12 +08:00
|
|
|
|
String code = rsJo.getString("code");
|
|
|
|
|
|
if (Objects.equals(code, "0")) {
|
2024-03-01 15:28:50 +08:00
|
|
|
|
return rsJo.getJSONObject("data");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
log.error("海康解析结果失败:{}", rs);
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-03-05 15:16:10 +08:00
|
|
|
|
|
2024-04-16 21:19:56 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 成功后,获取data结果
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param rsJo
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static JSONObject getJSONObjectData(JSONObject rsJo) {
|
|
|
|
|
|
String code = rsJo.getString("code");
|
|
|
|
|
|
if (Objects.equals(code, "0")) {
|
|
|
|
|
|
return rsJo.getJSONObject("data");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
log.error("海康解析结果失败:{}", rsJo.toJSONString());
|
2024-05-21 20:43:59 +08:00
|
|
|
|
throw new OpenAlertException("海康调用失败");
|
2024-04-16 21:19:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-05 15:16:10 +08:00
|
|
|
|
public static JSONArray getJSONArrayData(String rs) {
|
2024-04-10 13:36:06 +08:00
|
|
|
|
if (StrUtil.isBlank(rs)) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2024-03-05 15:16:10 +08:00
|
|
|
|
JSONObject rsJo = JSONArray.parseObject(rs);
|
2024-04-09 17:00:12 +08:00
|
|
|
|
String code = rsJo.getString("code");
|
|
|
|
|
|
if (Objects.equals(code, "0")) {
|
2024-03-05 15:16:10 +08:00
|
|
|
|
return rsJo.getJSONArray("data");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
log.error("海康解析结果失败:{}", rs);
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-09 17:00:12 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 是否请求成功
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param rs
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static boolean isSuccess(String rs) {
|
|
|
|
|
|
JSONObject rsJo = JSONObject.parseObject(rs);
|
|
|
|
|
|
String code = rsJo.getString("code");
|
|
|
|
|
|
if (Objects.equals(code, "0")) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-16 21:19:56 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 是否请求成功
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param rsJo
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static boolean isSuccess(JSONObject rsJo) {
|
|
|
|
|
|
String code = rsJo.getString("code");
|
|
|
|
|
|
if (Objects.equals(code, "0")) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 是否请求成功
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param rsJo
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static boolean isFail(JSONObject rsJo) {
|
|
|
|
|
|
String code = rsJo.getString("code");
|
|
|
|
|
|
if (Objects.equals(code, "0")) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-05 15:16:10 +08:00
|
|
|
|
|
2024-05-30 19:52:54 +08:00
|
|
|
|
public static void addPageParamIfAbsent(JSONObject param) {
|
|
|
|
|
|
param.putIfAbsent("pageNo", 1);
|
|
|
|
|
|
param.putIfAbsent("pageSize", 1000);
|
|
|
|
|
|
}
|
2024-02-27 12:53:37 +08:00
|
|
|
|
}
|