package beilv.hik.controller.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分页获取监控点资源
 */
@Slf4j
public class GetCameras {

    public static List<Map<String, Object>> getCameras(String host, String appKey, String appSecret) {
        List<Map<String, Object>> list = new ArrayList<>();

        ArtemisConfig.host = host; // 平台的ip端口
        ArtemisConfig.appKey = appKey;  // 密钥appkey
        ArtemisConfig.appSecret = appSecret;// 密钥appSecret

        final String ARTEMIS_PATH = "/artemis";
        final String previewURLsApi = ARTEMIS_PATH + "/api/resource/v1/cameras";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };
        String contentType = "application/json";
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo", 1);
        jsonBody.put("pageSize", 2);
        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")) {
            JSONArray jsonArray = JSONObject.parseArray(JSONObject.parseObject(json.get("data").toString()).get("list").toString());
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                list = objectMapper.readValue(jsonArray.toString(), new TypeReference<List<Map<String, Object>>>() {
                });
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }
        return list;
    }

    public static void main(String[] args) {
        List<Map<String, Object>> result = getCameras("218.62.80.146:1443", "28356728", "C3K889wQUgVlSj1BMay6");
        System.out.println("result结果示例: " + result);
    }
}
