BuildObjServiceImpl.java 15 KB

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