rain
2024-08-15 2927bb498574a0e30bcbb3a9f7ee9468636cf0d8
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.dji.sample.control.controller;
 
import com.dji.sample.common.model.ResponseResult;
import com.dji.sample.component.redis.RedisConst;
import com.dji.sample.component.redis.RedisOpsUtils;
import com.dji.sample.control.model.enums.CameraModeEnum;
import com.dji.sample.control.model.enums.DroneAuthorityEnum;
import com.dji.sample.control.model.enums.PayloadCommandsEnum;
import com.dji.sample.control.model.param.*;
import com.dji.sample.control.service.IControlService;
import com.dji.sample.log.aspect.SysLogAnnotation;
import com.dji.sample.manage.model.dto.CapacityCameraDTO;
import com.dji.sample.manage.model.dto.DeviceDTO;
import com.dji.sample.manage.model.enums.DeviceModeCodeEnum;
import com.dji.sample.manage.model.param.DeviceQueryParam;
import com.dji.sample.manage.model.receiver.OsdCameraReceiver;
import com.dji.sample.manage.model.receiver.OsdDockReceiver;
import com.dji.sample.manage.model.receiver.OsdSubDeviceReceiver;
import com.dji.sample.manage.service.ICapacityCameraService;
import com.dji.sample.manage.service.IDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
 
/**
 * @author sean
 * @version 1.2
 * @date 2022/7/29
 */
@RestController
@Slf4j
@RequestMapping("${url.control.prefix}${url.control.version}/devices")
public class DockController {
 
    @Autowired
    private IControlService controlService;
 
    @Autowired
    private ICapacityCameraService capacityCameraService;
 
    @Autowired
    private IDeviceService deviceService;
 
    @PostMapping("/{sn}/jobs/{service_identifier}")
    @SysLogAnnotation(operModul = "机场控制", operType = "控制指令", operDesc = "控制指令")
    public ResponseResult createControlJob(@PathVariable String sn,
                                           @PathVariable("service_identifier") String serviceIdentifier,
                                           @RequestBody(required = false) RemoteDebugParam param) {
        return controlService.controlDockDebug(sn, serviceIdentifier, param);
    }
 
    @PostMapping("/{sn}/jobs/fly-to-point")
    @SysLogAnnotation(operModul = "机场控制", operType = "控制指令", operDesc = "起飞")
    public ResponseResult flyToPoint(@PathVariable String sn, @Valid @RequestBody FlyToPointParam param) {
        return controlService.flyToPoint(sn, param);
    }
 
    @DeleteMapping("/{sn}/jobs/fly-to-point")
    @SysLogAnnotation(operModul = "机场控制", operType = "控制指令", operDesc = "停止起飞")
    public ResponseResult flyToPointStop(@PathVariable String sn) {
        return controlService.flyToPointStop(sn);
    }
 
    @PostMapping("/{sn}/jobs/takeoff-to-point")
    @SysLogAnnotation(operModul = "机场控制", operType = "控制指令", operDesc = "一键起飞")
    public ResponseResult takeoffToPoint(@PathVariable String sn, @Valid @RequestBody TakeoffToPointParam param) {
        return controlService.takeoffToPoint(sn, param);
    }
 
    @PostMapping("/{sn}/authority/flight")
    public ResponseResult seizeFlightAuthority(@PathVariable String sn) {
        return controlService.seizeAuthority(sn, DroneAuthorityEnum.FLIGHT, null);
    }
 
    @PostMapping("/{sn}/authority/payload")
    @SysLogAnnotation(operModul = "机场控制", operType = "控制指令", operDesc = "负载控制")
    public ResponseResult seizePayloadAuthority(@PathVariable String sn, @Valid @RequestBody DronePayloadParam param) {
        return controlService.seizeAuthority(sn, DroneAuthorityEnum.PAYLOAD, param);
    }
 
    @PostMapping("/{sn}/payload/commands")
    @SysLogAnnotation(operModul = "机场控制", operType = "控制指令", operDesc = "相机模式开关")
    public ResponseResult payloadCommands(@PathVariable String sn, @Valid @RequestBody PayloadCommandsParam param) throws Exception {
        param.setSn(sn);
        return controlService.payloadCommands(param);
    }
 
