Browse Source

feat:进行拍照的时候,清空temp

master
白凤吉 2 months ago
parent
commit
4a30da0f67
  1. 2
      src/main/java/com/iflytop/gd/app/controller/PhotoController.java
  2. 30
      src/main/java/com/iflytop/gd/app/service/api/PhotosService.java
  3. 5
      src/main/java/com/iflytop/gd/common/result/ResultCode.java

2
src/main/java/com/iflytop/gd/app/controller/PhotoController.java

@ -41,7 +41,7 @@ public class PhotoController {
@Operation(summary = "拍摄一张照片")
@GetMapping("/take")
@PostMapping("/take")
public Result<String> take(@RequestBody PhotoTakeDTO photoTakeDTO) throws Exception {
return Result.success(photosService.take(photoTakeDTO));
}

30
src/main/java/com/iflytop/gd/app/service/api/PhotosService.java

@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iflytop.gd.app.mapper.PhotosMapper;
import com.iflytop.gd.app.model.dto.PhotoSaveDTO;
import com.iflytop.gd.app.model.dto.PhotoTakeDTO;
import com.iflytop.gd.app.model.entity.Ores;
import com.iflytop.gd.app.model.entity.Photos;
import com.iflytop.gd.app.model.vo.PhotoListVO;
import com.iflytop.gd.app.model.vo.PhotoVO;
@ -21,6 +22,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -31,6 +33,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* 照片接口服务
@ -41,6 +44,7 @@ import java.util.stream.Collectors;
public class PhotosService extends ServiceImpl<PhotosMapper, Photos> {
private final CameraBaslerDriver driver;
private final SolutionModuleService solutionModuleService;
private final OresService oresService;
@Value("${photo.url}")
private String url;
@ -84,14 +88,28 @@ public class PhotosService extends ServiceImpl<PhotosMapper, Photos> {
}
public String take(PhotoTakeDTO photoTakeDTO) throws Exception {
Path directoryPath = Paths.get(path + "/temp");
try (Stream<Path> paths = Files.list(directoryPath)) {
paths.forEach(file -> {
try {
Files.delete(file);
} catch (IOException e) {
log.error("删除文件时发生错误", e);
}
});
} catch (IOException e) {
log.error("读取目录时发生错误", e);
}
if (photoTakeDTO.getLightIntensity() != null) {
solutionModuleService.fillLightOpen(photoTakeDTO.getLightIntensity());
}
try {
String filePath = path + "/temp/" + System.currentTimeMillis() + ".png";
String tempFilePath = "/temp/" + System.currentTimeMillis() + ".png";
String filePath = path + tempFilePath;
driver.enable();
driver.saveColorImg(filePath);
return url + filePath;
return url + tempFilePath;
} finally {
driver.disable();
if (photoTakeDTO.getLightIntensity() != null) {
@ -104,9 +122,10 @@ public class PhotosService extends ServiceImpl<PhotosMapper, Photos> {
Photos photos = this.getById(photoSaveDTO.getId());
if (photos == null) {//新增
String todayDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String tempPath = "/temp/" + photoSaveDTO.getFileName();
String imagePath = "/data/" + todayDate + "/" + photoSaveDTO.getFileName();
try {
Path sourcePath = Paths.get(path + "/temp/" + photoSaveDTO.getFileName());
Path sourcePath = Paths.get(path + tempPath);
Path targetPath = Paths.get(path + imagePath);
Path targetDirectory = targetPath.getParent();
if (!Files.exists(targetDirectory)) {
@ -122,7 +141,12 @@ public class PhotosService extends ServiceImpl<PhotosMapper, Photos> {
photos.setImagePath(imagePath);
photos.setMode(PhotoModeType.manual);
}
Ores ores = oresService.getById(photoSaveDTO.getOreId());
if (ores == null) {
throw new AppException(ResultCode.DATA_ALREADY_NOT_EXISTS);
}
photos.setOreId(photoSaveDTO.getOreId());
photos.setOreName(ores.getName());
photos.setProblem(photoSaveDTO.getProblem());
photos.setRemarks(photoSaveDTO.getRemarks());
this.saveOrUpdate(photos);

5
src/main/java/com/iflytop/gd/common/result/ResultCode.java

@ -36,12 +36,13 @@ public enum ResultCode implements IResultCode, Serializable {
INVALID_CREDENTIALS("4002", "用户名或密码错误"),
OPERATION_NOT_ALLOWED("4003", "业务操作不允许"),
DATA_ALREADY_EXISTS("4004", "数据已存在"),
CONTAINER_NOT_FOUND("4005", "未找到对应溶液容器"),
DATA_ALREADY_NOT_EXISTS("4005", "数据不存在"),
CRAFT_RUNNING("4101", "工艺正在执行"),
CRAFT_CONTEXT_NULL("4102", "请先配置该加热区工艺"),
CRAFT_NO_TRAY("4005", "工艺未找到托盘"),
CONTAINER_NOT_FOUND("4201", "未找到对应溶液容器"),
//============================ 5xxx系统 & 第三方 ============================
SYSTEM_ERROR("5000", "系统内部错误"),
SERVICE_UNAVAILABLE("5001", "服务暂不可用"),

Loading…
Cancel
Save