From e90d0fe42b652c9230c5e544d177dbedb3e57580 Mon Sep 17 00:00:00 2001
From: zhaowenyu <35589726@qq.com>
Date: Wed, 19 Nov 2025 13:51:26 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev' into dev
---
ard-work/src/main/java/com/ard/work/service/impl/ArdFlightTaskServiceServiceImpl.java | 415 +++++++++++++++++++++++++++++++++++++++++++++-------------
1 files changed, 319 insertions(+), 96 deletions(-)
diff --git a/ard-work/src/main/java/com/ard/work/service/impl/ArdFlightTaskServiceServiceImpl.java b/ard-work/src/main/java/com/ard/work/service/impl/ArdFlightTaskServiceServiceImpl.java
index 87b1be2..bcc4d7d 100644
--- a/ard-work/src/main/java/com/ard/work/service/impl/ArdFlightTaskServiceServiceImpl.java
+++ b/ard-work/src/main/java/com/ard/work/service/impl/ArdFlightTaskServiceServiceImpl.java
@@ -1,10 +1,8 @@
package com.ard.work.service.impl;
import com.ard.common.utils.DateUtils;
-import com.ard.common.utils.StringUtils;
import com.ard.common.utils.file.MimeTypeUtils;
import com.ard.common.utils.uuid.IdUtils;
-import com.ard.oss.service.IOssService;
import com.ard.oss.service.impl.OssServiceContext;
import com.ard.work.component.forest.TyClient;
import com.ard.work.domain.dto.BoxHistoryDTO;
@@ -18,23 +16,29 @@
import com.ard.work.mapper.ArdMediaMapper;
import com.ard.work.service.IArdFlightTaskService;
import com.ard.work.service.IArdServerInfoService;
-import org.apache.commons.io.FilenameUtils;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.scheduling.annotation.Async;
+import lombok.extern.slf4j.Slf4j;
+import org.bytedeco.javacv.FFmpegFrameGrabber;
+import org.bytedeco.javacv.Frame;
+import org.bytedeco.javacv.Java2DFrameConverter;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
-import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
+import javax.imageio.ImageIO;
import javax.net.ssl.*;
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.InputStream;
+import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;
-import java.util.Objects;
import java.util.concurrent.CompletableFuture;
+import java.util.stream.Collectors;
+@Slf4j
@Service
public class ArdFlightTaskServiceServiceImpl implements IArdFlightTaskService {
@@ -48,16 +52,23 @@
private TyClient tyClient;
@Resource
private OssServiceContext ossServiceContext;
-
@Resource
private ThreadPoolTaskExecutor threadPoolTaskExecutor; // 使用线程池执行异步任务
+ /**
+ * 查询飞行架次列表
+ *
+ * @param ardFlightTask 飞行架次
+ * @return 飞行架次集合
+ */
@Override
public List<ArdFlightTask> selectArdFlightTask(ArdFlightTask ardFlightTask) {
return ardFlightTaskMapper.selectArdFlightTask(ardFlightTask);
}
- // 同步所有服务的飞行历史
+ /**
+ * 同步所有服务的飞行历史
+ */
@Scheduled(fixedDelay = 10000)
public void syncAllFlightHistory() {
List<String> serverIds = ardServerInfoService.getServerIdsList();
@@ -75,12 +86,12 @@
return maxStartTime != null ? maxStartTime : new Date(0); // 如果没有数据,返回 Unix 纪元时间作为起点
}
- // 调用接口获取飞行历史数据
// 根据上次同步时间同步飞行历史
- public void syncFlightHistory(String serverId, Date lastSyncTime) {
+ public void syncFlightHistory(String serverId, Date startTime) {
// 创建查询条件 BoxHistoryDTO,并设置 startTime 作为过滤条件
- BoxHistoryDTO boxHistoryDTO = new BoxHistoryDTO();
- boxHistoryDTO.setStartTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, lastSyncTime)); // 传递 lastSyncTime 参数
+ BoxHistoryDTO boxHistoryDTO = BoxHistoryDTO.builder()
+ .startTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, startTime))
+ .build();
// 获取飞行历史
List<BoxHistoryReceiver> historyList = getBoxHistory(serverId, boxHistoryDTO);
@@ -93,118 +104,257 @@
}
}
+ // 将数据同步到本地数据库
+ private void syncToLocalDatabase(String serverId, BoxHistoryReceiver boxHistoryReceiver) {
+ // 检查是否已经存在相同任务ID的记录
+ ArdFlightTask ardFlightTask = ardFlightTaskMapper.getArdFlightTaskById(boxHistoryReceiver.getTaskId());
+ if (ardFlightTask == null) {
+ // 如果不存在,则插入新记录
+ log.debug("insert task {}", boxHistoryReceiver.getTaskId());
+ // 创建FlightTask实体对象并填充数据
+ ArdFlightTask flightTask = ArdFlightTask.builder()
+ .taskId(boxHistoryReceiver.getTaskId())
+ .boxSn(boxHistoryReceiver.getBoxSn())
+ .boxName(boxHistoryReceiver.getBoxName())
+ .taskName(boxHistoryReceiver.getTaskName())
+ .photoCount(Integer.parseInt(boxHistoryReceiver.getPhotoCount()))
+ .videoCount((boxHistoryReceiver.getVideoPathList() != null) ?
+ boxHistoryReceiver.getVideoPathList().size() : 0)
+ .videoPath(boxHistoryReceiver.getVideoPath())
+ .serverId(serverId)
+ .startTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, boxHistoryReceiver.getStartTime()))
+ .endTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, boxHistoryReceiver.getEndTime()))
+ .build();
+ flightTask.setCreateTime(DateUtils.getNowDate());
+ // 插入新记录
+ int rows = ardFlightTaskMapper.insertArdFlightTask(flightTask);
+ if (rows > 0) {
+ // 删除旧的媒体记录,防止重复插入
+ ardMediaMapper.deleteArdMediaByTaskId(flightTask.getTaskId());
+
+ // 插入视频
+ insertVideoToDatabase(flightTask, boxHistoryReceiver.getVideoPathList());
+
+ // 插入图片
+ insertPhotoToDatabase(flightTask, serverId, boxHistoryReceiver.getBoxSn());
+ }
+ } else {
+ // 如果存在,则更新记录
+ log.debug("Updating task {}", boxHistoryReceiver.getTaskId());
+ // 增量更新
+ updateVideoToDatabase(ardFlightTask, boxHistoryReceiver.getVideoPathList());
+ // 增量更新图片
+ updatePhotoToDatabase(ardFlightTask, serverId, boxHistoryReceiver.getBoxSn());
+
+ }
+ }
+
+
// 调用接口获取飞行历史数据
private List<BoxHistoryReceiver> getBoxHistory(String serverId, BoxHistoryDTO boxHistoryDTO) {
TyHttpCommonReceiver<List<BoxHistoryReceiver>> boxHistory = tyClient.getBoxHistory(serverId, boxHistoryDTO);
return boxHistory.getContent();
}
- // 将数据同步到本地数据库
- private void syncToLocalDatabase(String serverId, BoxHistoryReceiver boxHistoryReceiver) {
- // 创建FlightTask实体对象并填充数据
- ArdFlightTask flightTask = new ArdFlightTask();
- flightTask.setTaskId(boxHistoryReceiver.getTaskId());
- flightTask.setBoxSn(boxHistoryReceiver.getBoxSn());
- flightTask.setBoxName(boxHistoryReceiver.getBoxName());
- flightTask.setTaskName(boxHistoryReceiver.getTaskName());
+ //调用接口获取架次图片
+ private List<BoxPhotosReceiver> getBoxPhotos(String serverId, BoxPhotosDTO boxPhotosDTO) {
+ TyHttpCommonReceiver<List<BoxPhotosReceiver>> boxPhotos = tyClient.getBoxPhotos(serverId, boxPhotosDTO);
+ return boxPhotos.getContent();
+ }
- Integer photoCount = Integer.parseInt(boxHistoryReceiver.getPhotoCount());
- flightTask.setPhotoCount(photoCount);
- Integer videoCount = (boxHistoryReceiver.getVideoPathList() != null) ?
- boxHistoryReceiver.getVideoPathList().size() : 0;
- flightTask.setVideoCount(videoCount);
+ // 插入视频记录
+ private void insertVideoToDatabase(ArdFlightTask flightTask, List<String> videoPathList) {
+ if (videoPathList != null && !videoPathList.isEmpty()) {
+ for (String videoPath : videoPathList) {
+ ArdMedia ardMedia = ArdMedia.builder()
+ .id(IdUtils.simpleUUID())
+ .taskId(flightTask.getTaskId())
+ .mediaType(1) // 1表示视频
+ .sourceUrl(videoPath)
+ .fileSize(getFileSizeByUrl(videoPath))
+ .build();
+ CompletableFuture.supplyAsync(() -> {
+ try {
+ // 上传视频
+ String videoUrl = uploadFileToOss(flightTask.getBoxSn(), videoPath, "record",
+ MimeTypeUtils.VIDEO_MP4);
+ ardMedia.setMediaUrl(videoUrl);
+ // 生成缩略图 InputStream
+ ByteArrayInputStream thumbnailStream = generateThumbnailInputStream(videoPath, 2, 200);
+ if (thumbnailStream != null) {
+ String thumbnailUrl = ossServiceContext.upload("record",
+ flightTask.getBoxSn() + "/" + IdUtils.fastSimpleUUID() + ".jpg",
+ thumbnailStream,
+ MimeTypeUtils.IMAGE_JPEG);
+ ardMedia.setThumbnailUrl(thumbnailUrl);
+ }
- flightTask.setVideoPath(boxHistoryReceiver.getVideoPath());
- flightTask.setServerId(serverId);
+ ardMedia.setCreateTime(new Date());
+ return ardMedia;
- Date startTime = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, boxHistoryReceiver.getStartTime());
- flightTask.setStartTime(startTime);
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+ }, threadPoolTaskExecutor).thenAccept(am -> {
+ if (am != null) {
+ ardMediaMapper.insertArdMedia(am);
+ }
+ });
- Date endTime = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, boxHistoryReceiver.getEndTime());
- flightTask.setEndTime(endTime);
-
- flightTask.setCreateTime(DateUtils.getNowDate());
-
- // 检查是否已经存在相同任务ID的记录
- ArdFlightTask ardFlightTaskById = ardFlightTaskMapper.getArdFlightTaskById(flightTask.getTaskId());
- if (ardFlightTaskById != null) {
- return;
- }
-
- System.out.println("Syncing task " + boxHistoryReceiver.getTaskId());
-
- // 插入新记录
- int rows = ardFlightTaskMapper.insertArdFlightTask(flightTask);
- if (rows > 0) {
- // 删除旧的媒体记录,防止重复插入
- ardMediaMapper.deleteArdMediaByTaskId(flightTask.getTaskId());
-
- // 插入视频
- syncVideoToDatabase(flightTask, boxHistoryReceiver.getVideoPathList());
-
- // 插入图片
- syncPhotoToDatabase(flightTask, serverId, boxHistoryReceiver.getBoxSn());
+ }
}
}
- // 插入视频记录
- private void syncVideoToDatabase(ArdFlightTask flightTask, List<String> videoPathList) {
- if (videoPathList != null && !videoPathList.isEmpty()) {
- for (String videoPath : videoPathList) {
+ private void updateVideoToDatabase(ArdFlightTask flightTask, List<String> videoPathList) {
+ if (videoPathList == null) {
+ return;
+ }
+ Integer count = flightTask.getVideoCount();//获取本地库视频数量
+ int size = videoPathList.size();//获取接口返回的视频数量
+ if (count != null && size > count) {
+ log.info("录像数量不一致,需要更新数据库");
+ // 需要插入新的视频
+ List<ArdMedia> existingVideos = ardMediaMapper.selectArdMediaByTaskIdAndType(flightTask.getTaskId(), 1);
+ // 提取现有的视频路径
+ List<String> existingVideoPaths = existingVideos.stream()
+ .map(ArdMedia::getSourceUrl) // 获取每个视频的 URL
+ .collect(Collectors.toList());
- ArdMedia ardMedia = new ArdMedia();
- ardMedia.setId(IdUtils.simpleUUID());
- ardMedia.setTaskId(flightTask.getTaskId());
- ardMedia.setMediaType(1); // 1表示视频
- ardMedia.setSourceUrl(videoPath);
- // 异步上传文件到OSS,并在上传完成后插入数据库
- CompletableFuture.supplyAsync(() -> uploadFileToOss(flightTask.getBoxSn(), videoPath, "record",
- MimeTypeUtils.VIDEO_MP4), threadPoolTaskExecutor)
- .thenAccept(url -> {
- ardMedia.setMediaUrl(url); // 获取上传后的文件 URL
+ // 从接口返回的视频路径中找出新的视频
+ List<String> newVideos = videoPathList.stream()
+ .filter(videoPath -> !existingVideoPaths.contains(videoPath)) // 过滤出新的视频
+ .collect(Collectors.toList());
+ if (!newVideos.isEmpty()) {
+ // 插入新的视频记录
+ for (String videoPath : newVideos) {
+ ArdMedia ardMedia = ArdMedia.builder()
+ .id(IdUtils.simpleUUID())
+ .taskId(flightTask.getTaskId())
+ .mediaType(1) // 1表示视频
+ .fileSize(getFileSizeByUrl(videoPath))
+ .sourceUrl(videoPath)
+ .build();
+ CompletableFuture.supplyAsync(() -> {
+ try {
+ // 上传视频
+ String videoUrl = uploadFileToOss(flightTask.getBoxSn(), videoPath, "record",
+ MimeTypeUtils.VIDEO_MP4);
+ ardMedia.setMediaUrl(videoUrl);
+
+
+ // 生成缩略图 InputStream
+ ByteArrayInputStream thumbnailStream = generateThumbnailInputStream(videoPath, 2, 200);
+ if (thumbnailStream != null) {
+ String thumbnailUrl = ossServiceContext.upload("record",
+ flightTask.getBoxSn() + "/" + IdUtils.fastSimpleUUID() + ".jpg",
+ thumbnailStream,
+ MimeTypeUtils.IMAGE_JPEG);
+ ardMedia.setThumbnailUrl(thumbnailUrl);
+ }
+
ardMedia.setCreateTime(new Date());
- ardMediaMapper.insertArdMedia(ardMedia); // 插入数据库
- });
+ return ardMedia;
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+ }, threadPoolTaskExecutor).thenAccept(am -> {
+ if (am != null) {
+ ardMediaMapper.insertArdMedia(am);
+ }
+ });
+ }
+ // 更新任务的视频数量
+ flightTask.setVideoCount(flightTask.getVideoCount() + newVideos.size());
+ ardFlightTaskMapper.updateArdFlightTask(flightTask);
}
}
}
// 插入图片记录
- private void syncPhotoToDatabase(ArdFlightTask flightTask, String serverId, String boxSn) {
+ private void insertPhotoToDatabase(ArdFlightTask flightTask, String serverId, String boxSn) {
// 查询图片
BoxPhotosDTO boxPhotosDTO = new BoxPhotosDTO();
boxPhotosDTO.setTaskId(flightTask.getTaskId());
boxPhotosDTO.setBoxSn(boxSn);
boxPhotosDTO.setPageIndex(1);
- boxPhotosDTO.setPageSize(1000);
+ boxPhotosDTO.setPageSize(500);
+ List<BoxPhotosReceiver> photoList = getBoxPhotos(serverId, boxPhotosDTO);
+ if (photoList != null && !photoList.isEmpty()) {
+ addPhotos(flightTask, photoList);
+ }
+ }
+ private void updatePhotoToDatabase(ArdFlightTask flightTask, String serverId, String boxSn) {
+ int count = flightTask.getPhotoCount(); //获取本地库图片数量
+ // 查询图片
+ BoxPhotosDTO boxPhotosDTO = BoxPhotosDTO.builder()
+ .taskId(flightTask.getTaskId())
+ .boxSn(boxSn)
+ .pageIndex(1)
+ .pageSize(500)
+ .build();
TyHttpCommonReceiver<List<BoxPhotosReceiver>> boxPhotos = tyClient.getBoxPhotos(serverId, boxPhotosDTO);
List<BoxPhotosReceiver> photoList = boxPhotos.getContent();
+ int size = photoList.size();
- if (photoList != null && !photoList.isEmpty()) {
- for (BoxPhotosReceiver photo : photoList) {
- ArdMedia ardMedia = new ArdMedia();
- ardMedia.setId(IdUtils.simpleUUID());
- ardMedia.setTaskId(flightTask.getTaskId());
- ardMedia.setMediaType(0); // 0表示图片
- ardMedia.setFileSize(photo.getFileSize());
- ardMedia.setFileMd5(photo.getFileMd5());
- ardMedia.setSourceUrl(photo.getImgUrl());
- // 异步上传文件到OSS,并在上传完成后插入数据库
- CompletableFuture.supplyAsync(() -> uploadFileToOss(flightTask.getBoxSn(), photo.getImgUrl(), "pic",
- MimeTypeUtils.IMAGE_JPG), threadPoolTaskExecutor)
- .thenAccept(url -> {
- ardMedia.setMediaUrl(url); // 获取上传后的文件 URL
- ardMedia.setCreateTime(new Date());
- ardMediaMapper.insertArdMedia(ardMedia); // 插入数据库
- });
+ if (size > count) {
+ log.info("图片数量不一致,需要更新数据库");
+ // 获取本地数据库中已存在的图片记录(类型为 0 表示图片)
+ List<ArdMedia> existingPhotos = ardMediaMapper.selectArdMediaByTaskIdAndType(flightTask.getTaskId(), 0);
+ // 需要插入新的视频
+ // 提取现有的图片路径(根据 imgUrl 来判断是否重复)
+ List<String> existingPhotoUrls = existingPhotos.stream()
+ .map(ArdMedia::getSourceUrl) // 提取 ArdMedia 中的图片 URL
+ .collect(Collectors.toList());
+
+ // 筛选出 photoList 中没有出现在 existingPhotos 中的新图片
+ List<BoxPhotosReceiver> newPhotos = photoList.stream()
+ .filter(photo -> !existingPhotoUrls.contains(photo.getImgUrl())) // 过滤出新图片
+ .collect(Collectors.toList());
+ if (!newPhotos.isEmpty()) {
+ // 插入新的视频记录
+ addPhotos(flightTask, newPhotos);
+
+ // 更新任务的视频数量
+ flightTask.setPhotoCount(flightTask.getPhotoCount() + newPhotos.size());
+ ardFlightTaskMapper.updateArdFlightTask(flightTask);
}
}
}
+ private void addPhotos(ArdFlightTask flightTask, List<BoxPhotosReceiver> photoList) {
+ for (BoxPhotosReceiver photo : photoList) {
+ ArdMedia ardMedia = ArdMedia.builder()
+ .id(IdUtils.simpleUUID())
+ .taskId(flightTask.getTaskId())
+ .mediaType(0) // 0表示图片
+ .fileSize(photo.getFileSize())
+ .fileMd5(photo.getFileMd5())
+ .sourceUrl(photo.getImgUrl())
+ .photoId(photo.getPhotoId())
+ .longitude(photo.getLng())
+ .latitude(photo.getLat())
+ .height(photo.getHeight())
+ .captureTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, photo.getTimeStamp()))
+ .build();
+ // 异步上传文件到OSS,并在上传完成后插入数据库
+ CompletableFuture.supplyAsync(() -> uploadFileToOss(flightTask.getBoxSn(), photo.getImgUrl(), "pic",
+ MimeTypeUtils.IMAGE_JPG), threadPoolTaskExecutor)
+ .thenAccept(url -> {
+ ardMedia.setMediaUrl(url); // 获取上传后的文件 URL
+ ardMedia.setCreateTime(new Date());
+ ardMediaMapper.insertArdMedia(ardMedia); // 插入数据库
+ });
+ }
+ }
+
+
// 通用的上传方法,支持视频和图片等文件上传
- public String uploadFileToOss(String boxSn, String fileUrl, String bucket, String contentType) {
+ private String uploadFileToOss(String boxSn, String fileUrl, String bucket, String contentType) {
InputStream fileInputStream = null;
String fileOssUrl = "";
try {
@@ -233,12 +383,7 @@
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// 创建所有主机名都信任的HostnameVerifier
- HostnameVerifier allHostsValid = new HostnameVerifier() {
- @Override
- public boolean verify(String hostname, SSLSession session) {
- return true;
- }
- };
+ HostnameVerifier allHostsValid = (hostname, session) -> true;
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
// 现在可以正常访问
@@ -270,4 +415,82 @@
return fileOssUrl; // 返回上传后的文件OSS URL
}
+ // 获取文件大小
+ private long getFileSizeByUrl(String fileUrl) {
+ try {
+ URL url = new URL(fileUrl);
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ conn.setRequestMethod("HEAD");
+ conn.connect();
+ return conn.getContentLengthLong(); // 返回字节大小
+ } catch (Exception e) {
+ return -1;
+ }
+ }
+
+ // 生成视频缩略图
+ public ByteArrayInputStream generateThumbnailInputStream(String videoFile,
+ int skipFrames,
+ int targetWidth) throws Exception {
+ long start = System.currentTimeMillis();
+ FFmpegFrameGrabber grabber = null;
+
+ try {
+ grabber = new FFmpegFrameGrabber(videoFile);
+ grabber.start();
+
+ int totalFrames = grabber.getLengthInFrames();
+ System.out.println("视频总帧数: " + totalFrames);
+ System.out.println("视频时长: " + grabber.getLengthInTime() / 1000000 + "秒");
+
+ Frame frame = null;
+ int currentFrame = 0;
+
+ // 读取指定帧数后的第一帧有效图像
+ while (currentFrame < totalFrames) {
+ frame = grabber.grabImage();
+ if (frame == null) {
+ break;
+ }
+
+ if (currentFrame > skipFrames && frame.image != null) {
+ break;
+ }
+ currentFrame++;
+ }
+
+ if (frame == null || frame.image == null) {
+ throw new Exception("无法获取有效视频帧");
+ }
+
+ // 转换 Frame 为 BufferedImage
+ Java2DFrameConverter converter = new Java2DFrameConverter();
+ BufferedImage image = converter.getBufferedImage(frame);
+
+ // 按目标宽度等比缩放
+ int targetHeight = (int) ((double) targetWidth / image.getWidth() * image.getHeight());
+ BufferedImage scaledImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_3BYTE_BGR);
+ scaledImage.getGraphics().drawImage(image, 0, 0, targetWidth, targetHeight, null);
+
+ // 转成 ByteArrayInputStream
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ImageIO.write(scaledImage, "jpg", baos);
+ long cost = System.currentTimeMillis() - start;
+ System.out.println("处理耗时: " + cost + "ms");
+
+ return new ByteArrayInputStream(baos.toByteArray());
+
+ } finally {
+ if (grabber != null) {
+ try {
+ grabber.stop();
+ grabber.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
+
}
--
Gitblit v1.9.3