package com.ycl.service; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.hikvision.artemis.sdk.ArtemisHttpUtil; import com.hikvision.artemis.sdk.config.ArtemisConfig; import com.ycl.config.HKProperties; import com.ycl.domain.form.CameraCommandForm; import com.ycl.domain.query.CameraPlayQuery; import com.ycl.domain.query.CameraQuery; import com.ycl.domain.result.HKResult; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import java.util.*; /** * @author:xp * @date:2024/12/20 10:57 */ @Service public class HKService { private final HKProperties hkProperties; public HKService(HKProperties hkProperties) { this.hkProperties = hkProperties; } /** * 获取监控资源点-摄像头 * @see ... * */ public Map getCamerasPage(CameraQuery query) { /** * STEP1:设置平台参数,根据实际情况,设置host appkey appsecret 三个参数. */ ArtemisConfig.host = hkProperties.getHost(); // 平台的ip端口 ArtemisConfig.appKey = hkProperties.getAk(); // 密钥appkey ArtemisConfig.appSecret = hkProperties.getSk();// 密钥appSecret /** * STEP2:设置OpenAPI接口的上下文 */ final String ARTEMIS_PATH = "/artemis"; /** * STEP3:设置接口的URI地址 */ final String previewURLsApi = ARTEMIS_PATH + "/api/resource/v2/camera/search"; Map path = new HashMap(2) { { put("https://", previewURLsApi);//根据现场环境部署确认是http还是https } }; /** * STEP4:设置参数提交方式 */ String contentType = "application/json"; /** * STEP5:组装请求参数 */ JSONObject jsonBody = new JSONObject(); jsonBody.put("pageNo", query.getPageNo()); jsonBody.put("pageSize", query.getPageSize()); jsonBody.put("orderBy", "name"); jsonBody.put("orderType", "desc"); if (StringUtils.hasText(query.getName())) { // 查询条件 List expressions = new ArrayList<>(1); Map nameSearch = new HashMap<>(3); nameSearch.put("key", "name"); nameSearch.put("operator", 6); nameSearch.put("values", Arrays.asList(query.getName())); expressions.add(nameSearch); jsonBody.put("expressions", expressions); } String body = jsonBody.toJSONString(); /** * STEP6:调用接口 */ String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);// post请求application/json类型参数 HKResult hkResult = JSON.parseObject(result, HKResult.class); return hkResult.getData(); } /** * 获取视频流地址 * * @see ... * @param query * @return */ public Map previewURLs(CameraPlayQuery query) { /** * STEP1:设置平台参数,根据实际情况,设置host appkey appsecret 三个参数. */ ArtemisConfig.host = hkProperties.getHost(); // 平台的ip端口 ArtemisConfig.appKey = hkProperties.getAk(); // 密钥appkey ArtemisConfig.appSecret = hkProperties.getSk();// 密钥appSecret /** * STEP2:设置OpenAPI接口的上下文 */ final String ARTEMIS_PATH = "/artemis"; /** * STEP3:设置接口的URI地址 */ final String previewURLsApi = ARTEMIS_PATH + "/api/video/v2/cameras/previewURLs"; Map path = new HashMap(2) { { put("https://", previewURLsApi);//根据现场环境部署确认是http还是https } }; /** * STEP4:设置参数提交方式 */ String contentType = "application/json"; /** * STEP5:组装请求参数 */ JSONObject jsonBody = new JSONObject(); jsonBody.put("cameraIndexCode", query.getIndexCode()); jsonBody.put("protocol", query.getProtocol()); // jsonBody.put("netZoneCode", 53); String body = jsonBody.toJSONString(); /** * STEP6:调用接口 */ String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);// post请求application/json类型参数 HKResult hkResult = JSON.parseObject(result, HKResult.class); return hkResult.getData(); } /** * 控制摄像头 * * @see ... * @param form * @return */ public Boolean controlling(CameraCommandForm form) { /** * STEP1:设置平台参数,根据实际情况,设置host appkey appsecret 三个参数. */ ArtemisConfig.host = hkProperties.getHost(); // 平台的ip端口 ArtemisConfig.appKey = hkProperties.getAk(); // 密钥appkey ArtemisConfig.appSecret = hkProperties.getSk();// 密钥appSecret /** * STEP2:设置OpenAPI接口的上下文 */ final String ARTEMIS_PATH = "/artemis"; /** * STEP3:设置接口的URI地址 */ final String controllerApi = ARTEMIS_PATH + "/api/video/v1/ptzs/controlling"; Map path = new HashMap(2) { { put("https://", controllerApi);//根据现场环境部署确认是http还是https } }; /** * STEP4:设置参数提交方式 */ String contentType = "application/json"; /** * STEP5:组装请求参数 */ JSONObject jsonBody = new JSONObject(); jsonBody.put("cameraIndexCode", form.getIndexCode()); jsonBody.put("action", form.getAction()); jsonBody.put("command", form.getCommand()); String body = jsonBody.toJSONString(); /** * STEP6:调用接口 */ String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);// post请求application/json类型参数 HKResult hkResult = JSON.parseObject(result, HKResult.class); return "success".equals(hkResult.getMsg()); } }