ArtemisController.java 5.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
package com.zehong.web.controller.video;

import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.zehong.common.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 海康视频接口
 */
@RestController
@RequestMapping("artemis")
@Slf4j
public class ArtemisController {

    /**
     * 请根据自己的appKey和appSecret更换static静态块中的三个参数. [1 host]
     * 如果你选择的是和现场环境对接,host要修改为现场环境的ip,https端口默认为443,http端口默认为80.例如10.33.25.22:443 或者10.33.25.22:80
     * appKey和appSecret请按照或得到的appKey和appSecret更改.
     */
    static {
        // 代理API网关nginx服务器ip端口
        ArtemisConfig.host = "27.128.189.137:1443";
        // 秘钥appkey
        ArtemisConfig.appKey = "28616162";
        // 秘钥appSecret
        ArtemisConfig.appSecret = "5ueTWDOJ21jRbpHACAzF";
    }
    /**
     * 能力开放平台的网站路径
     */
    private static final String ARTEMIS_PATH = "/artemis";

    /**
     * 获取监控点预览取流URL
     */
    private static final String GET_PREVIEWURLS = ARTEMIS_PATH + "/api/video/v1/cameras/previewURLs";

    /**
     * 分页获取监控点资源
     */
    private static final String GET_CAMERAS = ARTEMIS_PATH + "/api/resource/v1/cameras";

    private static final String VEDIO_CONTROLLING = ARTEMIS_PATH + "/api/video/v1/ptzs/controlling";

    /**
     * 获取监控点预览取流URL
     * @return
     */
    @GetMapping("/getPreviewURLs")
    public Object getPreviewURLs(@RequestParam(value = "cameraIndexCode") String cameraIndexCode){
        /**
         * 根据API文档可以看出来,这是一个POST请求的Rest接口,而且传入的参数值为一个json
         * ArtemisHttpUtil工具类提供了doPostStringArtemis这个函数,一共六个参数在文档里写明其中的意思,因为接口是https,
         * 所以第一个参数path是一个hashmap类型,请put一个key-value,query为传入的参数,body为传入的json数据
         * 传入的contentType为application/json,accept不指定为null
         * header没有额外参数可不传,指定为null
         *
         */

        Map<String, String> path = new HashMap<String, String>(2) {
            {
                //根据现场环境部署确认是http还是https
                put("https://", GET_PREVIEWURLS);
            }
        };

        JSONObject jsonBody = new JSONObject();

        //jsonBody.put("cameraIndexCode", "2a9891a194c24747b277f3ea4836d433");
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("streamType", 1);
        jsonBody.put("protocol", "ws");
        jsonBody.put("transmode", 1);
        String body = jsonBody.toJSONString();
        // post请求application/json类型参数
        String result = ArtemisHttpUtil.doPostStringArtemis(path,body,null,null,"application/json",null);
        return AjaxResult.success(JSONObject.parseObject(result));
    }

    /**
     * 分页获取监控点资源
     * @param pageNo    页码
     * @param pageSize  每页条数
     * @return
     */
    @GetMapping("/getCameras")
    public Object getCameras(@RequestParam("pageNo") int pageNo, @RequestParam("pageSize") int pageSize){

        Map<String, String> path = new HashMap<String, String>(2) {
            {
                //根据现场环境部署确认是http还是https
                put("https://", GET_CAMERAS);
            }
        };

        JSONObject jsonBody = new JSONObject();

        jsonBody.put("pageNo", pageNo);
        jsonBody.put("pageSize", pageSize);
        jsonBody.put("treeCode", 0);
        String body = jsonBody.toJSONString();
        // post请求application/json类型参数
        String result = ArtemisHttpUtil.doPostStringArtemis(path,body,null,null,"application/json",null);
        return JSONObject.parseObject(result);
    }

    /**
     * 云控操作
     * @param cameraIndexCode
     * @param command
     * @param action
     * @return
     */
    @RequestMapping("/videoControlling")
    public Object videoControlling(@RequestParam("cameraIndexCode") String cameraIndexCode, @RequestParam("command") String command, @RequestParam("action") String action){
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                //根据现场环境部署确认是http还是https
                put("https://", VEDIO_CONTROLLING);
            }
        };
        JSONObject jsonBody = new JSONObject();

        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("action", action);
        jsonBody.put("command", command);
        jsonBody.put("speed", 40);
        String body = jsonBody.toJSONString();
        // post请求application/json类型参数
        String result = ArtemisHttpUtil.doPostStringArtemis(path,body,null,null,"application/json",null);
        return JSONObject.parseObject(result);
    }
}