BuildSxRelocationServiceImpl.java 15 KB

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