ArtemisController.java 7.92 KB
Newer Older
耿迪迪's avatar
耿迪迪 committed
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
package com.zehong.web.controller.system;

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 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")
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";
    /**
     * 获取对讲url
     */
    private static final String GET_TALKURLS = ARTEMIS_PATH + "/api/video/v1/cameras/talkURLs";

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

    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 AjaxResult getCameras(@RequestParam("pageNum") 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);
        return AjaxResult.success(JSONObject.parseObject(result));
    }
    /**
     * 分页获取监控点资源
     * @param pageNo    页码
     * @param pageSize  每页条数
     * @return
     */
    @GetMapping("/search")
    public AjaxResult getCamerasSearch(@RequestParam("pageNum") int pageNo ,@RequestParam("pageSize") int pageSize,@RequestParam("name") String name){

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

        JSONObject jsonBody = new JSONObject();

        jsonBody.put("pageNo", pageNo);
        jsonBody.put("pageSize", pageSize);
        jsonBody.put("name", name);
        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);
        return AjaxResult.success(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);
    }

    /**
     * 获取通话链接
     * @param cameraIndexCode
     * @return
     */
    @GetMapping("/getTalkURLs")
    public Object getTalkURLs(@RequestParam(value = "cameraIndexCode") String cameraIndexCode,
                              @RequestParam(value = "expand",required=false) String expand,
                              @RequestParam(value = "streamType",required=false) Integer streamType){

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

        JSONObject jsonBody = new JSONObject();
        if(expand==null){
            expand = "streamform=ps";
        }
        if(streamType==null){
            streamType = 1;
        }
        //jsonBody.put("cameraIndexCode", "2a9891a194c24747b277f3ea4836d433");
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("streamType", streamType);
        jsonBody.put("expand", expand);
        jsonBody.put("eurlExpand", "url");
        String body = jsonBody.toJSONString();
        // post请求application/json类型参数
        String result = ArtemisHttpUtil.doPostStringArtemis(path,body,null,null,"application/json",null);
        return AjaxResult.success(JSONObject.parseObject(result));
    }
}