VtsModelController.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. package com.fd.controller;
  2. import com.fd.Dto.FdageDto;
  3. import com.fd.Dto.PageDto;
  4. import com.fd.constant.CommandMsg;
  5. import com.fd.constant.MsgCode;
  6. import com.fd.constant.TypeCode;
  7. import com.fd.entity.FileEntity;
  8. import com.fd.entity.GenerateFileEntity;
  9. import com.fd.entity.ScheduleEntity;
  10. import com.fd.repository.FileRepository;
  11. import com.fd.repository.GenerateFileRepository;
  12. import com.fd.repository.ScheduleRepository;
  13. import com.fd.util.ResponseResult;
  14. import com.fd.server.CmdServer;
  15. import com.fd.server.FileServer;
  16. import com.fd.server.JsonServer;
  17. import io.swagger.annotations.ApiOperation;
  18. import lombok.extern.log4j.Log4j2;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.beans.factory.annotation.Value;
  21. import org.springframework.data.domain.Page;
  22. import org.springframework.data.domain.PageRequest;
  23. import org.springframework.data.domain.Sort;
  24. import org.springframework.web.bind.annotation.*;
  25. import org.springframework.web.multipart.MultipartFile;
  26. import java.util.Date;
  27. import java.util.Optional;
  28. import java.util.concurrent.atomic.AtomicBoolean;
  29. /**
  30. * Created by Owen on 2019/11/7 0007 10:19
  31. * 3D Model
  32. *
  33. * 时间很长的命令才需要多前程运行,其他命令不使用
  34. */
  35. @Log4j2
  36. @RestController
  37. @RequestMapping("api/vts/model")
  38. public class VtsModelController {
  39. @Value("${uploadFile.fdmodelPath}")
  40. private String fdmodelPath;
  41. @Autowired
  42. private FileServer fileServer;
  43. @Autowired
  44. private FileRepository fileRepository;
  45. @Autowired
  46. private JsonServer jsonServer;
  47. @Autowired
  48. private CmdServer cmdServer;
  49. @Autowired
  50. private ScheduleRepository scheduleRepository;
  51. @Autowired
  52. private GenerateFileRepository generateFileRepository;
  53. // 多线程共享变量
  54. // false 表示完成
  55. private static AtomicBoolean existsSlpk2vts = new AtomicBoolean(false);
  56. private static AtomicBoolean existsVts = new AtomicBoolean(false);
  57. @ApiOperation("upload model file list")
  58. @PostMapping(value = "list")
  59. private ResponseResult list(@RequestBody PageDto param){
  60. log.info("run list");
  61. Page<FileEntity> page = fileRepository.findByType(
  62. TypeCode.FILE_TYPE_MODEL,
  63. TypeCode.FILE_TYPE_MODEL_TILE,
  64. PageRequest.of(param.getPageNum(), param.getPageSize(), Sort.by("createTime").descending()));
  65. return new ResponseResult(200, page);
  66. }
  67. /**
  68. * 文件上传 到3d model
  69. * 上传大文件 xxx.slpk
  70. * @return
  71. */
  72. @ApiOperation("upload 3D model file")
  73. @PostMapping(value = "upload", consumes = { "multipart/form-data" })
  74. private ResponseResult upload(@RequestParam("file") MultipartFile file){
  75. log.info("run upload");
  76. return fileServer.uploadBigFile(file, fdmodelPath);
  77. }
  78. /**
  79. * 可能运行1s -- 12h
  80. * 需要记录进度
  81. * 需要多前程运行
  82. *
  83. */
  84. @ApiOperation("commandSlpk2vts")
  85. @GetMapping("/command/slpk2vts/{fileId}/")
  86. private ResponseResult commandSlpk2vts(@PathVariable("fileId") Long fileId) {
  87. log.info("run commandSlpk2vts : {}", fileId);
  88. Optional<FileEntity> entity = fileRepository.findById(fileId);
  89. if (!entity.isPresent()) {
  90. return new ResponseResult(50002, MsgCode.E50002);
  91. }
  92. FileEntity fileEntity = entity.get();
  93. String fileName = fileEntity.getFileName();
  94. fileName = fileName.substring(0, fileName.lastIndexOf("."));
  95. String cmd = CommandMsg.MODEL_SLPK2VTS;
  96. cmd = cmd.replaceAll("@fileName", fileName);
  97. log.info("cmd: {}", cmd);
  98. if (existsSlpk2vts.compareAndSet(false, true)) {
  99. new Thread(new CmdSlpk2vtsThread(cmd, fileId, fileEntity)).start();
  100. }
  101. return new ResponseResult(200, "success");
  102. }
  103. public class CmdSlpk2vtsThread implements Runnable{
  104. private String cmd;
  105. private Long fileId;
  106. private FileEntity fileEntity;
  107. public CmdSlpk2vtsThread(String cmd, Long fileId, FileEntity fileEntity) {
  108. this.cmd = cmd;
  109. this.fileId = fileId;
  110. this.fileEntity = fileEntity;
  111. }
  112. @Override
  113. public void run() {
  114. log.info("run CmdSlpk2vtsThread");
  115. Integer s = cmdServer.exeCmdSlpk2vts(cmd, fileId);
  116. if (s == 1) {
  117. log.info("cmd run: {}", MsgCode.E50005);
  118. }
  119. log.info("cmd run : success");
  120. fileEntity.setStatus(1); // 1:表示文件执行完成
  121. fileEntity.setUpdateTime(new Date());
  122. fileRepository.save(fileEntity);
  123. // 保存新产生的文件到数据库
  124. String fileName = fileEntity.getFileName();
  125. fileName = fileName.substring(0, fileName.lastIndexOf("."));
  126. String fileUrl = cmd.substring(cmd.indexOf("--output") + 9, cmd.indexOf("--tilesetId") - 1);
  127. FileEntity entity = new FileEntity();
  128. entity.setFileName(fileName);
  129. entity.setFileUrl(fileUrl);
  130. entity.setCreateTime(new Date());
  131. entity.setUpdateTime(new Date());
  132. entity.setType(TypeCode.FILE_TYPE_MODEL_TILE);
  133. fileRepository.save(entity);
  134. // 表示执行完成,释放锁
  135. existsSlpk2vts.set(false);
  136. }
  137. }
  138. /**
  139. * 可能运行1s -- 12h
  140. * 需要记录进度
  141. * 需要多前程运行
  142. */
  143. @ApiOperation("commandVts")
  144. @GetMapping("/command/vts/{fileId}/")
  145. private ResponseResult commandVts(@PathVariable("fileId") Long fileId) {
  146. log.info("run commandVts");
  147. Optional<FileEntity> entity = fileRepository.findById(fileId);
  148. if (!entity.isPresent()) {
  149. return new ResponseResult(50002, MsgCode.E50002);
  150. }
  151. FileEntity fileEntity = entity.get();
  152. String fileName = fileEntity.getFileName();
  153. fileName = fileName.substring(0, fileName.lastIndexOf("."));
  154. String cmd = CommandMsg.MODEL_VTS;
  155. cmd = cmd.replaceAll("@fileName", fileName);
  156. log.info("cmd: {}", cmd);
  157. if (existsVts.compareAndSet(false, true)) {
  158. new Thread(new CmdVtsThread(cmd, fileId, fileEntity)).start();
  159. }
  160. return new ResponseResult(200, "success");
  161. }
  162. public class CmdVtsThread implements Runnable{
  163. private String cmd;
  164. private Long fileId;
  165. private FileEntity fileEntity;
  166. public CmdVtsThread(String cmd, Long fileId, FileEntity fileEntity) {
  167. this.cmd = cmd;
  168. this.fileId = fileId;
  169. this.fileEntity = fileEntity;
  170. }
  171. @Override
  172. public void run() {
  173. log.info("run CmdVtsThread");
  174. Integer s = cmdServer.exeCmdVts(cmd, fileId);
  175. if (s == 1) {
  176. log.info("cmd run: {}", MsgCode.E50005);
  177. }
  178. log.info("cmd run : success");
  179. fileEntity.setStatus(2); // 2:表示VTS命令执行完成
  180. fileEntity.setUpdateTime(new Date());
  181. fileRepository.save(fileEntity);
  182. // 表示执行完成,释放锁
  183. existsVts.set(false);
  184. }
  185. }
  186. @ApiOperation("command model vts remove")
  187. @GetMapping("/command/vts/remove/{fileId}")
  188. private ResponseResult commandVtsRemove(@PathVariable("fileId") Long fileId) {
  189. log.info("run commandVtsRemove");
  190. GenerateFileEntity entity = generateFileRepository.findByFileId(fileId);
  191. if (entity == null) {
  192. return new ResponseResult(50002, MsgCode.E50002);
  193. }
  194. String fileName = entity.getFileName();
  195. String cmd = CommandMsg.MODEL_USER_VTS;
  196. cmd = cmd.replaceAll("@fileName", fileName);
  197. Integer s = cmdServer.exeCmd(cmd);
  198. if (s ==1) {
  199. log.info("cmd run: {}", MsgCode.E50005);
  200. return new ResponseResult(50005, MsgCode.E50005);
  201. }
  202. log.info("cmd run : success");
  203. return new ResponseResult(200, "success");
  204. }
  205. @ApiOperation("command change user vts")
  206. @GetMapping("/command/change/user/vts")
  207. private ResponseResult commandChangeUserVts() {
  208. log.info("run commandChangeUserVts");
  209. Integer s = cmdServer.exeCmd(CommandMsg.MODEL_USER_VTS);
  210. if (s ==1) {
  211. log.info("cmd run: {}", MsgCode.E50005);
  212. return new ResponseResult(50005, MsgCode.E50005);
  213. }
  214. log.info("cmd run : success");
  215. return new ResponseResult(200, "success");
  216. }
  217. /**
  218. * 删除文件
  219. */
  220. @GetMapping("delete/{fileId}/")
  221. private ResponseResult deleteFile(@PathVariable("fileId") Long fileId) {
  222. log.info("run deleteFile: {}", fileId);
  223. return fileServer.deleteFileById(fileId);
  224. }
  225. // @GetMapping("test")
  226. // private ResponseResult test() {
  227. // log.info("run test: {}");
  228. //
  229. //
  230. // return new ResponseResult(200, "123");
  231. // }
  232. /**
  233. * 查询命令处理进度
  234. * @param fileId 上传文件的id
  235. * @param type 执行命令的类型
  236. * @return
  237. */
  238. @ApiOperation("query progress bar; type: vts / slpk")
  239. @GetMapping("progress/{fileId}/{type}/")
  240. private ResponseResult queryProgress(@PathVariable("fileId") Long fileId, @PathVariable("type") String type){
  241. log.info("run queryProgress");
  242. log.info("fileId: {} , type: {}", fileId, type);
  243. ScheduleEntity results = scheduleRepository.findByFileIdAndType(fileId, type);
  244. return new ResponseResult(200, results);
  245. }
  246. @ApiOperation("update 4dage")
  247. @PostMapping("update/fdage")
  248. private ResponseResult updateFdage(@RequestBody FdageDto param){
  249. log.info("run updateFdage");
  250. jsonServer.updateFdage(param);
  251. return new ResponseResult(200,"success");
  252. }
  253. }