BuildObjServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package com.fdkankan.contro.mq.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  5. import com.fdkankan.common.constant.CommonStatus;
  6. import com.fdkankan.common.constant.ModelKind;
  7. import com.fdkankan.common.util.FileUtils;
  8. import com.fdkankan.contro.entity.ScenePlus;
  9. import com.fdkankan.contro.entity.ScenePro;
  10. import com.fdkankan.contro.entity.SceneProEdit;
  11. import com.fdkankan.contro.mq.service.IBuildSceneService;
  12. import com.fdkankan.contro.service.*;
  13. import com.fdkankan.contro.service.impl.FdkkV4Service;
  14. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  15. import com.fdkankan.model.constants.ConstantFileName;
  16. import com.fdkankan.model.constants.ConstantFilePath;
  17. import com.fdkankan.model.constants.UploadFilePath;
  18. import com.fdkankan.model.utils.CreateObjUtil;
  19. import com.fdkankan.rabbitmq.bean.BuildSceneCallMessage;
  20. import com.fdkankan.rabbitmq.bean.BuildSceneResultMqMessage;
  21. import com.fdkankan.rabbitmq.util.RabbitMqProducer;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.beans.factory.annotation.Value;
  25. import org.springframework.stereotype.Service;
  26. import org.springframework.util.ObjectUtils;
  27. import java.io.File;
  28. import java.util.List;
  29. import java.util.Objects;
  30. /**
  31. * <p>
  32. * TODO
  33. * </p>
  34. *
  35. * @author dengsixing
  36. * @since 2022/3/1
  37. **/
  38. @Slf4j
  39. @Service
  40. public class BuildObjServiceImpl implements IBuildSceneService {
  41. @Value("${queue.modeling.modeling-call}")
  42. private String queueModelingCall;
  43. @Value("${queue.modeling.obj.modeling-post}")
  44. private String queueObjModelingPost;
  45. @Value("${model.modelKind:3dtiles}")
  46. private String modelKind;
  47. @Autowired
  48. private RabbitMqProducer mqProducer;
  49. @Autowired
  50. private FYunFileServiceInterface fYunFileService;
  51. @Autowired
  52. private ISceneProService sceneProService;
  53. @Autowired
  54. private IFdkkLaserService fdkkLaserService;
  55. @Autowired
  56. private FdkkV4Service fdkkV4Service;
  57. @Autowired
  58. private IBuildSceneDTService buildSceneDTService;
  59. @Autowired
  60. private ISceneProEditService sceneProEditService;
  61. @Autowired
  62. private IScenePlusService scenePlusService;
  63. @Override
  64. public void buildScenePre(BuildSceneCallMessage message) {
  65. boolean success = false;
  66. try {
  67. String path = message.getPath();
  68. //根据相机类型,组装资源路径
  69. String laserObjFilePath = path + "_laser_obj";
  70. log.info("开始准备生成OBJ场景资源,路径:{}", laserObjFilePath);
  71. FileUtils.delAllFile(laserObjFilePath);
  72. //获取解压后的资源的data.fdage中的数据
  73. File folderPath = new File(path);
  74. fYunFileService.downloadFile(ConstantFilePath.OSS_PREFIX + path.replace(ConstantFilePath.BUILD_MODEL_LASER_PATH, "") + "/data.fdage", path + "/capture/data.fdage");
  75. JSONObject jsonObject = FileUtils.readJson(folderPath.getAbsolutePath() + File.separator + "capture" + File.separator + "data.fdage");
  76. if(ObjectUtils.isEmpty(jsonObject)){
  77. log.error("data.fdage文件不存在");
  78. return ;
  79. }
  80. if (!jsonObject.containsKey("exportMeshObj") || jsonObject.getIntValue("exportMeshObj") != 1) {
  81. // 写入data.fdage 防止重算
  82. jsonObject.put("exportMeshObj", 1);
  83. FileUtils.writeFile(folderPath.getAbsolutePath() + File.separator + "capture" + File.separator + "data.fdage", jsonObject.toJSONString());
  84. fYunFileService.uploadFile(folderPath.getAbsolutePath() + File.separator + "capture" + File.separator + "data.fdage",
  85. ConstantFilePath.OSS_PREFIX + path.replace(ConstantFilePath.BUILD_MODEL_LASER_PATH, "") + "/data.fdage");
  86. }
  87. // 重新构造datafdage文件,添加生成obt文件选项
  88. jsonObject.put("OnlyExportMeshObj",true);
  89. FileUtils.writeFile(laserObjFilePath + File.separator + "capture" + File.separator + "data.fdage", jsonObject.toJSONString());
  90. message.getBuildContext().put("previousPath",path);
  91. message.setCameraType("14");
  92. message.setResolution(jsonObject.getString("resolution"));
  93. message.setPath(laserObjFilePath);
  94. message.setResultReceiverMqName(queueObjModelingPost);
  95. //下载资源到本地
  96. this.downLoadSource(message, laserObjFilePath);
  97. //发送mq,就进行计算
  98. mqProducer.sendByWorkQueue(queueModelingCall, message);
  99. success = true;
  100. log.info("场景计算资源准备结束,场景码:{}", message.getSceneNum());
  101. }catch (Exception e){
  102. log.error("场景计算前置处理出错", e);
  103. }finally {
  104. //如果前置处理失败,发送钉钉消息
  105. if(!success){
  106. buildSceneDTService.handBaseFail("生成OBJ场景资源准备异常", message.getPath(), message.getSceneNum(), "计算控制服务器");
  107. }
  108. }
  109. }
  110. @Override
  111. public void downLoadSource(BuildSceneCallMessage message,String path) throws Exception{
  112. String prevoisPath = message.getBuildContext().get("previousPath").toString();
  113. FileUtils.copyFile(prevoisPath + "/caches/reconstruction/final.bin", path + "/caches/reconstruction/final.bin", true);
  114. FileUtils.copyFile(prevoisPath + "/caches/reconstruction/chunk.json", path + "/caches/reconstruction/chunk.json", true);
  115. FileUtils.copyFile(prevoisPath + "/caches/floor_group_fix.json", path + "/caches/floor_group_fix.json", true);
  116. FileUtils.copyDirectiory(prevoisPath + "/caches/images", path + "/caches/images");
  117. FileUtils.copyDirectiory(prevoisPath + "/caches/depthmap_csc", path + "/caches/depthmap_csc");
  118. FileUtils.copyDirectiory(prevoisPath + "/caches/depthmap_vis", path + "/caches/depthmap_vis");
  119. FileUtils.copyDirectiory(prevoisPath + "/caches/depthmap", path + "/caches/depthmap");
  120. FileUtils.copyFile(prevoisPath + "/caches/panorama.json", path + "/caches/panorama.json", true);
  121. FileUtils.copyFile(prevoisPath + "/results/laserData/laser.ply", path + "/results/laserData/laser.ply", true);
  122. }
  123. @Override
  124. public void buildScenePost(BuildSceneResultMqMessage message) throws Exception {
  125. if (!message.getBuildSuccess()) {
  126. log.error("生成OBJ场景计算失败!");
  127. // 发送钉钉消息,计算失败
  128. buildSceneDTService.handModelFail("生成OBJ场景计算失败!", message.getPath(), message.getBuildContext().get("sceneNum").toString(), message.getHostName());
  129. return;
  130. }
  131. String path = message.getPath();
  132. String laserObjFilePath = path;
  133. String projectNum = message.getBuildContext().get("sceneNum").toString();
  134. ScenePro scenePro = sceneProService.getByNum(projectNum);
  135. String version = "v4";
  136. String ossImagePath = String.format(UploadFilePath.IMG_VIEW_PATH, projectNum);
  137. //如果是微服务上线之前的存量场景且未生成过obj(未升级过),需要上传到v3的目录,在函数最后调用升级接口进行文件拷贝
  138. if(Objects.nonNull(scenePro) && scenePro.getIsUpgrade() != CommonStatus.YES.code().intValue()){
  139. ossImagePath = String.format(ConstantFilePath.IMAGE_PATH_FORMAT, projectNum);
  140. version = "v3";
  141. }
  142. //上传obj
  143. String resultsPath = path + File.separator + "results" + File.separator;
  144. if(this.modelKind.equals(ModelKind.DAM.code())){
  145. CreateObjUtil.convertTxtToDam(path + File.separator + "results" + File.separator + "tex" + File.separator + "modeldata.txt", path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam");
  146. CreateObjUtil.convertDamToLzma(path + File.separator + "results/");
  147. CreateObjUtil.convertTxtToDam(path + File.separator + "results" + File.separator + "tex" + File.separator + "modeldata.txt", path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam");
  148. File file = new File(path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam.lzma");
  149. while (!file.exists()) {
  150. Thread.sleep(60000);
  151. }
  152. fYunFileService.uploadFile(path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam.lzma", ossImagePath + ConstantFileName.modelUUID + "_50k.dam.lzma");
  153. fYunFileService.uploadFile(path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam", ossImagePath + ConstantFileName.modelUUID + "_50k.dam");
  154. }
  155. if(this.modelKind.equals(ModelKind.THREE_D_TILE.code())){
  156. String threedtilesPath = resultsPath + ModelKind.THREE_D_TILE.code();
  157. String oss3dtilesPath = ossImagePath + ModelKind.THREE_D_TILE.code();
  158. List<String> list = FileUtils.list(new File(threedtilesPath));
  159. if(CollUtil.isEmpty(list)){
  160. log.error("3dtiles目录异常,3dtiles地址:{}", new File(path + File.separator + "results" + File.separator + ModelKind.THREE_D_TILE.code()));
  161. throw new Exception("3dtiles目录异常");
  162. }
  163. //上传3dtiles文件
  164. fYunFileService.uploadFileByCommand(threedtilesPath, oss3dtilesPath);
  165. }
  166. String texPath = laserObjFilePath + File.separator + "results" + File.separator + "tex";
  167. File texFile = new File(texPath);
  168. if(texFile.exists()){
  169. for (File textureFile : texFile.listFiles()) {
  170. if(textureFile.getName().endsWith(".jpg")){
  171. fYunFileService.uploadFile(textureFile.getAbsolutePath(),
  172. ossImagePath + ConstantFileName.modelUUID + "_50k_texture_jpg_high1/"+textureFile.getName());
  173. }
  174. }
  175. }
  176. fYunFileService.uploadFile(laserObjFilePath + File.separator + "results" + File.separator + "tex/texture1.jpg",
  177. ossImagePath + ConstantFileName.modelUUID + "_50k_texture_jpg_high1/texture1.jpg");
  178. // 拷贝结果
  179. log.info("开始拷贝obj文件");
  180. FileUtils.copyFolderAllFiles(laserObjFilePath + "/results/mesh", laserObjFilePath + "/laserData/mesh/", true);
  181. //上传mesh文件
  182. fYunFileService.uploadFileByCommand(laserObjFilePath + "/results/mesh", String.format(UploadFilePath.DATA_VIEW_PATH, projectNum) + "mesh");
  183. fdkkLaserService.pushBuildStatusToLaserSystem(projectNum, laserObjFilePath + "/laserData/mesh");
  184. if(!ObjectUtils.isEmpty(scenePro)){
  185. LambdaUpdateWrapper<ScenePro> updateWrapper = new LambdaUpdateWrapper<ScenePro>()
  186. .set(ScenePro::getStatus, -2).eq(ScenePro::getNum, projectNum);
  187. sceneProService.update(updateWrapper);
  188. sceneProEditService.update(new LambdaUpdateWrapper<SceneProEdit>().set(SceneProEdit::getMapVisi,0)
  189. .eq(SceneProEdit::getProId,scenePro.getId()));
  190. }
  191. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(projectNum);
  192. if(!ObjectUtils.isEmpty(scenePlus)){
  193. LambdaUpdateWrapper<ScenePlus> plusUpdateWrapper = new LambdaUpdateWrapper<ScenePlus>()
  194. .set(ScenePlus::getSceneStatus, -2).eq(ScenePlus::getNum, projectNum);
  195. scenePlusService.update(plusUpdateWrapper);
  196. }
  197. // 如果未升级V4,则升级V4
  198. if("v3".equals(version)){
  199. fdkkV4Service.upgradeToV4(projectNum);
  200. }
  201. }
  202. }