BuildReverseE57SceneServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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.ZipUtil;
  5. import com.alibaba.fastjson.JSON;
  6. import com.alibaba.fastjson.JSONArray;
  7. import com.alibaba.fastjson.JSONObject;
  8. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  9. import com.fdkankan.common.constant.*;
  10. import com.fdkankan.common.util.FileUtils;
  11. import com.fdkankan.contro.constant.UserEditDataType;
  12. import com.fdkankan.contro.entity.*;
  13. import com.fdkankan.contro.mq.service.IBuildSceneService;
  14. import com.fdkankan.contro.service.*;
  15. import com.fdkankan.contro.service.impl.CommonServiceImpl;
  16. import com.fdkankan.fyun.config.FYunFileConfig;
  17. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  18. import com.fdkankan.model.constants.ConstantFileName;
  19. import com.fdkankan.model.constants.ConstantFilePath;
  20. import com.fdkankan.model.constants.UploadFilePath;
  21. import com.fdkankan.model.utils.CreateObjUtil;
  22. import com.fdkankan.rabbitmq.bean.BuildSceneCallMessage;
  23. import com.fdkankan.rabbitmq.bean.BuildSceneResultMqMessage;
  24. import com.fdkankan.rabbitmq.util.RabbitMqProducer;
  25. import lombok.extern.slf4j.Slf4j;
  26. import org.apache.commons.lang3.ObjectUtils;
  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 javax.annotation.Resource;
  32. import java.io.File;
  33. import java.nio.charset.StandardCharsets;
  34. import java.util.*;
  35. /**
  36. * <p>
  37. * TODO
  38. * </p>
  39. *
  40. * @author dengsixing
  41. * @since 2022/4/20
  42. **/
  43. @Slf4j
  44. @Service
  45. @RefreshScope
  46. public class BuildReverseE57SceneServiceImpl implements IBuildSceneService {
  47. public static final String logUrlFormat = "**algorithm-log**: [%sbuild_log/%s/e57/console.log](%sbuild_log/%s/e57/console.log)";
  48. @Value("${queue.modeling.reverseE57.modeling-post:reverseE57-modeling-post}")
  49. private String queueModelingPost;
  50. @Value("#{'${build.scene.post.not-delete-nas-nums:}'.split(',')}")
  51. private List<String> notDeleteNasNumList;
  52. @Value("${queue.application.laser.e57-modeling-done:e57-modeling-done}")
  53. private String queueE57ModelingDone;
  54. @Autowired
  55. private RabbitMqProducer mqProducer;
  56. @Resource
  57. private FYunFileServiceInterface fYunFileService;
  58. @Autowired
  59. private FYunFileConfig fYunFileConfig;
  60. @Autowired
  61. private IScenePlusService scenePlusService;
  62. @Autowired
  63. private IScenePlusExtService scenePlusExtService;
  64. @Autowired
  65. private IBuildSceneDTService buildSceneDTService;
  66. @Autowired
  67. private ICommonService commonService;
  68. @Autowired
  69. private IFdkkLaserService fdkkLaserService;
  70. @Autowired
  71. private ISceneEditControlsService sceneEditControlsService;
  72. @Override
  73. public void buildScenePre(BuildSceneCallMessage message) throws Exception{
  74. String num = message.getSceneNum();
  75. try {
  76. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  77. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  78. String dataSource = scenePlusExt.getDataSource();
  79. message.setPath(dataSource);
  80. String e57Path = (String)message.getExt().get("path");
  81. //下载资源到本地
  82. fYunFileService.downloadFile(e57Path, dataSource + "/capture");
  83. //发送mq,就进行计算
  84. // message.setPath(path);
  85. message.setResultReceiverMqName(queueModelingPost);
  86. message.setBizType("reverseE57");
  87. log.info("上传e57计算资源准备结束,场景码:{}", message.getSceneNum());
  88. }catch (Exception e){
  89. log.error("上传e57计算前置处理出错,num"+num, e);
  90. buildSceneDTService.handBaseFail("上传e57计算资源准备异常!", message.getPath(), message.getSceneNum(), "计算控制服务器");
  91. throw e;
  92. }
  93. }
  94. private String getOssPath(String path) {
  95. String ossPath = ConstantFilePath.OSS_PREFIX
  96. + path.replace(ConstantFilePath.BUILD_MODEL_PATH, "")
  97. .replace(ConstantFilePath.BUILD_MODEL_LASER_PATH, "");
  98. if (!ossPath.endsWith("/")) {
  99. ossPath = ossPath.concat("/");
  100. }
  101. return ossPath;
  102. }
  103. @Override
  104. public void downLoadSource(BuildSceneCallMessage buildSceneMqMessage,String path){
  105. String ossPath = getOssPath(buildSceneMqMessage.getPath());
  106. fYunFileService.downloadFileByCommand(path + "/capture", ossPath);
  107. }
  108. @Override
  109. public void buildScenePost(BuildSceneResultMqMessage message) throws Exception {
  110. String sceneCode = message.getBuildContext().get("sceneNum").toString();
  111. String path = message.getPath();
  112. try {
  113. // 上传计算日志
  114. //如果是重复计算,没有走到计算逻辑,不需要上传日志文件
  115. log.info("开始上传计算日志");
  116. String buildLogPath = String.format(UploadFilePath.BUILD_LOG_PATH, sceneCode);
  117. fYunFileService.uploadFile(path + File.separator + "console.log", buildLogPath + "console.log");
  118. log.info("计算日志上传完成");
  119. if (!message.getBuildSuccess()) {
  120. log.error("建模失败,修改状态为失败状态");
  121. scenePlusService.update(new LambdaUpdateWrapper<ScenePlus>()
  122. .set(ScenePlus::getSceneStatus, SceneStatus.FAILD.code())
  123. .eq(ScenePlus::getNum, sceneCode));
  124. return;
  125. }
  126. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(sceneCode);
  127. Integer cameraType = Integer.parseInt(message.getBuildContext().get("cameraType").toString());
  128. Map<String, String> uploadFiles = this.getUploadFiles(scenePlus,path);
  129. scenePlus.setPayStatus(PayStatus.PAY.code());
  130. scenePlus.setUpdateTime(new Date());
  131. scenePlus.setSceneStatus(SceneStatus.NO_DISPLAY.code());
  132. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  133. log.info("开始上传场景计算结果数据,num:{}", sceneCode);
  134. //上传文件
  135. fYunFileService.uploadMulFiles(uploadFiles);
  136. //生成floorpan.json
  137. commonService.uploadFloorplanJson(sceneCode, path);
  138. //清除用户编辑业务数据
  139. Set<String> bizs = new HashSet<>();
  140. bizs.add(UserEditDataType.BOX_MODEL.message());
  141. bizs.add(UserEditDataType.FLOORPLAN.message());
  142. bizs.add(UserEditDataType.FILTERS.message());
  143. commonService.initUserEditData(sceneCode, bizs, null);
  144. //上传计算结果文件
  145. commonService.uploadBuildResultData(sceneCode, path, SceneVersionType.V4.code());
  146. Object[] editInfoArr = commonService.updateEditInfo(scenePlus);
  147. SceneEditInfo sceneEditInfo = (SceneEditInfo)editInfoArr[0];
  148. SceneEditInfoExt sceneEditInfoExt = (SceneEditInfoExt)editInfoArr[1];
  149. SceneEditControls sceneEditControls = (SceneEditControls)editInfoArr[2];
  150. //如果相机容量不足,需要把场景的paystatus改为容量不足状态
  151. scenePlusExt.setOrigSpace(FileUtil.size(new File(path.concat(File.separator).concat("capture"))));
  152. if (cameraType == 14) {
  153. //计算成功 激光转台相机 同步 请求
  154. fdkkLaserService.syncBuildResult(scenePlus.getNum(), path, scenePlus.getCreateTime(), Calendar.getInstance().getTime(), scenePlusExt.getShootCount(), scenePlus.getPayStatus());
  155. sceneEditControlsService.update(new LambdaUpdateWrapper<SceneEditControls>().set(SceneEditControls::getShowMap,0)
  156. .eq(SceneEditControls::getEditInfoId,sceneEditInfo.getId()));
  157. sceneEditControls.setShowMap(0);
  158. } else if (new File(path + "/results/laserData/vision_edit.txt").exists()) {
  159. fdkkLaserService.cloudPointBuild(sceneCode,path);
  160. }
  161. // //删除计算目录
  162. if(CollUtil.isEmpty(notDeleteNasNumList) || !notDeleteNasNumList.contains(sceneCode)){
  163. CreateObjUtil.deleteFile(path.replace(ConstantFilePath.BUILD_MODEL_PATH, "/"));
  164. }
  165. this.uploadStatusJson(scenePlus, scenePlusExt);
  166. scenePlusService.updateById(scenePlus);
  167. scenePlusExtService.updateById(scenePlusExt);
  168. log.info("场景计算结果处理结束,场景码:{}", sceneCode);
  169. }catch (Exception e){
  170. log.error("场景计算结果处理出错,num"+sceneCode, e);
  171. buildSceneDTService.handBaseFail("场景计算结果处理出错!", message.getPath(), sceneCode, "计算控制服务器");
  172. throw e;
  173. }
  174. }
  175. private void uploadStatusJson(ScenePlus scenePlus, ScenePlusExt scenePlusExt){
  176. String num = scenePlus.getNum();
  177. String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, num);
  178. Integer status = 0;
  179. // 上传status JSON.
  180. JSONObject statusJson = new JSONObject();
  181. //临时将-2改成1,app还没完全更新
  182. statusJson.put("status", status);
  183. statusJson.put("webSite", scenePlusExt.getWebSite());
  184. statusJson.put("sceneNum", num);
  185. statusJson.put("thumb", scenePlusExt.getThumb());
  186. statusJson.put("payStatus", scenePlus.getPayStatus());
  187. statusJson.put("sceneScheme", scenePlusExt.getSceneScheme());
  188. FileUtils.writeFile(ConstantFilePath.SCENE_PATH + "data/data" + num + File.separator + "status.json", statusJson.toString());
  189. fYunFileService.uploadFile(statusJson.toJSONString().getBytes(StandardCharsets.UTF_8), dataViewPath + "status.json");
  190. }
  191. private Map<String, String> getUploadFiles(ScenePlus scenePlus,String path) throws Exception {
  192. if (ObjectUtils.isEmpty(scenePlus)) {
  193. throw new Exception("未找到场景信息:" + path);
  194. }
  195. String projectNum = scenePlus.getNum();
  196. String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, projectNum);
  197. String imagesPath = String.format(UploadFilePath.IMG_VIEW_PATH, projectNum);
  198. String videoPath = String.format(UploadFilePath.VIDEOS_VIEW_PATH, projectNum);
  199. String resultsPath = path + File.separator + "results" + File.separator;
  200. String uploadData = FileUtils.readFile(resultsPath + "upload.json");
  201. JSONArray array = JSONObject.parseObject(uploadData).getJSONArray("upload");
  202. JSONObject fileJson = null;
  203. String fileName = "";
  204. Map<String, String> map = new HashMap();
  205. for (int i = 0; i < array.size(); ++i) {
  206. fileJson = array.getJSONObject(i);
  207. fileName = fileJson.getString("file");
  208. String filePath = resultsPath + fileName;
  209. if (!(new File(filePath)).exists()) {
  210. throw new Exception(filePath + "文件不存在");
  211. }
  212. if ("vision2.txt".equals(fileName)) {
  213. CreateObjUtil.convertTxtToVisionmodeldata(resultsPath + "vision2.txt", resultsPath + "vision2.modeldata");
  214. map.put(resultsPath + "vision2.modeldata", imagesPath + "vision2.modeldata");
  215. map.put(resultsPath + "vision2.txt", imagesPath + "vision2.txt");
  216. }
  217. if (fileJson.getIntValue("clazz") == 2) {
  218. map.put(filePath, imagesPath + ConstantFileName.modelUUID + "_50k_texture_jpg_high1/" + fileName.replace("tex/", ""));
  219. } else if (fileJson.getIntValue("clazz") == 3) {
  220. map.put(filePath, imagesPath + "pan/high/" + fileName.replace("high/", ""));
  221. } else if (fileJson.getIntValue("clazz") == 4) {
  222. map.put(filePath, imagesPath + "pan/low/" + fileName.replace("low/", ""));
  223. } else if (fileJson.getIntValue("clazz") == 5) {
  224. map.put(filePath, imagesPath + fileName);
  225. } else if (fileJson.getIntValue("clazz") == 7) {
  226. map.put(filePath, imagesPath + fileName);
  227. } else if (fileJson.getIntValue("clazz") == 10) {
  228. String updown = FileUtils.readFile(filePath);
  229. JSONObject updownJson = JSONObject.parseObject(updown);
  230. String mappingOssPath = String.format("scene_edit_data/%s/data/", projectNum) + fileName.replace("updown", "mapping");
  231. map.put(filePath, mappingOssPath);
  232. } else {
  233. if (fileJson.getIntValue("clazz") == 11 || fileJson.getIntValue("clazz") == 12) {
  234. map.put(filePath, videoPath + fileName.replace("videos/", ""));
  235. if (fileName.contains(".mp4")) {
  236. map.put(resultsPath + fileName.replace("mp4", "flv"), videoPath + fileName.replace("videos/", "").replace("mp4", "flv"));
  237. }
  238. }
  239. if (fileJson.getIntValue("clazz") == 16) {
  240. map.put(filePath, dataViewPath + fileName);
  241. }
  242. if (fileJson.getIntValue("clazz") == 18) {
  243. map.put(filePath, imagesPath + fileName);
  244. }
  245. }
  246. }
  247. CreateObjUtil.convertTxtToVisionmodeldata(resultsPath + "vision.txt", resultsPath + "vision.modeldata");
  248. map.put(resultsPath + "vision.txt", imagesPath + "vision.txt");
  249. map.put(resultsPath + "vision.modeldata", imagesPath + "vision.modeldata");
  250. log.info("数据转换完成:" + projectNum);
  251. if(!new File("/mnt/4Dkankan/scene/data" + File.separator + "data" + projectNum).exists()){
  252. FileUtil.mkdir("/mnt/4Dkankan/scene/data" + File.separator + "data" + projectNum);
  253. }
  254. // map.put(resultsPath + "floorplan.json", dataViewPath + "floor.json");
  255. map.put(resultsPath + "floorplan_cad.json", dataViewPath + "floorplan_cad.json");
  256. map.put(path + File.separator + "capture/stitch_params.txt", dataViewPath + "stitch_params.txt");
  257. map.put(path + File.separator + "capture/Up.xml", dataViewPath + "Up.xml");
  258. map.put(path + File.separator + "capture/Up2.xml", dataViewPath + "Up2.xml");
  259. map.put(path + File.separator + "capture/Up.txt", dataViewPath + "Up.txt");
  260. map.put(path + File.separator + "capture/Up2.txt", dataViewPath + "Up2.txt");
  261. return map;
  262. }
  263. }