记录一次对接海康威视接口

1.背景

需要对接海康开发平台 视频应用服务中的视频能力中的 获取监控点预览取流URL

海康开发平台网址:https://open.hikvision.com/

对接的详细api地址:https://open.hikvision.com/docs/docId?productId=613976c8600ca3042410d942&version=%2Fc831e2917a7149f0a02772409dee5928&tagPath=API%E5%88%97%E8%A1%A8-%E8%A7%86%E9%A2%91%E5%BA%94%E7%94%A8%E6%9C%8D%E5%8A%A1-%E8%A7%86%E9%A2%91%E8%83%BD%E5%8A%9B

2.已有资源

海康对接所需的 AppKey/AppSecret(用于API调用的签名认证使用,成对出现,两个字符串)

3.项目开发

3.1引入海康封装的artemis-http-client-1.1.8.jar (在海康开放平台网页中找在线技术支持获取的,给我发了一个安全认证库的项目后,解压得到的)

3.2项目中加载第三方jar
记录一次对接海康威视接口_第1张图片
3.3代码开发

import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class HKUtil {

    @Value("${hk.host}")
    private String host;

    @Value("${hk.appKey}")
    private String appKey;

    @Value("${hk.appSecret}")
    private String appSecret;

    @Value("${hk.userId}")
    private String userId;


    private static final String ARTEMIS_PATH = "/artemis";
      /**
     * 通用海康接口
     * 调用POST请求类型(application/json)接口*
     *
     * @param url /api/video/v1/cameras/previewURLs
     * @return
     */
    public String publicHkInterface(JSONObject jsonBody, String url) throws Exception {
        ArtemisConfig config = new ArtemisConfig();
        config.setHost(host); //ip端口
        config.setAppKey(appKey);  // 秘钥appkey
        config.setAppSecret(appSecret);// 秘钥appSecret
 log.info("publicHkInterface  config:{}", config.toString());
        //artemis/api/video/v1/cameras/previewURLs
        final String getCamsApi = ARTEMIS_PATH + url;
        Map<String, String> path = new HashMap<String, String>(2);
        path.put("https://", getCamsApi);
        Map<String, String> header = new HashMap<String, String>(2);
        header.put("userId", userId);
         // post请求application/json类型参数
        String result = ArtemisHttpUtil.doPostStringArtemis(config, path, jsonBody.toJSONString(), null, null, "application/json", header);
        return result;
        
          }
    /**
     * 获取监控点预览取流URL
     *
     * @param cameraIndexCode 设备编号
     * @param protocol        协议
     * @return
     */
  public String camerasPreviewURLs(String cameraIndexCode, String protocol) throws Exception {
        log.info("camerasPreviewURLs  camera:{} protocol:{}", cameraIndexCode, protocol);
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("protocol", protocol);
        //https://ip:port/artemis/api/video/v1/cameras/previewURLs
        return publicHkInterface(jsonBody, "/api/video/v1/cameras/previewURLs");
    }
    
}
    @Autowired
    private HKUtil hkUtil;

    @PostMapping("/camera/getCameraWs")
    @ResponseBody
    public Response<Object> getCameraWs(@RequestParam("cameraId") String cameraId) {
        log.info("/camera/getWs  =====in");
        String result = null;
         try {
            result = hkUtil.camerasPreviewURLs(cameraId, "wss");
            if (StringUtils.isNotBlank(result)) {
                JSONObject jsonObject = JSONObject.parseObject(result);
                log.info("respJson:{}", jsonObject);
                String ws = "";
                 if (!jsonObject.isEmpty() && "0".equals(jsonObject.get("code"))) {
                    JSONObject data = jsonObject.getJSONObject("data");
                    ws = data.getString("url");
                    return new Response<>(ws);
                }
                log.error("调用HK接口异常返回:{}", result);
            } else {
                log.error("调用HK接口异常 null返回");
            }
             } catch (Exception e) {
            log.error("/camera/getCameraWs 异常:{}", e);
        }
        return new Response<>();
    }

4.项目yml配置

hk:
  host: 127.0.0.1:1443  #ip:port
  userId: userId #这个属性对我的接口而言并不是必备参数
  appKey:  AppKey
  appSecret: AppSecret

5.项目打包部署

因为引入了第三方jar。maven pom.xml 中需要引入相关的配置信息

   <dependency>
            <groupId>com.hkgroupId> #随便命名
            <artifactId>artemis-http-clientartifactId>   #随便命名
            <version>1.0version> #随便命名
            <type>jartype>
            <scope>systemscope>
            <systemPath>${project.basedir}/lib/artemis-http-client-1.1.8.jarsystemPath>  #这个得根据引入第三方jar位置填写
        dependency>
        
          <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    
                    <includeSystemScope>trueincludeSystemScope>
                configuration>
            plugin>

你可能感兴趣的:(java,spring,servlet)