BuildReverseE57SceneServiceImpl.java 15 KB

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