package cn.iocoder.yudao.module.system.service.hik;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

@Service
@Validated
@Slf4j
public class HikServiceImpl implements HikService{

    @Override
    public String getPreviewUrlsApi(String cameraCode, int streamType) {
        final String previewUrlsApi ="/artemis/api/video/v2/cameras/previewURLs";
        Map<String, String> path = new HashMap<String, String>(1) {
            {
                put("https://", previewUrlsApi);
            }
        };
        JSONObject body = new JSONObject();
        body.set("cameraIndexCode", cameraCode);
        body.set("streamType", streamType);
        body.set("protocol", "ws");
        body.set("transmode", 1);
        body.set("expand", "streamform=ps");
        try {
            String result = ArtemisHttpUtil
                    .doPostStringArtemis(path, JSONUtil.toJsonStr(body), null, null, "application/json") ;
            log.info("{}监控查看预览地址返回：{}", JSONUtil.toJsonStr(body), result);
            if (StringUtils.isEmpty(result)) {
//                throw new ServiceException("监控查看预览地址返回为空");
                System.out.println("监控查看预览地址返回为空");
            }
            JSONObject json = JSONUtil.parseObj(result);
            String url = "";
            if (json.getJSONObject("data") != null) {
                url = json.getJSONObject("data").getStr("url");
            }
            return url;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
