一.pom文件加入依赖

        <dependency>
			<groupId>com.hikvision.ga</groupId>
			<artifactId>artemis-http-client</artifactId>
			<version>1.1.3</version>
		</dependency>

二.yml文件配置秘钥

hikApi:
  host: xx.xx.xx.xx:443
  appKey: xxxxxxx
  appSecret: xxxxxxxxxxxxxxxxxxxx

三.创建配置文件HiKApiConfig

@Slf4j
@Configuration
public class HiKApiConfig {

    @Value(value = "${hikApi.host}")
    private String host;
    @Value(value = "${hikApi.appKey}")
    private String appKey;
    @Value(value = "${hikApi.appSecret}")
    private String appSecret;

    @Bean
    public void initHiK(){
        ArtemisConfig.host = host; // 平台的ip端口
        ArtemisConfig.appKey = appKey;  // 密钥appkey
        ArtemisConfig.appSecret = appSecret;// 密钥appSecret
    }
}

四.配置海康url常量HikConstant

public class HikConstant {

    public final static String HIK_GET_PERSON = "/api/resource/v2/person/personList";
    public final static String HIK_ADD_PERSON = "/api/resource/v1/person/single/add";
    public final static String HIK_BATCH_DEL_PERSON = "/api/resource/v1/person/batch/delete";
    public final static String HIK_UPDATE_PERSON = "/api/resource/v1/person/single/update";

    public final static String HIK_GET_ORG = "/api/resource/v1/org/orgList";
    public final static String HIK_ADD_ORG = "/api/resource/v1/org/batch/add";
    public final static String HIK_BATCH_DEL_ORG = "/api/resource/v1/org/batch/delete";
    public final static String HIK_UPDATE_ORG = "/api/resource/v1/org/single/update";
    public final static String HIK_UPDATE_FACE = "/api/resource/v1/face/single/update";
    public final static String HIK_ADD_FACE = "/api/resource/v1/face/single/add";
}

五.创建对应实体类

1.组织实体类

@Data
public class HiKOrgInfo {

    private String orgName;
    private String orgIndexCode;
    private String parentIndexCode;
    private Integer clientId;

    /*
    非必要
     */
//    private String orgNo;
//    private String orgPath;
}

 2.人员实体类

@Data
public class HikPersonInfo {

    private String personId;
    private String personName;
    private String gender;
    private String orgIndexCode;
    private String hyCode;
    private String birthday;
    private String phoneNo;
    private String email;
    private String certificateType;
    private String certificateNo;
    private String jobNo;
}

3.返回实体

@Data
public class HikResultDTO {

    private Integer total;

    private Integer pageNo;

    private Integer pageSize;

    private JSONArray list;
}

6.调用api工具类

public class GetHikApiData {

    public static List<HiKOrgInfo> getHiKOrgInfoList() {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo", "1");
        jsonBody.put("pageSize", 1000);
        JSONArray array = new JSONArray();
        GetHikApiData.getHiKApiDataList(HikConstant.HIK_GET_ORG, jsonBody, array);
        return JSON.parseArray(array.toJSONString(), HiKOrgInfo.class);
    }

    public static List<HikPersonInfo> getHikPersonInfoList() {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo", "1");
        jsonBody.put("pageSize", 1000);
        JSONArray array = new JSONArray();
        GetHikApiData.getHiKApiDataList(HikConstant.HIK_GET_PERSON, jsonBody, array);
        return JSON.parseArray(array.toJSONString(), HikPersonInfo.class);
    }

    public static void batchAddOrg(List<HiKOrgInfo> list) {
        List<HiKOrgInfo> list1 = new ArrayList<>();
        for (HiKOrgInfo info : list) {
            list1.add(info);
            JSONArray array = JSONArray.parseArray(JSON.toJSONString(list1));
            GetHikApiData.postHiKApiDataList(HikConstant.HIK_ADD_ORG, array);
            list1.clear();
        }
    }

    public static Integer batchAddPerson(List<HikPersonInfo> list) {
        int count = 0;
        for (HikPersonInfo info : list) {
            JSONObject jsonBody = JSONObject.parseObject(JSONObject.toJSONString(info));
            count = count + GetHikApiData.getHiKApiDataList(HikConstant.HIK_ADD_PERSON, jsonBody, null);
        }
        return count;
    }

