BuildReverseE57SceneServiceImpl.java 15 KB

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