wisdomisite-java/src/main/java/com/zhgd/xmgl/util/HikvisionUtil.java
2024-04-18 17:35:38 +08:00

149 lines
4.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.zhgd.xmgl.util;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* 海康接口
*/
@Slf4j
@Component
public class HikvisionUtil {
public static String doPost(String host, String path, String body, Map<String, String> querys, String appKey, String appSecret) throws Exception {
log.info("HikvisionUtil#doPost.url:{}", host + path);
log.info("HikvisionUtil#doPost.body:{}", body);
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 request = new Request(Method.POST_STRING, host, path, appKey, appSecret, Constants.DEFAULT_TIMEOUT * 10);
request.setHeaders(headers);
request.setQuerys(querys);
request.setStringBody(body);
Response response = Client.execute(request);
String responseStr = getResponseResult(response);
log.info("HikvisionUtil#doPost.getResponseResult:{}", responseStr);
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;
}
/**
* 成功后获取data结果
*
* @param rs
* @return
*/
public static JSONObject getJSONObjectData(String rs) {
JSONObject rsJo = JSONObject.parseObject(rs);
String code = rsJo.getString("code");
if (Objects.equals(code, "0")) {
return rsJo.getJSONObject("data");
} else {
log.error("海康解析结果失败:{}", rs);
return null;
}
}
/**
* 成功后获取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());
return null;
}
}
public static JSONArray getJSONArrayData(String rs) {
if (StrUtil.isBlank(rs)) {
return null;
}
JSONObject rsJo = JSONArray.parseObject(rs);
String code = rsJo.getString("code");
if (Objects.equals(code, "0")) {
return rsJo.getJSONArray("data");
} else {
log.error("海康解析结果失败:{}", rs);
return null;
}
}
/**
* 是否请求成功
*
* @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;
}
}
/**
* 是否请求成功
*
* @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;
}
}
}