BuildObjServiceImpl.java 16 KB

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