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);
|
}
|
}
|