403 lines
15 KiB
Java
403 lines
15 KiB
Java
package com.zhgd.xmgl.util;
|
||
|
||
import com.alibaba.fastjson.JSON;
|
||
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.jeecg.common.execption.OpenPromptException;
|
||
import com.zhgd.xmgl.base.HikvisionOrganization;
|
||
import com.zhgd.xmgl.base.HikvisionReservationCarInfo;
|
||
import com.zhgd.xmgl.modules.project.entity.Project;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.springframework.stereotype.Component;
|
||
|
||
import java.util.*;
|
||
|
||
/**
|
||
* 海康接口
|
||
*/
|
||
@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 getFixCarList(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());
|
||
}
|
||
|
||
/**
|
||
* 查询车辆列表v2-固定车辆
|
||
*
|
||
* @param carNumber
|
||
* @param project
|
||
* @param pageNo
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static JSONObject getFixCarListByCarNumber(String carNumber, Project project, Integer pageNo) throws Exception {
|
||
final String ARTEMIS_PATH = "/artemis";
|
||
final String path = ARTEMIS_PATH + "/api/resource/v2/vehicle/advance/vehicleList";
|
||
String host = "https://" + project.getArtemisConfigHost();
|
||
JSONObject jo = new JSONObject();
|
||
//模糊查询
|
||
jo.put("plateNo", carNumber);
|
||
jo.put("pageNo", pageNo);
|
||
jo.put("pageSize", 1000);
|
||
return doPostRtObj(host, path, jo.toJSONString(), null, project.getArtemisConfigAppKey(), project.getArtemisConfigAppSecret());
|
||
}
|
||
|
||
/**
|
||
* 查询车辆分类
|
||
*/
|
||
public static JSONArray getCategoryList(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);
|
||
}
|
||
|
||
/**
|
||
* 车辆群组绑定
|
||
*
|
||
* @param project
|
||
* @param param
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static JSONObject bindCarCategory(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/pms/v1/car/categoryBind";
|
||
String host = "https://" + project.getArtemisConfigHost();
|
||
return doPostRtObj(host, path, param.toJSONString(), null, project.getArtemisConfigAppKey(), project.getArtemisConfigAppSecret());
|
||
}
|
||
|
||
/**
|
||
* 删除人脸
|
||
*
|
||
* @param project
|
||
* @param workerFaceId
|
||
* @return
|
||
*/
|
||
public static JSONObject deleteWorkerFace(Project project, String workerFaceId) throws Exception {
|
||
final String ARTEMIS_PATH = "/artemis";
|
||
final String path = ARTEMIS_PATH + "/api/resource/v1/face/single/delete";
|
||
String host = "https://" + project.getArtemisConfigHost();
|
||
JSONObject jo = new JSONObject();
|
||
jo.put("faceId", workerFaceId);
|
||
return JSON.parseObject(doPost(host, path, jo.toJSONString(), null, project.getArtemisConfigAppKey(), project.getArtemisConfigAppSecret()));
|
||
|
||
}
|
||
|
||
/**
|
||
* 删除人员
|
||
*
|
||
* @param uniqueId
|
||
* @param project
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static JSONObject deleteWorkerById(String uniqueId, Project project) throws Exception {
|
||
final String ARTEMIS_PATH = "/artemis";
|
||
final String path = ARTEMIS_PATH + "/api/resource/v1/person/batch/delete";
|
||
String host = "https://" + project.getArtemisConfigHost();
|
||
JSONObject jsonBody = new JSONObject();
|
||
jsonBody.put("personIds", Arrays.asList(uniqueId));
|
||
String body = jsonBody.toJSONString();
|
||
return doPostRtObj(host, path, body, null, project.getArtemisConfigAppKey(), project.getArtemisConfigAppSecret());
|
||
}
|
||
|
||
/**
|
||
* 根据人员唯一字段获取人员详细信息
|
||
*
|
||
* @param project
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static JSONObject queryWorkerByCondition(Project project, JSONObject param) throws Exception {
|
||
final String ARTEMIS_PATH = "/artemis";
|
||
final String path = ARTEMIS_PATH + "/api/resource/v1/person/condition/personInfo";
|
||
String host = "https://" + project.getArtemisConfigHost();
|
||
return doPostRtObj(host, path, param.toJSONString(), null, project.getArtemisConfigAppKey(), project.getArtemisConfigAppSecret());
|
||
}
|
||
|
||
/**
|
||
* 获取停车库列表
|
||
*
|
||
* @param project
|
||
*/
|
||
public static JSONArray getParkList(Project project) throws Exception {
|
||
if (project == null || !Objects.equals(project.getSyncHikvision(), 1)) {
|
||
return null;
|
||
}
|
||
final String ARTEMIS_PATH = "/artemis";
|
||
final String path = ARTEMIS_PATH + "/api/resource/v1/park/parkList";
|
||
String host = "https://" + project.getArtemisConfigHost();
|
||
JSONObject jo = new JSONObject();
|
||
String rs = doPost(host, path, jo.toJSONString(), null, project.getArtemisConfigAppKey(), project.getArtemisConfigAppSecret());
|
||
return getJSONArrayData(rs);
|
||
}
|
||
|
||
/**
|
||
* 获取项目名称的停车场的parkIndexCode,没有一样名字的就取第一个停车场
|
||
*
|
||
* @param project
|
||
* @return
|
||
*/
|
||
public static String getProjectParkCode(Project project) throws Exception {
|
||
JSONArray parkList = getParkList(project);
|
||
if (parkList != null && parkList.size() > 0) {
|
||
for (int i = 0; i < parkList.size(); i++) {
|
||
JSONObject jo = parkList.getJSONObject(i);
|
||
if (Objects.equals(jo.getString("parkName"), project.getProjectName())) {
|
||
return jo.getString("parkIndexCode");
|
||
}
|
||
}
|
||
//没有一样名字的就取第一个停车场
|
||
return parkList.getJSONObject(0).getString("parkIndexCode");
|
||
} else {
|
||
throw new OpenPromptException("未查询到停车场信息");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 查询预约记录v2
|
||
*
|
||
* @param carNumber
|
||
* @param project
|
||
*/
|
||
public static JSONObject getReservationCarInfoList(String carNumber, Project project) throws Exception {
|
||
final String ARTEMIS_PATH = "/artemis";
|
||
final String path = ARTEMIS_PATH + "/api/pms/v2/reserveRecord/page";
|
||
String host = "https://" + project.getArtemisConfigHost();
|
||
HikvisionReservationCarInfo info = getHikvisionReservationCarInfoObj(carNumber, project);
|
||
return doPostRtObj(host, path, JSON.toJSONString(info), null, project.getArtemisConfigAppKey(), project.getArtemisConfigAppSecret());
|
||
}
|
||
|
||
/**
|
||
* 查询预约记录v2
|
||
*
|
||
* @param carNumber
|
||
* @param project
|
||
*/
|
||
public static JSONArray getReservationCarInfoDataList(String carNumber, Project project) throws Exception {
|
||
JSONObject jo = getReservationCarInfoList(carNumber, project);
|
||
return getJSONObjectData(jo).getJSONArray("list");
|
||
}
|
||
|
||
|
||
private static HikvisionReservationCarInfo getHikvisionReservationCarInfoObj(String carNumber, Project project) throws Exception {
|
||
String projectParkCode = HikvisionUtil.getProjectParkCode(project);
|
||
HikvisionReservationCarInfo hikvisionReservationCarInfo = new HikvisionReservationCarInfo();
|
||
hikvisionReservationCarInfo.setParkSyscode(projectParkCode);
|
||
hikvisionReservationCarInfo.setPlateNo(carNumber);
|
||
hikvisionReservationCarInfo.setResvState(0L);
|
||
//hikvisionReservationCarInfo.setResvWay();
|
||
//hikvisionReservationCarInfo.setAllowTimes();
|
||
//hikvisionReservationCarInfo.setIsCharge();
|
||
//hikvisionReservationCarInfo.setStartTime();
|
||
//hikvisionReservationCarInfo.setEndTime();
|
||
hikvisionReservationCarInfo.setPageNo(1L);
|
||
hikvisionReservationCarInfo.setPageSize(999L);
|
||
return hikvisionReservationCarInfo;
|
||
}
|
||
|
||
/**
|
||
* 获取组织列表v2
|
||
*
|
||
* @param project
|
||
* @param orgIndex
|
||
* @param parentOrgIndexCode
|
||
*/
|
||
public static String getOrgV2(Project project, String orgIndex, String parentOrgIndexCode) 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/org/advance/orgList";
|
||
String host = "https://" + project.getArtemisConfigHost();
|
||
JSONObject jo = new JSONObject();
|
||
jo.put("pageNo", 1);
|
||
jo.put("pageSize", 100);
|
||
jo.put("orgIndexCodes", orgIndex);
|
||
jo.put("parentOrgIndexCodes", parentOrgIndexCode);
|
||
String body = jo.toJSONString();
|
||
return doPost(host, path, body, null, project.getArtemisConfigAppKey(), project.getArtemisConfigAppSecret());
|
||
}
|
||
|
||
/**
|
||
* 批量添加组织
|
||
*
|
||
* @param project
|
||
* @param hikvisionOrganization
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static String addOrgFromHttp(Project project, HikvisionOrganization hikvisionOrganization) throws Exception {
|
||
final String ARTEMIS_PATH = "/artemis";
|
||
final String path = ARTEMIS_PATH + "/api/resource/v1/org/batch/add";
|
||
String host = "https://" + project.getArtemisConfigHost();
|
||
ArrayList<HikvisionOrganization> list = new ArrayList<>();
|
||
list.add(hikvisionOrganization);
|
||
return doPost(host, path, JSON.toJSONString(list), null, project.getArtemisConfigAppKey(), project.getArtemisConfigAppSecret());
|
||
}
|
||
|
||
public static String deleteOrgByIndexCodes(Project project, List<String> indexCodes) throws Exception {
|
||
final String ARTEMIS_PATH = "/artemis";
|
||
final String path = ARTEMIS_PATH + "/api/resource/v1/org/batch/delete";
|
||
String host = "https://" + project.getArtemisConfigHost();
|
||
JSONObject jo = new JSONObject();
|
||
jo.put("indexCodes", indexCodes);
|
||
String rs = doPost(host, path, jo.toJSONString(), null, project.getArtemisConfigAppKey(), project.getArtemisConfigAppSecret());
|
||
return rs;
|
||
}
|
||
}
|