551bc150f27c774e5771535d4e76390a8000f587.svn-base 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package cn.com.goldenwater.dcproj.utils.http;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.hikvision.artemis.sdk.ArtemisHttpUtil;
  5. import com.hikvision.artemis.sdk.config.ArtemisConfig;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.stereotype.Component;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. /**
  11. * HikvisionOpenApiUtil
  12. * 调用海康威视的 安全综合管理平台的工具类
  13. *
  14. * @author lxf
  15. * @version 1.0
  16. * @date 2023/08/10 12:39
  17. **/
  18. @Component
  19. public class HikvisionOpenApiUtil {
  20. private static String apiHost;
  21. private static String apiKey;
  22. private static String apiSecrect;
  23. public static String getApiHost() {
  24. return apiHost;
  25. }
  26. @Value("${fj.video.SERVER}")
  27. public void setApiHost(String apiHost) {
  28. HikvisionOpenApiUtil.apiHost = apiHost;
  29. // 代理API网关nginx服务器ip端口
  30. ArtemisConfig.host = apiHost;
  31. }
  32. public static String getApiKey() {
  33. return apiKey;
  34. }
  35. @Value("${fj.video.APIKEY}")
  36. public void setApiKey(String apiKey) {
  37. HikvisionOpenApiUtil.apiKey = apiKey;
  38. // 秘钥appkey
  39. ArtemisConfig.appKey = apiKey;
  40. }
  41. public static String getApiSecrect() {
  42. return apiSecrect;
  43. }
  44. @Value("${fj.video.APISECRET}")
  45. public void setApiSecrect(String apiSecrect) {
  46. HikvisionOpenApiUtil.apiSecrect = apiSecrect;
  47. // 秘钥appSecret
  48. ArtemisConfig.appSecret = apiSecrect;
  49. }
  50. /**
  51. * 请根据自己的appKey和appSecret更换static静态块中的三个参数. [1 host]
  52. * 如果你选择的是和现场环境对接,host要修改为现场环境的ip,https端口默认为443,http端口默认为80.例如10.33.25.22:443 或者10.33.25.22:80
  53. * appKey和appSecret请按照或得到的appKey和appSecret更改.
  54. * TODO 调用前先要清楚接口传入的是什么,是传入json就用doPostStringArtemis方法,下载图片doPostStringImgArtemis方法
  55. */
  56. public static void initConfig(){
  57. // 代理API网关nginx服务器ip端口
  58. ArtemisConfig.host = getApiHost();
  59. // 秘钥appkey
  60. ArtemisConfig.appKey = getApiKey();
  61. // 秘钥appSecret
  62. ArtemisConfig.appSecret = getApiSecrect();
  63. }
  64. /**
  65. * 能力开放平台的网站路径
  66. * TODO 路径不用修改,就是/artemis
  67. */
  68. private static final String ARTEMIS_PATH = "/artemis";
  69. /**
  70. * 调用POST请求类型(application/json)接口,这里以入侵报警事件日志为例
  71. * https://open.hikvision.com/docs/918519baf9904844a2b608e558b21bb6#e6798840
  72. * 获取预览地址
  73. * @return
  74. */
  75. public static JSONObject callPostPreviewURLs(String cameraIndexCode){
  76. initConfig();
  77. /**
  78. * http://10.33.47.50/artemis/api/scpms/v1/eventLogs/searches
  79. * 根据API文档可以看出来,这是一个POST请求的Rest接口,而且传入的参数值为一个json
  80. * ArtemisHttpUtil工具类提供了doPostStringArtemis这个函数,一共六个参数在文档里写明其中的意思,因为接口是https,
  81. * 所以第一个参数path是一个hashmap类型,请put一个key-value,query为传入的参数,body为传入的json数据
  82. * 传入的contentType为application/json,accept不指定为null
  83. * header没有额外参数可不传,指定为null
  84. *
  85. */
  86. final String getCamsApi = ARTEMIS_PATH+"/api/video/v1/cameras/previewURLs";
  87. /**
  88. * {
  89. * "cameraIndexCode": "748d84750e3a4a5bbad3cd4af9ed5101",
  90. * "streamType": 0,
  91. * "protocol": "rtsp",
  92. * "transmode": 1,
  93. * "expand": "transcode=0",
  94. * "streamform": "ps"
  95. * }
  96. */
  97. Map<String, String> path = new HashMap<String, String>(2) {
  98. {
  99. put("http://", getCamsApi);//根据现场环境部署确认是http还是https
  100. }
  101. };
  102. JSONObject jsonBody = new JSONObject();
  103. jsonBody.put("cameraIndexCode", cameraIndexCode);
  104. jsonBody.put("streamType", 0);
  105. jsonBody.put("protocol", "rtsp");
  106. jsonBody.put("transmode", 1);
  107. jsonBody.put("expand", "streamform=rtp");
  108. jsonBody.put("streamform", "rtp");
  109. String body = jsonBody.toJSONString();
  110. try {
  111. String result = ArtemisHttpUtil.doPostStringArtemis(path,body,null,null,"application/json",null);// post请求application/json类型参数
  112. JSONObject jsonResponse = JSONObject.parseObject(result);
  113. JSONObject data = jsonResponse.getJSONObject("data");
  114. return data;
  115. } catch (Exception e) {
  116. return new JSONObject();
  117. }
  118. }
  119. /**
  120. * 根据水库名称 获取水库的监控点信息列表
  121. * @return
  122. */
  123. public static JSONArray callPostResourcesByParams(String rsName){
  124. initConfig();
  125. /**
  126. * http://10.33.47.50/artemis/api/scpms/v1/eventLogs/searches
  127. * 根据API文档可以看出来,这是一个POST请求的Rest接口,而且传入的参数值为一个json
  128. * ArtemisHttpUtil工具类提供了doPostStringArtemis这个函数,一共六个参数在文档里写明其中的意思,因为接口是https,
  129. * 所以第一个参数path是一个hashmap类型,请put一个key-value,query为传入的参数,body为传入的json数据
  130. * 传入的contentType为application/json,accept不指定为null
  131. * header没有额外参数可不传,指定为null
  132. *
  133. */
  134. final String getCamsApi = ARTEMIS_PATH+"/api/irds/v1/resource/resourcesByParams";
  135. /**
  136. * {
  137. * "catalogCode": "region",
  138. * "name": "山仔水库",
  139. * "resourceType": "camera",
  140. * "pageNo": 1,
  141. * "pageSize": 20
  142. * }
  143. */
  144. Map<String, String> path = new HashMap<String, String>(2) {
  145. {
  146. put("http://", getCamsApi);//根据现场环境部署确认是http还是https
  147. }
  148. };
  149. JSONObject jsonBody = new JSONObject();
  150. jsonBody.put("name", rsName);
  151. jsonBody.put("catalogCode", "region");
  152. jsonBody.put("resourceType", "camera");
  153. jsonBody.put("pageNo", 1);
  154. jsonBody.put("pageSize", 100);
  155. String body = jsonBody.toJSONString();
  156. try {
  157. String result = ArtemisHttpUtil.doPostStringArtemis(path,body,null,null,"application/json",null);// post请求application/json类型参数
  158. JSONObject jsonResponse = JSONObject.parseObject(result);
  159. JSONObject data = jsonResponse.getJSONObject("data");
  160. JSONArray jsonArray = data.getJSONArray("list");
  161. return jsonArray;
  162. } catch (Exception e) {
  163. e.printStackTrace();
  164. return new JSONArray();
  165. }
  166. }
  167. }