JmgaServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package com.fdkankan.contro.service.impl;
  2. import cn.hutool.core.codec.Base64;
  3. import cn.hutool.core.collection.CollUtil;
  4. import cn.hutool.core.io.FileUtil;
  5. import cn.hutool.core.lang.UUID;
  6. import cn.hutool.core.util.ZipUtil;
  7. import cn.hutool.http.HttpUtil;
  8. import com.alibaba.fastjson.JSON;
  9. import com.alibaba.fastjson.JSONArray;
  10. import com.alibaba.fastjson.JSONObject;
  11. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  12. import com.fdkankan.common.constant.ErrorCode;
  13. import com.fdkankan.common.exception.BusinessException;
  14. import com.fdkankan.common.util.CmdUtils;
  15. import com.fdkankan.contro.bean.SendCallAlgorithmDetail;
  16. import com.fdkankan.contro.constant.ZipConstant;
  17. import com.fdkankan.contro.entity.OrigFileUpload;
  18. import com.fdkankan.contro.entity.OrigFileUploadBatch;
  19. import com.fdkankan.contro.entity.User;
  20. import com.fdkankan.contro.service.*;
  21. import com.fdkankan.contro.vo.SendCallAlgorithmParam;
  22. import com.fdkankan.contro.vo.UploadSceneOrigParamVo;
  23. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  24. import com.fdkankan.model.constants.ConstantFilePath;
  25. import com.fdkankan.rabbitmq.util.RabbitMqProducer;
  26. import com.fdkankan.web.util.RSAEncrypt;
  27. import lombok.extern.slf4j.Slf4j;
  28. import net.lingala.zip4j.core.ZipFile;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.stereotype.Service;
  31. import javax.annotation.Resource;
  32. import java.io.File;
  33. import java.nio.charset.StandardCharsets;
  34. import java.util.*;
  35. import java.util.stream.Collectors;
  36. @Slf4j
  37. @Service
  38. public class JmgaServiceImpl implements IJmgaService {
  39. @Autowired
  40. private IOrigFileUploadBatchService origFileUploadBatchService;
  41. @Autowired
  42. private IOrigFileUploadService origFileUploadService;
  43. @Resource
  44. private FYunFileServiceInterface fileServiceInterface;
  45. @Resource
  46. private RabbitMqProducer mqProducer;
  47. @Resource
  48. private FYunFileServiceInterface fYunFileService;
  49. @Autowired
  50. private ISceneFileBuildService sceneFileBuildService;
  51. @Autowired
  52. private IUserService userService;
  53. @Override
  54. public void checkFileWhole(String uuid, String dataSource, JSONObject fdageData) {
  55. int camType = fdageData.getJSONObject("cam").getIntValue("type");
  56. //从data.fdage中获取需要上传的文件列表
  57. JSONArray points = fdageData.getJSONArray("points");
  58. if(CollUtil.isEmpty(points)){
  59. return;
  60. }
  61. List<String> fileList = new ArrayList<>();
  62. for (Object point : points) {
  63. JSONObject pointJson = (JSONObject) point;
  64. if(camType == 5){//圆周率相机
  65. String name = pointJson.getString("name");
  66. fileList.add(name);
  67. }else{//四维相机
  68. JSONArray imgs = pointJson.getJSONArray("imgs");
  69. if(CollUtil.isEmpty(imgs)){
  70. continue;
  71. }
  72. for (Object img : imgs) {
  73. JSONObject imgJson = (JSONObject) img;
  74. String name = imgJson.getString("name");
  75. fileList.add(name);
  76. }
  77. }
  78. }
  79. //查询当前待通知计算批次文件是否存在
  80. List<OrigFileUploadBatch> batchList = origFileUploadBatchService.list(new LambdaQueryWrapper<OrigFileUploadBatch>().eq(OrigFileUploadBatch::getUuid, uuid));
  81. if(CollUtil.isNotEmpty(batchList)){
  82. Set<String> batchIds = batchList.stream().map(v -> v.getBatchId()).collect(Collectors.toSet());
  83. List<OrigFileUpload> origFileList = origFileUploadService.list(new LambdaQueryWrapper<OrigFileUpload>().in(OrigFileUpload::getBatchId, batchIds));
  84. //过滤出不存在的
  85. fileList = fileList.stream().filter(v -> origFileList.stream().noneMatch(k -> k.getFileName().equals(v))).collect(Collectors.toList());
  86. }
  87. // //查询oss文件目录是否存在
  88. // String homePath = SceneUtil.getHomePath(dataSource);
  89. // List<String> ossKeyList = fileServiceInterface.listRemoteFiles(homePath);
  90. // fileList = fileList.stream().filter(v->ossKeyList.stream().noneMatch(k->FileUtil.getName(k).equals(v))).collect(Collectors.toList());
  91. //
  92. if(CollUtil.isNotEmpty(fileList)){
  93. String args = JSON.toJSONString(fileList);
  94. if(fileList.size() > 5){
  95. args = fileList.size() + "个文件";
  96. }
  97. throw new BusinessException(ErrorCode.FAILURE_CODE_4001, args);
  98. }
  99. }
  100. @Override
  101. public void sendStatus(Map<String, Object> param) {
  102. mqProducer.sendByWorkQueue("jmga-event-notice", param);
  103. }
  104. @Override
  105. public void checkLackFile(String dataSource) {
  106. String dataFdagePath = dataSource.concat(File.separator).concat("capture").concat(File.separator).concat("data.fdage");
  107. if(!FileUtil.exist(dataFdagePath)){
  108. throw new BusinessException(ErrorCode.FAILURE_CODE_4001.code(), ErrorCode.FAILURE_CODE_4001.formatMessage("data.fdage"));
  109. }
  110. JSONObject fdageData = null;
  111. try {
  112. fdageData = JSON.parseObject(FileUtil.readUtf8String(dataFdagePath));
  113. }catch (Exception e){
  114. throw new BusinessException(ErrorCode.FAILURE_CODE_4001.code(), ErrorCode.FAILURE_CODE_4001.formatMessage("data.fdage"));
  115. }
  116. int camType = fdageData.getJSONObject("cam").getIntValue("type");
  117. if(camType == 1 || camType == 2 || camType == 9 || camType == 10 || camType == 11){
  118. Integer shootCount = fdageData.getJSONArray("points").size();
  119. if(shootCount > 0){//有点位代表架站式
  120. String parametersPath = dataSource.concat(File.separator).concat("capture").concat(File.separator).concat("parameters.json");
  121. if(!FileUtil.exist(parametersPath)){
  122. throw new BusinessException(ErrorCode.FAILURE_CODE_4001.code(), ErrorCode.FAILURE_CODE_4001.formatMessage("parameters.json"));
  123. }
  124. String sfmDataBinPath = dataSource.concat(File.separator).concat("capture").concat(File.separator).concat("sfm_data.bin");
  125. if(!FileUtil.exist(sfmDataBinPath)){
  126. throw new BusinessException(ErrorCode.FAILURE_CODE_4001.code(), ErrorCode.FAILURE_CODE_4001.formatMessage("sfm_data.bin"));
  127. }
  128. String upName = camType > 2 ? "Up.txt" : "Up.xml";
  129. String upTxtPath = dataSource.concat(File.separator).concat("capture").concat(File.separator).concat(upName);
  130. if(!FileUtil.exist(upTxtPath)){
  131. throw new BusinessException(ErrorCode.FAILURE_CODE_4001.code(), ErrorCode.FAILURE_CODE_4001.formatMessage(upName));
  132. }
  133. }
  134. }
  135. if(camType == 5){
  136. String parametersPath = dataSource.concat(File.separator).concat("capture").concat(File.separator).concat("images/parameters.json");
  137. if(!FileUtil.exist(parametersPath)){
  138. throw new BusinessException(ErrorCode.FAILURE_CODE_4001.code(), ErrorCode.FAILURE_CODE_4001.formatMessage("parameters.json"));
  139. }
  140. String sfmDataBinPath = dataSource.concat(File.separator).concat("capture").concat(File.separator).concat("result/reconstruction/sfm_data.bin");
  141. if(!FileUtil.exist(sfmDataBinPath)){
  142. throw new BusinessException(ErrorCode.FAILURE_CODE_4001.code(), ErrorCode.FAILURE_CODE_4001.formatMessage("sfm_data.bin"));
  143. }
  144. }
  145. //从data.fdage中获取需要上传的文件列表
  146. JSONArray points = fdageData.getJSONArray("points");
  147. if(CollUtil.isEmpty(points)){
  148. return;
  149. }
  150. for (Object point : points) {
  151. JSONObject pointJson = (JSONObject) point;
  152. if(camType == 5){//圆周率相机
  153. String name = pointJson.getString("name");
  154. this.checkFileAvailable(dataSource, name);
  155. }else{//四维相机
  156. JSONArray imgs = pointJson.getJSONArray("imgs");
  157. for (Object img : imgs) {
  158. JSONObject imgJson = (JSONObject) img;
  159. String name = imgJson.getString("name");
  160. this.checkFileAvailable(dataSource, name);
  161. }
  162. }
  163. }
  164. }
  165. private void checkFileAvailable(String dataSource, String fileName){
  166. List<File> fileList = FileUtil.loopFiles(dataSource.concat(File.separator).concat("capture"));
  167. if(CollUtil.isEmpty(fileList)){
  168. throw new BusinessException(ErrorCode.FAILURE_CODE_4001.code(), ErrorCode.FAILURE_CODE_4001.formatMessage(fileName));
  169. }
  170. for (File file : fileList) {
  171. if(file.getName().equals(fileName) && file.length() > 0){
  172. return;
  173. }
  174. }
  175. throw new BusinessException(ErrorCode.FAILURE_CODE_4001.code(), ErrorCode.FAILURE_CODE_4001.formatMessage(fileName));
  176. }
  177. @Override
  178. public void uploadSceneOrig(UploadSceneOrigParamVo param) throws Exception {
  179. User user = userService.getById(param.getUserId());
  180. if(Objects.isNull(user)){
  181. throw new BusinessException(ErrorCode.USER_NOT_EXIST);
  182. }
  183. String uuid = UUID.randomUUID().toString();
  184. String zipName = uuid + ".zip";
  185. String zipDir = "/oss/4dkankan/bd_server/";
  186. fYunFileService.downloadFileByCommand(zipDir + zipName, param.getFilePath());
  187. // fYunFileService.deleteFile(param.getFilePath());
  188. //解压缩
  189. // String unzipCmd = "unzip -P " + ZipConstant.zipPassword + " " + zipDir + zipName + " -d " + zipDir + uuid;
  190. // CmdUtils.callLineSh(unzipCmd);
  191. ZipFile zipFile = new ZipFile(new File(zipDir + zipName));
  192. if (zipFile.isEncrypted()) {
  193. zipFile.setPassword(ZipConstant.zipPassword);
  194. zipFile.extractAll(zipDir + uuid);
  195. } else {
  196. ZipUtil.unzip(zipDir + zipName, zipDir + uuid);
  197. }
  198. // FileUtil.del(zipDir + zipName);
  199. //去读data.fdage
  200. File dataFdageFile = FileUtil.loopFiles(zipDir + uuid).stream().filter(v -> v.getName().equals("data.fdage")).findFirst().get();
  201. if(dataFdageFile == null){
  202. throw new BusinessException(ErrorCode.FAILURE_CODE_4002.code(), ErrorCode.FAILURE_CODE_4002.formatMessage("data.fdage"));
  203. }
  204. JSONObject dataFdageObj = JSONObject.parseObject(FileUtil.readUtf8String(dataFdageFile));
  205. int camType = dataFdageObj.getJSONObject("cam").getIntValue("type");
  206. String snCode = dataFdageObj.getJSONObject("cam").getString("uuid");
  207. String uuidTime = dataFdageObj.getString("uuidtime");
  208. String uniCode = snCode + "_" + uuidTime;
  209. String fileId = sceneFileBuildService.getFileId(snCode, uniCode);
  210. String homePath = "/oss/4dkankan/" + ConstantFilePath.OSS_PREFIX + snCode + "/" + fileId + "/" + uniCode;
  211. FileUtil.mkdir(homePath);
  212. String cpCmd = "cp -p -r " + zipDir + uuid + "/*/* " + homePath;
  213. CmdUtils.callLineSh(cpCmd);
  214. // FileUtil.del(zipDir + uuid);
  215. String params = snCode + "#" + fileId + "#" + uniCode;
  216. String encode = Base64.encode(RSAEncrypt.encrypt(RSAEncrypt.loadPublicKeyByStr(RSAEncrypt.loadPublicKeyByFile()), params.getBytes(StandardCharsets.UTF_8)));
  217. if(camType == 9 || camType == 10 || camType == 11){
  218. sceneFileBuildService.turntableUploadSuccess(encode, user);
  219. }else{
  220. sceneFileBuildService.uploadSuccessBuild(encode, user);
  221. }
  222. }
  223. @Override
  224. public void noticeBuildBd(String ossHost, String uuid, String dir) {
  225. String prefix = "http://" + ossHost + "/";
  226. String path = "/oss/4dkankan/bd_server/" + dir;
  227. List<File> files = FileUtil.loopFiles(path).stream().filter(file -> !file.getAbsolutePath().contains("backup")).collect(Collectors.toList());
  228. files.parallelStream().forEach(file ->{
  229. String url = prefix.concat(file.getAbsolutePath().substring(1).replace("/4dkankan",""));
  230. SendCallAlgorithmDetail detail = new SendCallAlgorithmDetail();
  231. detail.setFileName(FileUtil.getName(file.getAbsolutePath()));
  232. detail.setUuid(uuid);
  233. SendCallAlgorithmParam param = new SendCallAlgorithmParam();
  234. param.setFilepath(url);
  235. param.setDetails(detail);
  236. HttpUtil.post("http://127.0.0.1:8085/api/scene/file/sendCallAlgorithm", JSON.toJSONString(param));
  237. });
  238. }
  239. }