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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.genersoft.iot.vmp.vmanager.gb28181.ptz;
 
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;
 
import javax.sip.message.Response;
 
import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
 
import java.util.UUID;
 
@Api(tags = "云台控制")
@CrossOrigin
@RestController
@RequestMapping("/api/ptz")
public class PtzController {
 
    private final static Logger logger = LoggerFactory.getLogger(PtzController.class);
 
    @Autowired
    private SIPCommander cmder;
 
    @Autowired
    private IVideoManagerStorager storager;
 
    @Autowired
    private DeferredResultHolder resultHolder;
 
    /***
     * 云台控制
     * @param deviceId 设备id
     * @param channelId 通道id
     * @param cmdCode        指令码
     * @param horizonSpeed    水平移动速度
     * @param verticalSpeed    垂直移动速度
     * @param zoomSpeed        缩放速度
     * @return String 控制结果
     */
    @ApiOperation("云台控制")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备ID", dataTypeClass = String.class),
            @ApiImplicitParam(name = "channelId", value = "通道ID", dataTypeClass = String.class),
            @ApiImplicitParam(name = "cmdCode", value = "指令码", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "horizonSpeed", value = "水平速度", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "verticalSpeed", value = "垂直速度", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "zoomSpeed", value = "缩放速度", dataTypeClass = Integer.class),
    })
    @PostMapping("/control/{deviceId}/{channelId}")
    public ResponseEntity<String> ptz(@PathVariable String deviceId,@PathVariable String channelId,int cmdCode, int horizonSpeed, int verticalSpeed, int zoomSpeed){
 
        if (logger.isDebugEnabled()) {
            logger.debug(String.format("设备云台控制 API调用,deviceId:%s ,channelId:%s ,cmdCode:%d ,horizonSpeed:%d ,verticalSpeed:%d ,zoomSpeed:%d",deviceId, channelId, cmdCode, horizonSpeed, verticalSpeed, zoomSpeed));
        }
        Device device = storager.queryVideoDevice(deviceId);
 
        cmder.frontEndCmd(device, channelId, cmdCode, horizonSpeed, verticalSpeed, zoomSpeed);
        return new ResponseEntity<String>("success",HttpStatus.OK);
    }
 
    @ApiOperation("通用前端控制命令")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备ID", dataTypeClass = String.class),
            @ApiImplicitParam(name = "channelId", value = "通道ID", dataTypeClass = String.class),
            @ApiImplicitParam(name = "cmdCode", value = "指令码", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "parameter1", value = "数据一", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "parameter2", value = "数据二", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "combindCode2", value = "组合码二", dataTypeClass = Integer.class),
    })
    @PostMapping("/front_end_command/{deviceId}/{channelId}")
    public ResponseEntity<String> frontEndCommand(@PathVariable String deviceId,@PathVariable String channelId,int cmdCode, int parameter1, int parameter2, int combindCode2){
 
        if (logger.isDebugEnabled()) {
            logger.debug(String.format("设备云台控制 API调用,deviceId:%s ,channelId:%s ,cmdCode:%d parameter1:%d parameter2:%d",deviceId, channelId, cmdCode, parameter1, parameter2));
        }
        Device device = storager.queryVideoDevice(deviceId);
 
        cmder.frontEndCmd(device, channelId, cmdCode, parameter1, parameter2, combindCode2);
        return new ResponseEntity<String>("success",HttpStatus.OK);
    }
 
    @ApiOperation("预置位查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备ID", dataTypeClass = String.class),
            @ApiImplicitParam(name = "channelId", value = "通道ID", dataTypeClass = String.class),
    })
    @GetMapping("/preset/query/{deviceId}/{channelId}")
    public DeferredResult<ResponseEntity<String>> presetQueryApi(@PathVariable String deviceId, @PathVariable String channelId) {
        if (logger.isDebugEnabled()) {
            logger.debug("设备预置位查询API调用");
        }
        Device device = storager.queryVideoDevice(deviceId);
        String uuid =  UUID.randomUUID().toString();
        String key =  DeferredResultHolder.CALLBACK_CMD_PRESETQUERY + (StringUtils.isEmpty(channelId) ? deviceId : channelId);
        DeferredResult<ResponseEntity<String>> result = new DeferredResult<ResponseEntity<String >> (3 * 1000L);
        result.onTimeout(()->{
            logger.warn(String.format("获取设备预置位超时"));
            // 释放rtpserver
            RequestMessage msg = new RequestMessage();
            msg.setId(uuid);
            msg.setKey(key);
            msg.setData("获取设备预置位超时");
            resultHolder.invokeResult(msg);
        });
        resultHolder.put(key, uuid, result);
        if (resultHolder.exist(key, null)) {
            return result;
        }
        cmder.presetQuery(device, channelId, event -> {
            Response response = event.getResponse();
            RequestMessage msg = new RequestMessage();
            msg.setId(uuid);
            msg.setKey(key);
            msg.setData(String.format("获取设备预置位失败,错误码: %s, %s", response.getStatusCode(), response.getReasonPhrase()));
            resultHolder.invokeResult(msg);
        });
 
        return result;
    }
}