|
|
@ -4,7 +4,11 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
import com.iflytop.colortitration.app.common.enums.MultipleModuleCode; |
|
|
|
import com.iflytop.colortitration.app.core.state.DeviceState; |
|
|
|
import com.iflytop.colortitration.app.model.dto.PhotoSaveDTO; |
|
|
|
import com.iflytop.colortitration.app.websocket.server.WebSocketMessageType; |
|
|
|
import com.iflytop.colortitration.app.websocket.server.WebSocketSender; |
|
|
|
import com.iflytop.colortitration.common.base.BasePageQuery; |
|
|
|
import com.iflytop.colortitration.common.enums.PhotoModeType; |
|
|
|
import com.iflytop.colortitration.common.exception.AppException; |
|
|
@ -16,17 +20,19 @@ import com.iflytop.colortitration.common.result.ResultCode; |
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.core.io.Resource; |
|
|
|
import org.springframework.core.io.ResourceLoader; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.util.StreamUtils; |
|
|
|
|
|
|
|
import java.io.*; |
|
|
|
import java.nio.file.Files; |
|
|
|
import java.nio.file.Path; |
|
|
|
import java.nio.file.Paths; |
|
|
|
import java.nio.file.StandardCopyOption; |
|
|
|
import java.time.LocalDate; |
|
|
|
import java.time.format.DateTimeFormatter; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.List; |
|
|
|
import java.util.*; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
/** |
|
|
@ -36,6 +42,9 @@ import java.util.stream.Collectors; |
|
|
|
@Service |
|
|
|
@RequiredArgsConstructor |
|
|
|
public class PhotosService extends ServiceImpl<PhotosMapper, Photos> { |
|
|
|
private final ResourceLoader resourceLoader; |
|
|
|
private final DeviceState deviceState; |
|
|
|
private final WebSocketSender webSocketSender; |
|
|
|
|
|
|
|
@Value("${photo.url}") |
|
|
|
private String url; |
|
|
@ -79,32 +88,42 @@ public class PhotosService extends ServiceImpl<PhotosMapper, Photos> { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
public String take() 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); |
|
|
|
// } |
|
|
|
// |
|
|
|
// try { |
|
|
|
// String tempFilePath = "/temp/" + System.currentTimeMillis() + ".png"; |
|
|
|
// String filePath = path + tempFilePath; |
|
|
|
// driver.enable(); |
|
|
|
// driver.saveColorImg(filePath); |
|
|
|
// return url + tempFilePath; |
|
|
|
// } catch (Exception e) { |
|
|
|
// throw new AppException(ResultCode.SYSTEM_ERROR); |
|
|
|
// } finally { |
|
|
|
// driver.disable(); |
|
|
|
// } |
|
|
|
return null; |
|
|
|
public void take(MultipleModuleCode moduleCode) throws IOException { |
|
|
|
File file = getStaticFile("1.png"); |
|
|
|
deviceState.getTitrationModuleStateMap().get(moduleCode).setCurrentPhoto(file); |
|
|
|
|
|
|
|
// 1. 读取图片并转 Base64 |
|
|
|
byte[] bytes = Files.readAllBytes(file.toPath()); |
|
|
|
String b64 = Base64.getEncoder().encodeToString(bytes); |
|
|
|
String dataUrl = "data:image/png;base64," + b64; |
|
|
|
|
|
|
|
// 2. 构造要发送的对象 |
|
|
|
Map<String, Object> payload = new HashMap<>(); |
|
|
|
payload.put("moduleCode", moduleCode); |
|
|
|
payload.put("image", dataUrl); |
|
|
|
|
|
|
|
webSocketSender.push(WebSocketMessageType.PHOTO, payload); |
|
|
|
} |
|
|
|
|
|
|
|
public File getStaticFile(String fileName) throws IOException { |
|
|
|
Resource res = resourceLoader.getResource("classpath:static/" + fileName); |
|
|
|
if (!res.exists()) { |
|
|
|
throw new FileNotFoundException("找不到资源"); |
|
|
|
} |
|
|
|
try { |
|
|
|
// 运行在“解压”目录下,直接返回真实文件 |
|
|
|
return res.getFile(); |
|
|
|
} catch (IOException e) { |
|
|
|
// 运行在 JAR 内部,复制到临时文件 |
|
|
|
File tmp = File.createTempFile("static-" + fileName, ".png"); |
|
|
|
try (InputStream is = res.getInputStream(); |
|
|
|
OutputStream os = new FileOutputStream(tmp)) { |
|
|
|
StreamUtils.copy(is, os); |
|
|
|
} |
|
|
|
//在 JVM 退出时删除 |
|
|
|
tmp.deleteOnExit(); |
|
|
|
return tmp; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void save(PhotoSaveDTO photoSaveDTO) { |
|
|
|