Compare commits
4 Commits
f5663d7552
...
3f15e9f3ed
| Author | SHA1 | Date |
|---|---|---|
|
|
3f15e9f3ed | |
|
|
eb4651f682 | |
|
|
f20216fc29 | |
|
|
6bda88ab0c |
|
|
@ -13,6 +13,7 @@ import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -130,7 +131,26 @@ public class FlightServiceImpl implements FlightService
|
||||||
result.put("createTime", flight.getCreateTime());
|
result.put("createTime", flight.getCreateTime());
|
||||||
|
|
||||||
List<PreCheckLogEntity> preCheckLogs = preCheckLogMapper.selectPreCheckLogListByFlightId(flight.getFlightId());
|
List<PreCheckLogEntity> preCheckLogs = preCheckLogMapper.selectPreCheckLogListByFlightId(flight.getFlightId());
|
||||||
result.put("preCheckLogs", preCheckLogs);
|
List<Map<String, Object>> preCheckLogsWithExecTime = new ArrayList<>();
|
||||||
|
Date prevTime = flight.getCreateTime();
|
||||||
|
|
||||||
|
for (PreCheckLogEntity log : preCheckLogs) {
|
||||||
|
Map<String, Object> logMap = new HashMap<>();
|
||||||
|
logMap.put("logId", log.getLogId());
|
||||||
|
logMap.put("flightId", log.getFlightId());
|
||||||
|
logMap.put("logContent", log.getLogContent());
|
||||||
|
logMap.put("success", log.getSuccess());
|
||||||
|
logMap.put("createTime", log.getCreateTime());
|
||||||
|
logMap.put("updateTime", log.getUpdateTime());
|
||||||
|
|
||||||
|
if (log.getCreateTime() != null && prevTime != null) {
|
||||||
|
logMap.put("execTime", log.getCreateTime().getTime() - prevTime.getTime());
|
||||||
|
prevTime = log.getCreateTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
preCheckLogsWithExecTime.add(logMap);
|
||||||
|
}
|
||||||
|
result.put("preCheckLogs", preCheckLogsWithExecTime);
|
||||||
|
|
||||||
List<FlightLogEntity> flightLogs = flightLogMapper.selectFlightLogListByFlightId(flight.getFlightId());
|
List<FlightLogEntity> flightLogs = flightLogMapper.selectFlightLogListByFlightId(flight.getFlightId());
|
||||||
result.put("flightLogs", flightLogs);
|
result.put("flightLogs", flightLogs);
|
||||||
|
|
|
||||||
|
|
@ -36,28 +36,28 @@ public class FlightStateChangeListener implements StateChangeListener {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStateChange(String sn, MachineStates newStates) {
|
public void onStateChange(String sn, MachineStates newStates) {
|
||||||
try {
|
// try {
|
||||||
DroneState droneState = newStates.getDroneState();
|
// DroneState droneState = newStates.getDroneState();
|
||||||
if (droneState == DroneState.UNKNOWN) {
|
// if (droneState == DroneState.UNKNOWN) {
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
FlightEntity flight = flightService.getOrCreateCurrentFlight(sn);
|
// FlightEntity flight = flightService.getOrCreateCurrentFlight(sn);
|
||||||
if (flight == null) {
|
// if (flight == null) {
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
String currentStatus = flight.getStatus();
|
// String currentStatus = flight.getStatus();
|
||||||
String newStatus = mapDroneStateToFlightStatus(droneState);
|
// String newStatus = mapDroneStateToFlightStatus(droneState);
|
||||||
|
//
|
||||||
if (!currentStatus.equals(newStatus)) {
|
// if (!currentStatus.equals(newStatus)) {
|
||||||
flightService.updateFlightStatus(flight.getFlightId(), newStatus);
|
// flightService.updateFlightStatus(flight.getFlightId(), newStatus);
|
||||||
log.info("状态变化更新飞行状态: sn={}, droneState={}, flightStatus={}",
|
// log.info("状态变化更新飞行状态: sn={}, droneState={}, flightStatus={}",
|
||||||
sn, droneState, newStatus);
|
// sn, droneState, newStatus);
|
||||||
}
|
// }
|
||||||
} catch (Exception e) {
|
// } catch (Exception e) {
|
||||||
log.error("状态变化监听器处理失败: sn={}, error={}", sn, e.getMessage(), e);
|
// log.error("状态变化监听器处理失败: sn={}, error={}", sn, e.getMessage(), e);
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
private String mapDroneStateToFlightStatus(DroneState droneState) {
|
private String mapDroneStateToFlightStatus(DroneState droneState) {
|
||||||
|
|
|
||||||
|
|
@ -1,104 +0,0 @@
|
||||||
package com.ruoyi.device.websocket;
|
|
||||||
|
|
||||||
import com.alibaba.fastjson2.JSON;
|
|
||||||
import com.ruoyi.device.mapper.entity.FlightLogEntity;
|
|
||||||
import com.ruoyi.device.service.FlightService;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import jakarta.websocket.*;
|
|
||||||
import jakarta.websocket.server.PathParam;
|
|
||||||
import jakarta.websocket.server.ServerEndpoint;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.concurrent.CopyOnWriteArraySet;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 飞行日志WebSocket
|
|
||||||
*
|
|
||||||
* @author ruoyi
|
|
||||||
* @date 2026-02-25
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
@Component
|
|
||||||
@ServerEndpoint("/websocket/flightLog/{deviceSn}")
|
|
||||||
public class FlightLogWebSocket {
|
|
||||||
|
|
||||||
private Session session;
|
|
||||||
|
|
||||||
private String deviceSn;
|
|
||||||
|
|
||||||
private static final CopyOnWriteArraySet<FlightLogWebSocket> sessions = new CopyOnWriteArraySet<>();
|
|
||||||
|
|
||||||
private static final Map<String, FlightLogWebSocket> sessionMap = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
private static FlightService flightService;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public void setFlightService(FlightService flightService) {
|
|
||||||
FlightLogWebSocket.flightService = flightService;
|
|
||||||
}
|
|
||||||
|
|
||||||
@OnOpen
|
|
||||||
public void onOpen(Session session, @PathParam("deviceSn") String deviceSn) {
|
|
||||||
this.session = session;
|
|
||||||
this.deviceSn = deviceSn;
|
|
||||||
sessions.add(this);
|
|
||||||
sessionMap.put(session.getId(), this);
|
|
||||||
log.info("飞行日志WebSocket连接建立: sessionId={}, deviceSn={}", session.getId(), deviceSn);
|
|
||||||
}
|
|
||||||
|
|
||||||
@OnClose
|
|
||||||
public void onClose() {
|
|
||||||
sessions.remove(this);
|
|
||||||
if (session != null) {
|
|
||||||
sessionMap.remove(session.getId());
|
|
||||||
log.info("飞行日志WebSocket连接关闭: sessionId={}, deviceSn={}", session.getId(), deviceSn);
|
|
||||||
} else {
|
|
||||||
log.info("飞行日志WebSocket连接关闭: session为null");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@OnMessage
|
|
||||||
public void onMessage(String message) {
|
|
||||||
log.info("收到飞行日志WebSocket消息: sessionId={}, message={}", session.getId(), message);
|
|
||||||
}
|
|
||||||
|
|
||||||
@OnError
|
|
||||||
public void onError(Session session, Throwable error) {
|
|
||||||
log.error("飞行日志WebSocket错误: sessionId={}, deviceSn={}, error={}",
|
|
||||||
session.getId(), deviceSn, error.getMessage(), error);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void sendMessage(String message) {
|
|
||||||
try {
|
|
||||||
if (session != null && session.isOpen()) {
|
|
||||||
session.getBasicRemote().sendText(message);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("发送飞行日志WebSocket消息失败: deviceSn={}, error={}", deviceSn, e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Scheduled(fixedRate = 3000)
|
|
||||||
public void broadcast() {
|
|
||||||
for (FlightLogWebSocket ws : sessions) {
|
|
||||||
Map<String, Object> flightData = flightService.getLatestFlightWithLogs(ws.deviceSn);
|
|
||||||
if (flightData == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
String status = (String) flightData.get("status");
|
|
||||||
List<FlightLogEntity> logs = (List<FlightLogEntity>) flightData.get("flightLogs");
|
|
||||||
|
|
||||||
Map<String, Object> response = new ConcurrentHashMap<>();
|
|
||||||
response.put("status", status);
|
|
||||||
response.put("logs", logs);
|
|
||||||
|
|
||||||
ws.sendMessage(JSON.toJSONString(response));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package com.ruoyi.device.websocket;
|
package com.ruoyi.device.websocket;
|
||||||
|
|
||||||
import com.alibaba.fastjson2.JSON;
|
import com.alibaba.fastjson2.JSON;
|
||||||
|
import com.ruoyi.device.mapper.entity.FlightLogEntity;
|
||||||
import com.ruoyi.device.mapper.entity.PreCheckLogEntity;
|
import com.ruoyi.device.mapper.entity.PreCheckLogEntity;
|
||||||
import com.ruoyi.device.service.FlightService;
|
import com.ruoyi.device.service.FlightService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
@ -11,25 +12,23 @@ import org.springframework.stereotype.Component;
|
||||||
import jakarta.websocket.*;
|
import jakarta.websocket.*;
|
||||||
import jakarta.websocket.server.PathParam;
|
import jakarta.websocket.server.PathParam;
|
||||||
import jakarta.websocket.server.ServerEndpoint;
|
import jakarta.websocket.server.ServerEndpoint;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.CopyOnWriteArraySet;
|
import java.util.concurrent.CopyOnWriteArraySet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自检日志WebSocket
|
* 飞行日志WebSocket (包含自检日志和飞行日志)
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
* @date 2026-02-25
|
* @date 2026-02-25
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@ServerEndpoint("/websocket/preCheckLog/{deviceSn}")
|
@ServerEndpoint("/websocket/flightLog/{deviceSn}")
|
||||||
public class PreCheckLogWebSocket {
|
public class PreCheckLogWebSocket {
|
||||||
|
|
||||||
private Session session;
|
private Session session;
|
||||||
|
|
||||||
private String deviceSn;
|
private String deviceSn;
|
||||||
|
|
||||||
private static final CopyOnWriteArraySet<PreCheckLogWebSocket> sessions = new CopyOnWriteArraySet<>();
|
private static final CopyOnWriteArraySet<PreCheckLogWebSocket> sessions = new CopyOnWriteArraySet<>();
|
||||||
|
|
@ -49,7 +48,7 @@ public class PreCheckLogWebSocket {
|
||||||
this.deviceSn = deviceSn;
|
this.deviceSn = deviceSn;
|
||||||
sessions.add(this);
|
sessions.add(this);
|
||||||
sessionMap.put(session.getId(), this);
|
sessionMap.put(session.getId(), this);
|
||||||
log.info("自检日志WebSocket连接建立: sessionId={}, deviceSn={}", session.getId(), deviceSn);
|
log.info("飞行日志WebSocket连接建立: sessionId={}, deviceSn={}", session.getId(), deviceSn);
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnClose
|
@OnClose
|
||||||
|
|
@ -92,14 +91,7 @@ public class PreCheckLogWebSocket {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
String status = (String) flightData.get("status");
|
ws.sendMessage(JSON.toJSONString(flightData));
|
||||||
List<PreCheckLogEntity> logs = (List<PreCheckLogEntity>) flightData.get("preCheckLogs");
|
|
||||||
|
|
||||||
Map<String, Object> response = new ConcurrentHashMap<>();
|
|
||||||
response.put("status", status);
|
|
||||||
response.put("logs", logs);
|
|
||||||
|
|
||||||
ws.sendMessage(JSON.toJSONString(response));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue