ApiDeviceController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package com.genersoft.iot.vmp.web.gb28181;
  2. import com.alibaba.fastjson2.JSONArray;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.genersoft.iot.vmp.conf.exception.ControllerException;
  5. import com.genersoft.iot.vmp.gb28181.bean.Device;
  6. import com.genersoft.iot.vmp.gb28181.bean.PresetQuerySipReq;
  7. import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
  8. import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage;
  9. import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
  10. import com.genersoft.iot.vmp.service.IDeviceService;
  11. import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
  12. import com.genersoft.iot.vmp.vmanager.bean.DeferredResultEx;
  13. import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
  14. import com.genersoft.iot.vmp.web.gb28181.dto.DeviceChannelExtend;
  15. import com.github.pagehelper.PageInfo;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.util.ObjectUtils;
  20. import org.springframework.web.bind.annotation.GetMapping;
  21. import org.springframework.web.bind.annotation.RequestMapping;
  22. import org.springframework.web.bind.annotation.RequestParam;
  23. import org.springframework.web.bind.annotation.RestController;
  24. import org.springframework.web.context.request.async.DeferredResult;
  25. import javax.sip.InvalidArgumentException;
  26. import javax.sip.SipException;
  27. import java.text.ParseException;
  28. import java.util.*;
  29. /**
  30. * API兼容:设备信息
  31. */
  32. @SuppressWarnings("unchecked")
  33. @RestController
  34. @RequestMapping(value = "/api/v1/device")
  35. public class ApiDeviceController {
  36. private final static Logger logger = LoggerFactory.getLogger(ApiDeviceController.class);
  37. @Autowired
  38. private IVideoManagerStorage storager;
  39. @Autowired
  40. private SIPCommander cmder;
  41. @Autowired
  42. private IDeviceService deviceService;
  43. @Autowired
  44. private DeferredResultHolder resultHolder;
  45. /**
  46. * 分页获取设备列表 现在直接返回,尚未实现分页
  47. * @param start
  48. * @param limit
  49. * @param q
  50. * @param online
  51. * @return
  52. */
  53. @GetMapping(value = "/list")
  54. public JSONObject list( @RequestParam(required = false)Integer start,
  55. @RequestParam(required = false)Integer limit,
  56. @RequestParam(required = false)String q,
  57. @RequestParam(required = false)Boolean online ){
  58. // if (logger.isDebugEnabled()) {
  59. // logger.debug("查询所有视频设备API调用");
  60. // }
  61. JSONObject result = new JSONObject();
  62. List<Device> devices;
  63. if (start == null || limit ==null) {
  64. devices = storager.queryVideoDeviceList(online);
  65. result.put("DeviceCount", devices.size());
  66. }else {
  67. PageInfo<Device> deviceList = storager.queryVideoDeviceList(start/limit, limit,online);
  68. result.put("DeviceCount", deviceList.getTotal());
  69. devices = deviceList.getList();
  70. }
  71. JSONArray deviceJSONList = new JSONArray();
  72. devices.stream().forEach(device -> {
  73. JSONObject deviceJsonObject = new JSONObject();
  74. deviceJsonObject.put("ID", device.getDeviceId());
  75. deviceJsonObject.put("Name", device.getName());
  76. deviceJsonObject.put("Type", "GB");
  77. deviceJsonObject.put("ChannelCount", device.getChannelCount());
  78. deviceJsonObject.put("RecvStreamIP", "");
  79. deviceJsonObject.put("CatalogInterval", 3600); // 通道目录抓取周期
  80. deviceJsonObject.put("SubscribeInterval", device.getSubscribeCycleForCatalog()); // 订阅周期(秒), 0 表示后台不周期订阅
  81. deviceJsonObject.put("Online", device.isOnLine());
  82. deviceJsonObject.put("Password", "");
  83. deviceJsonObject.put("MediaTransport", device.getTransport());
  84. deviceJsonObject.put("RemoteIP", device.getIp());
  85. deviceJsonObject.put("RemotePort", device.getPort());
  86. deviceJsonObject.put("LastRegisterAt", "");
  87. deviceJsonObject.put("LastKeepaliveAt", "");
  88. deviceJsonObject.put("UpdatedAt", "");
  89. deviceJsonObject.put("CreatedAt", "");
  90. deviceJSONList.add(deviceJsonObject);
  91. });
  92. result.put("DeviceList",deviceJSONList);
  93. return result;
  94. }
  95. @GetMapping(value = "/channellist")
  96. public JSONObject channellist( String serial,
  97. @RequestParam(required = false)String channel_type,
  98. @RequestParam(required = false)String code ,
  99. @RequestParam(required = false)String dir_serial ,
  100. @RequestParam(required = false)Integer start,
  101. @RequestParam(required = false)Integer limit,
  102. @RequestParam(required = false)String q,
  103. @RequestParam(required = false)Boolean online ){
  104. JSONObject result = new JSONObject();
  105. List<DeviceChannelExtend> deviceChannels;
  106. List<String> channelIds = null;
  107. if (!ObjectUtils.isEmpty(code)) {
  108. String[] split = code.trim().split(",");
  109. channelIds = Arrays.asList(split);
  110. }
  111. List<DeviceChannelExtend> allDeviceChannelList = storager.queryChannelsByDeviceId(serial,channelIds,online);
  112. if (start == null || limit ==null) {
  113. deviceChannels = allDeviceChannelList;
  114. result.put("ChannelCount", deviceChannels.size());
  115. }else {
  116. if (start > allDeviceChannelList.size()) {
  117. deviceChannels = new ArrayList<>();
  118. }else {
  119. if (start + limit < allDeviceChannelList.size()) {
  120. deviceChannels = allDeviceChannelList.subList(start, start + limit);
  121. }else {
  122. deviceChannels = allDeviceChannelList.subList(start, allDeviceChannelList.size());
  123. }
  124. }
  125. result.put("ChannelCount", allDeviceChannelList.size());
  126. }
  127. JSONArray channleJSONList = new JSONArray();
  128. deviceChannels.stream().forEach(deviceChannelExtend -> {
  129. JSONObject deviceJOSNChannel = new JSONObject();
  130. deviceJOSNChannel.put("ID", deviceChannelExtend.getChannelId());
  131. deviceJOSNChannel.put("DeviceID", deviceChannelExtend.getDeviceId());
  132. deviceJOSNChannel.put("DeviceName", deviceChannelExtend.getDeviceName());
  133. deviceJOSNChannel.put("DeviceOnline", deviceChannelExtend.isDeviceOnline());
  134. deviceJOSNChannel.put("Channel", 0); // TODO 自定义序号
  135. deviceJOSNChannel.put("Name", deviceChannelExtend.getName());
  136. deviceJOSNChannel.put("Custom", false);
  137. deviceJOSNChannel.put("CustomName", "");
  138. deviceJOSNChannel.put("SubCount", deviceChannelExtend.getSubCount()); // TODO ? 子节点数, SubCount > 0 表示该通道为子目录
  139. deviceJOSNChannel.put("SnapURL", "");
  140. deviceJOSNChannel.put("Manufacturer ", deviceChannelExtend.getManufacture());
  141. deviceJOSNChannel.put("Model", deviceChannelExtend.getModel());
  142. deviceJOSNChannel.put("Owner", deviceChannelExtend.getOwner());
  143. deviceJOSNChannel.put("CivilCode", deviceChannelExtend.getCivilCode());
  144. deviceJOSNChannel.put("Address", deviceChannelExtend.getAddress());
  145. deviceJOSNChannel.put("Parental", deviceChannelExtend.getParental()); // 当为通道设备时, 是否有通道子设备, 1-有,0-没有
  146. deviceJOSNChannel.put("ParentID", deviceChannelExtend.getParentId()); // 直接上级编号
  147. deviceJOSNChannel.put("Secrecy", deviceChannelExtend.getSecrecy());
  148. deviceJOSNChannel.put("RegisterWay", 1); // 注册方式, 缺省为1, 允许值: 1, 2, 3
  149. // 1-IETF RFC3261,
  150. // 2-基于口令的双向认证,
  151. // 3-基于数字证书的双向认证
  152. deviceJOSNChannel.put("Status", deviceChannelExtend.isStatus() ? "ON":"OFF");
  153. deviceJOSNChannel.put("Longitude", deviceChannelExtend.getLongitude());
  154. deviceJOSNChannel.put("Latitude", deviceChannelExtend.getLatitude());
  155. deviceJOSNChannel.put("PTZType ", deviceChannelExtend.getPTZType()); // 云台类型, 0 - 未知, 1 - 球机, 2 - 半球,
  156. // 3 - 固定枪机, 4 - 遥控枪机
  157. deviceJOSNChannel.put("CustomPTZType", "");
  158. deviceJOSNChannel.put("StreamID", deviceChannelExtend.getStreamId()); // StreamID 直播流ID, 有值表示正在直播
  159. deviceJOSNChannel.put("NumOutputs ", -1); // 直播在线人数
  160. channleJSONList.add(deviceJOSNChannel);
  161. });
  162. result.put("ChannelList", channleJSONList);
  163. return result;
  164. }
  165. /**
  166. * 设备信息 - 获取下级通道预置位
  167. * @param serial 设备编号
  168. * @param code 通道编号,通过 /api/v1/device/channellist 获取的 ChannelList.ID, 该参数和 channel 二选一传递即可
  169. * @param channel 通道序号, 默认值: 1
  170. * @param fill 是否填充空置预置位,当下级返回预置位,但不够255个时,自动填充空置预置位到255个, 默认值: true, 允许值: true, false
  171. * @param timeout 超时时间(秒) 默认值: 15
  172. * @return
  173. */
  174. @GetMapping(value = "/fetchpreset")
  175. private DeferredResult<Object> list(String serial,
  176. @RequestParam(required = false)Integer channel,
  177. @RequestParam(required = false)String code,
  178. @RequestParam(required = false)Boolean fill,
  179. @RequestParam(required = false)Integer timeout){
  180. if (logger.isDebugEnabled()) {
  181. logger.debug("<模拟接口> 获取下级通道预置位 API调用,deviceId:{} ,channel:{} ,code:{} ,fill:{} ,timeout:{} ",
  182. serial, channel, code, fill, timeout);
  183. }
  184. Device device = storager.queryVideoDevice(serial);
  185. String uuid = UUID.randomUUID().toString();
  186. String key = DeferredResultHolder.CALLBACK_CMD_PRESETQUERY + (ObjectUtils.isEmpty(code) ? serial : code);
  187. DeferredResult<Object> result = new DeferredResult<> (timeout * 1000L);
  188. DeferredResultEx<Object> deferredResultEx = new DeferredResultEx<>(result);
  189. result.onTimeout(()->{
  190. logger.warn("<模拟接口> 获取设备预置位超时");
  191. // 释放rtpserver
  192. RequestMessage msg = new RequestMessage();
  193. msg.setId(uuid);
  194. msg.setKey(key);
  195. msg.setData("wait for presetquery timeout["+timeout+"s]");
  196. resultHolder.invokeResult(msg);
  197. });
  198. if (resultHolder.exist(key, null)) {
  199. return result;
  200. }
  201. deferredResultEx.setFilter(filterResult->{
  202. List<PresetQuerySipReq> presetQuerySipReqList = (List<PresetQuerySipReq>)filterResult;
  203. HashMap<String, Object> resultMap = new HashMap<>();
  204. resultMap.put("DeviceID", code);
  205. resultMap.put("Result", "OK");
  206. resultMap.put("SumNum", presetQuerySipReqList.size());
  207. ArrayList<Map<String, Object>> presetItemList = new ArrayList<>(presetQuerySipReqList.size());
  208. for (PresetQuerySipReq presetQuerySipReq : presetQuerySipReqList) {
  209. Map<String, Object> item = new HashMap<>();
  210. item.put("PresetID", presetQuerySipReq.getPresetId());
  211. item.put("PresetName", presetQuerySipReq.getPresetName());
  212. item.put("PresetEnable", true);
  213. presetItemList.add(item);
  214. }
  215. resultMap.put("PresetItemList",presetItemList );
  216. return resultMap;
  217. });
  218. resultHolder.put(key, uuid, deferredResultEx);
  219. try {
  220. cmder.presetQuery(device, code, event -> {
  221. RequestMessage msg = new RequestMessage();
  222. msg.setId(uuid);
  223. msg.setKey(key);
  224. msg.setData(String.format("获取设备预置位失败,错误码: %s, %s", event.statusCode, event.msg));
  225. resultHolder.invokeResult(msg);
  226. });
  227. } catch (InvalidArgumentException | SipException | ParseException e) {
  228. logger.error("[命令发送失败] 获取设备预置位: {}", e.getMessage());
  229. throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage());
  230. }
  231. return result;
  232. }
  233. }