BuildObjServiceImpl.java 13 KB

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