123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- package com.fd.controller;
- import com.fd.Dto.FdageDto;
- import com.fd.Dto.PageDto;
- import com.fd.constant.CommandMsg;
- import com.fd.constant.MsgCode;
- import com.fd.constant.TypeCode;
- import com.fd.entity.FileEntity;
- import com.fd.entity.GenerateFileEntity;
- import com.fd.entity.ScheduleEntity;
- import com.fd.repository.FileRepository;
- import com.fd.repository.GenerateFileRepository;
- import com.fd.repository.ScheduleRepository;
- import com.fd.util.ResponseResult;
- import com.fd.server.CmdServer;
- import com.fd.server.FileServer;
- import com.fd.server.JsonServer;
- import io.swagger.annotations.ApiOperation;
- import lombok.extern.log4j.Log4j2;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.PageRequest;
- import org.springframework.data.domain.Sort;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.multipart.MultipartFile;
- import java.util.Date;
- import java.util.Optional;
- import java.util.concurrent.atomic.AtomicBoolean;
- /**
- * Created by Owen on 2019/11/7 0007 10:19
- * 3D Model
- *
- * 时间很长的命令才需要多前程运行,其他命令不使用
- */
- @Log4j2
- @RestController
- @RequestMapping("api/vts/model")
- public class VtsModelController {
- @Value("${uploadFile.fdmodelPath}")
- private String fdmodelPath;
- @Autowired
- private FileServer fileServer;
- @Autowired
- private FileRepository fileRepository;
- @Autowired
- private JsonServer jsonServer;
- @Autowired
- private CmdServer cmdServer;
- @Autowired
- private ScheduleRepository scheduleRepository;
- @Autowired
- private GenerateFileRepository generateFileRepository;
- // 多线程共享变量
- // false 表示完成
- private static AtomicBoolean existsSlpk2vts = new AtomicBoolean(false);
- private static AtomicBoolean existsVts = new AtomicBoolean(false);
- @ApiOperation("upload model file list")
- @PostMapping(value = "list")
- private ResponseResult list(@RequestBody PageDto param){
- log.info("run list");
- Page<FileEntity> page = fileRepository.findByType(
- TypeCode.FILE_TYPE_MODEL,
- TypeCode.FILE_TYPE_MODEL_TILE,
- PageRequest.of(param.getPageNum(), param.getPageSize(), Sort.by("createTime").descending()));
- return new ResponseResult(200, page);
- }
- /**
- * 文件上传 到3d model
- * 上传大文件 xxx.slpk
- * @return
- */
- @ApiOperation("upload 3D model file")
- @PostMapping(value = "upload", consumes = { "multipart/form-data" })
- private ResponseResult upload(@RequestParam("file") MultipartFile file){
- log.info("run upload");
- return fileServer.uploadBigFile(file, fdmodelPath);
- }
- /**
- * 可能运行1s -- 12h
- * 需要记录进度
- * 需要多前程运行
- *
- */
- @ApiOperation("commandSlpk2vts")
- @GetMapping("/command/slpk2vts/{fileId}/")
- private ResponseResult commandSlpk2vts(@PathVariable("fileId") Long fileId) {
- log.info("run commandSlpk2vts : {}", fileId);
- Optional<FileEntity> entity = fileRepository.findById(fileId);
- if (!entity.isPresent()) {
- return new ResponseResult(50002, MsgCode.E50002);
- }
- FileEntity fileEntity = entity.get();
- String fileName = fileEntity.getFileName();
- fileName = fileName.substring(0, fileName.lastIndexOf("."));
- String cmd = CommandMsg.MODEL_SLPK2VTS;
- cmd = cmd.replaceAll("@fileName", fileName);
- log.info("cmd: {}", cmd);
- if (existsSlpk2vts.compareAndSet(false, true)) {
- new Thread(new CmdSlpk2vtsThread(cmd, fileId, fileEntity)).start();
- }
- return new ResponseResult(200, "success");
- }
- public class CmdSlpk2vtsThread implements Runnable{
- private String cmd;
- private Long fileId;
- private FileEntity fileEntity;
- public CmdSlpk2vtsThread(String cmd, Long fileId, FileEntity fileEntity) {
- this.cmd = cmd;
- this.fileId = fileId;
- this.fileEntity = fileEntity;
- }
- @Override
- public void run() {
- log.info("run CmdSlpk2vtsThread");
- Integer s = cmdServer.exeCmdSlpk2vts(cmd, fileId);
- if (s == 1) {
- log.info("cmd run: {}", MsgCode.E50005);
- }
- log.info("cmd run : success");
- fileEntity.setStatus(1); // 1:表示文件执行完成
- fileEntity.setUpdateTime(new Date());
- fileRepository.save(fileEntity);
- // 保存新产生的文件到数据库
- String fileName = fileEntity.getFileName();
- fileName = fileName.substring(0, fileName.lastIndexOf("."));
- String fileUrl = cmd.substring(cmd.indexOf("--output") + 9, cmd.indexOf("--tilesetId") - 1);
- FileEntity entity = new FileEntity();
- entity.setFileName(fileName);
- entity.setFileUrl(fileUrl);
- entity.setCreateTime(new Date());
- entity.setUpdateTime(new Date());
- entity.setType(TypeCode.FILE_TYPE_MODEL_TILE);
- fileRepository.save(entity);
- // 表示执行完成,释放锁
- existsSlpk2vts.set(false);
- }
- }
- /**
- * 可能运行1s -- 12h
- * 需要记录进度
- * 需要多前程运行
- */
- @ApiOperation("commandVts")
- @GetMapping("/command/vts/{fileId}/")
- private ResponseResult commandVts(@PathVariable("fileId") Long fileId) {
- log.info("run commandVts");
- Optional<FileEntity> entity = fileRepository.findById(fileId);
- if (!entity.isPresent()) {
- return new ResponseResult(50002, MsgCode.E50002);
- }
- FileEntity fileEntity = entity.get();
- String fileName = fileEntity.getFileName();
- fileName = fileName.substring(0, fileName.lastIndexOf("."));
- String cmd = CommandMsg.MODEL_VTS;
- cmd = cmd.replaceAll("@fileName", fileName);
- log.info("cmd: {}", cmd);
- if (existsVts.compareAndSet(false, true)) {
- new Thread(new CmdVtsThread(cmd, fileId, fileEntity)).start();
- }
- return new ResponseResult(200, "success");
- }
- public class CmdVtsThread implements Runnable{
- private String cmd;
- private Long fileId;
- private FileEntity fileEntity;
- public CmdVtsThread(String cmd, Long fileId, FileEntity fileEntity) {
- this.cmd = cmd;
- this.fileId = fileId;
- this.fileEntity = fileEntity;
- }
- @Override
- public void run() {
- log.info("run CmdVtsThread");
- Integer s = cmdServer.exeCmdVts(cmd, fileId);
- if (s == 1) {
- log.info("cmd run: {}", MsgCode.E50005);
- }
- log.info("cmd run : success");
- fileEntity.setStatus(2); // 2:表示VTS命令执行完成
- fileEntity.setUpdateTime(new Date());
- fileRepository.save(fileEntity);
- // 表示执行完成,释放锁
- existsVts.set(false);
- }
- }
- @ApiOperation("command model vts remove")
- @GetMapping("/command/vts/remove/{fileId}")
- private ResponseResult commandVtsRemove(@PathVariable("fileId") Long fileId) {
- log.info("run commandVtsRemove");
- GenerateFileEntity entity = generateFileRepository.findByFileId(fileId);
- if (entity == null) {
- return new ResponseResult(50002, MsgCode.E50002);
- }
- String fileName = entity.getFileName();
- String cmd = CommandMsg.MODEL_USER_VTS;
- cmd = cmd.replaceAll("@fileName", fileName);
- Integer s = cmdServer.exeCmd(cmd);
- if (s ==1) {
- log.info("cmd run: {}", MsgCode.E50005);
- return new ResponseResult(50005, MsgCode.E50005);
- }
- log.info("cmd run : success");
- return new ResponseResult(200, "success");
- }
- @ApiOperation("command change user vts")
- @GetMapping("/command/change/user/vts")
- private ResponseResult commandChangeUserVts() {
- log.info("run commandChangeUserVts");
- Integer s = cmdServer.exeCmd(CommandMsg.MODEL_USER_VTS);
- if (s ==1) {
- log.info("cmd run: {}", MsgCode.E50005);
- return new ResponseResult(50005, MsgCode.E50005);
- }
- log.info("cmd run : success");
- return new ResponseResult(200, "success");
- }
- /**
- * 删除文件
- */
- @GetMapping("delete/{fileId}/")
- private ResponseResult deleteFile(@PathVariable("fileId") Long fileId) {
- log.info("run deleteFile: {}", fileId);
- return fileServer.deleteFileById(fileId);
- }
- // @GetMapping("test")
- // private ResponseResult test() {
- // log.info("run test: {}");
- //
- //
- // return new ResponseResult(200, "123");
- // }
- /**
- * 查询命令处理进度
- * @param fileId 上传文件的id
- * @param type 执行命令的类型
- * @return
- */
- @ApiOperation("query progress bar; type: vts / slpk")
- @GetMapping("progress/{fileId}/{type}/")
- private ResponseResult queryProgress(@PathVariable("fileId") Long fileId, @PathVariable("type") String type){
- log.info("run queryProgress");
- log.info("fileId: {} , type: {}", fileId, type);
- ScheduleEntity results = scheduleRepository.findByFileIdAndType(fileId, type);
- return new ResponseResult(200, results);
- }
- @ApiOperation("update 4dage")
- @PostMapping("update/fdage")
- private ResponseResult updateFdage(@RequestBody FdageDto param){
- log.info("run updateFdage");
- jsonServer.updateFdage(param);
- return new ResponseResult(200,"success");
- }
- }
|