FdModelController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 io.swagger.annotations.ApiOperation;
  16. import lombok.extern.log4j.Log4j2;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.beans.factory.annotation.Value;
  20. import org.springframework.web.bind.annotation.*;
  21. import org.springframework.web.multipart.MultipartFile;
  22. import javax.annotation.PostConstruct;
  23. import java.io.File;
  24. import java.util.Date;
  25. import java.util.concurrent.BlockingQueue;
  26. import java.util.concurrent.LinkedBlockingQueue;
  27. import java.util.concurrent.TimeUnit;
  28. import java.util.concurrent.atomic.AtomicInteger;
  29. /**
  30. * Created by Owen on 2019/11/12 0012 9:40
  31. * <p>
  32. * 3D模型数据
  33. */
  34. @Log4j2
  35. @RequestMapping("api/fdModel")
  36. @RestController
  37. public class FdModelController {
  38. @Value("${input.file.path}")
  39. private String INPUT_FILE_PATH;
  40. @Value("${output.file.path}")
  41. private String OUTPUT_FILE_PATH;
  42. @Autowired
  43. private FileServer fileServer;
  44. @Autowired
  45. private CmdServer cmdServer;
  46. @Autowired
  47. private ModelServer modelServer;
  48. // 队列
  49. BlockingQueue<String> queue = new LinkedBlockingQueue<String>(5);
  50. BlockingQueue<MyQueue> modelQueue = new LinkedBlockingQueue<MyQueue>(5);
  51. private static AtomicInteger count = new AtomicInteger();
  52. /**
  53. * 初始化队列
  54. *
  55. * @return
  56. */
  57. @PostConstruct
  58. private void init() {
  59. new Thread(new modelSliceConsumerThread(modelQueue)).start();
  60. }
  61. // @ApiOperation("测试生成队列")
  62. // @GetMapping("command/queProducer/{fileId}/")
  63. // private R queProducer(@PathVariable("fileId") Long fileId) {
  64. // log.info("run queProducer: {}", fileId);
  65. //
  66. // // 命令产生的是文件夹
  67. // FileEntity fileEntity = new FileEntity();
  68. // fileEntity.setFileName(fileId.toString());
  69. // fileEntity.setFileUrl("33");
  70. // fileEntity.setCreateTime(new Date());
  71. // fileEntity.setUpdateTime(new Date());
  72. // fileEntity.setStatus(5);
  73. //
  74. // fileEntity = fileServer.save(fileEntity);
  75. //
  76. // MyQueue que = new MyQueue();
  77. // que.setObj(fileEntity);
  78. // que.setStr("cmd1");
  79. //
  80. // // 将数据存入队列中
  81. // boolean offer = false;
  82. // try {
  83. // offer = oQueue.offer(que, 2, TimeUnit.SECONDS);
  84. // } catch (InterruptedException e) {
  85. // e.printStackTrace();
  86. // }
  87. // if (offer) {
  88. // System.out.println("生产者,存入" + que.toString() + "到队列中,成功.");
  89. // } else {
  90. // System.out.println("生产者,存入" + que.toString() + "到队列中,失败.");
  91. // }
  92. //
  93. // return new R(200, fileEntity);
  94. //
  95. // }
  96. /**
  97. * 消费队列
  98. */
  99. public class modelSliceConsumerThread implements Runnable{
  100. private BlockingQueue<MyQueue> queue;
  101. private boolean isRun = true;
  102. public modelSliceConsumerThread(BlockingQueue<MyQueue> queue){
  103. this.queue = queue;
  104. }
  105. @Override
  106. public void run() {
  107. log.warn("run modelSliceConsumerThread");
  108. while (isRun) {
  109. try {
  110. MyQueue data = queue.poll(2, TimeUnit.SECONDS);
  111. if (data != null) {
  112. log.info("消费者,拿到队列中的数据data:" + data.toString());
  113. Integer integer = cmdServer.exeCmdModelSlice(data.getStr());
  114. OutputFileEntity obj = data.getOutputFile();
  115. if (integer != 0) {
  116. log.info("error command exeCmdModelSlice");
  117. // 如果命令运行失败,删除刚才创建的实体类
  118. // o:代表切片失败
  119. obj.setStatus(0);
  120. modelServer.save(obj);
  121. return;
  122. }
  123. }
  124. // Thread.sleep(4000);
  125. } catch (InterruptedException e) {
  126. isRun = false;
  127. e.printStackTrace();
  128. }
  129. }
  130. }
  131. }
  132. // @ApiOperation("生成队列")
  133. // @PostMapping(value = "test")
  134. // private R test() {
  135. // log.info("run test");
  136. // // 将数据存入队列中
  137. // String data = count.incrementAndGet() + "";
  138. //
  139. // boolean offer = false;
  140. // try {
  141. // // 将数据存入队列中
  142. // offer = queue.offer(data, 2, TimeUnit.SECONDS);
  143. // } catch (InterruptedException e) {
  144. // e.printStackTrace();
  145. // }
  146. // if (offer) {
  147. // System.out.println("生产者,存入" + data + "到队列中,成功.");
  148. // } else {
  149. // System.out.println("生产者,存入" + data + "到队列中,失败.");
  150. // }
  151. // return new R(200, data);
  152. // }
  153. // @ApiOperation("消费队列")
  154. // @PostMapping(value = "test2")
  155. // private R test2() {
  156. // log.info("run test");
  157. //
  158. //
  159. // String data = null;
  160. // try {
  161. // data = queue.poll(2, TimeUnit.SECONDS);
  162. // } catch (InterruptedException e) {
  163. // e.printStackTrace();
  164. // }
  165. // if (data != null) {
  166. // System.out.println("消费者,拿到队列中的数据data:" + data);
  167. // }
  168. // return new R(200, data);
  169. // }
  170. public class ConsumerThread implements Runnable{
  171. @Override
  172. public void run() {
  173. log.warn("run ConsumerThread");
  174. while (true) {
  175. try {
  176. String data = queue.poll(2, TimeUnit.SECONDS);
  177. if (data != null) {
  178. log.info("消费者,拿到队列中的数据data:" + data);
  179. }
  180. Thread.sleep(2000);
  181. } catch (InterruptedException e) {
  182. e.printStackTrace();
  183. }
  184. }
  185. }
  186. }
  187. @ApiOperation("上传3D模型数据,只能上传zip文件")
  188. @PostMapping(value = "upload", consumes = {"multipart/form-data"})
  189. private R upload(@RequestParam("file") MultipartFile file) {
  190. log.info("run upload");
  191. // 文件名全名
  192. String fileName = file.getOriginalFilename();
  193. String s = StringUtils.substringAfterLast(fileName, ".");
  194. if (!"zip".equals(s)) {
  195. return new R(50007, MsgCode.E50007);
  196. }
  197. return modelServer.uploadBigFile(file);
  198. }
  199. @ApiOperation("解压zip文件")
  200. @GetMapping("unzip/{fileId}/")
  201. private R fileUnzip(@PathVariable("fileId") Long fileId) {
  202. log.info("run fileUnzip: {}", fileId);
  203. // FileEntity entity = fileServer.findById(fileId);
  204. OutputFileEntity entity = modelServer.findById(fileId);
  205. FileEntity uploadFiel = fileServer.findById(entity.getUploadId());
  206. String outputPath = OUTPUT_FILE_PATH + "unzip";
  207. FileUtils.createDir(outputPath);
  208. boolean unzip = FileUtils.unzip(uploadFiel.getFileUrl(), outputPath);
  209. if (!unzip) {
  210. log.info("zip error: {}", MsgCode.E51001);
  211. return new R(50001, MsgCode.E51001);
  212. }
  213. String fileName = StringUtils.substringBeforeLast(entity.getFileName(), ".");
  214. entity.setStatus(4);
  215. entity.setUpdateTime(new Date());
  216. entity.setUnZipPath(outputPath + File.separator + fileName);
  217. entity = modelServer.save(entity);
  218. return new R(200, entity);
  219. }
  220. @ApiOperation("获取3D模型数据列表")
  221. @PostMapping(value = "list")
  222. private R list(@RequestBody PageDto param) {
  223. log.info("run list");
  224. return modelServer.findByType(TypeCode.FILE_TYPE_MODEL, param);
  225. }
  226. /**
  227. * 删除文件
  228. */
  229. @ApiOperation("删除文件")
  230. @GetMapping("delete/{fileId}/")
  231. private R deleteFile(@PathVariable("fileId") Long fileId) {
  232. log.info("run deleteFile: {}", fileId);
  233. return fileServer.deleteById(fileId);
  234. }
  235. @ApiOperation("倾斜摄影数据切片")
  236. @GetMapping("command/osgb/{fileId}/")
  237. private R cmdModelSlice(@PathVariable("fileId") Long fileId) {
  238. log.info("run cmdModelSlice: {}", fileId);
  239. // FileEntity entity = fileServer.findById(fileId);
  240. OutputFileEntity entity = modelServer.findById(fileId);
  241. String fileName = StringUtils.substringBeforeLast(entity.getFileName(), ".");
  242. String outputPath = OUTPUT_FILE_PATH + "model";
  243. FileUtils.createDir(outputPath);
  244. outputPath = outputPath + File.separator + fileName;
  245. // 传入的是目录
  246. String cmd = Command.MODEL_SLICE_OSGB;
  247. cmd = cmd.replace("@inputFile", entity.getUnZipPath());
  248. cmd = cmd.replace("@outputFile", outputPath);
  249. log.info("cmd: {}", cmd);
  250. // 命令产生的是文件夹
  251. entity.setStatus(5);
  252. entity.setUpdateTime(new Date());
  253. entity.setSlicePath(outputPath);
  254. entity = modelServer.save(entity);
  255. // 把数据放入队列中
  256. MyQueue data = new MyQueue();
  257. data.setOutputFile(entity);
  258. data.setStr(cmd);
  259. try {
  260. modelQueue.offer(data, 1, TimeUnit.SECONDS);
  261. log.info("入队成功");
  262. } catch (InterruptedException e) {
  263. e.printStackTrace();
  264. }
  265. return new R(200, entity);
  266. }
  267. // @ApiOperation("倾斜摄影数据切片")
  268. // @GetMapping("command/osgb/{fileId}/")
  269. // private R cmdModelSlice(@PathVariable("fileId") Long fileId) {
  270. // log.info("run cmdModelSlice: {}", fileId);
  271. //
  272. //
  273. //// // 将数据存入队列中
  274. //// String data = count.incrementAndGet() + "";
  275. //// boolean offer = false;
  276. //// try {
  277. //// // 将数据存入队列中
  278. //// offer = queue.offer(data, 2, TimeUnit.SECONDS);
  279. //// } catch (InterruptedException e) {
  280. //// e.printStackTrace();
  281. //// }
  282. //// if (offer) {
  283. //// System.out.println("生产者,存入" + data + "到队列中,成功.");
  284. //// } else {
  285. //// System.out.println("生产者,存入" + data + "到队列中,失败.");
  286. //// }
  287. //
  288. //
  289. // FileEntity entity = fileServer.findById(fileId);
  290. // // 传入的是目录
  291. // String cmd = Command.MODEL_SLICE_OSGB;
  292. // cmd = cmd.replace("@fileName", entity.getFileName());
  293. // log.info("cmd: {}", cmd);
  294. //
  295. // // 命令产生的是文件夹
  296. // FileEntity fileEntity = new FileEntity();
  297. // fileEntity.setFileName(entity.getFileName());
  298. // fileEntity.setFileUrl(OUTPUT_FILE_PATH + entity.getFileName());
  299. // fileEntity.setCreateTime(new Date());
  300. // fileEntity.setUpdateTime(new Date());
  301. // fileEntity.setStatus(5);
  302. //
  303. // fileEntity = fileServer.save(fileEntity);
  304. //
  305. // // 多线程运行切片
  306. // new Thread(new ModelSliceThread(cmd, fileEntity)).start();
  307. //
  308. //
  309. // return new R(200, fileEntity);
  310. //
  311. // }
  312. // public class ModelSliceThread implements Runnable {
  313. //
  314. // private volatile boolean flag = true;
  315. //
  316. // private String cmd;
  317. //
  318. // private FileEntity entity;
  319. //
  320. // private ModelSliceThread(String cmd, FileEntity entity) {
  321. // this.cmd = cmd;
  322. // this.entity = entity;
  323. //
  324. // }
  325. //
  326. // @Override
  327. // public void run() {
  328. // log.warn("run ModelSliceThread");
  329. //
  330. //// queue.poll(2, Ti)
  331. //
  332. // Integer integer = cmdServer.exeCmdModelSlice(cmd);
  333. // if (integer != 0) {
  334. // log.info("error command exeCmdModelSlice");
  335. // // 如果命令运行失败,删除刚才创建的实体类
  336. // fileServer.deleteById(entity.getId());
  337. // return;
  338. // }
  339. // log.warn("end RasterSliceThread");
  340. // }
  341. // }
  342. @ApiOperation("移动数据到服务器上")
  343. @GetMapping("move/{fileId}/")
  344. private R moveFile(@PathVariable("fileId") Long fileId) {
  345. log.info("run moveFile: {}", fileId);
  346. return modelServer.moveFileToServer(fileId);
  347. }
  348. }