RealTimePreviewVideoController.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. package com.zksy.visualization.controller;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.databind.JsonNode;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import com.zksy.common.core.domain.Result;
  6. import com.zksy.visualization.config.IccConfigProperty;
  7. import com.zksy.visualization.domain.request.*;
  8. import com.zksy.visualization.domain.response.RtspUrlResponse;
  9. import io.swagger.annotations.Api;
  10. import io.swagger.annotations.ApiOperation;
  11. import lombok.extern.slf4j.Slf4j;
  12. import okhttp3.*;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.data.redis.core.RedisTemplate;
  15. import org.springframework.web.bind.annotation.PostMapping;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import java.io.IOException;
  19. import java.text.ParseException;
  20. import java.text.SimpleDateFormat;
  21. import java.util.Date;
  22. /**
  23. * @author Administrator
  24. * @version 1.0
  25. * @project dh-server-micro
  26. * @description 视频实时预览
  27. * @date 2024/12/12 13:32:23
  28. */
  29. @RestController
  30. @Api(tags = "视频实时预览", description = "视频实时预览")
  31. @RequestMapping("/realTimePreviewVideo")
  32. @Slf4j
  33. public class RealTimePreviewVideoController {
  34. @Autowired
  35. private IccConfigProperty iccConfigProperty;
  36. @Autowired
  37. private OkHttpClient httpClient;
  38. @Autowired
  39. private RedisTemplate<String,String> redisTemplate;
  40. private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
  41. /**
  42. * TODO 视频路径
  43. * @param
  44. * @return void
  45. * @author Administrator
  46. * @date 2024/12/12 14:43:37
  47. */
  48. @ApiOperation(value = "获取视频路径")
  49. @PostMapping("/getRtspUrl")
  50. public Result getRtspUrl(RtspUrlRequest request){
  51. ObjectMapper objectMapper = new ObjectMapper();
  52. String jsonParams = null;
  53. try {
  54. jsonParams = objectMapper.writeValueAsString(request);
  55. } catch (JsonProcessingException e) {
  56. log.error("JSON序列化失败: {}", e);
  57. return Result.error("请求失败");
  58. }
  59. String authorization = redisTemplate
  60. .opsForValue()
  61. .get("Authorization:" + iccConfigProperty.getUsername());
  62. // 创建请求体
  63. RequestBody body = RequestBody.create(jsonParams, JSON);
  64. // 构建请求
  65. Request requestHttp = new Request.Builder()
  66. .url("https://" + iccConfigProperty.getHost() + "/evo-apigw/admin/API/MTS/Video/StartVideo")
  67. .addHeader("Authorization", authorization)
  68. .post(body)
  69. .build();
  70. // 发送请求
  71. try (Response response = httpClient.newCall(requestHttp).execute()) {
  72. if (!response.isSuccessful()) {
  73. return Result.error("请求失败");
  74. }
  75. ResponseBody responseBody = response.body();
  76. if (responseBody != null) {
  77. String responseString = responseBody.string();
  78. JsonNode rootNode = objectMapper.readTree(responseString);
  79. JsonNode success = rootNode.path("success");
  80. if(success.isBoolean() && success.asBoolean()) {
  81. JsonNode dataNode = rootNode.path("data");
  82. if (dataNode.isObject()) {
  83. RtspUrlResponse intelligentPanelResponse = objectMapper.treeToValue(dataNode, RtspUrlResponse.class);
  84. return Result.ok(intelligentPanelResponse);
  85. } else {
  86. return Result.ok(null);
  87. }
  88. }else {
  89. JsonNode errMsgNode = rootNode.path("errMsg");
  90. String errMsg = errMsgNode.isTextual() ? errMsgNode.asText() : "未知错误";
  91. return Result.error(errMsg);
  92. }
  93. }
  94. } catch (IOException e) {
  95. log.error("请求失败: {}", e);
  96. }
  97. return Result.error("请求失败");
  98. }
  99. /**
  100. * 获取RTSP流地址
  101. * @param rtspUrlRequest
  102. * @return
  103. */
  104. /* public RtspUrlResponse getRtspUrl1(RtspUrlRequest rtspUrlRequest){
  105. RtspUrlResponse response=null;
  106. OauthConfigUserPwdInfo config = oauthConfigUtil.getOauthConfig();
  107. try {
  108. log.info("RealTimePreviewDemo,getRtspUrl,request:{}", JSONUtil.toJsonStr(rtspUrlRequest));
  109. response = HttpUtils.executeJson("/evo-apigw/admin/API/MTS/Video/StartVideo", rtspUrlRequest,null, Method.POST , config, RtspUrlResponse.class);
  110. log.info("RealTimePreviewDemo,getRtspUrl,response:{}", JSONUtil.toJsonStr(response));
  111. } catch (ClientException e) {
  112. log.error(e.getErrMsg(), e);
  113. }
  114. if(!response.getCode().equals("1000")) {
  115. log.info("获取rtsp流地址失败:{}",response.getErrMsg());
  116. }
  117. return response;
  118. }*/
  119. /* @ApiOperation(value = "rtsp以文件形式回放录像")
  120. @PostMapping("/getRTSPFileReplay")
  121. public Result getRTSPFileReplay(VideoRecordQueryRequest request){
  122. ObjectMapper objectMapper = new ObjectMapper();
  123. String jsonParams = null;
  124. try {
  125. jsonParams = objectMapper.writeValueAsString(request);
  126. } catch (JsonProcessingException e) {
  127. log.error("JSON序列化失败: {}", e);
  128. return Result.error("请求失败");
  129. }
  130. String authorization = redisTemplate
  131. .opsForValue()
  132. .get("Authorization:" + iccConfigProperty.getUsername());
  133. // 创建请求体
  134. RequestBody body = RequestBody.create(jsonParams, JSON);
  135. // 构建请求
  136. Request requestHttp = new Request.Builder()
  137. .url("https://" + iccConfigProperty.getHost() + "/evo-apigw/admin/API/SS/Playback/StartPlaybackByFile")
  138. .addHeader("Authorization", authorization)
  139. .post(body)
  140. .build();
  141. // 发送请求
  142. try (Response response = httpClient.newCall(requestHttp).execute()) {
  143. if (!response.isSuccessful()) {
  144. return Result.error("请求失败");
  145. }
  146. ResponseBody responseBody = response.body();
  147. if (responseBody != null) {
  148. String responseString = responseBody.string();
  149. JsonNode rootNode = objectMapper.readTree(responseString);
  150. JsonNode success = rootNode.path("success");
  151. if("true".equals(success.asText())) {
  152. JsonNode dataNode = rootNode.path("data");
  153. if (dataNode != null) {
  154. return Result.ok(dataNode);
  155. } else {
  156. return Result.ok(null);
  157. }
  158. }else {
  159. return Result.error("rtsp以文件形式回放录像失败");
  160. }
  161. }
  162. } catch (IOException e) {
  163. log.error("请求失败: {}", e);
  164. }
  165. return Result.error("请求失败");
  166. }*/
  167. @ApiOperation(value = "rtsp以时间形式回放录像")
  168. @PostMapping("/getRTSPDateReplay")
  169. public Result getRTSPDateReplay(VideoRecordDateQueryRequest request){
  170. String startTime = DateToTimeStamp(request.getData().getStartTime());
  171. String endTime = DateToTimeStamp(request.getData().getEndTime());
  172. request.getData().setStartTime(startTime);
  173. request.getData().setEndTime(endTime);
  174. ObjectMapper objectMapper = new ObjectMapper();
  175. String jsonParams = null;
  176. try {
  177. jsonParams = objectMapper.writeValueAsString(request);
  178. } catch (JsonProcessingException e) {
  179. log.error("JSON序列化失败: {}", e);
  180. return Result.error("请求失败");
  181. }
  182. String authorization = redisTemplate
  183. .opsForValue()
  184. .get("Authorization:" + iccConfigProperty.getUsername());
  185. // 创建请求体
  186. RequestBody body = RequestBody.create(jsonParams, JSON);
  187. // 构建请求
  188. Request requestHttp = new Request.Builder()
  189. .url("https://" + iccConfigProperty.getHost() + "/evo-apigw/admin/API/SS/Playback/StartPlaybackByTime")
  190. .addHeader("Authorization", authorization)
  191. .post(body)
  192. .build();
  193. // 发送请求
  194. try (Response response = httpClient.newCall(requestHttp).execute()) {
  195. if (!response.isSuccessful()) {
  196. return Result.error("请求失败");
  197. }
  198. ResponseBody responseBody = response.body();
  199. if (responseBody != null) {
  200. String responseString = responseBody.string();
  201. JsonNode rootNode = objectMapper.readTree(responseString);
  202. JsonNode success = rootNode.path("success");
  203. if("true".equals(success.asText())) {
  204. JsonNode dataNode = rootNode.path("data");
  205. if (dataNode != null) {
  206. return Result.ok(dataNode);
  207. } else {
  208. return Result.ok(null);
  209. }
  210. }else {
  211. return Result.error("rtsp以时间形式回放录像失败");
  212. }
  213. }
  214. } catch (IOException e) {
  215. log.error("请求失败: {}", e);
  216. }
  217. return Result.error("请求失败");
  218. }
  219. @ApiOperation(value = "HLS、RTMP录像回放(FLV不支持)")
  220. @PostMapping("/getHRReplay")
  221. public Result getHRReplay(VideoRecordHRQueryRequest request){
  222. ObjectMapper objectMapper = new ObjectMapper();
  223. String jsonParams = null;
  224. try {
  225. jsonParams = objectMapper.writeValueAsString(request);
  226. } catch (JsonProcessingException e) {
  227. log.error("JSON序列化失败: {}", e);
  228. return Result.error("请求失败");
  229. }
  230. String authorization = redisTemplate
  231. .opsForValue()
  232. .get("Authorization:" + iccConfigProperty.getUsername());
  233. // 创建请求体
  234. RequestBody body = RequestBody.create(jsonParams, JSON);
  235. // 构建请求
  236. Request requestHttp = new Request.Builder()
  237. .url("https://" + iccConfigProperty.getHost() + "/evo-apigw/admin/API/video/stream/record")
  238. .addHeader("Authorization", authorization)
  239. .post(body)
  240. .build();
  241. // 发送请求
  242. try (Response response = httpClient.newCall(requestHttp).execute()) {
  243. if (!response.isSuccessful()) {
  244. return Result.error("请求失败");
  245. }
  246. ResponseBody responseBody = response.body();
  247. if (responseBody != null) {
  248. String responseString = responseBody.string();
  249. JsonNode rootNode = objectMapper.readTree(responseString);
  250. JsonNode success = rootNode.path("success");
  251. if("true".equals(success.asText())) {
  252. JsonNode dataNode = rootNode.path("data");
  253. if (dataNode != null) {
  254. return Result.ok(dataNode);
  255. } else {
  256. return Result.ok(null);
  257. }
  258. }else {
  259. JsonNode code = rootNode.path("code");
  260. if("2239".equals(code.asText())){
  261. return Result.error("未查询到录像");
  262. }else {
  263. return Result.error("HLS、RTMP录像回放(FLV不支持)失败");
  264. }
  265. }
  266. }
  267. } catch (IOException e) {
  268. log.error("请求失败: {}", e);
  269. }
  270. return Result.error("请求失败");
  271. }
  272. @ApiOperation(value = "云台镜头控制",notes = "控制云台镜头的变焦、变倍及光圈大小\n" +
  273. "命令command=1时,云台会开启动作,不调command=0会一直转动\n" +
  274. "异步发送操作指令到设备端,需开启动作与停止动作间隔一定时间,保证顺序执行\n" +
  275. "不推荐多用户同时操作同一个设备,多用户操作以最后一次操作为准")
  276. @PostMapping("/getOperateCamera")
  277. public Result getOperateCamera(CameraControlRequest request){
  278. ObjectMapper objectMapper = new ObjectMapper();
  279. String jsonParams = null;
  280. try {
  281. jsonParams = objectMapper.writeValueAsString(request);
  282. } catch (JsonProcessingException e) {
  283. log.error("JSON序列化失败: {}", e);
  284. return Result.error("请求失败");
  285. }
  286. String authorization = redisTemplate
  287. .opsForValue()
  288. .get("Authorization:" + iccConfigProperty.getUsername());
  289. // 创建请求体
  290. RequestBody body = RequestBody.create(jsonParams, JSON);
  291. // 构建请求
  292. Request requestHttp = new Request.Builder()
  293. .url("https://" + iccConfigProperty.getHost() + "/evo-apigw/admin/API/DMS/Ptz/OperateCamera")
  294. .addHeader("Authorization", authorization)
  295. .post(body)
  296. .build();
  297. // 发送请求
  298. try (Response response = httpClient.newCall(requestHttp).execute()) {
  299. if (!response.isSuccessful()) {
  300. return Result.error("请求失败");
  301. }
  302. ResponseBody responseBody = response.body();
  303. if (responseBody != null) {
  304. String responseString = responseBody.string();
  305. JsonNode rootNode = objectMapper.readTree(responseString);
  306. JsonNode success = rootNode.path("success");
  307. if("true".equals(success.asText())) {
  308. JsonNode dataNode = rootNode.path("data");
  309. if (dataNode != null) {
  310. return Result.ok(dataNode);
  311. } else {
  312. return Result.ok(null);
  313. }
  314. }else {
  315. JsonNode code = rootNode.path("code");
  316. if("1001".equals(code.asText())){
  317. return Result.error("返回失败");
  318. }else {
  319. return Result.error("云台镜头控制失败");
  320. }
  321. }
  322. }
  323. } catch (IOException e) {
  324. log.error("请求失败: {}", e);
  325. }
  326. return Result.error("请求失败");
  327. }
  328. @ApiOperation(value = "云台镜头控制",notes = "控制云台进行上下左右等八个方向的移动 命令command=1时,云台会开启动作,不调command=0会一直转动\n" +
  329. "异步发送操作指令到设备端,需开启动作与停止动作间隔一定时间,保证顺序执行\n" +
  330. "不推荐多用户同时操作同一个设备,多用户操作以最后一次操作为准")
  331. @PostMapping("/getOperateDirect")
  332. public Result getOperateDirect(OperateDirectRequest request){
  333. ObjectMapper objectMapper = new ObjectMapper();
  334. String jsonParams = null;
  335. try {
  336. jsonParams = objectMapper.writeValueAsString(request);
  337. } catch (JsonProcessingException e) {
  338. log.error("JSON序列化失败: {}", e);
  339. return Result.error("请求失败");
  340. }
  341. String authorization = redisTemplate
  342. .opsForValue()
  343. .get("Authorization:" + iccConfigProperty.getUsername());
  344. // 创建请求体
  345. RequestBody body = RequestBody.create(jsonParams, JSON);
  346. // 构建请求
  347. Request requestHttp = new Request.Builder()
  348. .url("https://" + iccConfigProperty.getHost() + "/evo-apigw/admin/API/DMS/Ptz/OperateDirect")
  349. .addHeader("Authorization", authorization)
  350. .post(body)
  351. .build();
  352. // 发送请求
  353. try (Response response = httpClient.newCall(requestHttp).execute()) {
  354. if (!response.isSuccessful()) {
  355. return Result.error("请求失败");
  356. }
  357. ResponseBody responseBody = response.body();
  358. if (responseBody != null) {
  359. String responseString = responseBody.string();
  360. JsonNode rootNode = objectMapper.readTree(responseString);
  361. JsonNode success = rootNode.path("success");
  362. if("true".equals(success.asText())) {
  363. JsonNode dataNode = rootNode.path("data");
  364. if (dataNode != null) {
  365. return Result.ok(dataNode);
  366. } else {
  367. return Result.ok(null);
  368. }
  369. }else {
  370. JsonNode code = rootNode.path("code");
  371. if("1001".equals(code.asText())){
  372. return Result.error("返回失败");
  373. }else {
  374. return Result.error("云台方向控制失败");
  375. }
  376. }
  377. }
  378. } catch (IOException e) {
  379. log.error("请求失败: {}", e);
  380. }
  381. return Result.error("请求失败");
  382. }
  383. @ApiOperation(value = "云台功能控制",notes = "控制云台的巡航、循迹及灯光等功能 命令command=1时,云台会开启动作,不调command=0会一直转动\n" +
  384. "异步发送操作指令到设备端,需开启动作与停止动作间隔一定时间,保证顺序执行\n" +
  385. "不推荐多用户同时操作同一个设备,多用户操作以最后一次操作为准")
  386. @PostMapping("/getOperateFunction")
  387. public Result getOperateFunction(OperateDirectRequest request){
  388. ObjectMapper objectMapper = new ObjectMapper();
  389. String jsonParams = null;
  390. try {
  391. jsonParams = objectMapper.writeValueAsString(request);
  392. } catch (JsonProcessingException e) {
  393. log.error("JSON序列化失败: {}", e);
  394. return Result.error("请求失败");
  395. }
  396. String authorization = redisTemplate
  397. .opsForValue()
  398. .get("Authorization:" + iccConfigProperty.getUsername());
  399. // 创建请求体
  400. RequestBody body = RequestBody.create(jsonParams, JSON);
  401. // 构建请求
  402. Request requestHttp = new Request.Builder()
  403. .url("https://" + iccConfigProperty.getHost() + "/evo-apigw/admin/API/DMS/Ptz/OperateFunction")
  404. .addHeader("Authorization", authorization)
  405. .post(body)
  406. .build();
  407. // 发送请求
  408. try (Response response = httpClient.newCall(requestHttp).execute()) {
  409. if (!response.isSuccessful()) {
  410. return Result.error("请求失败");
  411. }
  412. ResponseBody responseBody = response.body();
  413. if (responseBody != null) {
  414. String responseString = responseBody.string();
  415. JsonNode rootNode = objectMapper.readTree(responseString);
  416. JsonNode success = rootNode.path("success");
  417. if("true".equals(success.asText())) {
  418. JsonNode dataNode = rootNode.path("data");
  419. if (dataNode != null) {
  420. return Result.ok(dataNode);
  421. } else {
  422. return Result.ok(null);
  423. }
  424. }else {
  425. JsonNode code = rootNode.path("code");
  426. if("1001".equals(code.asText())){
  427. return Result.error("返回失败");
  428. }else {
  429. return Result.error("云台功能控制失败");
  430. }
  431. }
  432. }
  433. } catch (IOException e) {
  434. log.error("请求失败: {}", e);
  435. }
  436. return Result.error("请求失败");
  437. }
  438. public String DateToTimeStamp(String dateTimeString){
  439. // 定义日期格式
  440. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  441. try {
  442. // 解析字符串为Date对象
  443. Date date = dateFormat.parse(dateTimeString);
  444. // 将Date对象转换为时间戳(秒)
  445. long timestamp = date.getTime() / 1000;
  446. return String.valueOf(timestamp);
  447. } catch (ParseException e) {
  448. e.printStackTrace();
  449. }
  450. return dateTimeString;
  451. }
  452. }