BuildObjServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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.math.BigDecimal;
  35. import java.util.*;
  36. /**
  37. * <p>
  38. * TODO
  39. * </p>
  40. *
  41. * @author dengsixing
  42. * @since 2022/3/1
  43. **/
  44. @Slf4j
  45. @Service
  46. @RefreshScope
  47. public class BuildObjServiceImpl implements IBuildSceneService {
  48. @Value("${queue.modeling.modeling-call}")
  49. private String queueModelingCall;
  50. @Value("${queue.modeling.obj.modeling-post}")
  51. private String queueObjModelingPost;
  52. @Value("${queue.modeling.obj.modeling-pre}")
  53. private String queueObjModelingPre;
  54. @Value("${model.modelKind:3dtiles}")
  55. private String modelKind;
  56. @Value("#{'${model.3dtiles.sceneSource:}'.split(',')}")
  57. private List<Integer> sdTilesSceneSourceList;
  58. @Value("${env:gn}")
  59. private String env;
  60. @Autowired
  61. private RabbitMqProducer mqProducer;
  62. @Resource
  63. private FYunFileServiceInterface fYunFileService;
  64. @Autowired
  65. private ISceneProService sceneProService;
  66. @Autowired
  67. private IFdkkLaserService fdkkLaserService;
  68. @Autowired
  69. private FdkkV4Service fdkkV4Service;
  70. @Autowired
  71. private IBuildSceneDTService buildSceneDTService;
  72. @Autowired
  73. private ISceneProEditService sceneProEditService;
  74. @Autowired
  75. private IScenePlusService scenePlusService;
  76. @Autowired
  77. private ISceneBuildProcessLogService sceneBuildProcessLogService;
  78. @Autowired
  79. private ICommonService commonService;
  80. @Autowired
  81. private IScenePlusExtService scenePlusExtService;
  82. @Autowired
  83. private RedisUtil redisUtil;
  84. @Autowired
  85. private ICameraDetailService cameraDetailService;
  86. @Autowired
  87. private ICompanyService companyService;
  88. @Autowired
  89. private IBuildService buildService;
  90. @Override
  91. public void buildScenePre(BuildSceneCallMessage message) throws Exception{
  92. boolean success = false;
  93. String num = message.getSceneNum();
  94. String path = message.getPath();
  95. String laserObjFilePath = path + "_laser_obj";
  96. try {
  97. //根据相机类型,组装资源路径
  98. log.info("开始准备生成OBJ场景资源,路径:{}", laserObjFilePath);
  99. FileUtils.delAllFile(laserObjFilePath);
  100. //获取解压后的资源的data.fdage中的数据
  101. fYunFileService.downloadFile(ConstantFilePath.OSS_PREFIX + path.replace(ConstantFilePath.BUILD_MODEL_LASER_PATH, "") + "/data.fdage", laserObjFilePath + "/capture/data.fdage");
  102. JSONObject jsonObject = FileUtils.readJson(laserObjFilePath + File.separator + "capture" + File.separator + "data.fdage");
  103. if(ObjectUtils.isEmpty(jsonObject)){
  104. log.error("data.fdage文件不存在");
  105. return ;
  106. }
  107. //下载资源到本地
  108. this.downLoadSource(message, laserObjFilePath);
  109. if (!jsonObject.containsKey("exportMeshObj") || jsonObject.getIntValue("exportMeshObj") != 1) {
  110. // 写入data.fdage 防止重算
  111. jsonObject.put("exportMeshObj", 1);
  112. FileUtil.writeUtf8String(jsonObject.toJSONString(), laserObjFilePath + File.separator + "capture" + File.separator + "data.fdage");
  113. fYunFileService.uploadFile(laserObjFilePath + File.separator + "capture" + File.separator + "data.fdage",
  114. ConstantFilePath.OSS_PREFIX + path.replace(ConstantFilePath.BUILD_MODEL_LASER_PATH, "") + "/data.fdage");
  115. }
  116. // 重新构造datafdage文件,添加生成obt文件选项
  117. jsonObject.put("OnlyExportMeshObj",true);
  118. FileUtils.writeFile(laserObjFilePath + File.separator + "capture" + File.separator + "data.fdage", jsonObject.toJSONString());
  119. message.getBuildContext().put("previousPath",path);
  120. message.setCameraType("14");
  121. message.setResolution(jsonObject.getString("resolution"));
  122. message.setPath(laserObjFilePath);
  123. message.setResultReceiverMqName(queueObjModelingPost);
  124. message.setBizType("obj");
  125. JSONObject fdageData = commonService.getFdageData(laserObjFilePath + File.separator + "capture" + File.separator + "data.fdage");
  126. Map<String, Object> dataExtras = new HashMap<>(1);
  127. dataExtras.put("big_depthmap", true);
  128. buildService.writeDataJson(message, fdageData, null, dataExtras);
  129. success = true;
  130. log.info("场景计算资源准备结束,场景码:{}", message.getSceneNum());
  131. }catch (Exception e){
  132. log.error("生成OBJ场景资源准备异常,num=" + num, e);
  133. buildSceneDTService.handBaseFail("生成OBJ场景资源准备异常", message.getPath(), message.getSceneNum(), "计算控制服务器");
  134. throw e;
  135. }finally {
  136. fdkkLaserService.pushBuildStatusToLaserSystem(num, laserObjFilePath + "/laserData/mesh", success ? CommonOperStatus.SUCCESS.code() : CommonOperStatus.FAILD.code());
  137. }
  138. }
  139. @Override
  140. public void downLoadSource(BuildSceneCallMessage message,String path) throws Exception{
  141. //下载原始资源文件
  142. String ossPath = commonService.getOssOrignPath(message.getPath());
  143. fYunFileService.downloadFileByCommand(path + File.separator + "capture", ossPath);
  144. //下载caches/image,算法会执行快一些
  145. String ossResultPath = String.format(UploadFilePath.scene_result_data_path, message.getSceneNum());
  146. fYunFileService.downloadFileByCommand(path + "/caches/images/", ossResultPath + "caches/images/");
  147. //下载点位校准文件,刘强版本大改,要求只有包含字段vision_edit_version且vision_edit_version的值是1.0时,才下载vision_edit.txt文件进行计算,其他情况不保留
  148. //2025-04-01,刘强再次要求,vision_edit_version >= 1.0 可以下载vision_edit.txt进行计算
  149. if(fYunFileService.fileExist(ossResultPath + "extras/" + "vision_edit.txt")){
  150. String fileContent = fYunFileService.getFileContent(ossResultPath + "extras/" + "vision_edit.txt");
  151. JSONObject jsonObject = JSON.parseObject(fileContent);
  152. if(jsonObject.containsKey("vision_edit_version")){
  153. BigDecimal visionEditVersion = jsonObject.getBigDecimal("vision_edit_version");
  154. if(visionEditVersion.compareTo(new BigDecimal("1.0")) != -1){
  155. fYunFileService.downloadFile(ossResultPath + "extras/vision_edit.txt", path + "/extras/vision_edit.txt");
  156. }
  157. }
  158. }
  159. }
  160. public static void main(String[] args) {
  161. JSONObject o = new JSONObject();
  162. o.put("vision_edit_version", "1.1");
  163. System.out.println(o.getBigDecimal("vision_edit_version"));
  164. }
  165. @Override
  166. public void buildScenePost(BuildSceneResultMqMessage message) throws Exception {
  167. String path = message.getPath();
  168. String projectNum = message.getBuildContext().get("sceneNum").toString();
  169. String laserObjFilePath = path;
  170. boolean success = false;
  171. try {
  172. //上传计算日志
  173. String buildLogPath = String.format(UploadFilePath.BUILD_LOG_PATH, projectNum);
  174. fYunFileService.uploadFile(path + File.separator + "console.log", buildLogPath + "console.log");
  175. if (!message.getBuildSuccess()) {
  176. log.error("生成OBJ场景计算失败!");
  177. scenePlusService.update(new LambdaUpdateWrapper<ScenePlus>()
  178. .set(ScenePlus::getSceneStatus, SceneStatus.FAILD.code())
  179. .eq(ScenePlus::getNum, projectNum));
  180. // 发送钉钉消息,计算失败
  181. buildSceneDTService.handModelFail("生成OBJ场景计算失败!", message.getPath(), message.getBuildContext().get("sceneNum").toString(), message.getHostName());
  182. return;
  183. }
  184. ScenePro scenePro = sceneProService.getByNum(projectNum);
  185. String version = "v4";
  186. String ossImagePath = String.format(UploadFilePath.IMG_VIEW_PATH, projectNum);
  187. //如果是微服务上线之前的存量场景且未生成过obj(未升级过),需要上传到v3的目录,在函数最后调用升级接口进行文件拷贝
  188. if(Objects.nonNull(scenePro) && scenePro.getIsUpgrade() != CommonStatus.YES.code().intValue()){
  189. ossImagePath = String.format(ConstantFilePath.IMAGE_PATH_FORMAT, projectNum);
  190. version = "v3";
  191. }
  192. //上传obj
  193. String resultsPath = path + File.separator + "results" + File.separator;
  194. if(this.modelKind.equals(ModelKind.DAM.code())){
  195. CreateObjUtil.convertTxtToDam(path + File.separator + "results" + File.separator + "tex" + File.separator + "modeldata.txt", path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam");
  196. CreateObjUtil.convertDamToLzma(path + File.separator + "results/");
  197. CreateObjUtil.convertTxtToDam(path + File.separator + "results" + File.separator + "tex" + File.separator + "modeldata.txt", path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam");
  198. File file = new File(path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam.lzma");
  199. while (!file.exists()) {
  200. Thread.sleep(60000);
  201. }
  202. fYunFileService.uploadFile(path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam.lzma", ossImagePath + ConstantFileName.modelUUID + "_50k.dam.lzma");
  203. fYunFileService.uploadFile(path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam", ossImagePath + ConstantFileName.modelUUID + "_50k.dam");
  204. }
  205. if(this.modelKind.equals(ModelKind.THREE_D_TILE.code())
  206. && CollUtil.isNotEmpty(sdTilesSceneSourceList)
  207. && sdTilesSceneSourceList.contains(SceneSource.JG.code())){
  208. String threedtilesPath = resultsPath + ModelKind.THREE_D_TILE.code();
  209. String oss3dtilesPath = ossImagePath + ModelKind.THREE_D_TILE.code();
  210. List<String> list = FileUtils.list(new File(threedtilesPath));
  211. if(CollUtil.isEmpty(list)){
  212. log.error("3dtiles目录异常,3dtiles地址:{}", new File(path + File.separator + "results" + File.separator + ModelKind.THREE_D_TILE.code()));
  213. throw new Exception("3dtiles目录异常");
  214. }
  215. //先删除
  216. fYunFileService.deleteFolder(oss3dtilesPath);
  217. //上传3dtiles文件
  218. fYunFileService.uploadFileByCommand(threedtilesPath, oss3dtilesPath);
  219. }
  220. String texPath = laserObjFilePath + File.separator + "results" + File.separator + "tex";
  221. File texFile = new File(texPath);
  222. if(texFile.exists()){
  223. for (File textureFile : texFile.listFiles()) {
  224. if(textureFile.getName().endsWith(".jpg")){
  225. fYunFileService.uploadFile(textureFile.getAbsolutePath(),
  226. ossImagePath + ConstantFileName.modelUUID + "_50k_texture_jpg_high1/"+textureFile.getName());
  227. }
  228. }
  229. }
  230. fYunFileService.uploadFile(laserObjFilePath + File.separator + "results" + File.separator + "tex/texture1.jpg",
  231. ossImagePath + ConstantFileName.modelUUID + "_50k_texture_jpg_high1/texture1.jpg");
  232. // 拷贝结果
  233. log.info("开始拷贝obj文件");
  234. FileUtils.copyFolderAllFiles(laserObjFilePath + "/results/mesh", laserObjFilePath + "/laserData/mesh/", true);
  235. //先删除
  236. fYunFileService.deleteFolder(String.format(UploadFilePath.DATA_VIEW_PATH, projectNum) + "mesh");
  237. //上传mesh文件
  238. fYunFileService.uploadFileByCommand(laserObjFilePath + "/results/mesh", String.format(UploadFilePath.DATA_VIEW_PATH, projectNum) + "mesh");
  239. CreateObjUtil.convertTxtToVisionmodeldata(resultsPath + "vision.txt", resultsPath + "vision.modeldata");
  240. fYunFileService.uploadFile(resultsPath + "vision.txt", ossImagePath.concat("vision.txt"));
  241. fYunFileService.uploadFile(resultsPath + "vision.modeldata", ossImagePath.concat("vision.modeldata"));
  242. if(!ObjectUtils.isEmpty(scenePro)){
  243. LambdaUpdateWrapper<ScenePro> updateWrapper = new LambdaUpdateWrapper<ScenePro>()
  244. .set(ScenePro::getStatus, -2).eq(ScenePro::getNum, projectNum);
  245. sceneProService.update(updateWrapper);
  246. sceneProEditService.update(new LambdaUpdateWrapper<SceneProEdit>().set(SceneProEdit::getMapVisi,0)
  247. .eq(SceneProEdit::getProId,scenePro.getId()));
  248. }
  249. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(projectNum);
  250. if(Objects.nonNull(scenePlus)){
  251. // //清除用户编辑业务数据
  252. // Set<String> bizs = new HashSet<>();
  253. // bizs.add(UserEditDataType.BOX_MODEL.message());
  254. // bizs.add(UserEditDataType.FLOORPLAN.message());
  255. // commonService.initUserEditData(projectNum, bizs, null);
  256. LambdaUpdateWrapper<ScenePlus> plusUpdateWrapper = new LambdaUpdateWrapper<ScenePlus>()
  257. .set(ScenePlus::getSceneStatus, -2).eq(ScenePlus::getNum, projectNum);
  258. scenePlusService.update(plusUpdateWrapper);
  259. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  260. scenePlusExt.setAlgorithmTime(Calendar.getInstance().getTime());
  261. //计算容量
  262. Long space = commonService.getSpace(projectNum);
  263. scenePlusExt.setSpace(space);
  264. scenePlusExt.setMeshComputeTime(message.getComputeTime());
  265. Object[] editInfoArr = commonService.updateEditInfo(scenePlus);
  266. SceneEditInfo sceneEditInfo = (SceneEditInfo)editInfoArr[0];
  267. SceneEditInfoExt sceneEditInfoExt = (SceneEditInfoExt)editInfoArr[1];
  268. SceneEditControls sceneEditControls = (SceneEditControls)editInfoArr[2];
  269. if(this.modelKind.equals(ModelKind.THREE_D_TILE.code())
  270. && CollUtil.isNotEmpty(sdTilesSceneSourceList)
  271. && sdTilesSceneSourceList.contains(scenePlus.getSceneSource())){
  272. scenePlusExt.setModelKind(ModelKind.THREE_D_TILE.code());
  273. scenePlusExtService.updateById(scenePlusExt);
  274. }
  275. log.info("生成scene.json上传oss并设置缓存,num:{}", projectNum);
  276. CameraDetail cameraDetail = cameraDetailService.getByCameraId(scenePlus.getCameraId());
  277. Company company = !org.apache.commons.lang3.ObjectUtils.isEmpty(cameraDetail.getCompanyId()) ? companyService.getById(cameraDetail.getCompanyId()) : null;
  278. //写scene.json
  279. log.info("---------------------videos:{}", scenePlusExt.getVideos());
  280. commonService.writeSceneJson(projectNum,sceneEditInfo, sceneEditInfoExt, sceneEditControls, scenePlus,scenePlusExt,company);
  281. }
  282. //重置异步操作记录
  283. commonService.removeSceneAsynOperLog(projectNum);
  284. // 如果未升级V4,则升级V4
  285. if("v3".equals(version)){
  286. fdkkV4Service.upgradeToV4(projectNum);
  287. }
  288. success = true;
  289. //国际环境需要发邮件通知
  290. if("eur".equals(env)){
  291. commonService.sendEmail(projectNum, "obj");
  292. }
  293. }catch (Exception e){
  294. log.error("生成OBJ场景计算结果处理异常,num=" + projectNum, e);
  295. buildSceneDTService.handBaseFail("生成OBJ场景计算结果处理异常", message.getPath(), projectNum, "计算控制服务器");
  296. throw e;
  297. }finally {
  298. fdkkLaserService.pushBuildStatusToLaserSystem(projectNum, laserObjFilePath + "/laserData/mesh", success ? CommonOperStatus.SUCCESS.code() : CommonOperStatus.FAILD.code());
  299. }
  300. }
  301. }