HikVersionController.java 4.1 KB
package com.skua.modules.app.video;

import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.skua.core.aspect.annotation.AutoLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@Api(tags="海康威视视频流地址获取")
@RestController
@RequestMapping("/app/video/hikVideo")
public class HikVersionController {


    @AutoLog(value = "地址")
    @ApiOperation(value="地址", notes="地址")
    @PostMapping(value = "/previewURL")
    public JSONObject GetCameraPreviewURL(@RequestBody Map<String,String> map) {
        //String code, String url, String protocol,String streamform
        if (map.isEmpty()){
            return null;
        }
        String code = map.get("code");
        String url = map.get("url");
        if (StringUtils.isBlank(url)){
            url = "106.225.156.168:8667";
        }
        String protocol = map.get("protocol");
        String streamform = map.get("streamform");
        /**
         * STEP1:设置平台参数,根据实际情况,设置host appkey appsecret 三个参数.
         */
        //ArtemisConfig.host = "189.200.0.86:8667"; // artemis网关服务器ip端口
        //ArtemisConfig.host = "106.225.156.168:8667"; // artemis网关服务器ip端口
        ArtemisConfig.host = url; // artemis网关服务器ip端口
        ArtemisConfig.appKey = "27232944";  // 秘钥appkey
        ArtemisConfig.appSecret = "jZJacfeacQmK6Bn9iOHe";// 秘钥appSecret

        /**
         * STEP2:设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3:设置接口的URI地址
         */
        final String previewURLsApi = ARTEMIS_PATH + "/api/video/v1/cameras/previewURLs";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4:设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5:组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("protocol", protocol);
        jsonBody.put("expand", "streamform="+streamform);
        //jsonBody.put("cameraIndexCode", "f570f0fc5cbc4904b3634e4fb8771dc0,5a96159594ff44ad86a00f43795f458a");bde554256a344764be61573cc2dd3ed9
        jsonBody.put("cameraIndexCode", code);
        jsonBody.put("streamType", 0);
        //jsonBody.put("protocol", "ws");
        jsonBody.put("transmode", 1);
        //jsonBody.put("expand", "streamform=ps");
        String body = jsonBody.toJSONString();
        /**
         * STEP6:调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);// post请求application/json类型参数
        JSONObject jsonObject = null;
        if (!"".equals(result)&&result!=null){
            jsonObject = JSONObject.parseObject(result);
        }
        return jsonObject;
    }

    //public static void main(String[] args) {
    //
    //    JSONObject jsonObject1 = new HikVersionController().GetCameraPreviewURL("5a96159594ff44ad86a00f43795f458a","");
    //    JSONObject jsonObject2 = new HikVersionController().GetCameraPreviewURL("5ea4697ee3744365953ccf6f70b808fa","");
    //    //JSONObject jsonObject1 = new HikVersionController().GetCameraPreviewURL("4c48ac7ffce44dcea75a9785effaf317");
    //    //JSONObject jsonObject2 = new HikVersionController().GetCameraPreviewURL("4807adcf1efd4a45a50e91a4875a3882");
    //    System.out.println("result结果示例: " + jsonObject1);
    //    System.out.println("result结果示例: " + jsonObject2);
    //}
}