    public static void batchDelOrg(List<String> list) {
        JSONObject object = new JSONObject();
        object.put("indexCodes", list);
        JSONObject jsonBody = JSONObject.parseObject(object.toJSONString());
        GetHikApiData.getHiKApiDataList(HikConstant.HIK_BATCH_DEL_ORG, jsonBody, null);
    }

    public static void batchDelPerson(List<String> list) {
        JSONObject object = new JSONObject();
        object.put("personIds", list);
        JSONObject jsonBody = JSONObject.parseObject(object.toJSONString());
        GetHikApiData.getHiKApiDataList(HikConstant.HIK_BATCH_DEL_PERSON, jsonBody, null);
    }

    public static Integer batchUpdateOrg(List<HiKOrgInfo> list) {
        int count = 0;
        for (HiKOrgInfo info : list) {
            JSONObject jsonBody = JSONObject.parseObject(JSON.toJSONString(info));
            count = count + GetHikApiData.getHiKApiDataList(HikConstant.HIK_UPDATE_ORG, jsonBody, null);
        }
        return count;
    }

    public static Integer batchUpdatePerson(List<HikPersonInfo> list) {
        int count = 0;
        for (HikPersonInfo info : list) {
            JSONObject jsonBody = JSONObject.parseObject(JSON.toJSONString(info));
            count = count + GetHikApiData.getHiKApiDataList(HikConstant.HIK_UPDATE_PERSON, jsonBody, null);
        }
        return count;
    }

    public static Integer batchUpdateFace(List<HikPersonInfo> list) {
        int count = 0;
        for (HikPersonInfo info : list) {
            JSONObject jsonBody = new JSONObject();
            jsonBody.put("faceId", info.getPersonPhoto().getJSONObject(0).get("personPhotoIndexCode").toString());
            jsonBody.put("faceData",info.getPhoto());
            count = count + GetHikApiData.getHiKApiDataList(HikConstant.HIK_UPDATE_FACE, jsonBody, null);
        }
        return count;
    }

    public static Integer batchAddFace(List<HikPersonInfo> list) {
        int count = 0;
        for (HikPersonInfo info : list) {
            JSONObject jsonBody = new JSONObject();
            jsonBody.put("personId", info.getPersonId());
            jsonBody.put("faceData",info.getPhoto());
            count = count + GetHikApiData.getHiKApiDataList(HikConstant.HIK_ADD_FACE, jsonBody, null);
        }
        return count;
    }

    public static Integer getHiKApiDataList(String url, JSONObject obj, JSONArray list) {
        /**
         * STEP2:设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";
        /**
         * STEP3:设置接口的URI地址
         */
        final String previewURLsApi = ARTEMIS_PATH + url;
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };
        /**
         * STEP4:设置参数提交方式
         */
        String contentType = "application/json";
        /**
         * STEP5:组装请求参数
         */
        String body = JSON.toJSONString(obj);
        /**
         * STEP6:调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);// post请求application/json类型参数
        JSONObject res = JSONObject.parseObject(result);
        if (res != null) {
            if ("success".equals(res.get("msg").toString())) {
                if (list != null) {
                    JSONObject data = res.getJSONObject("data");
                    if (data != null) {
                        HikResultDTO dto = JSON.parseObject(data.toJSONString(), HikResultDTO.class);
                        if (CollectionUtils.isNotEmpty(dto.getList())) {
                            list.addAll(dto.getList());
                            if (dto.getTotal() / 1000.0 > obj.getInteger("pageNo")) {
                                Integer pageNo = obj.getInteger("pageNo") + 1;
                                obj.put("pageNo", pageNo);
                                getHiKApiDataList(url, obj, list);
                            }
                        }
                    }
                }
                return 1;
            }else{
                return 0;
            }
        } else {
            throw new RuntimeException("接口无数据");
        }
    }

    public static void postHiKApiDataList(String url, JSONArray array) {
        /**
         * STEP2:设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";
        /**
         * STEP3:设置接口的URI地址
         */
        final String previewURLsApi = ARTEMIS_PATH + url;
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };
        /**
         * STEP4:设置参数提交方式
         */
        String contentType = "application/json";
        /**
         * STEP5:组装请求参数
         */
        String body = JSON.toJSONString(array);
        /**
         * STEP6:调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);// post请求application/json类型参数
        JSONObject res = JSONObject.parseObject(result);
        if (res != null) {
            String flag = res.get("msg").toString();
            if (!"success".equals(flag)) {
                throw new RuntimeException("组织新增接口调用失败");
            }
        }
    }
}

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