liusuyi
2025-12-22 2fd6dc640f0f94ea5c1109aa7a8fd3ae33643dec
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
package com.ard.work.listener;
 
import com.ard.work.component.websocket.utils.WebSocketUtil;
import com.ard.work.domain.event.DeviceStatusEvent;
import com.ard.work.domain.message.BaseMessage;
import com.ard.work.service.IArdFlightTaskPartService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 无人机状态事件监听
 *
 * @author lsy
 * @date 2025/12/19
 */
 
@Component
@Slf4j
public class DeviceStatusEventListener {
 
    @Autowired
    private IArdFlightTaskPartService ardFlightTaskPartService;
 
    @EventListener
    public void handleDeviceStatusEvent(DeviceStatusEvent event) {
        String boxSn = event.getBoxSn();
        boolean isOnline = event.isOnline();
 
        if (isOnline) {
            log.info("设备 {} 已上线", boxSn);
            sendUavStatus(boxSn, "online");
        } else {
            log.info("设备 {} 已离线", boxSn);
            sendUavStatus(boxSn, "offline");
            //将剩余轨迹分片数据入库
            ardFlightTaskPartService.flushRemaining(boxSn);
        }
    }
 
    /**
     * 发送无人机状态消息到所有客户端
     */
    private void sendUavStatus(String boxSn, String status) {
        BaseMessage<Map<String, Object>> baseMessage = new BaseMessage<>();
        baseMessage.setType("uav_status");
 
        Map<String, Object> payload = new HashMap<>();
        payload.put("boxSn", boxSn);
        payload.put("status", status);
 
        baseMessage.setPayload(payload);
        WebSocketUtil.sendToAll(baseMessage);
    }
}