1. 海康的依赖

  1. # 1.pom中添加海康的依赖
  2. <!-- 海康的openAPI依赖 -->
  3. <dependency>
  4. <groupId>com.hikvision.ga</groupId>
  5. <artifactId>artemis-http-client</artifactId>
  6. <version>1.1.3</version>
  7. </dependency>
  8. # 2.application.yml中,添加海康平台的key和secret
  9. hk:
  10. host: 172.22.1.100:18443
  11. appKey: 22263225
  12. appSecret: MRpLUwlUZ0d5UanJfLqS

2.hik的连接方式

  1. # 1.HkiConfig 配置信息
  2. package com.qif.videocontrol.hikvision.config;
  3. import com.hikvision.artemis.sdk.config.ArtemisConfig;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.stereotype.Component;
  8. /**
  9. * @param
  10. * @author shi wei
  11. * @create 2020/8/20
  12. * @return
  13. */
  14. @Component
  15. @ConditionalOnProperty(prefix = "message.service", name = "impl", havingValue = "hk")
  16. public class HKConfiguration {
  17. private static String host;
  18. private static String appKey;
  19. private static String appSecret;
  20. @Value("${hk.host")
  21. public void setHost(String host) {
  22. HKConfiguration.host = host;
  23. }
  24. @Value("${hk.appKey}")
  25. public void setAppKey(String appKey) {
  26. HKConfiguration.appKey = appKey;
  27. }
  28. @Value("${hk.appSecret}")
  29. public void setAppSecret(String appSecret) {
  30. HKConfiguration.appSecret = appSecret;
  31. }
  32. @Bean
  33. public static void getArtemisConfig() {
  34. // 代理API网关nginx服务器ip端口
  35. ArtemisConfig.host = host;
  36. // 秘钥appkey
  37. ArtemisConfig.appKey = appKey;
  38. // 秘钥appSecret
  39. ArtemisConfig.appSecret = appSecret;
  40. }
  41. }
  1. # HkiService--海康的API调用
  2. ## 需要配合海康的管理平台使用,使用方面局限。
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.hikvision.artemis.sdk.ArtemisHttpUtil;
  5. import com.qif.videocontrol.camera.pojo.bo.StreamRespBO;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.springframework.stereotype.Service;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. /**
  12. * @param
  13. * @author shi wei
  14. * @create 2020/8/20
  15. * @return
  16. */
  17. @Slf4j
  18. @Service
  19. public class HKService {
  20. /**
  21. * 开放平台的网站路径
  22. */
  23. private static final String ARTEMIS_PATH = "/artemis";
  24. public static JSONObject hkCameraCtrl(JSONObject jsonBody, String url) {
  25. log.info("调用海康API的请求{},参数:{}",url, jsonBody.toJSONString());
  26. Map<String, String> path = new HashMap<>();
  27. path.put("https://", ARTEMIS_PATH + url);
  28. String result = ArtemisHttpUtil.doPostStringArtemis(path, jsonBody.toJSONString(), null, null, "application/json", null);
  29. return StringUtils.isNotBlank(result) ? JSONObject.parseObject(result):new JSONObject();
  30. }
  31. /**
  32. * 调用海康云台的控制
  33. * @param jsonObject
  34. */
  35. public StreamRespBO hkCommand(JSONObject jsonObject) {
  36. JSONObject result = HKService.hkCameraCtrl(jsonObject, "/api/video/v1/ptzs/controlling");
  37. StreamRespBO streamRespBO = new StreamRespBO();
  38. if (result.isEmpty()) {
  39. log.info("海康控制平台没有获取查询信息。");
  40. throw new RuntimeException("海康云控平台没有找到记录");
  41. }else if(("0").equals(result.get("code"))) {
  42. streamRespBO.setCode("200");
  43. streamRespBO.setData(JSONObject.toJSONString(result.get("data")));
  44. streamRespBO.setSuccess(true);
  45. }else {
  46. log.error("海康控制平台返回异常,异常信息:{}", result.get("msg"));
  47. streamRespBO.setCode("500");
  48. streamRespBO.setData(JSONObject.toJSONString(result.get("msg")));
  49. streamRespBO.setSuccess(true);
  50. }
  51. return streamRespBO;
  52. }
  53. /**
  54. * 调用海康云台的控制
  55. * @param jsonObject
  56. * @param url
  57. */
  58. public JSONObject hkCommand(JSONObject jsonObject, String url) {
  59. JSONObject result = HKService.hkCameraCtrl(jsonObject, url);
  60. if (result.isEmpty()) {
  61. log.info("海康控制平台没有获取查询信息。");
  62. throw new RuntimeException("海康云控平台没有找到记录");
  63. }
  64. else if(!("0").equals(result.get("code"))) {
  65. log.error("海康控制平台返回异常,异常信息:{}", result.get("msg"));
  66. throw new RuntimeException("海康云台控制异常,错误为:"+ result.get("msg"));
  67. }
  68. return result;
  69. }
  70. public JSONObject hkCommandNoLog(JSONObject jsonObject, String url) {
  71. Map<String, String> path = new HashMap<>();
  72. path.put("https://", ARTEMIS_PATH + url);
  73. String result = ArtemisHttpUtil.doPostStringArtemis(path, jsonObject.toJSONString(), null, null, "application/json", null);
  74. JSONObject object = StringUtils.isNotBlank(result) ? JSONObject.parseObject(result):new JSONObject();
  75. if (object.isEmpty()) {
  76. log.info("海康控制平台没有获取查询信息。");
  77. throw new RuntimeException("海康云控平台没有找到记录");
  78. }
  79. else if(!("0").equals(object.get("code"))) {
  80. log.error("海康控制平台返回异常,异常信息:{}", object.get("msg"));
  81. throw new RuntimeException("海康云台控制异常,错误为:"+ object.get("msg"));
  82. }
  83. return object;
  84. }
  85. /**
  86. * 设置一个万能的停止操作
  87. * @param cameraIndexCode
  88. */
  89. public void univStop(String cameraIndexCode) {
  90. JSONObject jsonObject = new JSONObject();
  91. jsonObject.put("cameraIndexCode", cameraIndexCode);
  92. jsonObject.put("action", 1);
  93. jsonObject.put("command", "STOP_TRACK");
  94. this.hkCommand(jsonObject);
  95. }
  96. }