|
@@ -1,29 +1,20 @@
|
|
|
package com.uavps.framework.udp;
|
|
|
|
|
|
-import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
-import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.uavps.common.utils.spring.SpringUtils;
|
|
|
-import com.uavps.framework.FileUtils;
|
|
|
import com.uavps.framework.config.UdpConfig;
|
|
|
import com.uavps.framework.udp.utils.UdpDataUtils;
|
|
|
import com.uavps.framework.utils.UdpUtils;
|
|
|
import com.uavps.system.service.IUavpsAircraftService;
|
|
|
-import com.uavps.system.udp.vo.Aircraft;
|
|
|
-import com.uavps.system.udp.vo.AircraftFormation;
|
|
|
-import com.uavps.system.udp.vo.UdpData;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.websocket.Session;
|
|
|
-import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.net.DatagramPacket;
|
|
|
import java.net.DatagramSocket;
|
|
|
import java.net.SocketException;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.concurrent.BlockingQueue;
|
|
|
-import java.util.concurrent.LinkedBlockingQueue;
|
|
|
+import java.time.Instant;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -54,22 +45,6 @@ public class UdpServerService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 用于存储待写入的UDP包数据
|
|
|
- private static final BlockingQueue<LogEntry> logQueue = new LinkedBlockingQueue<>();
|
|
|
-
|
|
|
- // 日志条目类
|
|
|
- private static class LogEntry {
|
|
|
- final byte[] data;
|
|
|
- final int length;
|
|
|
- final long time;
|
|
|
-
|
|
|
- LogEntry(byte[] data, int length, long time) {
|
|
|
- this.data = data;
|
|
|
- this.length = length;
|
|
|
- this.time = time;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
public void receive(Session session) {
|
|
|
try {
|
|
|
//创建一个服务端对象,注册端口
|
|
@@ -78,14 +53,13 @@ public class UdpServerService {
|
|
|
DatagramPacket packet = new DatagramPacket(data, data.length);
|
|
|
|
|
|
String uavData = "";
|
|
|
- Date initDate = new Date();
|
|
|
- String json = "";
|
|
|
+ long initMilli = Instant.now().toEpochMilli();
|
|
|
+
|
|
|
while (true) {
|
|
|
- json = "";
|
|
|
// 设置初始时间,用于计算每个数据的时间差
|
|
|
if(TaskInfo.INSTANCE.getInitFlag()){
|
|
|
- initDate = new Date();
|
|
|
- log.info("任务Id:{},任务开始,开始时间是:{}", TaskInfo.INSTANCE.getBizId(), initDate);
|
|
|
+ initMilli = Instant.now().toEpochMilli();
|
|
|
+ log.info("任务Id:{},任务开始,开始时间是:{}", TaskInfo.INSTANCE.getBizId(), initMilli);
|
|
|
}
|
|
|
|
|
|
//使用数据包接收客户端发送的数据
|
|
@@ -94,13 +68,14 @@ public class UdpServerService {
|
|
|
byte[] remoteData = UdpDataUtils.hexStringToBytes(uavData);
|
|
|
// 将日志条目放入队列
|
|
|
// 复制数据以避免后续修改影响
|
|
|
- int length = remoteData.length;
|
|
|
- byte[] dataCopy = new byte[length];
|
|
|
- System.arraycopy(remoteData, 0, dataCopy, 0, length);
|
|
|
- long lastTime = new Date().getTime() - initDate.getTime();
|
|
|
- logQueue.add(new LogEntry(dataCopy, length, lastTime));
|
|
|
+// int length = remoteData.length;
|
|
|
+// byte[] dataCopy = new byte[length];
|
|
|
+// System.arraycopy(remoteData, 0, dataCopy, 0, length);
|
|
|
+ long lastTime = Instant.now().toEpochMilli() - initMilli;
|
|
|
+ String jsonResult = UdpUtils.getJson(remoteData);
|
|
|
+ TaskInfo.INSTANCE.getLogQueue().add(new TaskInfo.LogEntry(jsonResult, lastTime));
|
|
|
TaskInfo.INSTANCE.setLastTime(lastTime);
|
|
|
- session.getBasicRemote().sendText(UdpUtils.getJson(remoteData));
|
|
|
+ session.getBasicRemote().sendText(jsonResult);
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
log.error("UdpServerService.receive error", e);
|
|
@@ -108,40 +83,5 @@ public class UdpServerService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 文件写入线程
|
|
|
- private static class FileWriterThread extends Thread {
|
|
|
- private volatile boolean running = true;
|
|
|
-
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- try (FileOutputStream fos = new FileOutputStream(TaskInfo.INSTANCE.getFilePath(), true)) {
|
|
|
- while (running || !logQueue.isEmpty()) {
|
|
|
- LogEntry entry = logQueue.take(); // 阻塞直到有数据
|
|
|
-
|
|
|
- // 格式化时间戳(10位数字 + 冒号)
|
|
|
- String timestamp = String.format("%010d:", entry.time);
|
|
|
-
|
|
|
- // 先写入时间戳
|
|
|
- fos.write(timestamp.getBytes());
|
|
|
|
|
|
- // 再写入实际数据
|
|
|
- fos.write(entry.data, 0, entry.length);
|
|
|
-
|
|
|
- // 可选: 每条记录后加换行
|
|
|
- fos.write('\n');
|
|
|
-
|
|
|
- fos.flush();
|
|
|
- }
|
|
|
- } catch (IOException | InterruptedException e) {
|
|
|
- if (running) { // 仅在非正常关闭时打印错误
|
|
|
- System.err.println("File writer error: " + e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void shutdown() {
|
|
|
- running = false;
|
|
|
- this.interrupt(); // 中断阻塞的take操作
|
|
|
- }
|
|
|
- }
|
|
|
}
|