| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- package com.persagy.cameractl.service;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import com.persagy.cameractl.conf.CameraApiConfig;
- import com.persagy.cameractl.utils.Camera;
- import com.persagy.cameractl.utils.ResultClass;
- import cn.hutool.core.util.StrUtil;
- import cn.hutool.crypto.digest.MD5;
- import cn.hutool.http.HttpUtil;
- import cn.hutool.json.JSONArray;
- import cn.hutool.json.JSONObject;
- /**
- * 本类中的 openAPI 为 IVMS 8700,非综合安防管理平台,所有接口默认超时20s,且不支持配置,所以我们这里配置为30s
- *
- * @version 1.0.0
- * @company persagy
- * @author zhangqiankun
- * @date 2022年6月16日 上午9:52:52
- */
- @Service
- public class OpenAPIService {
- public static final Logger logger = LoggerFactory.getLogger(OpenAPIService.class);
-
- public static final String URL_PARAM_TOKEN = "?token=";
- /** 获取默认用户UUID【V2.7】 */
- public static final String GET_DEFAULT_USER_ID = "/openapi/service/base/user/getDefaultUserUuid";
- /** 获取所有网域【V2.7】 */
- public static final String GET_ALL_NET_ZONE = "/openapi/service/base/netZone/getNetZones";
- /** 根据监控点UUID集和网域UUID分页获取录像计划【V2.7】 */
- public static final String GET_RECORDPLANS_BY_CAMERAUUID = "/openapi/service/vss/playback/getRecordPlansByCameraUuids";
- /** 根据录像计划UUID和网域UUID获取回放参数【V2.7】 */
- public static final String PLAY_BACK_URI = "/openapi/service/vss/playback/getPlaybackParamByPlanUuid";
- @Autowired
- private CameraApiConfig cameraApiConfig;
-
- private volatile String opUserUuid;
- private volatile String netZoneUuid;
-
- public ResultClass playBack(Camera camera) {
- if (!StrUtil.isAllNotBlank(opUserUuid, netZoneUuid)) {
- throw new RuntimeException("missing parameter[opUserUuid、netZoneUuid]");
- }
- ResultClass returnResult = new ResultClass();
-
- // 1.获取录像计划UUID
- JSONObject recordPlansByCameraUuids = this.getRecordPlansByCameraUuids(camera);
- if (recordPlansByCameraUuids == null) {
- throw new RuntimeException("not exist recordPlan, cameraUUID: " + camera.cameraIndexCode);
- }
-
- // 2.根据录像计划UUID,获取回放视频
- String paramByPlanUuid = this.getPlaybackParamByPlanUuid(camera, recordPlansByCameraUuids);
- returnResult.resultData = paramByPlanUuid;
- returnResult.name = true;
- returnResult.reason = "回放文件获取成功";
- return returnResult;
- }
- /**
- * 获取回放文本
- *
- * @param camera
- * @param recordPlansByCameraUuids
- * @return
- * @date 2022年6月20日 下午3:46:11
- */
- public String getPlaybackParamByPlanUuid(Camera camera, JSONObject recordPlansByCameraUuids) {
- // 获取录像计划UUID
- long currentTimeMillis = System.currentTimeMillis();
-
- JSONObject params = new JSONObject();
- params.set("appkey", cameraApiConfig.getHkApiKey());
- params.set("time", currentTimeMillis);
- params.set("planType", recordPlansByCameraUuids.getInt("planType"));
- params.set("recordPlanUuid", recordPlansByCameraUuids.getStr("recordPlanUuid"));
- params.set("opUserUuid", opUserUuid);
- params.set("netZoneUuid", netZoneUuid);
- String playBackStr = PLAY_BACK_URI + params.toString() + cameraApiConfig.getHkApiSecret();
- logger.info("md5 playBackStr: {}", playBackStr);
-
- String playBackToken = MD5.create().digestHex(playBackStr).toUpperCase();
- String playBackContent = HttpUtil.post(PLAY_BACK_URI + URL_PARAM_TOKEN + playBackToken, params.toString(), 30000);
- logger.info("playBackContent: {}", playBackContent);
-
- JSONObject playBackJson = convert2Json(playBackContent);
- JSONObject playBackData = playBackJson.getJSONObject("data");
- if (playBackData != null) {
- return playBackData.getStr("playBackXml");
- }
- return null;
- }
-
- /**
- * 获取录像计划UUID
- *
- * @param camera
- * @return
- * @date 2022年6月20日 下午3:46:11
- */
- public JSONObject getRecordPlansByCameraUuids(Camera camera) {
- // 获取录像计划UUID
- long currentTimeMillis = System.currentTimeMillis();
-
- JSONObject params = new JSONObject();
- params.set("appkey", cameraApiConfig.getHkApiKey());
- params.set("time", currentTimeMillis);
- params.set("pageNo", 1);
- params.set("pageSize", 15);
- params.set("opUserUuid", opUserUuid);
- params.set("cameraUuids", camera.cameraIndexCode);
- params.set("netZoneUuid", netZoneUuid);
- String recordPlanStr = GET_RECORDPLANS_BY_CAMERAUUID + params.toString() + cameraApiConfig.getHkApiSecret();
- logger.info("md5 recordPlanStr: {}", recordPlanStr);
-
- String recordPlanToken = MD5.create().digestHex(recordPlanStr).toUpperCase();
- String recordPlanContent = HttpUtil.post(GET_RECORDPLANS_BY_CAMERAUUID + URL_PARAM_TOKEN + recordPlanToken, params.toString(), 30000);
- logger.info("recordPlanContent: {}", recordPlanContent);
-
- JSONObject recordPlanJson = convert2Json(recordPlanContent);
- JSONObject recordPlanData = recordPlanJson.getJSONObject("data");
- if (recordPlanData != null) {
- JSONArray jsonArray = recordPlanData.getJSONArray("list");
- if (jsonArray == null || jsonArray.size() == 0) {
- return null;
- }
- return jsonArray.getJSONObject(0);
- }
- return null;
- }
-
- /**
- * 获取默认的用户ID和网域ID
- *
- * @date 2022年6月20日 下午3:18:36
- */
- public synchronized void queryUserIdAndNetZoneId() {
- // 1.获取默认用户ID
- long currentTimeMillis = System.currentTimeMillis();
-
- JSONObject params = new JSONObject();
- params.set("appkey", cameraApiConfig.getHkApiKey());
- params.set("time", currentTimeMillis);
- String userStr = GET_DEFAULT_USER_ID + params.toString() + cameraApiConfig.getHkApiSecret();
- logger.info("md5 userStr: {}", userStr);
-
- String userToken = MD5.create().digestHex(userStr).toUpperCase();
- String userContent = HttpUtil.post(GET_DEFAULT_USER_ID + URL_PARAM_TOKEN + userToken, params.toString(), 30000);
- logger.info("userContent: {}", userContent);
-
- JSONObject defaultUser = convert2Json(userContent);
- this.opUserUuid = defaultUser.getStr("data");
- if (StrUtil.isBlank(opUserUuid)) {
- throw new RuntimeException("not exist default user id");
- }
-
- // 2.获取网域ID
- long currentTime = System.currentTimeMillis();
-
- JSONObject zoneObject = new JSONObject();
- zoneObject.set("appkey", cameraApiConfig.getHkApiKey());
- zoneObject.set("time", currentTime);
- zoneObject.set("opUserUuid", opUserUuid);
- String zoneStr = GET_ALL_NET_ZONE + zoneObject.toString() + cameraApiConfig.getHkApiSecret();
- logger.info("md5 zoneStr: {}", zoneStr);
-
- String zoneToken = MD5.create().digestHex(zoneStr).toUpperCase();
- String zoneContent = HttpUtil.post(GET_ALL_NET_ZONE + URL_PARAM_TOKEN + zoneToken, zoneObject.toString(), 30000);
- logger.info("zoneContent: {}", zoneContent);
-
- JSONObject defaultZone = convert2Json(zoneContent);
- JSONArray defaultZoneArray = defaultZone.getJSONArray("data");
- if (defaultZoneArray != null && defaultZoneArray.size() != 0) {
- JSONObject jsonObject = defaultZoneArray.getJSONObject(0);
- this.netZoneUuid = jsonObject.getStr("netZoneUuid");
- if (StrUtil.isBlank(netZoneUuid)) {
- throw new RuntimeException("not exist default zone id");
- }
- }
- }
-
- /**
- * 将响应数据转为JSON对象
- *
- * @param responseContent
- * @return
- * @date 2022年6月20日 下午2:55:18
- */
- private static JSONObject convert2Json(String responseContent) {
- if (StrUtil.isBlank(responseContent)) {
- logger.error("reponse is null 【】" + responseContent);
- throw new RuntimeException("响应数据为空");
- }
-
- JSONObject jsonObject = new JSONObject(responseContent, false);
- return jsonObject;
- }
-
- }
|