zhaowenyu
2025-12-18 b1ea8a05f56f92c66dc478af5100bc1b1cb764d3
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
package com.ard.sample.manage.service.impl;
 
import com.ard.common.core.redis.RedisConst;
import com.ard.common.core.redis.RedisOpsUtils;
import com.ard.sample.component.mqtt.model.CommonTopicReceiver;
import com.ard.sample.component.websocket.config.ConcurrentWebSocketSession;
import com.ard.sample.component.websocket.enums.MessageType;
import com.ard.sample.component.websocket.handler.DockWebSocketHandler;
import com.ard.sample.component.websocket.model.BizCodeEnum;
import com.ard.sample.component.websocket.model.CustomWebSocketMessage;
import com.ard.sample.manage.model.dto.DeviceDTO;
import com.ard.sample.manage.model.dto.TelemetryDTO;
import com.ard.sample.manage.model.enums.DeviceDomainEnum;
import com.ard.sample.manage.model.receiver.OsdDockReceiver;
import com.ard.sample.manage.service.IDeviceService;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.Collection;
 
/**
 * @author sean
 * @version 1.0
 * @date 2022/5/11
 */
@Service
public class DockOSDServiceImpl extends AbstractTSAService {
 
    @Autowired
    private DockWebSocketHandler _dockWebSocketHandler;
 
    @Autowired
    private IDeviceService _deviceService;
 
    public DockOSDServiceImpl() {
        super(null);
    }
 
 
    @Override
    public void pushTelemetryData(Collection<ConcurrentWebSocketSession> sessions,
                                  CustomWebSocketMessage<TelemetryDTO> message, Object Object) {
 
    }
 
    @Override
    public void handleOSD(CommonTopicReceiver receiver, DeviceDTO device,
                          Collection<ConcurrentWebSocketSession> webSessions,
                          CustomWebSocketMessage<TelemetryDTO> wsMessage) {
 
        if (DeviceDomainEnum.DOCK.getVal() == device.getDomain()) {
            wsMessage.setBizCode(BizCodeEnum.DOCK_OSD.getCode());
            OsdDockReceiver data = mapper.convertValue(receiver.getData(), OsdDockReceiver.class);
//            receiver.setType(MessageType.STATUS.getType());
            String dockOsd = "";
            String dockOsdData = "";
            try {
                dockOsdData = mapper.writeValueAsString(receiver.getData());
                if (dockOsdData.contains("flighttask_step_code")) {
                    JsonNode jsonNode = mapper.readTree(dockOsdData);
                    if (jsonNode.has("flighttask_step_code")) {
                        String deviceSn = device.getDeviceSn();
                        Long code = jsonNode.get("flighttask_step_code").asLong();
                        if (code != null) {
                            //只要状态 "0":"作业准备中","1":"飞行作业中","5":"任务空闲"
                            if (code == 0 || code == 1 || code == 5) {
                                _deviceService.updateWorkType(deviceSn, code);
                                RedisOpsUtils.set(RedisConst.OSD_STATUS_PREFIX + deviceSn, code);
                            }
                        }
                    }
                    receiver.setType(MessageType.STATUS.getType() + "-flighttask");
                } else if (dockOsdData.contains("network_state")) {
                    JsonNode jsonNode = mapper.readTree(dockOsdData);
                    if (jsonNode.has("drone_in_dock") && jsonNode.get("sub_device").has("device_online_status")) {
                        if (jsonNode.get("sub_device").has("device_sn")) {
                            String childDeviceSn = jsonNode.get("sub_device").get("device_sn").asText();
                            Long inDock = jsonNode.get("drone_in_dock").asLong();
                            Long deviceOnlineStatus = jsonNode.get("sub_device").get("device_online_status").asLong();
                            if (inDock.equals(1L) && deviceOnlineStatus.equals(0L)) {// 飞行器在舱内且关机状态为6
                                _deviceService.updateWorkType(childDeviceSn, 6L);
                                RedisOpsUtils.set(RedisConst.OSD_STATUS_PREFIX + device.getChildDeviceSn(), 6);
                            }
                        }
                    }
                    if (jsonNode.has("cover_state")) {
                        // 舱盖状态存redis
                        RedisOpsUtils.set(RedisConst.COVER_STATE_OSD_PREFIX + device.getDeviceSn(), jsonNode.get("cover_state").asInt());
//                        //实时监控机场内外舱盖状态
//                        Integer coverState = jsonNode.get("cover_state").asInt();
//                        if(coverState != null && (coverState == 1 || coverState == 0)) {
//                            // 执行0和1的互转逻辑,结果赋值给变量(保持Integer类型)
//                            Integer newCoverState = coverState == 0 ? 1 : 0;
//                            _deviceService.setRedisCameraPosition(device.getDeviceSn(), newCoverState);
//                        }
 
                    }
                    receiver.setType(MessageType.STATUS.getType() + "-network");
                } else if (dockOsdData.contains("job_number")) {
                    receiver.setType(MessageType.STATUS.getType() + "-job");
                }
                dockOsd = mapper.writeValueAsString(receiver);
            } catch (Exception e) {
                e.printStackTrace();
            }
            wsMessage.getData().setHost(data);
            sendMessageService.sendBatch(webSessions, wsMessage);
            // 发送给websocket客户端
            _dockWebSocketHandler.sendMessageToSubscribers(device.getDeviceSn(), MessageType.STATUS.getType(), dockOsd, "");
        }
    }
}