rain
2024-08-14 75e6eea8cd3fac9cc888e2ffc9cdb126fab8429d
src/main/java/com/dji/sample/control/controller/DockController.java
@@ -1,14 +1,33 @@
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
@@ -23,7 +42,14 @@
    @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) {
@@ -31,16 +57,19 @@
    }
    @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);
    }
@@ -51,15 +80,127 @@
    }
    @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);
    }
}