BuildObjServiceImpl.java 15 KB

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