BuildSxRelocationServiceImpl.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package com.fdkankan.contro.mq.service.impl;
  2. import cn.hutool.core.io.FileUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  6. import com.fdkankan.common.constant.*;
  7. import com.fdkankan.common.util.FileUtils;
  8. import com.fdkankan.contro.entity.RelocationBatch;
  9. import com.fdkankan.contro.entity.RelocationBatchDetail;
  10. import com.fdkankan.contro.entity.ScenePlus;
  11. import com.fdkankan.contro.entity.ScenePlusExt;
  12. import com.fdkankan.contro.mq.service.IBuildSceneService;
  13. import com.fdkankan.contro.service.*;
  14. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  15. import com.fdkankan.model.constants.ConstantFileName;
  16. import com.fdkankan.model.constants.ConstantFilePath;
  17. import com.fdkankan.model.constants.UploadFilePath;
  18. import com.fdkankan.model.enums.ModelTypeEnums;
  19. import com.fdkankan.model.utils.CreateObjUtil;
  20. import com.fdkankan.rabbitmq.bean.BuildSceneCallMessage;
  21. import com.fdkankan.rabbitmq.bean.BuildSceneResultMqMessage;
  22. import com.fdkankan.rabbitmq.util.RabbitMqProducer;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.apache.commons.lang3.ObjectUtils;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.beans.factory.annotation.Value;
  27. import org.springframework.cloud.context.config.annotation.RefreshScope;
  28. import org.springframework.stereotype.Service;
  29. import javax.annotation.Resource;
  30. import java.io.File;
  31. import java.nio.charset.StandardCharsets;
  32. import java.util.Date;
  33. import java.util.HashMap;
  34. import java.util.List;
  35. import java.util.Map;
  36. import java.util.stream.Collectors;
  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 BuildSxRelocationServiceImpl implements IBuildSceneService {
  49. @Value("${queue.modeling.sx-relocation-post:sx-relocation-post}")
  50. private String queueModelingPost;
  51. @Value("${model.type:#{null}}")
  52. private String modelType;
  53. @Value("${env:gn}")
  54. private String env;
  55. @Autowired
  56. private RabbitMqProducer mqProducer;
  57. @Resource
  58. private FYunFileServiceInterface fYunFileService;
  59. @Autowired
  60. private IScenePlusService scenePlusService;
  61. @Autowired
  62. private IScenePlusExtService scenePlusExtService;
  63. @Autowired
  64. private ICommonService commonService;
  65. @Autowired
  66. private IBuildService buildService;
  67. @Autowired
  68. private IRelocationBatchDetailService relocationBatchDetailService;
  69. @Autowired
  70. private IRelocationBatchService relocationBatchService;
  71. @Override
  72. public void buildScenePre(BuildSceneCallMessage message) throws Exception{
  73. String num = message.getSceneNum();
  74. Long batchId = Long.valueOf(message.getExt().get("batchId").toString());
  75. ScenePlus scenePlusByNum = scenePlusService.getScenePlusByNum(num);
  76. try {
  77. //重新计算时需要删除文件夹,否知使用缓存
  78. if(new File(message.getPath() + File.separator + "results").exists()){
  79. FileUtils.deleteDirectory(message.getPath() + File.separator + "results");
  80. }
  81. //由于刘强说caches会影响计算结果,所以这里删除caches
  82. if(new File(message.getPath() + File.separator + "caches").exists()){
  83. FileUtils.deleteDirectory(message.getPath() + File.separator + "caches");
  84. }
  85. this.downLoadSource(message, message.getPath());
  86. message.setResultReceiverMqName(queueModelingPost);
  87. Map<String, Object> buildContext = new HashMap<>();
  88. buildContext.put("sceneNum",message.getSceneNum());
  89. message.setBuildContext(buildContext);
  90. Map<String, String> dataMap = new HashMap<>();
  91. dataMap.put("splitType", "SPLIT_V31");
  92. dataMap.put("skyboxType", "SKYBOX_V6");
  93. if(ModelTypeEnums.TILE_CODE.equals(modelType)){
  94. dataMap.put("skyboxType", "SKYBOX_V14");
  95. }
  96. buildService.writeDataJson(message, new JSONObject(), dataMap, null);
  97. }catch (Exception e){
  98. scenePlusByNum.setSceneStatus(SceneStatus.FAILD.code());
  99. scenePlusService.updateById(scenePlusByNum);
  100. //修改重定位记录为退出计算
  101. relocationBatchService.update(new LambdaUpdateWrapper<RelocationBatch>().eq(RelocationBatch::getId, batchId).set(RelocationBatch::getStatus, 2));
  102. throw e;
  103. }
  104. }
  105. @Override
  106. public void downLoadSource(BuildSceneCallMessage buildSceneMqMessage,String path){
  107. String num = buildSceneMqMessage.getSceneNum();
  108. Long batchId = Long.valueOf(buildSceneMqMessage.getExt().get("batchId").toString());
  109. //下载深巡缓存数据
  110. String extrasPath = path + File.separator + "extras";
  111. fYunFileService.downloadFileByCommand(extrasPath, String.format(UploadFilePath.scene_result_data_path, num) + "pose/");
  112. String imagesPath = extrasPath + File.separator + "images";
  113. //下载图片
  114. String queryPath = imagesPath + File.separator + "query";
  115. List<RelocationBatchDetail> detailList = relocationBatchDetailService.listByBatchId(batchId);
  116. detailList.stream().forEach(v->{
  117. fYunFileService.downloadFile(v.getBucket(), v.getImgKey(), queryPath + File.separator + v.getFileName());
  118. });
  119. //生成query.json
  120. List<String> imgNameList = FileUtil.listFileNames(queryPath);
  121. List<JSONObject> filenames = imgNameList.stream().map(v -> {
  122. JSONObject a = new JSONObject();
  123. a.put("filename", v);
  124. return a;
  125. }).collect(Collectors.toList());
  126. JSONObject query = new JSONObject();
  127. query.put("query", filenames);
  128. FileUtil.writeUtf8String(query.toJSONString(), extrasPath + File.separator + "query.json");
  129. }
  130. @Override
  131. public void buildScenePost(BuildSceneResultMqMessage message) throws Exception {
  132. String sceneCode = message.getBuildContext().get("sceneNum").toString();
  133. Long batchId = Long.valueOf(message.getExt().get("batchId").toString());
  134. String path = message.getPath();
  135. Map<String, Object> downParams = new HashMap<>();
  136. downParams.put("sceneCode", sceneCode);
  137. downParams.put("path", path);
  138. try {
  139. // 上传计算日志
  140. //如果是重复计算,没有走到计算逻辑,不需要上传日志文件
  141. log.info("开始上传计算日志");
  142. String buildLogPath = String.format(UploadFilePath.BUILD_LOG_PATH, sceneCode);
  143. fYunFileService.uploadFile(path + File.separator + "console.log", buildLogPath + "console.log");
  144. log.info("计算日志上传完成");
  145. if (!message.getBuildSuccess()) {
  146. log.error("重定位计算报错,修改状态为失败状态");
  147. scenePlusService.update(new LambdaUpdateWrapper<ScenePlus>()
  148. .set(ScenePlus::getSceneStatus, SceneStatus.FAILD.code())
  149. .eq(ScenePlus::getNum, sceneCode));
  150. // 发送钉钉消息,计算失败
  151. return;
  152. }
  153. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(sceneCode);
  154. Map<String, String> uploadFiles = this.getUploadFiles(scenePlus,path);
  155. scenePlus.setPayStatus(PayStatus.PAY.code());
  156. scenePlus.setUpdateTime(new Date());
  157. scenePlus.setSceneStatus(SceneStatus.NO_DISPLAY.code());
  158. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  159. log.info("开始上传场景计算结果数据,num:{}", sceneCode);
  160. //上传文件
  161. fYunFileService.uploadMulFiles(uploadFiles);
  162. //上传caches/images
  163. String ossResultPath = String.format(UploadFilePath.scene_result_data_path, sceneCode);
  164. String localCachesImagePath = path + "/caches/images/";
  165. String ossCachesImagePath = ossResultPath + "caches/images/";
  166. if(FileUtil.exist(localCachesImagePath)){
  167. fYunFileService.uploadFileByCommand(localCachesImagePath, ossCachesImagePath);
  168. }
  169. //容量统计
  170. Long space = commonService.getSpace(sceneCode);
  171. Object[] editInfoArr = commonService.updateEditInfo(scenePlus);
  172. //如果相机容量不足,需要把场景的paystatus改为容量不足状态
  173. scenePlus.setPayStatus(commonService.getPayStatus(scenePlus.getCameraId(), space, new JSONObject()));
  174. this.uploadStatusJson(scenePlus, scenePlusExt);
  175. //目前重定位只能用看见的全景图去做重定位,所以这里固定给4k
  176. scenePlusExt.setSceneResolution(SceneResolution.four_K.code());
  177. scenePlusService.updateById(scenePlus);
  178. scenePlusExtService.updateById(scenePlusExt);
  179. //国际环境需要发邮件通知
  180. if("eur".equals(env) &&
  181. !scenePlus.getSceneSource().equals(SceneSource.JG.code()) &&
  182. !scenePlus.getSceneSource().equals(SceneSource.SG.code())){
  183. commonService.sendEmail(sceneCode, "relocation");
  184. }
  185. //发送消息到点云系统处理
  186. downParams.put("status", CommonSuccessStatus.SUCCESS.code());
  187. log.info("场景重定位计算结果处理结束,场景码:{}", sceneCode);
  188. }catch (Exception e){
  189. log.error("场景重定位计算结果处理出错,num"+sceneCode, e);
  190. downParams.put("status", CommonSuccessStatus.FAIL.code());
  191. throw e;
  192. } finally {
  193. //发送消息到激光系统做处理
  194. mqProducer.sendByWorkQueue("sx-relocation-done", downParams);
  195. //修改重定位记录为退出计算
  196. relocationBatchService.update(new LambdaUpdateWrapper<RelocationBatch>().eq(RelocationBatch::getId, batchId).set(RelocationBatch::getStatus, 2));
  197. }
  198. }
  199. private Map<String, String> getUploadFiles(ScenePlus scenePlus,String path) throws Exception {
  200. if (ObjectUtils.isEmpty(scenePlus)) {
  201. throw new Exception("未找到场景信息:" + path);
  202. }
  203. String projectNum = scenePlus.getNum();
  204. String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, projectNum);
  205. String imagesPath = String.format(UploadFilePath.IMG_VIEW_PATH, projectNum);
  206. String videoPath = String.format(UploadFilePath.VIDEOS_VIEW_PATH, projectNum);
  207. String resultsPath = path + File.separator + "results" + File.separator;
  208. String uploadData = FileUtils.readFile(resultsPath + "upload.json");
  209. JSONArray array = JSONObject.parseObject(uploadData).getJSONArray("upload");
  210. JSONObject fileJson = null;
  211. String fileName = "";
  212. Map<String, String> map = new HashMap();
  213. for (int i = 0; i < array.size(); ++i) {
  214. fileJson = array.getJSONObject(i);
  215. fileName = fileJson.getString("file");
  216. String filePath = resultsPath + fileName;
  217. if (!(new File(filePath)).exists()) {
  218. throw new Exception(filePath + "文件不存在");
  219. }
  220. if ("vision2.txt".equals(fileName)) {
  221. CreateObjUtil.convertTxtToVisionmodeldata(resultsPath + "vision2.txt", resultsPath + "vision2.modeldata");
  222. map.put(resultsPath + "vision2.modeldata", imagesPath + "vision2.modeldata");
  223. map.put(resultsPath + "vision2.txt", imagesPath + "vision2.txt");
  224. }
  225. if (fileJson.getIntValue("clazz") == 2) {
  226. map.put(filePath, imagesPath + ConstantFileName.modelUUID + "_50k_texture_jpg_high1/" + fileName.replace("tex/", ""));
  227. } else if (fileJson.getIntValue("clazz") == 3) {
  228. map.put(filePath, imagesPath + "pan/high/" + fileName.replace("high/", ""));
  229. } else if (fileJson.getIntValue("clazz") == 4) {
  230. map.put(filePath, imagesPath + "pan/low/" + fileName.replace("low/", ""));
  231. } else if (fileJson.getIntValue("clazz") == 5) {
  232. map.put(filePath, imagesPath + fileName);
  233. } else if (fileJson.getIntValue("clazz") == 7) {
  234. map.put(filePath, imagesPath + fileName);
  235. } else if (fileJson.getIntValue("clazz") == 10) {
  236. String updown = FileUtils.readFile(filePath);
  237. JSONObject updownJson = JSONObject.parseObject(updown);
  238. String mappingOssPath = String.format("scene_edit_data/%s/data/", projectNum) + fileName.replace("updown", "mapping");
  239. map.put(filePath, mappingOssPath);
  240. } else {
  241. if (fileJson.getIntValue("clazz") == 11 || fileJson.getIntValue("clazz") == 12) {
  242. map.put(filePath, videoPath + fileName.replace("videos/", ""));
  243. if (fileName.contains(".mp4")) {
  244. map.put(resultsPath + fileName.replace("mp4", "flv"), videoPath + fileName.replace("videos/", "").replace("mp4", "flv"));
  245. }
  246. }
  247. if (fileJson.getIntValue("clazz") == 16) {
  248. map.put(filePath, dataViewPath + fileName);
  249. }
  250. if (fileJson.getIntValue("clazz") == 18) {
  251. map.put(filePath, imagesPath + fileName);
  252. }
  253. }
  254. }
  255. CreateObjUtil.convertTxtToVisionmodeldata(resultsPath + "vision.txt", resultsPath + "vision.modeldata");
  256. map.put(resultsPath + "vision.txt", imagesPath + "vision.txt");
  257. map.put(resultsPath + "vision.modeldata", imagesPath + "vision.modeldata");
  258. return map;
  259. }
  260. private void uploadStatusJson(ScenePlus scenePlus, ScenePlusExt scenePlusExt){
  261. String num = scenePlus.getNum();
  262. String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, num);
  263. Integer status = 1;
  264. // 上传status JSON.
  265. JSONObject statusJson = new JSONObject();
  266. //临时将-2改成1,app还没完全更新
  267. statusJson.put("status", status);
  268. statusJson.put("webSite", scenePlusExt.getWebSite());
  269. statusJson.put("sceneNum", num);
  270. statusJson.put("thumb", scenePlusExt.getThumb());
  271. statusJson.put("payStatus", scenePlus.getPayStatus());
  272. statusJson.put("sceneScheme", scenePlusExt.getSceneScheme());
  273. FileUtils.writeFile(ConstantFilePath.SCENE_PATH + "data/data" + num + File.separator + "status.json", statusJson.toString());
  274. fYunFileService.uploadFile(statusJson.toJSONString().getBytes(StandardCharsets.UTF_8), dataViewPath + "status.json");
  275. }
  276. }