wangmengmeng
2025-04-26 96250617dbbefce55b5966c94880e2b07b6c98df
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
package com.dji.sample.control.service.impl;
 
import com.dji.sample.component.websocket.model.BizCodeEnum;
import com.dji.sample.component.websocket.service.IWebSocketMessageService;
import com.dji.sample.component.websocketWmm.WebSocketServerPlayBack;
import com.dji.sample.control.model.dto.ResultNotifyDTO;
import com.dji.sample.manage.model.dto.DeviceDTO;
import com.dji.sample.manage.model.enums.UserTypeEnum;
import com.dji.sample.manage.service.IDeviceRedisService;
import com.dji.sdk.cloudapi.control.*;
import com.dji.sdk.cloudapi.control.api.AbstractControlService;
import com.dji.sdk.mqtt.MqttReply;
import com.dji.sdk.mqtt.drc.DrcUpData;
import com.dji.sdk.mqtt.drc.TopicDrcRequest;
import com.dji.sdk.mqtt.drc.TopicDrcResponse;
import com.dji.sdk.mqtt.events.EventsDataRequest;
import com.dji.sdk.mqtt.events.TopicEventsRequest;
import com.dji.sdk.mqtt.events.TopicEventsResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Service;
 
import java.util.Optional;
 
/**
 * @author sean
 * @version 1.7
 * @date 2023/7/4
 */
@Service
@Slf4j
public class SDKControlService extends AbstractControlService {
 
    @Autowired
    private IWebSocketMessageService webSocketMessageService;
 
    @Autowired
    private IDeviceRedisService deviceRedisService;
 
    @Autowired
    private ObjectMapper mapper;
 
    @Override
    public TopicEventsResponse<MqttReply> flyToPointProgress(TopicEventsRequest<FlyToPointProgress> request, MessageHeaders headers) {
        String dockSn  = request.getGateway();
 
        Optional<DeviceDTO> deviceOpt = deviceRedisService.getDeviceOnline(dockSn);
        if (deviceOpt.isEmpty()) {
            log.error("The dock is offline.");
            return null;
        }
 
        ObjectMapper mapper = new ObjectMapper();
        String flyToPointProgress = "";
        try {
 
            flyToPointProgress = mapper.writeValueAsString(request.getData());
        } catch (JsonProcessingException e) {
            throw new RuntimeException("trans json failed.");
        }
        WebSocketServerPlayBack.sendInfo(BizCodeEnum.FLY_TO_POINT_PROGRESS, flyToPointProgress);
        return new TopicEventsResponse<MqttReply>().setData(MqttReply.success());
    }
 
    @Override
    public TopicEventsResponse<MqttReply> takeoffToPointProgress(TopicEventsRequest<TakeoffToPointProgress> request, MessageHeaders headers) {
        String dockSn  = request.getGateway();
 
        Optional<DeviceDTO> deviceOpt = deviceRedisService.getDeviceOnline(dockSn);
        if (deviceOpt.isEmpty()) {
            log.error("The dock is offline.");
            return null;
        }
 
 
//        TakeoffToPointProgress eventsReceiver = request.getData();
//        webSocketMessageService.sendBatch(deviceOpt.get().getWorkspaceId(), UserTypeEnum.WEB.getVal(),
//                BizCodeEnum.TAKE_OFF_TO_POINT_PROGRESS.getCode(),
//                ResultNotifyDTO.builder().sn(dockSn)
//                        .message(eventsReceiver.getResult().toString())
//                        .result(eventsReceiver.getResult().getCode())
//                        .build());
 
        ObjectMapper mapper = new ObjectMapper();
        String takeoffToPointProgress = "";
        try {
            takeoffToPointProgress = mapper.writeValueAsString(request.getData());
        } catch (JsonProcessingException e) {
            throw new RuntimeException("trans json failed.");
        }
        WebSocketServerPlayBack.sendInfo(BizCodeEnum.TAKE_OFF_TO_POINT_PROGRESS, takeoffToPointProgress);
        return new TopicEventsResponse<MqttReply>().setData(MqttReply.success());
    }
 
