songww
2020-05-13 6ecd801c2365feb4e65f6684065aa97f11615797
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.genersoft.iot.vmp.vmanager.device;
 
import java.util.List;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;
 
import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.event.DeviceOffLineDetector;
import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
 
@RestController
@RequestMapping("/api")
public class DeviceController {
    
    private final static Logger logger = LoggerFactory.getLogger(DeviceController.class);
    
    @Autowired
    private IVideoManagerStorager storager;
    
    @Autowired
    private SIPCommander cmder;
    
    @Autowired
    private DeferredResultHolder resultHolder;
    
    @Autowired
    private DeviceOffLineDetector offLineDetector;
    
    @GetMapping("/devices/{deviceId}")
    public ResponseEntity<Device> devices(@PathVariable String deviceId){
        
        if (logger.isDebugEnabled()) {
            logger.debug("查询视频设备API调用,deviceId:" + deviceId);
        }
        
        Device device = storager.queryVideoDevice(deviceId);
        return new ResponseEntity<>(device,HttpStatus.OK);
    }
    
    @GetMapping("/devices")
    public ResponseEntity<List<Device>> devices(){
        
        if (logger.isDebugEnabled()) {
            logger.debug("查询所有视频设备API调用");
        }
        
        List<Device> deviceList = storager.queryVideoDeviceList(null);
        return new ResponseEntity<>(deviceList,HttpStatus.OK);
    }
    
    @PostMapping("/devices/{deviceId}/sync")
    public DeferredResult<ResponseEntity<Device>> devicesSync(@PathVariable String deviceId){
        
        if (logger.isDebugEnabled()) {
            logger.debug("设备信息同步API调用,deviceId:" + deviceId);
        }
        
        Device device = storager.queryVideoDevice(deviceId);
        cmder.catalogQuery(device);
        DeferredResult<ResponseEntity<Device>> result = new DeferredResult<ResponseEntity<Device>>();
        resultHolder.put(DeferredResultHolder.CALLBACK_CMD_CATALOG+deviceId, result);
        return result;
    }
    
    @PostMapping("/devices/{deviceId}/delete")
    public ResponseEntity<String> delete(@PathVariable String deviceId){
        
        if (logger.isDebugEnabled()) {
            logger.debug("设备信息删除API调用,deviceId:" + deviceId);
        }
        
        if (offLineDetector.isOnline(deviceId)) {
            return new ResponseEntity<String>("不允许删除在线设备!", HttpStatus.NOT_ACCEPTABLE);
        }
        boolean isSuccess = storager.delete(deviceId);
        if (isSuccess) {
            JSONObject json = new JSONObject();
            json.put("deviceId", deviceId);
            return new ResponseEntity<>(json.toString(),HttpStatus.OK);
        } else {
            logger.warn("设备预览API调用失败!");
            return new ResponseEntity<String>("设备预览API调用失败!", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}