BuildObjServiceImpl.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. //发送mq,就进行计算
  123. mqProducer.sendByWorkQueue(queueModelingCall, message);
  124. success = true;
  125. log.info("场景计算资源准备结束,场景码:{}", message.getSceneNum());
  126. }catch (Exception e){
  127. log.error("生成OBJ场景资源准备异常,num=" + num, e);
  128. buildSceneDTService.handBaseFail("生成OBJ场景资源准备异常", message.getPath(), message.getSceneNum(), "计算控制服务器");
  129. throw e;
  130. }finally {
  131. fdkkLaserService.pushBuildStatusToLaserSystem(num, laserObjFilePath + "/laserData/mesh", success ? CommonOperStatus.SUCCESS.code() : CommonOperStatus.FAILD.code());
  132. }
  133. }
  134. @Override
  135. public void downLoadSource(BuildSceneCallMessage message,String path) throws Exception{
  136. //下载原始资源文件
  137. String ossPath = commonService.getOssOrignPath(message.getPath());
  138. fYunFileService.downloadFileByCommand(path + File.separator + "capture", ossPath);
  139. //下载caches/image,算法会执行快一些
  140. String ossResultPath = String.format(UploadFilePath.scene_result_data_path, message.getSceneNum());
  141. fYunFileService.downloadFileByCommand(path + "/caches/images/", ossResultPath + "caches/images/");
  142. //下载点位校准文件
  143. fYunFileService.downloadFileByCommand(path + "/extras/", ossResultPath + "extras/");
  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. //上传计算日志
  153. String buildLogPath = String.format(UploadFilePath.BUILD_LOG_PATH, projectNum);
  154. fYunFileService.uploadFile(path + File.separator + "console.log", buildLogPath + "console.log");
  155. if (!message.getBuildSuccess()) {
  156. log.error("生成OBJ场景计算失败!");
  157. // 发送钉钉消息,计算失败
  158. buildSceneDTService.handModelFail("生成OBJ场景计算失败!", message.getPath(), message.getBuildContext().get("sceneNum").toString(), message.getHostName());
  159. return;
  160. }
  161. ScenePro scenePro = sceneProService.getByNum(projectNum);
  162. String version = "v4";
  163. String ossImagePath = String.format(UploadFilePath.IMG_VIEW_PATH, projectNum);
  164. //如果是微服务上线之前的存量场景且未生成过obj(未升级过),需要上传到v3的目录,在函数最后调用升级接口进行文件拷贝
  165. if(Objects.nonNull(scenePro) && scenePro.getIsUpgrade() != CommonStatus.YES.code().intValue()){
  166. ossImagePath = String.format(ConstantFilePath.IMAGE_PATH_FORMAT, projectNum);
  167. version = "v3";
  168. }
  169. //上传obj
  170. String resultsPath = path + File.separator + "results" + File.separator;
  171. if(this.modelKind.equals(ModelKind.DAM.code())){
  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. CreateObjUtil.convertDamToLzma(path + File.separator + "results/");
  174. CreateObjUtil.convertTxtToDam(path + File.separator + "results" + File.separator + "tex" + File.separator + "modeldata.txt", path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam");
  175. File file = new File(path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam.lzma");
  176. while (!file.exists()) {
  177. Thread.sleep(60000);
  178. }
  179. fYunFileService.uploadFile(path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam.lzma", ossImagePath + ConstantFileName.modelUUID + "_50k.dam.lzma");
  180. fYunFileService.uploadFile(path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam", ossImagePath + ConstantFileName.modelUUID + "_50k.dam");
  181. }
  182. if(this.modelKind.equals(ModelKind.THREE_D_TILE.code())
  183. && CollUtil.isNotEmpty(sdTilesSceneSourceList)
  184. && sdTilesSceneSourceList.contains(SceneSource.JG.code())){
  185. String threedtilesPath = resultsPath + ModelKind.THREE_D_TILE.code();
  186. String oss3dtilesPath = ossImagePath + ModelKind.THREE_D_TILE.code();
  187. List<String> list = FileUtils.list(new File(threedtilesPath));
  188. if(CollUtil.isEmpty(list)){
  189. log.error("3dtiles目录异常,3dtiles地址:{}", new File(path + File.separator + "results" + File.separator + ModelKind.THREE_D_TILE.code()));
  190. throw new Exception("3dtiles目录异常");
  191. }
  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. //上传mesh文件
  211. fYunFileService.uploadFileByCommand(laserObjFilePath + "/results/mesh", String.format(UploadFilePath.DATA_VIEW_PATH, projectNum) + "mesh");
  212. CreateObjUtil.convertTxtToVisionmodeldata(resultsPath + "vision.txt", resultsPath + "vision.modeldata");
  213. fYunFileService.uploadFile(resultsPath + "vision.txt", ossImagePath.concat("vision.txt"));
  214. fYunFileService.uploadFile(resultsPath + "vision.modeldata", ossImagePath.concat("vision.modeldata"));
  215. if(!ObjectUtils.isEmpty(scenePro)){
  216. LambdaUpdateWrapper<ScenePro> updateWrapper = new LambdaUpdateWrapper<ScenePro>()
  217. .set(ScenePro::getStatus, -2).eq(ScenePro::getNum, projectNum);
  218. sceneProService.update(updateWrapper);
  219. sceneProEditService.update(new LambdaUpdateWrapper<SceneProEdit>().set(SceneProEdit::getMapVisi,0)
  220. .eq(SceneProEdit::getProId,scenePro.getId()));
  221. }
  222. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(projectNum);
  223. if(Objects.nonNull(scenePlus)){
  224. //清除用户编辑业务数据
  225. Set<String> bizs = new HashSet<>();
  226. bizs.add(UserEditDataType.BOX_MODEL.message());
  227. bizs.add(UserEditDataType.FLOORPLAN.message());
  228. commonService.initUserEditData(projectNum, bizs, null);
  229. LambdaUpdateWrapper<ScenePlus> plusUpdateWrapper = new LambdaUpdateWrapper<ScenePlus>()
  230. .set(ScenePlus::getSceneStatus, -2).eq(ScenePlus::getNum, projectNum);
  231. scenePlusService.update(plusUpdateWrapper);
  232. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  233. scenePlusExt.setAlgorithmTime(Calendar.getInstance().getTime());
  234. //计算容量
  235. Long space = commonService.getSpace(projectNum);
  236. scenePlusExt.setSpace(space);
  237. Object[] editInfoArr = commonService.updateEditInfo(scenePlus);
  238. SceneEditInfo sceneEditInfo = (SceneEditInfo)editInfoArr[0];
  239. SceneEditInfoExt sceneEditInfoExt = (SceneEditInfoExt)editInfoArr[1];
  240. SceneEditControls sceneEditControls = (SceneEditControls)editInfoArr[2];
  241. if(this.modelKind.equals(ModelKind.THREE_D_TILE.code())
  242. && CollUtil.isNotEmpty(sdTilesSceneSourceList)
  243. && sdTilesSceneSourceList.contains(scenePlus.getSceneSource())){
  244. scenePlusExt.setModelKind(ModelKind.THREE_D_TILE.code());
  245. scenePlusExtService.updateById(scenePlusExt);
  246. }
  247. log.info("生成scene.json上传oss并设置缓存,num:{}", projectNum);
  248. CameraDetail cameraDetail = cameraDetailService.getByCameraId(scenePlus.getCameraId());
  249. Company company = !org.apache.commons.lang3.ObjectUtils.isEmpty(cameraDetail.getCompanyId()) ? companyService.getById(cameraDetail.getCompanyId()) : null;
  250. //写scene.json
  251. commonService.writeSceneJson(projectNum, new JSONObject(),sceneEditInfo, sceneEditInfoExt, sceneEditControls, scenePlus,scenePlusExt,company);
  252. }
  253. // 如果未升级V4,则升级V4
  254. if("v3".equals(version)){
  255. fdkkV4Service.upgradeToV4(projectNum);
  256. }
  257. success = true;
  258. //国际环境需要发邮件通知
  259. if("eur".equals(env)){
  260. commonService.sendEmail(projectNum);
  261. }
  262. }catch (Exception e){
  263. log.error("生成OBJ场景计算结果处理异常,num=" + projectNum, e);
  264. buildSceneDTService.handBaseFail("生成OBJ场景计算结果处理异常", message.getPath(), projectNum, "计算控制服务器");
  265. throw e;
  266. }finally {
  267. fdkkLaserService.pushBuildStatusToLaserSystem(projectNum, laserObjFilePath + "/laserData/mesh", success ? CommonOperStatus.SUCCESS.code() : CommonOperStatus.FAILD.code());
  268. }
  269. }
  270. }