FdModelController.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package com.fd.controller;
  2. import com.fd.constant.Command;
  3. import com.fd.constant.MsgCode;
  4. import com.fd.constant.TypeCode;
  5. import com.fd.dto.PageDto;
  6. import com.fd.dto.MyQueue;
  7. import com.fd.entity.FileEntity;
  8. import com.fd.entity.FileSchedule;
  9. import com.fd.entity.OutputFileEntity;
  10. import com.fd.server.CmdServer;
  11. import com.fd.server.FileServer;
  12. import com.fd.server.ModelServer;
  13. import com.fd.util.FileUtils;
  14. import com.fd.util.R;
  15. import com.fd.util.RegexUtils;
  16. import io.swagger.annotations.ApiOperation;
  17. import lombok.extern.log4j.Log4j2;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.beans.factory.annotation.Value;
  21. import org.springframework.web.bind.annotation.*;
  22. import org.springframework.web.multipart.MultipartFile;
  23. import javax.annotation.PostConstruct;
  24. import java.io.File;
  25. import java.util.Date;
  26. import java.util.concurrent.BlockingQueue;
  27. import java.util.concurrent.LinkedBlockingQueue;
  28. import java.util.concurrent.TimeUnit;
  29. import java.util.concurrent.atomic.AtomicInteger;
  30. /**
  31. * Created by Owen on 2019/11/12 0012 9:40
  32. * <p>
  33. * 3D模型数据
  34. */
  35. @Log4j2
  36. @RequestMapping("api/fdModel")
  37. @RestController
  38. public class FdModelController {
  39. @Value("${input.file.path}")
  40. private String INPUT_FILE_PATH;
  41. @Value("${output.file.path}")
  42. private String OUTPUT_FILE_PATH;
  43. @Autowired
  44. private FileServer fileServer;
  45. @Autowired
  46. private CmdServer cmdServer;
  47. @Autowired
  48. private ModelServer modelServer;
  49. // 队列
  50. BlockingQueue<String> queue = new LinkedBlockingQueue<String>(5);
  51. BlockingQueue<MyQueue> modelQueue = new LinkedBlockingQueue<MyQueue>(5);
  52. private static AtomicInteger count = new AtomicInteger();
  53. /**
  54. * 初始化队列
  55. *
  56. * @return
  57. */
  58. @PostConstruct
  59. private void init() {
  60. new Thread(new modelSliceConsumerThread(modelQueue)).start();
  61. }
  62. /**
  63. * 消费队列
  64. */
  65. public class modelSliceConsumerThread implements Runnable{
  66. private BlockingQueue<MyQueue> queue;
  67. private boolean isRun = true;
  68. public modelSliceConsumerThread(BlockingQueue<MyQueue> queue){
  69. this.queue = queue;
  70. }
  71. @Override
  72. public void run() {
  73. log.warn("run modelSliceConsumerThread");
  74. while (true) {
  75. try {
  76. MyQueue data = queue.poll(2, TimeUnit.SECONDS);
  77. if (data != null) {
  78. log.info("消费者,拿到队列中的数据data:" + data.toString());
  79. Integer integer = cmdServer.exeCmdModelSlice(data.getStr());
  80. OutputFileEntity obj = data.getOutputFile();
  81. if (integer != 0) {
  82. log.info("error command exeCmdModelSlice");
  83. // 如果命令运行失败,删除刚才创建的实体类
  84. // o:代表切片失败
  85. obj.setStatus(0);
  86. modelServer.save(obj);
  87. return;
  88. }
  89. }
  90. // Thread.sleep(4000);
  91. } catch (InterruptedException e) {
  92. // isRun = false;
  93. e.printStackTrace();
  94. }
  95. }
  96. }
  97. }
  98. @ApiOperation("上传3D模型数据,只能上传zip文件")
  99. @PostMapping(value = "upload", consumes = {"multipart/form-data"})
  100. private R upload(@RequestParam("file") MultipartFile file) {
  101. log.info("run upload");
  102. // 文件名全名
  103. String fileName = file.getOriginalFilename();
  104. // 文件是否包含中文字符
  105. if (RegexUtils.regexChinese(fileName)) {
  106. return new R(51005, MsgCode.E51005);
  107. }
  108. String s = StringUtils.substringAfterLast(fileName, ".");
  109. if (!"zip".equals(s)) {
  110. return new R(50007, MsgCode.E50007);
  111. }
  112. FileEntity entity = modelServer.findByFileName(fileName);
  113. if (entity != null) {
  114. return new R(51006, MsgCode.E51006);
  115. }
  116. return modelServer.uploadBigFile(file);
  117. }
  118. @ApiOperation("解压zip文件")
  119. @GetMapping("unzip/{fileId}/")
  120. private R fileUnzip(@PathVariable("fileId") Long fileId) {
  121. log.info("run fileUnzip: {}", fileId);
  122. // FileEntity entity = fileServer.findById(fileId);
  123. OutputFileEntity entity = modelServer.findById(fileId);
  124. FileEntity uploadFiel = fileServer.findById(entity.getUploadId());
  125. String outputPath = OUTPUT_FILE_PATH + "unzip";
  126. FileUtils.createDir(outputPath);
  127. boolean unzip = FileUtils.unzip(uploadFiel.getFileUrl(), outputPath);
  128. if (!unzip) {
  129. log.info("zip error: {}", MsgCode.E51001);
  130. return new R(50001, MsgCode.E51001);
  131. }
  132. String fileName = StringUtils.substringBeforeLast(entity.getFileName(), ".");
  133. entity.setStatus(4);
  134. entity.setUpdateTime(new Date());
  135. entity.setUnZipPath(outputPath + File.separator + fileName);
  136. entity = modelServer.save(entity);
  137. return new R(200, entity);
  138. }
  139. @ApiOperation("获取3D模型数据列表")
  140. @PostMapping(value = "list")
  141. private R list(@RequestBody PageDto param) {
  142. // log.info("run list");
  143. return modelServer.findByType(TypeCode.FILE_TYPE_MODEL, param);
  144. }
  145. /**
  146. * 删除文件
  147. */
  148. @ApiOperation("删除文件")
  149. @GetMapping("delete/{fileId}/")
  150. private R deleteFile(@PathVariable("fileId") Long fileId) {
  151. log.info("run deleteFile: {}", fileId);
  152. return modelServer.deleteById(fileId);
  153. }
  154. @ApiOperation("倾斜摄影数据切片")
  155. @GetMapping("command/osgb/{fileId}/")
  156. private R cmdModelSlice(@PathVariable("fileId") Long fileId) {
  157. log.info("run cmdModelSlice: {}", fileId);
  158. // FileEntity entity = fileServer.findById(fileId);
  159. OutputFileEntity entity = modelServer.findById(fileId);
  160. String fileName = StringUtils.substringBeforeLast(entity.getFileName(), ".");
  161. String outputPath = OUTPUT_FILE_PATH + "model";
  162. FileUtils.createDir(outputPath);
  163. outputPath = outputPath + File.separator + fileName;
  164. // 传入的是目录
  165. String cmd = Command.MODEL_SLICE_OSGB;
  166. cmd = cmd.replace("@inputFile", entity.getUnZipPath());
  167. cmd = cmd.replace("@outputFile", outputPath);
  168. log.info("cmd: {}", cmd);
  169. // 命令产生的是文件夹
  170. entity.setStatus(5);
  171. entity.setUpdateTime(new Date());
  172. entity.setSlicePath(outputPath);
  173. entity = modelServer.save(entity);
  174. // 把数据放入队列中
  175. MyQueue data = new MyQueue();
  176. data.setOutputFile(entity);
  177. data.setStr(cmd);
  178. try {
  179. modelQueue.offer(data, 1, TimeUnit.SECONDS);
  180. log.info("入队成功");
  181. } catch (InterruptedException e) {
  182. e.printStackTrace();
  183. }
  184. return new R(200, entity);
  185. }
  186. @ApiOperation("移动数据到服务器上")
  187. @GetMapping("move/{fileId}/")
  188. private R moveFile(@PathVariable("fileId") Long fileId) {
  189. log.info("run moveFile: {}", fileId);
  190. return modelServer.moveFileToServer(fileId);
  191. }
  192. }