DeviceController.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package com.genersoft.iot.vmp.vmanager.device;
  2. import java.util.List;
  3. import com.genersoft.iot.vmp.common.PageResult;
  4. import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.http.HttpStatus;
  9. import org.springframework.http.ResponseEntity;
  10. import org.springframework.web.bind.annotation.*;
  11. import org.springframework.web.context.request.async.DeferredResult;
  12. import com.alibaba.fastjson.JSONObject;
  13. import com.genersoft.iot.vmp.gb28181.bean.Device;
  14. import com.genersoft.iot.vmp.gb28181.event.DeviceOffLineDetector;
  15. import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
  16. import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
  17. import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
  18. @CrossOrigin
  19. @RestController
  20. @RequestMapping("/api")
  21. public class DeviceController {
  22. private final static Logger logger = LoggerFactory.getLogger(DeviceController.class);
  23. @Autowired
  24. private IVideoManagerStorager storager;
  25. @Autowired
  26. private SIPCommander cmder;
  27. @Autowired
  28. private DeferredResultHolder resultHolder;
  29. @Autowired
  30. private DeviceOffLineDetector offLineDetector;
  31. @GetMapping("/devices/{deviceId}")
  32. public ResponseEntity<Device> devices(@PathVariable String deviceId){
  33. if (logger.isDebugEnabled()) {
  34. logger.debug("查询视频设备API调用,deviceId:" + deviceId);
  35. }
  36. Device device = storager.queryVideoDevice(deviceId);
  37. return new ResponseEntity<>(device,HttpStatus.OK);
  38. }
  39. @GetMapping("/devices")
  40. public PageResult<Device> devices(int page, int count){
  41. if (logger.isDebugEnabled()) {
  42. logger.debug("查询所有视频设备API调用");
  43. }
  44. return storager.queryVideoDeviceList(null, page, count);
  45. }
  46. /**
  47. * 分页查询通道数
  48. * @param deviceId 设备id
  49. * @param page 当前页
  50. * @param count 每页条数
  51. * @return 通道列表
  52. */
  53. @GetMapping("/devices/{deviceId}/channels")
  54. public ResponseEntity<PageResult> channels(@PathVariable String deviceId,
  55. int page, int count,
  56. @RequestParam(required = false) String query,
  57. @RequestParam(required = false) String online,
  58. @RequestParam(required = false) Boolean channelType
  59. ){
  60. if (logger.isDebugEnabled()) {
  61. logger.debug("查询所有视频设备API调用");
  62. }
  63. PageResult pageResult = storager.queryChannelsByDeviceId(deviceId, query, channelType, online, page, count);
  64. return new ResponseEntity<>(pageResult,HttpStatus.OK);
  65. }
  66. @PostMapping("/devices/{deviceId}/sync")
  67. public DeferredResult<ResponseEntity<Device>> devicesSync(@PathVariable String deviceId){
  68. if (logger.isDebugEnabled()) {
  69. }
  70. logger.debug("设备信息同步API调用,deviceId:" + deviceId);
  71. Device device = storager.queryVideoDevice(deviceId);
  72. cmder.catalogQuery(device);
  73. DeferredResult<ResponseEntity<Device>> result = new DeferredResult<ResponseEntity<Device>>();
  74. resultHolder.put(DeferredResultHolder.CALLBACK_CMD_CATALOG+deviceId, result);
  75. return result;
  76. }
  77. @PostMapping("/devices/{deviceId}/delete")
  78. public ResponseEntity<String> delete(@PathVariable String deviceId){
  79. if (logger.isDebugEnabled()) {
  80. logger.debug("设备信息删除API调用,deviceId:" + deviceId);
  81. }
  82. if (offLineDetector.isOnline(deviceId)) {
  83. return new ResponseEntity<String>("不允许删除在线设备!", HttpStatus.NOT_ACCEPTABLE);
  84. }
  85. boolean isSuccess = storager.delete(deviceId);
  86. if (isSuccess) {
  87. JSONObject json = new JSONObject();
  88. json.put("deviceId", deviceId);
  89. return new ResponseEntity<>(json.toString(),HttpStatus.OK);
  90. } else {
  91. logger.warn("设备预览API调用失败!");
  92. return new ResponseEntity<String>("设备预览API调用失败!", HttpStatus.INTERNAL_SERVER_ERROR);
  93. }
  94. }
  95. /**
  96. * 分页查询通道数
  97. * @param channelId 通道id
  98. * @param page 当前页
  99. * @param count 每页条数
  100. * @return 子通道列表
  101. */
  102. @GetMapping("/subChannels/{deviceId}/{channelId}/channels")
  103. public ResponseEntity<PageResult> subChannels(@PathVariable String deviceId,
  104. @PathVariable String channelId,
  105. int page,
  106. int count,
  107. @RequestParam(required = false) String query,
  108. @RequestParam(required = false) String online,
  109. @RequestParam(required = false) Boolean channelType){
  110. if (logger.isDebugEnabled()) {
  111. logger.debug("查询所有视频设备API调用");
  112. }
  113. DeviceChannel deviceChannel = storager.queryChannel(deviceId,channelId);
  114. if (deviceChannel == null) {
  115. PageResult<DeviceChannel> deviceChannelPageResult = new PageResult<>();
  116. new ResponseEntity<>(deviceChannelPageResult,HttpStatus.OK);
  117. }
  118. PageResult pageResult = storager.querySubChannels(deviceId, channelId, query, channelType, online, page, count);
  119. return new ResponseEntity<>(pageResult,HttpStatus.OK);
  120. }
  121. @PostMapping("/channel/update/{deviceId}")
  122. public ResponseEntity<PageResult> updateChannel(@PathVariable String deviceId,DeviceChannel channel){
  123. storager.updateChannel(deviceId, channel);
  124. return new ResponseEntity<>(null,HttpStatus.OK);
  125. }
  126. @GetMapping("/devices/{deviceId}/transport/{transport}")
  127. @PostMapping("/devices/{deviceId}/transport/{transport}")
  128. public ResponseEntity<PageResult> updateTransport(@PathVariable String deviceId, @PathVariable String transport){
  129. Device device = storager.queryVideoDevice(deviceId);
  130. device.setTransport(transport);
  131. storager.updateDevice(device);
  132. return new ResponseEntity<>(null,HttpStatus.OK);
  133. }
  134. }