package beilv.hik.controller.utils;

import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;

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

/**
 * 获取监控点预览取流URLv2
 */
public class GetCameraPreviewURL {

    public static String getCameraPreviewURL(String host, String appKey, String appSecret, String cameraIndexCode) {
        String url = null;
        ArtemisConfig.host = host; // 平台的ip端口
        ArtemisConfig.appKey = appKey;  // 密钥appkey
        ArtemisConfig.appSecret = appSecret;// 密钥appSecret
        final String ARTEMIS_PATH = "/artemis";
        final String previewURLsApi = ARTEMIS_PATH + "/api/video/v1/cameras/previewURLs";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };
        String contentType = "application/json";
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("streamType", 0);
        jsonBody.put("protocol", "ws");
        jsonBody.put("transmode", 1);
        jsonBody.put("expand", "streamform=ps");
        String body = jsonBody.toJSONString();
        JSONObject json = JSONObject.parseObject(ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null));// post请求application/json类型参数
        if (json.get("code").equals("0")) {
            url = JSONObject.parseObject(json.get("data").toString()).get("url").toString();
        }
        return url;
    }

    public static void main(String[] args) {
        String result = getCameraPreviewURL("218.62.80.146:1443", "28356728", "C3K889wQUgVlSj1BMay6", "fd98e88146904ceba1e30081e61b47f9");
        System.out.println("result结果示例: " + result);
    }
}
