OpenAPIService.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package com.persagy.cameractl.service;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import com.persagy.cameractl.conf.CameraApiConfig;
  7. import com.persagy.cameractl.utils.Camera;
  8. import com.persagy.cameractl.utils.ResultClass;
  9. import cn.hutool.core.util.StrUtil;
  10. import cn.hutool.crypto.digest.MD5;
  11. import cn.hutool.http.HttpUtil;
  12. import cn.hutool.json.JSONArray;
  13. import cn.hutool.json.JSONObject;
  14. /**
  15. * 本类中的 openAPI 为 IVMS 8700,非综合安防管理平台,所有接口默认超时20s,且不支持配置,所以我们这里配置为30s
  16. *
  17. * @version 1.0.0
  18. * @company persagy
  19. * @author zhangqiankun
  20. * @date 2022年6月16日 上午9:52:52
  21. */
  22. @Service
  23. public class OpenAPIService {
  24. public static final Logger logger = LoggerFactory.getLogger(OpenAPIService.class);
  25. public static final String URL_PARAM_TOKEN = "?token=";
  26. /** 获取默认用户UUID【V2.7】 */
  27. public static final String GET_DEFAULT_USER_ID = "/openapi/service/base/user/getDefaultUserUuid";
  28. /** 获取所有网域【V2.7】 */
  29. public static final String GET_ALL_NET_ZONE = "/openapi/service/base/netZone/getNetZones";
  30. /** 根据监控点UUID集和网域UUID分页获取录像计划【V2.7】 */
  31. public static final String GET_RECORDPLANS_BY_CAMERAUUID = "/openapi/service/vss/playback/getRecordPlansByCameraUuids";
  32. /** 根据录像计划UUID和网域UUID获取回放参数【V2.7】 */
  33. public static final String PLAY_BACK_URI = "/openapi/service/vss/playback/getPlaybackParamByPlanUuid";
  34. @Autowired
  35. private CameraApiConfig cameraApiConfig;
  36. private volatile String opUserUuid;
  37. private volatile String netZoneUuid;
  38. public ResultClass playBack(Camera camera) {
  39. if (!StrUtil.isAllNotBlank(opUserUuid, netZoneUuid)) {
  40. throw new RuntimeException("missing parameter[opUserUuid、netZoneUuid]");
  41. }
  42. ResultClass returnResult = new ResultClass();
  43. // 1.获取录像计划UUID
  44. JSONObject recordPlansByCameraUuids = this.getRecordPlansByCameraUuids(camera);
  45. if (recordPlansByCameraUuids == null) {
  46. throw new RuntimeException("not exist recordPlan, cameraUUID: " + camera.cameraIndexCode);
  47. }
  48. // 2.根据录像计划UUID,获取回放视频
  49. String paramByPlanUuid = this.getPlaybackParamByPlanUuid(camera, recordPlansByCameraUuids);
  50. returnResult.resultData = paramByPlanUuid;
  51. returnResult.name = true;
  52. returnResult.reason = "回放文件获取成功";
  53. return returnResult;
  54. }
  55. /**
  56. * 获取回放文本
  57. *
  58. * @param camera
  59. * @param recordPlansByCameraUuids
  60. * @return
  61. * @date 2022年6月20日 下午3:46:11
  62. */
  63. public String getPlaybackParamByPlanUuid(Camera camera, JSONObject recordPlansByCameraUuids) {
  64. // 获取录像计划UUID
  65. long currentTimeMillis = System.currentTimeMillis();
  66. JSONObject params = new JSONObject();
  67. params.set("appkey", cameraApiConfig.getHkApiKey());
  68. params.set("time", currentTimeMillis);
  69. params.set("planType", recordPlansByCameraUuids.getInt("planType"));
  70. params.set("recordPlanUuid", recordPlansByCameraUuids.getStr("recordPlanUuid"));
  71. params.set("opUserUuid", opUserUuid);
  72. params.set("netZoneUuid", netZoneUuid);
  73. String playBackStr = PLAY_BACK_URI + params.toString() + cameraApiConfig.getHkApiSecret();
  74. logger.info("md5 playBackStr: {}", playBackStr);
  75. String playBackToken = MD5.create().digestHex(playBackStr).toUpperCase();
  76. String playBackContent = HttpUtil.post(PLAY_BACK_URI + URL_PARAM_TOKEN + playBackToken, params.toString(), 30000);
  77. logger.info("playBackContent: {}", playBackContent);
  78. JSONObject playBackJson = convert2Json(playBackContent);
  79. JSONObject playBackData = playBackJson.getJSONObject("data");
  80. if (playBackData != null) {
  81. return playBackData.getStr("playBackXml");
  82. }
  83. return null;
  84. }
  85. /**
  86. * 获取录像计划UUID
  87. *
  88. * @param camera
  89. * @return
  90. * @date 2022年6月20日 下午3:46:11
  91. */
  92. public JSONObject getRecordPlansByCameraUuids(Camera camera) {
  93. // 获取录像计划UUID
  94. long currentTimeMillis = System.currentTimeMillis();
  95. JSONObject params = new JSONObject();
  96. params.set("appkey", cameraApiConfig.getHkApiKey());
  97. params.set("time", currentTimeMillis);
  98. params.set("pageNo", 1);
  99. params.set("pageSize", 15);
  100. params.set("opUserUuid", opUserUuid);
  101. params.set("cameraUuids", camera.cameraIndexCode);
  102. params.set("netZoneUuid", netZoneUuid);
  103. String recordPlanStr = GET_RECORDPLANS_BY_CAMERAUUID + params.toString() + cameraApiConfig.getHkApiSecret();
  104. logger.info("md5 recordPlanStr: {}", recordPlanStr);
  105. String recordPlanToken = MD5.create().digestHex(recordPlanStr).toUpperCase();
  106. String recordPlanContent = HttpUtil.post(GET_RECORDPLANS_BY_CAMERAUUID + URL_PARAM_TOKEN + recordPlanToken, params.toString(), 30000);
  107. logger.info("recordPlanContent: {}", recordPlanContent);
  108. JSONObject recordPlanJson = convert2Json(recordPlanContent);
  109. JSONObject recordPlanData = recordPlanJson.getJSONObject("data");
  110. if (recordPlanData != null) {
  111. JSONArray jsonArray = recordPlanData.getJSONArray("list");
  112. if (jsonArray == null || jsonArray.size() == 0) {
  113. return null;
  114. }
  115. return jsonArray.getJSONObject(0);
  116. }
  117. return null;
  118. }
  119. /**
  120. * 获取默认的用户ID和网域ID
  121. *
  122. * @date 2022年6月20日 下午3:18:36
  123. */
  124. public synchronized void queryUserIdAndNetZoneId() {
  125. // 1.获取默认用户ID
  126. long currentTimeMillis = System.currentTimeMillis();
  127. JSONObject params = new JSONObject();
  128. params.set("appkey", cameraApiConfig.getHkApiKey());
  129. params.set("time", currentTimeMillis);
  130. String userStr = GET_DEFAULT_USER_ID + params.toString() + cameraApiConfig.getHkApiSecret();
  131. logger.info("md5 userStr: {}", userStr);
  132. String userToken = MD5.create().digestHex(userStr).toUpperCase();
  133. String userContent = HttpUtil.post(GET_DEFAULT_USER_ID + URL_PARAM_TOKEN + userToken, params.toString(), 30000);
  134. logger.info("userContent: {}", userContent);
  135. JSONObject defaultUser = convert2Json(userContent);
  136. this.opUserUuid = defaultUser.getStr("data");
  137. if (StrUtil.isBlank(opUserUuid)) {
  138. throw new RuntimeException("not exist default user id");
  139. }
  140. // 2.获取网域ID
  141. long currentTime = System.currentTimeMillis();
  142. JSONObject zoneObject = new JSONObject();
  143. zoneObject.set("appkey", cameraApiConfig.getHkApiKey());
  144. zoneObject.set("time", currentTime);
  145. zoneObject.set("opUserUuid", opUserUuid);
  146. String zoneStr = GET_ALL_NET_ZONE + zoneObject.toString() + cameraApiConfig.getHkApiSecret();
  147. logger.info("md5 zoneStr: {}", zoneStr);
  148. String zoneToken = MD5.create().digestHex(zoneStr).toUpperCase();
  149. String zoneContent = HttpUtil.post(GET_ALL_NET_ZONE + URL_PARAM_TOKEN + zoneToken, zoneObject.toString(), 30000);
  150. logger.info("zoneContent: {}", zoneContent);
  151. JSONObject defaultZone = convert2Json(zoneContent);
  152. JSONArray defaultZoneArray = defaultZone.getJSONArray("data");
  153. if (defaultZoneArray != null && defaultZoneArray.size() != 0) {
  154. JSONObject jsonObject = defaultZoneArray.getJSONObject(0);
  155. this.netZoneUuid = jsonObject.getStr("netZoneUuid");
  156. if (StrUtil.isBlank(netZoneUuid)) {
  157. throw new RuntimeException("not exist default zone id");
  158. }
  159. }
  160. }
  161. /**
  162. * 将响应数据转为JSON对象
  163. *
  164. * @param responseContent
  165. * @return
  166. * @date 2022年6月20日 下午2:55:18
  167. */
  168. private static JSONObject convert2Json(String responseContent) {
  169. if (StrUtil.isBlank(responseContent)) {
  170. logger.error("reponse is null 【】" + responseContent);
  171. throw new RuntimeException("响应数据为空");
  172. }
  173. JSONObject jsonObject = new JSONObject(responseContent, false);
  174. return jsonObject;
  175. }
  176. }