#update
This commit is contained in:
@@ -1,67 +1,181 @@
|
||||
package com.maibu.controller;
|
||||
|
||||
import com.maibu.core.business.WorkRecord;
|
||||
import com.maibu.core.business.path.LatAndLngEntity;
|
||||
import com.maibu.core.controller.BaseController;
|
||||
import com.maibu.core.domain.R;
|
||||
import com.maibu.memory.GlobalMemory;
|
||||
import com.maibu.service.impl.HostComputerService;
|
||||
import com.maibu.service.impl.WorkRecordService;
|
||||
import com.maibu.utils.MinioUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/iot/host")
|
||||
public class HostComputerController extends BaseController {
|
||||
@Autowired
|
||||
private MinioUtil minioUtil;
|
||||
|
||||
@Autowired
|
||||
private WorkRecordService workRecordService;
|
||||
private static final int BATCH_SIZE = 5000;
|
||||
|
||||
@Autowired
|
||||
private HostComputerService hostComputerService;
|
||||
|
||||
@PostMapping(value = "/add", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
public R addWorkRecord(
|
||||
@RequestPart("file") MultipartFile file, // 接收上传的图片
|
||||
@RequestPart("workRecord") WorkRecord workRecord // 接收表单里的 JSON 对象
|
||||
/**
|
||||
* 上传 csv.gz 轨迹文件并解析为 LatAndLngEntity 列表(分批处理,每批 BATCH_SIZE 条)
|
||||
*
|
||||
* <p>CSV 格式:第 1 列=cell_index,第 2 列=x(lat),第 3 列=y(lng)</p>
|
||||
*
|
||||
* @param file csv.gz 压缩文件
|
||||
* @param taskId 任务ID
|
||||
* @return 解析后的完整轨迹数据
|
||||
*/
|
||||
@PostMapping(value = "/upload-path-csv", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
public R uploadCsv(
|
||||
@RequestPart("file") MultipartFile file,
|
||||
@RequestParam("taskId") Long taskId
|
||||
) {
|
||||
int i = -1;
|
||||
|
||||
WorkRecord workRecord1 = workRecordService.selectWorkRecordByWorkNameAndSiteId(workRecord.getWorkName(), workRecord.getSiteId());
|
||||
if (workRecord1 != null) {
|
||||
return R.fail("该任务已存在");
|
||||
if (file == null || file.isEmpty()) {
|
||||
return R.fail("文件不能为空");
|
||||
}
|
||||
if (taskId == null) {
|
||||
return R.fail("taskId 不能为空");
|
||||
}
|
||||
|
||||
try (InputStream inputStream = file.getInputStream()) {
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
if (originalFilename == null || !originalFilename.toLowerCase().endsWith(".csv.gz")) {
|
||||
return R.fail("只支持 .csv.gz 格式文件");
|
||||
}
|
||||
|
||||
String fileName = "routePlanImage/rt" + workRecord.getWorkName() + System.currentTimeMillis() + ".png";
|
||||
log.info("收到 csv.gz 文件: taskId={}, fileName={}, size={}", taskId, originalFilename, file.getSize());
|
||||
|
||||
// 上传到 MinIO
|
||||
minioUtil.uploadFile(fileName, inputStream, "image/png");
|
||||
List<LatAndLngEntity> allPath = new ArrayList<>();
|
||||
List<LatAndLngEntity> batch = new ArrayList<>(BATCH_SIZE);
|
||||
int batchIndex = 0;
|
||||
|
||||
// 获取访问 URL
|
||||
String url = minioUtil.getPublicUrl(fileName);
|
||||
System.out.println("✅ 图片上传成功,访问地址: " + url);
|
||||
try (GZIPInputStream gzipInputStream = new GZIPInputStream(file.getInputStream());
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(gzipInputStream))) {
|
||||
|
||||
// 这里可以把 URL 存到 workRecord 里
|
||||
workRecord.setImgUrl(url);
|
||||
workRecord.setUserId(getUserId());
|
||||
workRecord.setOrgId(getLoginUser().getOrgId());
|
||||
String line;
|
||||
int lineNum = 0;
|
||||
|
||||
while ((line = reader.readLine()) != null) {
|
||||
lineNum++;
|
||||
if (line.trim().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String[] values = line.split(",");
|
||||
|
||||
if (lineNum == 1) {
|
||||
log.info("CSV 表头: {}", Arrays.toString(values));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (values.length < 3) {
|
||||
log.warn("第 {} 行列数不足3列,跳过: {}", lineNum, line);
|
||||
continue;
|
||||
}
|
||||
|
||||
LatAndLngEntity entity = parseLatLng(values);
|
||||
if (entity != null) {
|
||||
batch.add(entity);
|
||||
}
|
||||
|
||||
if (batch.size() >= BATCH_SIZE) {
|
||||
batchIndex++;
|
||||
processBatch(taskId, batch, batchIndex);
|
||||
allPath.addAll(batch);
|
||||
batch.clear();
|
||||
log.info("csv 解析进度: taskId={}, 第 {} 批({}条), 累计 {} 条",
|
||||
taskId, batchIndex, BATCH_SIZE, allPath.size());
|
||||
}
|
||||
}
|
||||
|
||||
if (!batch.isEmpty()) {
|
||||
batchIndex++;
|
||||
processBatch(taskId, batch, batchIndex);
|
||||
allPath.addAll(batch);
|
||||
batch.clear();
|
||||
}
|
||||
|
||||
log.info("csv 解析完成: taskId={}, 共 {} 个轨迹点, 分 {} 批处理", taskId, allPath.size(), batchIndex);
|
||||
|
||||
GlobalMemory.taskPathMap.put(taskId, allPath);
|
||||
|
||||
|
||||
return R.ok();
|
||||
|
||||
i = hostComputerService.saveWorkRecord(workRecord);
|
||||
} catch (Exception e) {
|
||||
return R.fail("新增失败: " + e.getMessage());
|
||||
log.error("csv.gz 解析失败: taskId={}, error={}", taskId, e.getMessage(), e);
|
||||
return R.fail("文件解析失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 CSV 行解析 LatAndLngEntity
|
||||
* 第 2 列(x) → lat,第 3 列(y) → lng
|
||||
*/
|
||||
private LatAndLngEntity parseLatLng(String[] values) {
|
||||
try {
|
||||
String xStr = values[1].trim();
|
||||
String yStr = values[2].trim();
|
||||
|
||||
if (xStr.isEmpty() || yStr.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
LatAndLngEntity entity = new LatAndLngEntity();
|
||||
entity.setLat(Double.parseDouble(xStr));
|
||||
entity.setLng(Double.parseDouble(yStr));
|
||||
return entity;
|
||||
} catch (NumberFormatException e) {
|
||||
log.warn("解析轨迹点失败: values={}, error={}", Arrays.toString(values), e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 分批处理轨迹数据
|
||||
*
|
||||
* <p>每批 BATCH_SIZE 条数据触发一次,可用于:</p>
|
||||
* <ul>
|
||||
* <li>批量入库(insertBatch)</li>
|
||||
* <li>发布 MQTT 消息到上位机</li>
|
||||
* <li>数据校验/统计</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param taskId 任务ID
|
||||
* @param path 当前批次的轨迹点列表
|
||||
* @param batchIndex 批次序号(从 1 开始)
|
||||
*/
|
||||
protected void processBatch(Long taskId, List<LatAndLngEntity> path, int batchIndex) {
|
||||
if (path == null || path.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
return i <= 0 ? R.fail("新增失败") : R.ok("新增成功");
|
||||
int size = path.size();
|
||||
double minLat = Double.MAX_VALUE, maxLat = -Double.MAX_VALUE;
|
||||
double minLng = Double.MAX_VALUE, maxLng = -Double.MAX_VALUE;
|
||||
|
||||
for (LatAndLngEntity p : path) {
|
||||
if (p.getLat() < minLat) minLat = p.getLat();
|
||||
if (p.getLat() > maxLat) maxLat = p.getLat();
|
||||
if (p.getLng() < minLng) minLng = p.getLng();
|
||||
if (p.getLng() > maxLng) maxLng = p.getLng();
|
||||
}
|
||||
|
||||
log.info("【第 {} 批处理】taskId={}, 本批 {} 条, lat范围=[{}, {}], lng范围=[{}, {}]",
|
||||
batchIndex, taskId, size,
|
||||
String.format("%.6f", minLat), String.format("%.6f", maxLat),
|
||||
String.format("%.6f", minLng), String.format("%.6f", maxLng));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.maibu.controller;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
import com.maibu.core.domain.AjaxResult;
|
||||
import com.maibu.memory.GlobalMemory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -110,4 +112,9 @@ public class WorkRecordController extends BaseController {
|
||||
return R.ok(path);
|
||||
}
|
||||
|
||||
@GetMapping("getTaskPath")
|
||||
public AjaxResult getTaskPath(@RequestParam Long taskId) {
|
||||
return AjaxResult.success(GlobalMemory.taskPathMap.get(taskId));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.maibu.dto.host;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Coverage {
|
||||
|
||||
private double cell_size;//覆盖分辨率,单位m
|
||||
|
||||
private double angle;//覆盖作业方向,单位度
|
||||
|
||||
private int start_point_index;//覆盖起始边界点序号
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.maibu.dto.host;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class HostWorkRecordCreateDTO {
|
||||
|
||||
private String task_id; //任务唯一编号
|
||||
private String command_id;//指令唯一编号
|
||||
private String mode;//导航任务模式
|
||||
private String action;//任务动作 start/pause/resume/cancel
|
||||
private String frame_id; //WGS84
|
||||
|
||||
private List<PointEntity> points;
|
||||
private Coverage coverage;
|
||||
|
||||
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.maibu.dto.host;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class PointEntity {
|
||||
|
||||
private int index;//目标点序号
|
||||
private double latitude;
|
||||
private double longitude;
|
||||
private double yaw;//目标航向,正北0°、顺时针正、0-360°
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.maibu.service.impl;
|
||||
|
||||
import com.maibu.core.business.WorkRecord;
|
||||
import com.maibu.memory.GlobalMemory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -13,13 +14,5 @@ public class HostComputerService {
|
||||
private WorkRecordService workRecordService;
|
||||
|
||||
|
||||
public int saveWorkRecord(WorkRecord workRecord) throws Exception {
|
||||
|
||||
// 发送 mqtt 消息到上位机
|
||||
|
||||
|
||||
return workRecordService.addWorkRecord(workRecord);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user