MobilePositionController.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package com.genersoft.iot.vmp.gb28181.controller;
  2. import com.genersoft.iot.vmp.conf.exception.ControllerException;
  3. import com.genersoft.iot.vmp.conf.security.JwtUtils;
  4. import com.genersoft.iot.vmp.gb28181.bean.Device;
  5. import com.genersoft.iot.vmp.gb28181.bean.MobilePosition;
  6. import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
  7. import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage;
  8. import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
  9. import com.genersoft.iot.vmp.gb28181.service.IDeviceChannelService;
  10. import com.genersoft.iot.vmp.gb28181.service.IDeviceService;
  11. import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
  12. import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
  13. import com.github.pagehelper.util.StringUtil;
  14. import io.swagger.v3.oas.annotations.Operation;
  15. import io.swagger.v3.oas.annotations.Parameter;
  16. import io.swagger.v3.oas.annotations.security.SecurityRequirement;
  17. import io.swagger.v3.oas.annotations.tags.Tag;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.web.bind.annotation.*;
  21. import org.springframework.web.context.request.async.DeferredResult;
  22. import javax.sip.InvalidArgumentException;
  23. import javax.sip.SipException;
  24. import java.text.ParseException;
  25. import java.util.List;
  26. import java.util.UUID;
  27. /**
  28. * 位置信息管理
  29. */
  30. @Tag(name = "位置信息管理")
  31. @Slf4j
  32. @RestController
  33. @RequestMapping("/api/position")
  34. public class MobilePositionController {
  35. @Autowired
  36. private IVideoManagerStorage storager;
  37. @Autowired
  38. private SIPCommander cmder;
  39. @Autowired
  40. private DeferredResultHolder resultHolder;
  41. @Autowired
  42. private IDeviceService deviceService;
  43. @Autowired
  44. private IDeviceChannelService deviceChannelService;
  45. /**
  46. * 查询历史轨迹
  47. * @param deviceId 设备ID
  48. * @param start 开始时间
  49. * @param end 结束时间
  50. * @return
  51. */
  52. @Operation(summary = "查询历史轨迹", security = @SecurityRequirement(name = JwtUtils.HEADER))
  53. @Parameter(name = "deviceId", description = "设备国标编号", required = true)
  54. @Parameter(name = "channelId", description = "通道国标编号")
  55. @Parameter(name = "start", description = "开始时间")
  56. @Parameter(name = "end", description = "结束时间")
  57. @GetMapping("/history/{deviceId}")
  58. public List<MobilePosition> positions(@PathVariable String deviceId,
  59. @RequestParam(required = false) String channelId,
  60. @RequestParam(required = false) String start,
  61. @RequestParam(required = false) String end) {
  62. if (StringUtil.isEmpty(start)) {
  63. start = null;
  64. }
  65. if (StringUtil.isEmpty(end)) {
  66. end = null;
  67. }
  68. return storager.queryMobilePositions(deviceId, channelId, start, end);
  69. }
  70. /**
  71. * 查询设备最新位置
  72. * @param deviceId 设备ID
  73. * @return
  74. */
  75. @Operation(summary = "查询设备最新位置", security = @SecurityRequirement(name = JwtUtils.HEADER))
  76. @Parameter(name = "deviceId", description = "设备国标编号", required = true)
  77. @GetMapping("/latest/{deviceId}")
  78. public MobilePosition latestPosition(@PathVariable String deviceId) {
  79. return storager.queryLatestPosition(deviceId);
  80. }
  81. /**
  82. * 获取移动位置信息
  83. * @param deviceId 设备ID
  84. * @return
  85. */
  86. @Operation(summary = "获取移动位置信息", security = @SecurityRequirement(name = JwtUtils.HEADER))
  87. @Parameter(name = "deviceId", description = "设备国标编号", required = true)
  88. @GetMapping("/realtime/{deviceId}")
  89. public DeferredResult<MobilePosition> realTimePosition(@PathVariable String deviceId) {
  90. Device device = deviceService.getDeviceByDeviceId(deviceId);
  91. String uuid = UUID.randomUUID().toString();
  92. String key = DeferredResultHolder.CALLBACK_CMD_MOBILE_POSITION + deviceId;
  93. try {
  94. cmder.mobilePostitionQuery(device, event -> {
  95. RequestMessage msg = new RequestMessage();
  96. msg.setId(uuid);
  97. msg.setKey(key);
  98. msg.setData(String.format("获取移动位置信息失败,错误码: %s, %s", event.statusCode, event.msg));
  99. resultHolder.invokeResult(msg);
  100. });
  101. } catch (InvalidArgumentException | SipException | ParseException e) {
  102. log.error("[命令发送失败] 获取移动位置信息: {}", e.getMessage());
  103. throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage());
  104. }
  105. DeferredResult<MobilePosition> result = new DeferredResult<MobilePosition>(5*1000L);
  106. result.onTimeout(()->{
  107. log.warn(String.format("获取移动位置信息超时"));
  108. // 释放rtpserver
  109. RequestMessage msg = new RequestMessage();
  110. msg.setId(uuid);
  111. msg.setKey(key);
  112. msg.setData("Timeout");
  113. resultHolder.invokeResult(msg);
  114. });
  115. resultHolder.put(key, uuid, result);
  116. return result;
  117. }
  118. /**
  119. * 订阅位置信息
  120. * @param deviceId 设备ID
  121. * @param expires 订阅超时时间
  122. * @param interval 上报时间间隔
  123. * @return true = 命令发送成功
  124. */
  125. @Operation(summary = "订阅位置信息", security = @SecurityRequirement(name = JwtUtils.HEADER))
  126. @Parameter(name = "deviceId", description = "设备国标编号", required = true)
  127. @Parameter(name = "expires", description = "订阅超时时间", required = true)
  128. @Parameter(name = "interval", description = "上报时间间隔", required = true)
  129. @GetMapping("/subscribe/{deviceId}")
  130. public void positionSubscribe(@PathVariable String deviceId,
  131. @RequestParam String expires,
  132. @RequestParam String interval) {
  133. if (StringUtil.isEmpty(interval)) {
  134. interval = "5";
  135. }
  136. Device device = deviceService.getDeviceByDeviceId(deviceId);
  137. device.setSubscribeCycleForMobilePosition(Integer.parseInt(expires));
  138. device.setMobilePositionSubmissionInterval(Integer.parseInt(interval));
  139. deviceService.updateCustomDevice(device);
  140. }
  141. }