    @Override
    public TopicEventsResponse<MqttReply> drcStatusNotify(TopicEventsRequest<DrcStatusNotify> request, MessageHeaders headers) {
        String dockSn  = request.getGateway();
 
        Optional<DeviceDTO> deviceOpt = deviceRedisService.getDeviceOnline(dockSn);
        if (deviceOpt.isEmpty()) {
            return null;
        }
 
        ObjectMapper mapper = new ObjectMapper();
        String drcStatusNotify = "";
        try {
            drcStatusNotify = mapper.writeValueAsString(request.getData());
        } catch (JsonProcessingException e) {
            throw new RuntimeException("trans json failed.");
        }
        WebSocketServerPlayBack.sendInfo(BizCodeEnum.DRC_STATUS_NOTIFY, drcStatusNotify);
        return new TopicEventsResponse<MqttReply>().setData(MqttReply.success());
    }
 
    @Override
    public TopicEventsResponse<MqttReply> joystickInvalidNotify(TopicEventsRequest<JoystickInvalidNotify> request, MessageHeaders headers) {
        String dockSn  = request.getGateway();
 
        Optional<DeviceDTO> deviceOpt = deviceRedisService.getDeviceOnline(dockSn);
        if (deviceOpt.isEmpty()) {
            return null;
        }
 
        JoystickInvalidNotify eventsReceiver = request.getData();
        webSocketMessageService.sendBatch(
                deviceOpt.get().getWorkspaceId(), UserTypeEnum.WEB.getVal(), BizCodeEnum.JOYSTICK_INVALID_NOTIFY.getCode(),
                ResultNotifyDTO.builder().sn(dockSn)
                        .message(eventsReceiver.getReason().getMessage())
                        .result(eventsReceiver.getReason().getVal()).build());
 
        WebSocketServerPlayBack.sendInfo(request.toString());
        return new TopicEventsResponse<MqttReply>().setData(MqttReply.success());
    }
 
    @Override
    public TopicEventsResponse<MqttReply> cameraPhotoTakeProgress(TopicEventsRequest<EventsDataRequest<CameraPhotoTakeProgress>> request, MessageHeaders headers) {
 
        WebSocketServerPlayBack.sendInfo(request.toString());
        return new TopicEventsResponse<MqttReply>().setData(MqttReply.success());
    }
 
    @Override
    public void droneControlUp(TopicDrcRequest<DrcUpData<DroneControlResponse>> request, MessageHeaders headers) {
        WebSocketServerPlayBack.sendInfo(BizCodeEnum.DRONE_CONTROL_UP, request.getData().toString());
    }
 
    @Override
    public void droneEmergencyStopUp(TopicDrcRequest<DrcUpData> request, MessageHeaders headers) {
        WebSocketServerPlayBack.sendInfo(BizCodeEnum.DRONE_EMERGENCY_TOP_UP, request.getData().toString());
    }
 
    @Override
    public void heartBeatUp(TopicDrcRequest<HeartBeatRequest> request, MessageHeaders headers) {
        WebSocketServerPlayBack.sendInfo(BizCodeEnum.HEART_BEAT_UP, request.getData().toString());
    }
 
    @Override
    public void delayInfoPush(TopicDrcRequest<DelayInfoPush> request, MessageHeaders headers) {
        WebSocketServerPlayBack.sendInfo(BizCodeEnum.DELAY_INFO_PUSH, request.getData().toString());
    }
 
    @Override
    public void osdInfoPush(TopicDrcRequest<OsdInfoPush> request, MessageHeaders headers) {
        WebSocketServerPlayBack.sendInfo(BizCodeEnum.OSD_INFO_PUSH, request.getData().toString());
    }
 
    @Override
    public void hsiInfoPush(TopicDrcRequest<HsiInfoPush> request, MessageHeaders headers) {
        WebSocketServerPlayBack.sendInfo(BizCodeEnum.HSI_INFO_PUSH, request.getData().toString());
    }
 
}