BuildObjServiceImpl.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. message.setBizType("obj");
  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. //先删除
  192. fYunFileService.deleteFolder(oss3dtilesPath);
  193. //上传3dtiles文件
  194. fYunFileService.uploadFileByCommand(threedtilesPath, oss3dtilesPath);
  195. }
  196. String texPath = laserObjFilePath + File.separator + "results" + File.separator + "tex";
  197. File texFile = new File(texPath);
  198. if(texFile.exists()){
  199. for (File textureFile : texFile.listFiles()) {
  200. if(textureFile.getName().endsWith(".jpg")){
  201. fYunFileService.uploadFile(textureFile.getAbsolutePath(),
  202. ossImagePath + ConstantFileName.modelUUID + "_50k_texture_jpg_high1/"+textureFile.getName());
  203. }
  204. }
  205. }
  206. fYunFileService.uploadFile(laserObjFilePath + File.separator + "results" + File.separator + "tex/texture1.jpg",
  207. ossImagePath + ConstantFileName.modelUUID + "_50k_texture_jpg_high1/texture1.jpg");
  208. // 拷贝结果
  209. log.info("开始拷贝obj文件");
  210. FileUtils.copyFolderAllFiles(laserObjFilePath + "/results/mesh", laserObjFilePath + "/laserData/mesh/", true);
  211. //先删除
  212. fYunFileService.deleteFolder(String.format(UploadFilePath.DATA_VIEW_PATH, projectNum) + "mesh");
  213. //上传mesh文件
  214. fYunFileService.uploadFileByCommand(laserObjFilePath + "/results/mesh", String.format(UploadFilePath.DATA_VIEW_PATH, projectNum) + "mesh");
  215. CreateObjUtil.convertTxtToVisionmodeldata(resultsPath + "vision.txt", resultsPath + "vision.modeldata");
  216. fYunFileService.uploadFile(resultsPath + "vision.txt", ossImagePath.concat("vision.txt"));
  217. fYunFileService.uploadFile(resultsPath + "vision.modeldata", ossImagePath.concat("vision.modeldata"));
  218. if(!ObjectUtils.isEmpty(scenePro)){
  219. LambdaUpdateWrapper<ScenePro> updateWrapper = new LambdaUpdateWrapper<ScenePro>()
  220. .set(ScenePro::getStatus, -2).eq(ScenePro::getNum, projectNum);
  221. sceneProService.update(updateWrapper);
  222. sceneProEditService.update(new LambdaUpdateWrapper<SceneProEdit>().set(SceneProEdit::getMapVisi,0)
  223. .eq(SceneProEdit::getProId,scenePro.getId()));
  224. }
  225. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(projectNum);
  226. if(Objects.nonNull(scenePlus)){
  227. // //清除用户编辑业务数据
  228. // Set<String> bizs = new HashSet<>();
  229. // bizs.add(UserEditDataType.BOX_MODEL.message());
  230. // bizs.add(UserEditDataType.FLOORPLAN.message());
  231. // commonService.initUserEditData(projectNum, bizs, null);
  232. LambdaUpdateWrapper<ScenePlus> plusUpdateWrapper = new LambdaUpdateWrapper<ScenePlus>()
  233. .set(ScenePlus::getSceneStatus, -2).eq(ScenePlus::getNum, projectNum);
  234. scenePlusService.update(plusUpdateWrapper);
  235. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  236. scenePlusExt.setAlgorithmTime(Calendar.getInstance().getTime());
  237. //计算容量
  238. Long space = commonService.getSpace(projectNum);
  239. scenePlusExt.setSpace(space);
  240. Object[] editInfoArr = commonService.updateEditInfo(scenePlus);
  241. SceneEditInfo sceneEditInfo = (SceneEditInfo)editInfoArr[0];
  242. SceneEditInfoExt sceneEditInfoExt = (SceneEditInfoExt)editInfoArr[1];
  243. SceneEditControls sceneEditControls = (SceneEditControls)editInfoArr[2];
  244. if(this.modelKind.equals(ModelKind.THREE_D_TILE.code())
  245. && CollUtil.isNotEmpty(sdTilesSceneSourceList)
  246. && sdTilesSceneSourceList.contains(scenePlus.getSceneSource())){
  247. scenePlusExt.setModelKind(ModelKind.THREE_D_TILE.code());
  248. scenePlusExtService.updateById(scenePlusExt);
  249. }
  250. log.info("生成scene.json上传oss并设置缓存,num:{}", projectNum);
  251. CameraDetail cameraDetail = cameraDetailService.getByCameraId(scenePlus.getCameraId());
  252. Company company = !org.apache.commons.lang3.ObjectUtils.isEmpty(cameraDetail.getCompanyId()) ? companyService.getById(cameraDetail.getCompanyId()) : null;
  253. //写scene.json
  254. commonService.writeSceneJson(projectNum,sceneEditInfo, sceneEditInfoExt, sceneEditControls, scenePlus,scenePlusExt,company);
  255. }
  256. //重置异步操作记录
  257. commonService.removeSceneAsynOperLog(projectNum);
  258. // 如果未升级V4,则升级V4
  259. if("v3".equals(version)){
  260. fdkkV4Service.upgradeToV4(projectNum);
  261. }
  262. success = true;
  263. //国际环境需要发邮件通知
  264. if("eur".equals(env)){
  265. commonService.sendEmail(projectNum, "obj");
  266. }
  267. }catch (Exception e){
  268. log.error("生成OBJ场景计算结果处理异常,num=" + projectNum, e);
  269. buildSceneDTService.handBaseFail("生成OBJ场景计算结果处理异常", message.getPath(), projectNum, "计算控制服务器");
  270. throw e;
  271. }finally {
  272. fdkkLaserService.pushBuildStatusToLaserSystem(projectNum, laserObjFilePath + "/laserData/mesh", success ? CommonOperStatus.SUCCESS.code() : CommonOperStatus.FAILD.code());
  273. }
  274. }
  275. }