182 lines
5.8 KiB
Java
182 lines
5.8 KiB
Java
package com.zhgd.xmgl.util;
|
||
|
||
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 com.zhgd.jeecg.common.execption.OpenAlertException;
|
||
import com.zhgd.xmgl.modules.project.entity.Project;
|
||
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 JSONObject doPostRtObj(String host, String path, String body, Map<String, String> querys, String appKey, String appSecret) throws Exception {
|
||
return JSONObject.parseObject(doPost(host, path, body, querys, appKey, appSecret));
|
||
}
|
||
|
||
public static String doPost(String host, String path, String body, Map<String, String> querys, String appKey, String appSecret) throws Exception {
|
||
log.info("调用海康接口.url:{}", host + path);
|
||
log.info("调用海康接口.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 * 30);
|
||
request.setHeaders(headers);
|
||
request.setQuerys(querys);
|
||
request.setStringBody(body);
|
||
Response response = Client.execute(request);
|
||
String responseStr = getResponseResult(response);
|
||
log.info("调用海康接口.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);
|
||
throw new OpenAlertException("海康返回错误码");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 成功后,获取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());
|
||
throw new OpenAlertException("海康返回错误码");
|
||
}
|
||
}
|
||
|
||
public static JSONArray getJSONArrayData(String rs) {
|
||
JSONObject rsJo = JSONArray.parseObject(rs);
|
||
String code = rsJo.getString("code");
|
||
if (Objects.equals(code, "0")) {
|
||
return rsJo.getJSONArray("data");
|
||
} else {
|
||
log.error("海康返回错误码:{}", rs);
|
||
throw new OpenAlertException("海康返回错误码");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 是否请求成功
|
||
*
|
||
* @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;
|
||
}
|
||
}
|
||
|
||
|
||
public static void addPageParamIfAbsent(JSONObject param) {
|
||
param.putIfAbsent("pageNo", 1);
|
||
param.putIfAbsent("pageSize", 1000);
|
||
}
|
||
|
||
/**
|
||
* 查询车辆列表v2
|
||
*
|
||
* @param project
|
||
* @return
|
||
*/
|
||
public static JSONObject getCarList(Project project, JSONObject param) throws Exception {
|
||
if (project == null || !Objects.equals(project.getSyncHikvision(), 1)) {
|
||
return null;
|
||
}
|
||
final String ARTEMIS_PATH = "/artemis";
|
||
final String path = ARTEMIS_PATH + "/api/resource/v2/vehicle/advance/vehicleList";
|
||
String host = "https://" + project.getArtemisConfigHost();
|
||
return doPostRtObj(host, path, param.toJSONString(), null, project.getArtemisConfigAppKey(), project.getArtemisConfigAppSecret());
|
||
}
|
||
|
||
/**
|
||
* 查询车辆分类
|
||
*/
|
||
public static JSONArray categorySearch(Project project) throws Exception {
|
||
final String ARTEMIS_PATH = "/artemis";
|
||
final String path = ARTEMIS_PATH + "/api/pms/v1/car/category/search";
|
||
String host = "https://" + project.getArtemisConfigHost();
|
||
JSONObject jo = new JSONObject();
|
||
String rs = doPost(host, path, jo.toJSONString(), null, project.getArtemisConfigAppKey(), project.getArtemisConfigAppSecret());
|
||
return getJSONArrayData(rs);
|
||
}
|
||
}
|