123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- package com.fd.controller;
- import com.fd.constant.Command;
- import com.fd.constant.MsgCode;
- import com.fd.constant.TypeCode;
- import com.fd.dto.PageDto;
- import com.fd.dto.MyQueue;
- import com.fd.entity.FileEntity;
- import com.fd.entity.FileSchedule;
- import com.fd.entity.OutputFileEntity;
- import com.fd.server.CmdServer;
- import com.fd.server.FileServer;
- import com.fd.server.ModelServer;
- import com.fd.util.FileUtils;
- import com.fd.util.R;
- import io.swagger.annotations.ApiOperation;
- import lombok.extern.log4j.Log4j2;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.multipart.MultipartFile;
- import javax.annotation.PostConstruct;
- import java.io.File;
- import java.util.Date;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.LinkedBlockingQueue;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.atomic.AtomicInteger;
- /**
- * Created by Owen on 2019/11/12 0012 9:40
- * <p>
- * 3D模型数据
- */
- @Log4j2
- @RequestMapping("api/fdModel")
- @RestController
- public class FdModelController {
- @Value("${input.file.path}")
- private String INPUT_FILE_PATH;
- @Value("${output.file.path}")
- private String OUTPUT_FILE_PATH;
- @Autowired
- private FileServer fileServer;
- @Autowired
- private CmdServer cmdServer;
- @Autowired
- private ModelServer modelServer;
- // 队列
- BlockingQueue<String> queue = new LinkedBlockingQueue<String>(5);
- BlockingQueue<MyQueue> modelQueue = new LinkedBlockingQueue<MyQueue>(5);
- private static AtomicInteger count = new AtomicInteger();
- /**
- * 初始化队列
- *
- * @return
- */
- @PostConstruct
- private void init() {
- new Thread(new modelSliceConsumerThread(modelQueue)).start();
- }
- // @ApiOperation("测试生成队列")
- // @GetMapping("command/queProducer/{fileId}/")
- // private R queProducer(@PathVariable("fileId") Long fileId) {
- // log.info("run queProducer: {}", fileId);
- //
- // // 命令产生的是文件夹
- // FileEntity fileEntity = new FileEntity();
- // fileEntity.setFileName(fileId.toString());
- // fileEntity.setFileUrl("33");
- // fileEntity.setCreateTime(new Date());
- // fileEntity.setUpdateTime(new Date());
- // fileEntity.setStatus(5);
- //
- // fileEntity = fileServer.save(fileEntity);
- //
- // MyQueue que = new MyQueue();
- // que.setObj(fileEntity);
- // que.setStr("cmd1");
- //
- // // 将数据存入队列中
- // boolean offer = false;
- // try {
- // offer = oQueue.offer(que, 2, TimeUnit.SECONDS);
- // } catch (InterruptedException e) {
- // e.printStackTrace();
- // }
- // if (offer) {
- // System.out.println("生产者,存入" + que.toString() + "到队列中,成功.");
- // } else {
- // System.out.println("生产者,存入" + que.toString() + "到队列中,失败.");
- // }
- //
- // return new R(200, fileEntity);
- //
- // }
- /**
- * 消费队列
- */
- public class modelSliceConsumerThread implements Runnable{
- private BlockingQueue<MyQueue> queue;
- private boolean isRun = true;
- public modelSliceConsumerThread(BlockingQueue<MyQueue> queue){
- this.queue = queue;
- }
- @Override
- public void run() {
- log.warn("run modelSliceConsumerThread");
- while (isRun) {
- try {
- MyQueue data = queue.poll(2, TimeUnit.SECONDS);
- if (data != null) {
- log.info("消费者,拿到队列中的数据data:" + data.toString());
- Integer integer = cmdServer.exeCmdModelSlice(data.getStr());
- OutputFileEntity obj = data.getOutputFile();
- if (integer != 0) {
- log.info("error command exeCmdModelSlice");
- // 如果命令运行失败,删除刚才创建的实体类
- // o:代表切片失败
- obj.setStatus(0);
- modelServer.save(obj);
- return;
- }
- }
- // Thread.sleep(4000);
- } catch (InterruptedException e) {
- isRun = false;
- e.printStackTrace();
- }
- }
- }
- }
- // @ApiOperation("生成队列")
- // @PostMapping(value = "test")
- // private R test() {
- // log.info("run test");
- // // 将数据存入队列中
- // String data = count.incrementAndGet() + "";
- //
- // boolean offer = false;
- // try {
- // // 将数据存入队列中
- // offer = queue.offer(data, 2, TimeUnit.SECONDS);
- // } catch (InterruptedException e) {
- // e.printStackTrace();
- // }
- // if (offer) {
- // System.out.println("生产者,存入" + data + "到队列中,成功.");
- // } else {
- // System.out.println("生产者,存入" + data + "到队列中,失败.");
- // }
- // return new R(200, data);
- // }
- // @ApiOperation("消费队列")
- // @PostMapping(value = "test2")
- // private R test2() {
- // log.info("run test");
- //
- //
- // String data = null;
- // try {
- // data = queue.poll(2, TimeUnit.SECONDS);
- // } catch (InterruptedException e) {
- // e.printStackTrace();
- // }
- // if (data != null) {
- // System.out.println("消费者,拿到队列中的数据data:" + data);
- // }
- // return new R(200, data);
- // }
- public class ConsumerThread implements Runnable{
- @Override
- public void run() {
- log.warn("run ConsumerThread");
- while (true) {
- try {
- String data = queue.poll(2, TimeUnit.SECONDS);
- if (data != null) {
- log.info("消费者,拿到队列中的数据data:" + data);
- }
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- @ApiOperation("上传3D模型数据,只能上传zip文件")
- @PostMapping(value = "upload", consumes = {"multipart/form-data"})
- private R upload(@RequestParam("file") MultipartFile file) {
- log.info("run upload");
- // 文件名全名
- String fileName = file.getOriginalFilename();
- String s = StringUtils.substringAfterLast(fileName, ".");
- if (!"zip".equals(s)) {
- return new R(50007, MsgCode.E50007);
- }
- return modelServer.uploadBigFile(file);
- }
- @ApiOperation("解压zip文件")
- @GetMapping("unzip/{fileId}/")
- private R fileUnzip(@PathVariable("fileId") Long fileId) {
- log.info("run fileUnzip: {}", fileId);
- // FileEntity entity = fileServer.findById(fileId);
- OutputFileEntity entity = modelServer.findById(fileId);
- FileEntity uploadFiel = fileServer.findById(entity.getUploadId());
- String outputPath = OUTPUT_FILE_PATH + "unzip";
- FileUtils.createDir(outputPath);
- boolean unzip = FileUtils.unzip(uploadFiel.getFileUrl(), outputPath);
- if (!unzip) {
- log.info("zip error: {}", MsgCode.E51001);
- return new R(50001, MsgCode.E51001);
- }
- String fileName = StringUtils.substringBeforeLast(entity.getFileName(), ".");
- entity.setStatus(4);
- entity.setUpdateTime(new Date());
- entity.setUnZipPath(outputPath + File.separator + fileName);
- entity = modelServer.save(entity);
- return new R(200, entity);
- }
- @ApiOperation("获取3D模型数据列表")
- @PostMapping(value = "list")
- private R list(@RequestBody PageDto param) {
- log.info("run list");
- return modelServer.findByType(TypeCode.FILE_TYPE_MODEL, param);
- }
- /**
- * 删除文件
- */
- @ApiOperation("删除文件")
- @GetMapping("delete/{fileId}/")
- private R deleteFile(@PathVariable("fileId") Long fileId) {
- log.info("run deleteFile: {}", fileId);
- return fileServer.deleteById(fileId);
- }
- @ApiOperation("倾斜摄影数据切片")
- @GetMapping("command/osgb/{fileId}/")
- private R cmdModelSlice(@PathVariable("fileId") Long fileId) {
- log.info("run cmdModelSlice: {}", fileId);
- // FileEntity entity = fileServer.findById(fileId);
- OutputFileEntity entity = modelServer.findById(fileId);
- String fileName = StringUtils.substringBeforeLast(entity.getFileName(), ".");
- String outputPath = OUTPUT_FILE_PATH + "model";
- FileUtils.createDir(outputPath);
- outputPath = outputPath + File.separator + fileName;
- // 传入的是目录
- String cmd = Command.MODEL_SLICE_OSGB;
- cmd = cmd.replace("@inputFile", entity.getUnZipPath());
- cmd = cmd.replace("@outputFile", outputPath);
- log.info("cmd: {}", cmd);
- // 命令产生的是文件夹
- entity.setStatus(5);
- entity.setUpdateTime(new Date());
- entity.setSlicePath(outputPath);
- entity = modelServer.save(entity);
- // 把数据放入队列中
- MyQueue data = new MyQueue();
- data.setOutputFile(entity);
- data.setStr(cmd);
- try {
- modelQueue.offer(data, 1, TimeUnit.SECONDS);
- log.info("入队成功");
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return new R(200, entity);
- }
- // @ApiOperation("倾斜摄影数据切片")
- // @GetMapping("command/osgb/{fileId}/")
- // private R cmdModelSlice(@PathVariable("fileId") Long fileId) {
- // log.info("run cmdModelSlice: {}", fileId);
- //
- //
- //// // 将数据存入队列中
- //// String data = count.incrementAndGet() + "";
- //// boolean offer = false;
- //// try {
- //// // 将数据存入队列中
- //// offer = queue.offer(data, 2, TimeUnit.SECONDS);
- //// } catch (InterruptedException e) {
- //// e.printStackTrace();
- //// }
- //// if (offer) {
- //// System.out.println("生产者,存入" + data + "到队列中,成功.");
- //// } else {
- //// System.out.println("生产者,存入" + data + "到队列中,失败.");
- //// }
- //
- //
- // FileEntity entity = fileServer.findById(fileId);
- // // 传入的是目录
- // String cmd = Command.MODEL_SLICE_OSGB;
- // cmd = cmd.replace("@fileName", entity.getFileName());
- // log.info("cmd: {}", cmd);
- //
- // // 命令产生的是文件夹
- // FileEntity fileEntity = new FileEntity();
- // fileEntity.setFileName(entity.getFileName());
- // fileEntity.setFileUrl(OUTPUT_FILE_PATH + entity.getFileName());
- // fileEntity.setCreateTime(new Date());
- // fileEntity.setUpdateTime(new Date());
- // fileEntity.setStatus(5);
- //
- // fileEntity = fileServer.save(fileEntity);
- //
- // // 多线程运行切片
- // new Thread(new ModelSliceThread(cmd, fileEntity)).start();
- //
- //
- // return new R(200, fileEntity);
- //
- // }
- // public class ModelSliceThread implements Runnable {
- //
- // private volatile boolean flag = true;
- //
- // private String cmd;
- //
- // private FileEntity entity;
- //
- // private ModelSliceThread(String cmd, FileEntity entity) {
- // this.cmd = cmd;
- // this.entity = entity;
- //
- // }
- //
- // @Override
- // public void run() {
- // log.warn("run ModelSliceThread");
- //
- //// queue.poll(2, Ti)
- //
- // Integer integer = cmdServer.exeCmdModelSlice(cmd);
- // if (integer != 0) {
- // log.info("error command exeCmdModelSlice");
- // // 如果命令运行失败,删除刚才创建的实体类
- // fileServer.deleteById(entity.getId());
- // return;
- // }
- // log.warn("end RasterSliceThread");
- // }
- // }
- @ApiOperation("移动数据到服务器上")
- @GetMapping("move/{fileId}/")
- private R moveFile(@PathVariable("fileId") Long fileId) {
- log.info("run moveFile: {}", fileId);
- return modelServer.moveFileToServer(fileId);
- }
- }
|