    @GetMapping("/{sn}/payload/photoAndVideoCmd/{type}")
    @SysLogAnnotation(operModul = "云台拍照录像控制", operType = "控制指令", operDesc = "云台拍照录像控制")
    public ResponseResult photoAndVideoCmd(@PathVariable String sn, @PathVariable String type) throws Exception {
 
        String bid = UUID.randomUUID().toString();
 
        List<DeviceDTO> list = deviceService.getDevicesByParams(DeviceQueryParam.builder().deviceSn(sn).build());
 
        if (list.isEmpty()) {
            return ResponseResult.error("机场离线!");
        }
 
        DeviceDTO deviceDTO = list.get(0);
 
        //无人机设备相机信息
        String key = RedisConst.OSD_PREFIX + deviceDTO.getChildDeviceSn();
        OsdSubDeviceReceiver redisData = (OsdSubDeviceReceiver) RedisOpsUtils.get(key);
        log.info("无人机设备相机信息osd:{}",redisData);
 
        if (null == redisData) {
            return ResponseResult.error("无人机设备离线!");
        }
 
        //无人机osd信息
        List<OsdCameraReceiver> osdCameraReceiverList = redisData.getCameras();
        OsdCameraReceiver cameraDTO = osdCameraReceiverList.get(0);
 
        //获取负载控制
        ResponseResult result = getPayload(sn,cameraDTO);
        log.info("获取负载:{}",result);
        if (result.getCode() != 0) {
            return result;
        }
 
        PayloadCommandsParam param = new PayloadCommandsParam();
        param.setSn(sn);
        DronePayloadParam data = new DronePayloadParam();
        data.setPayloadIndex(cameraDTO.getPayloadIndex());
 
        //拍照
        switch (type) {
            case "photo":
                if (!Objects.equals(cameraDTO.getCameraMode().getMode(), CameraModeEnum.PHOTO.getMode())) {
                    //切换模式
                    ResponseResult swResult = switchingMode(sn, CameraModeEnum.PHOTO, cameraDTO);
                    log.info("摄像头切换状态:{}", swResult);
                }
                param.setCmd(PayloadCommandsEnum.CAMERA_PHOTO_TAKE);
                param.setData(data);
                break;
            case "video_start": {//开始录像
                if (!Objects.equals(cameraDTO.getCameraMode().getMode(), CameraModeEnum.VIDEO.getMode())) {
                    //切换模式
                    ResponseResult swResult = switchingMode(sn, CameraModeEnum.VIDEO, cameraDTO);
                    log.info("摄像头切换状态:{}", swResult);
                }
                param.setCmd(PayloadCommandsEnum.CAMERA_RECORDING_START);
                param.setData(data);
                break;
            }
            case "video_stop": {//结束录像
                if (!Objects.equals(cameraDTO.getCameraMode().getMode(), CameraModeEnum.VIDEO.getMode())) {
                    //切换模式
                    ResponseResult swResult = switchingMode(sn, CameraModeEnum.VIDEO, cameraDTO);
                    log.info("摄像头切换状态:{}", swResult);
                }
                param.setCmd(PayloadCommandsEnum.CAMERA_RECORDING_STOP);
                param.setData(data);
                break;
            }
            default:
                return ResponseResult.error(-1, "未知类型!");
        }
 
 
        Thread.sleep(2000); // 延迟2000毫秒(2秒)
 
        return controlService.payloadCommands(param,bid);
    }
 
    /**
     * 获取负载权限
     * @param sn 无人机设备号
     * @return
     */
    private ResponseResult getPayload(String sn,OsdCameraReceiver cameraDTO) {
        DronePayloadParam param = new DronePayloadParam();
        param.setPayloadIndex(cameraDTO.getPayloadIndex());
        return controlService.seizeAuthority(sn, DroneAuthorityEnum.PAYLOAD, param);//获取无人机负载权限
    }
 
    /**
     * 负载模式切换
     * @param sn 无人机设备号
     * @param mode 模式  相机或者录像模式
     * @param cameraDTO 相机数据
     * @return
     * @throws Exception
     */
    private ResponseResult switchingMode(String sn, CameraModeEnum mode, OsdCameraReceiver cameraDTO) throws Exception {
        PayloadCommandsParam param = new PayloadCommandsParam();
        param.setSn(sn);
        param.setCmd(PayloadCommandsEnum.CAMERA_MODE_SWitCH);
        DronePayloadParam data = new DronePayloadParam();
        data.setPayloadIndex(cameraDTO.getPayloadIndex());
        data.setCameraMode(mode);
        param.setData(data);
        return controlService.payloadCommands(param);
    }
 
 
}