1. 海康的依赖
# 1.pom中添加海康的依赖
<!-- 海康的openAPI依赖 -->
<dependency>
<groupId>com.hikvision.ga</groupId>
<artifactId>artemis-http-client</artifactId>
<version>1.1.3</version>
</dependency>
# 2.application.yml中,添加海康平台的key和secret
hk:
host: 172.22.1.100:18443
appKey: 22263225
appSecret: MRpLUwlUZ0d5UanJfLqS
2.hik的连接方式
# 1.HkiConfig 配置信息
package com.qif.videocontrol.hikvision.config;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
/**
* @param
* @author shi wei
* @create 2020/8/20
* @return
*/
@Component
@ConditionalOnProperty(prefix = "message.service", name = "impl", havingValue = "hk")
public class HKConfiguration {
private static String host;
private static String appKey;
private static String appSecret;
@Value("${hk.host")
public void setHost(String host) {
HKConfiguration.host = host;
}
@Value("${hk.appKey}")
public void setAppKey(String appKey) {
HKConfiguration.appKey = appKey;
}
@Value("${hk.appSecret}")
public void setAppSecret(String appSecret) {
HKConfiguration.appSecret = appSecret;
}
@Bean
public static void getArtemisConfig() {
// 代理API网关nginx服务器ip端口
ArtemisConfig.host = host;
// 秘钥appkey
ArtemisConfig.appKey = appKey;
// 秘钥appSecret
ArtemisConfig.appSecret = appSecret;
}
}
# HkiService--海康的API调用
## 需要配合海康的管理平台使用,使用方面局限。
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.qif.videocontrol.camera.pojo.bo.StreamRespBO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* @param
* @author shi wei
* @create 2020/8/20
* @return
*/
@Slf4j
@Service
public class HKService {
/**
* 开放平台的网站路径
*/
private static final String ARTEMIS_PATH = "/artemis";
public static JSONObject hkCameraCtrl(JSONObject jsonBody, String url) {
log.info("调用海康API的请求{},参数:{}",url, jsonBody.toJSONString());
Map<String, String> path = new HashMap<>();
path.put("https://", ARTEMIS_PATH + url);
String result = ArtemisHttpUtil.doPostStringArtemis(path, jsonBody.toJSONString(), null, null, "application/json", null);
return StringUtils.isNotBlank(result) ? JSONObject.parseObject(result):new JSONObject();
}
/**
* 调用海康云台的控制
* @param jsonObject
*/
public StreamRespBO hkCommand(JSONObject jsonObject) {
JSONObject result = HKService.hkCameraCtrl(jsonObject, "/api/video/v1/ptzs/controlling");
StreamRespBO streamRespBO = new StreamRespBO();
if (result.isEmpty()) {
log.info("海康控制平台没有获取查询信息。");
throw new RuntimeException("海康云控平台没有找到记录");
}else if(("0").equals(result.get("code"))) {
streamRespBO.setCode("200");
streamRespBO.setData(JSONObject.toJSONString(result.get("data")));
streamRespBO.setSuccess(true);
}else {
log.error("海康控制平台返回异常,异常信息:{}", result.get("msg"));
streamRespBO.setCode("500");
streamRespBO.setData(JSONObject.toJSONString(result.get("msg")));
streamRespBO.setSuccess(true);
}
return streamRespBO;
}
/**
* 调用海康云台的控制
* @param jsonObject
* @param url
*/
public JSONObject hkCommand(JSONObject jsonObject, String url) {
JSONObject result = HKService.hkCameraCtrl(jsonObject, url);
if (result.isEmpty()) {
log.info("海康控制平台没有获取查询信息。");
throw new RuntimeException("海康云控平台没有找到记录");
}
else if(!("0").equals(result.get("code"))) {
log.error("海康控制平台返回异常,异常信息:{}", result.get("msg"));
throw new RuntimeException("海康云台控制异常,错误为:"+ result.get("msg"));
}
return result;
}
public JSONObject hkCommandNoLog(JSONObject jsonObject, String url) {
Map<String, String> path = new HashMap<>();
path.put("https://", ARTEMIS_PATH + url);
String result = ArtemisHttpUtil.doPostStringArtemis(path, jsonObject.toJSONString(), null, null, "application/json", null);
JSONObject object = StringUtils.isNotBlank(result) ? JSONObject.parseObject(result):new JSONObject();
if (object.isEmpty()) {
log.info("海康控制平台没有获取查询信息。");
throw new RuntimeException("海康云控平台没有找到记录");
}
else if(!("0").equals(object.get("code"))) {
log.error("海康控制平台返回异常,异常信息:{}", object.get("msg"));
throw new RuntimeException("海康云台控制异常,错误为:"+ object.get("msg"));
}
return object;
}
/**
* 设置一个万能的停止操作
* @param cameraIndexCode
*/
public void univStop(String cameraIndexCode) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("cameraIndexCode", cameraIndexCode);
jsonObject.put("action", 1);
jsonObject.put("command", "STOP_TRACK");
this.hkCommand(jsonObject);
}
}