BuildObjServiceImpl.java 14 KB

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