ApiDeviceController.java 12 KB

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