BuildSxRelocationServiceImpl.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. package com.fdkankan.contro.mq.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  7. import com.fdkankan.common.constant.*;
  8. import com.fdkankan.common.util.FileUtils;
  9. import com.fdkankan.contro.entity.*;
  10. import com.fdkankan.contro.mq.service.IBuildSceneService;
  11. import com.fdkankan.contro.service.*;
  12. import com.fdkankan.fyun.config.FYunFileConfig;
  13. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  14. import com.fdkankan.model.constants.ConstantFileName;
  15. import com.fdkankan.model.constants.ConstantFilePath;
  16. import com.fdkankan.model.constants.UploadFilePath;
  17. import com.fdkankan.model.enums.ModelTypeEnums;
  18. import com.fdkankan.model.utils.CreateObjUtil;
  19. import com.fdkankan.model.utils.SceneUtil;
  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.*;
  33. /**
  34. * <p>
  35. * TODO
  36. * </p>
  37. *
  38. * @author dengsixing
  39. * @since 2022/4/20
  40. **/
  41. @Slf4j
  42. @Service
  43. @RefreshScope
  44. public class BuildSxRelocationServiceImpl implements IBuildSceneService {
  45. @Value("${queue.modeling.modeling-call}")
  46. private String queueModelingCall;
  47. @Value("${queue.modeling.sx-relocation-post:sx-relocation-post}")
  48. private String queueModelingPost;
  49. @Value("${model.type:#{null}}")
  50. private String modelType;
  51. @Value("${env:gn}")
  52. private String env;
  53. @Value("#{'${build.scene.post.not-delete-nas-nums:}'.split(',')}")
  54. private List<String> notDeleteNasNumList;
  55. @Autowired
  56. private RabbitMqProducer mqProducer;
  57. @Resource
  58. private FYunFileServiceInterface fYunFileService;
  59. @Autowired
  60. private FYunFileConfig fYunFileConfig;
  61. @Autowired
  62. private IScenePlusService scenePlusService;
  63. @Autowired
  64. private IScenePlusExtService scenePlusExtService;
  65. @Autowired
  66. private IBuildSceneDTService buildSceneDTService;
  67. @Autowired
  68. private ICommonService commonService;
  69. @Autowired
  70. private ISceneColdStorageService sceneColdStorageService;
  71. @Autowired
  72. private IntermitSceneService intermitSceneService;
  73. @Autowired
  74. private IBuildService buildService;
  75. @Override
  76. public void buildScenePre(BuildSceneCallMessage message) throws Exception{
  77. String num = message.getSceneNum();
  78. ScenePlus scenePlusByNum = scenePlusService.getScenePlusByNum(num);
  79. Map<String, Object> ext = message.getExt();
  80. try {
  81. //重新计算时需要删除文件夹,否知使用缓存
  82. if(new File(message.getPath() + File.separator + "results").exists()){
  83. FileUtils.deleteDirectory(message.getPath() + File.separator + "results");
  84. }
  85. //由于刘强说caches会影响计算结果,所以这里删除caches
  86. if(new File(message.getPath() + File.separator + "caches").exists()){
  87. FileUtils.deleteDirectory(message.getPath() + File.separator + "caches");
  88. }
  89. message.setResultReceiverMqName(queueModelingPost);
  90. ScenePlusExt scenePlusExtByPlusId = scenePlusExtService.getScenePlusExtByPlusId(scenePlusByNum.getId());
  91. String dataFdageKey = SceneUtil.getHomePath(scenePlusExtByPlusId.getDataSource()) + "data.fdage";
  92. JSONObject jsonObject = JSON.parseObject(fYunFileService.getFileContent(dataFdageKey));
  93. if (!ObjectUtils.isEmpty(modelType)) {
  94. // 修改dataFdage文件
  95. jsonObject.put("modelType", modelType);
  96. }
  97. Map<String, String> dataMap = buildService.getTypeString(message.getCameraType(), message.getAlgorithm(), message.getResolution(),jsonObject);
  98. dataMap.put("splitType", "SPLIT_V31");
  99. buildService.writeDataJson(message, jsonObject, dataMap, null);
  100. }catch (Exception e){
  101. scenePlusByNum.setSceneStatus(SceneStatus.FAILD.code());
  102. scenePlusService.updateById(scenePlusByNum);
  103. buildSceneDTService.handBaseFail("深巡场景重定位资源准备异常!", message.getPath(), message.getSceneNum(), "计算控制服务器");
  104. throw e;
  105. }
  106. }
  107. private String getOssPath(String path) {
  108. String ossPath = ConstantFilePath.OSS_PREFIX
  109. + path.replace(ConstantFilePath.BUILD_MODEL_PATH, "")
  110. .replace(ConstantFilePath.BUILD_MODEL_LASER_PATH, "");
  111. if (!ossPath.endsWith("/")) {
  112. ossPath = ossPath.concat("/");
  113. }
  114. return ossPath;
  115. }
  116. @Override
  117. public void downLoadSource(BuildSceneCallMessage buildSceneMqMessage,String path){
  118. String ossPath = getOssPath(path);
  119. fYunFileService.downloadFileByCommand(path + File.separator + "capture", ossPath);
  120. }
  121. @Override
  122. public void buildScenePost(BuildSceneResultMqMessage message) throws Exception {
  123. String sceneCode = message.getBuildContext().get("sceneNum").toString();
  124. String path = message.getPath();
  125. Map<String, Object> downParams = new HashMap<>();
  126. downParams.put("sceneCode", sceneCode);
  127. downParams.put("path", path);
  128. try {
  129. // 上传计算日志
  130. //如果是重复计算,没有走到计算逻辑,不需要上传日志文件
  131. log.info("开始上传计算日志");
  132. String buildLogPath = String.format(UploadFilePath.BUILD_LOG_PATH, sceneCode);
  133. fYunFileService.uploadFile(path + File.separator + "console.log", buildLogPath + "console.log");
  134. log.info("计算日志上传完成");
  135. if (!message.getBuildSuccess()) {
  136. log.error("建模失败,修改状态为失败状态");
  137. scenePlusService.update(new LambdaUpdateWrapper<ScenePlus>()
  138. .set(ScenePlus::getSceneStatus, SceneStatus.FAILD.code())
  139. .eq(ScenePlus::getNum, sceneCode));
  140. // 发送钉钉消息,计算失败
  141. buildSceneDTService.handModelFail("深巡场景重定向计算失败", message.getPath(), sceneCode, message.getHostName());
  142. return;
  143. }
  144. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(sceneCode);
  145. Map<String, String> uploadFiles = this.getUploadFiles(scenePlus,path);
  146. scenePlus.setPayStatus(PayStatus.PAY.code());
  147. scenePlus.setUpdateTime(new Date());
  148. scenePlus.setSceneStatus(SceneStatus.NO_DISPLAY.code());
  149. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  150. log.info("开始上传场景计算结果数据,num:{}", sceneCode);
  151. //上传文件
  152. fYunFileService.uploadMulFiles(uploadFiles);
  153. //容量统计
  154. Long space = commonService.getSpace(sceneCode);
  155. Object[] editInfoArr = commonService.updateEditInfo(scenePlus);
  156. //如果相机容量不足,需要把场景的paystatus改为容量不足状态
  157. scenePlus.setPayStatus(commonService.getPayStatus(scenePlus.getCameraId(), space, new JSONObject()));
  158. this.uploadStatusJson(scenePlus, scenePlusExt);
  159. scenePlusService.updateById(scenePlus);
  160. scenePlusExtService.updateById(scenePlusExt);
  161. //国际环境需要发邮件通知
  162. if("eur".equals(env) &&
  163. !scenePlus.getSceneSource().equals(SceneSource.JG.code()) &&
  164. !scenePlus.getSceneSource().equals(SceneSource.SG.code())){
  165. commonService.sendEmail(sceneCode, "relocation");
  166. }
  167. //发送消息到点云系统处理
  168. downParams.put("status", CommonSuccessStatus.SUCCESS.code());
  169. log.info("场景计算结果处理结束,场景码:{}", sceneCode);
  170. }catch (Exception e){
  171. log.error("场景计算结果处理出错,num"+sceneCode, e);
  172. downParams.put("status", CommonSuccessStatus.FAIL.code());
  173. buildSceneDTService.handBaseFail("场景计算结果处理出错!", message.getPath(), sceneCode, "计算控制服务器");
  174. throw e;
  175. } finally {
  176. //发送消息到激光系统做处理
  177. mqProducer.sendByWorkQueue("sx-relocation-done", downParams);
  178. }
  179. }
  180. private Map<String, String> getUploadFiles(ScenePlus scenePlus,String path) throws Exception {
  181. if (ObjectUtils.isEmpty(scenePlus)) {
  182. throw new Exception("未找到场景信息:" + path);
  183. }
  184. String projectNum = scenePlus.getNum();
  185. String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, projectNum);
  186. String imagesPath = String.format(UploadFilePath.IMG_VIEW_PATH, projectNum);
  187. String videoPath = String.format(UploadFilePath.VIDEOS_VIEW_PATH, projectNum);
  188. String resultsPath = path + File.separator + "results" + File.separator;
  189. String uploadData = FileUtils.readFile(resultsPath + "upload.json");
  190. JSONArray array = JSONObject.parseObject(uploadData).getJSONArray("upload");
  191. JSONObject fileJson = null;
  192. String fileName = "";
  193. Map<String, String> map = new HashMap();
  194. for (int i = 0; i < array.size(); ++i) {
  195. fileJson = array.getJSONObject(i);
  196. fileName = fileJson.getString("file");
  197. String filePath = resultsPath + fileName;
  198. if (!(new File(filePath)).exists()) {
  199. throw new Exception(filePath + "文件不存在");
  200. }
  201. if ("vision2.txt".equals(fileName)) {
  202. CreateObjUtil.convertTxtToVisionmodeldata(resultsPath + "vision2.txt", resultsPath + "vision2.modeldata");
  203. map.put(resultsPath + "vision2.modeldata", imagesPath + "vision2.modeldata");
  204. map.put(resultsPath + "vision2.txt", imagesPath + "vision2.txt");
  205. }
  206. if (fileJson.getIntValue("clazz") == 2) {
  207. map.put(filePath, imagesPath + ConstantFileName.modelUUID + "_50k_texture_jpg_high1/" + fileName.replace("tex/", ""));
  208. } else if (fileJson.getIntValue("clazz") == 3) {
  209. map.put(filePath, imagesPath + "pan/high/" + fileName.replace("high/", ""));
  210. } else if (fileJson.getIntValue("clazz") == 4) {
  211. map.put(filePath, imagesPath + "pan/low/" + fileName.replace("low/", ""));
  212. } else if (fileJson.getIntValue("clazz") == 5) {
  213. map.put(filePath, imagesPath + fileName);
  214. } else if (fileJson.getIntValue("clazz") == 7) {
  215. map.put(filePath, imagesPath + fileName);
  216. } else if (fileJson.getIntValue("clazz") == 10) {
  217. String updown = FileUtils.readFile(filePath);
  218. JSONObject updownJson = JSONObject.parseObject(updown);
  219. String mappingOssPath = String.format("scene_edit_data/%s/data/", projectNum) + fileName.replace("updown", "mapping");
  220. map.put(filePath, mappingOssPath);
  221. } else {
  222. if (fileJson.getIntValue("clazz") == 11 || fileJson.getIntValue("clazz") == 12) {
  223. map.put(filePath, videoPath + fileName.replace("videos/", ""));
  224. if (fileName.contains(".mp4")) {
  225. map.put(resultsPath + fileName.replace("mp4", "flv"), videoPath + fileName.replace("videos/", "").replace("mp4", "flv"));
  226. }
  227. }
  228. if (fileJson.getIntValue("clazz") == 16) {
  229. map.put(filePath, dataViewPath + fileName);
  230. }
  231. if (fileJson.getIntValue("clazz") == 18) {
  232. map.put(filePath, imagesPath + fileName);
  233. }
  234. }
  235. }
  236. CreateObjUtil.convertTxtToVisionmodeldata(resultsPath + "vision.txt", resultsPath + "vision.modeldata");
  237. map.put(resultsPath + "vision.txt", imagesPath + "vision.txt");
  238. map.put(resultsPath + "vision.modeldata", imagesPath + "vision.modeldata");
  239. return map;
  240. }
  241. private void uploadStatusJson(ScenePlus scenePlus, ScenePlusExt scenePlusExt){
  242. String num = scenePlus.getNum();
  243. String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, num);
  244. Integer status = 1;
  245. // 上传status JSON.
  246. JSONObject statusJson = new JSONObject();
  247. //临时将-2改成1,app还没完全更新
  248. statusJson.put("status", status);
  249. statusJson.put("webSite", scenePlusExt.getWebSite());
  250. statusJson.put("sceneNum", num);
  251. statusJson.put("thumb", scenePlusExt.getThumb());
  252. statusJson.put("payStatus", scenePlus.getPayStatus());
  253. statusJson.put("sceneScheme", scenePlusExt.getSceneScheme());
  254. FileUtils.writeFile(ConstantFilePath.SCENE_PATH + "data/data" + num + File.separator + "status.json", statusJson.toString());
  255. fYunFileService.uploadFile(statusJson.toJSONString().getBytes(StandardCharsets.UTF_8), dataViewPath + "status.json");
  256. }
  257. private void updateDbPlus(int sceneSource,Long space,String videosJson, Long computeTime,boolean isObj,ScenePlusExt scenePlusExt){
  258. scenePlusExt.setSpace(space);
  259. scenePlusExt.setComputeTime(computeTime);
  260. scenePlusExt.setAlgorithmTime(new Date());
  261. scenePlusExt.setVideos(videosJson);
  262. scenePlusExt.setIsObj(isObj ? 1 : 0);
  263. if(ModelTypeEnums.TILE_CODE.equals(modelType)){
  264. scenePlusExt.setSceneScheme(3);
  265. }
  266. switch (SceneSource.get(sceneSource)){
  267. case BM:
  268. scenePlusExt.setSceneResolution(SceneResolution.two_K.code());
  269. scenePlusExt.setSceneFrom(SceneFrom.PRO.code());
  270. break;
  271. case SM:
  272. scenePlusExt.setSceneResolution(SceneResolution.one_k.code());
  273. scenePlusExt.setSceneFrom(SceneFrom.LITE.code());
  274. break;
  275. case ZT:
  276. scenePlusExt.setSceneResolution(SceneResolution.four_K.code());
  277. scenePlusExt.setSceneFrom(SceneFrom.MINION.code());
  278. break;
  279. case JG:
  280. scenePlusExt.setSceneResolution(SceneResolution.four_K.code());
  281. scenePlusExt.setSceneFrom(SceneFrom.LASER.code());
  282. break;
  283. case SG:
  284. scenePlusExt.setSceneResolution(SceneResolution.four_K.code());
  285. scenePlusExt.setSceneFrom(SceneFrom.LASER.code());
  286. break;
  287. }
  288. String sceneKind = scenePlusExt.getSceneScheme() == 3 ? SceneKind.FACE.code():SceneKind.TILES.code();
  289. scenePlusExt.setSceneKind(sceneKind);
  290. // scenePlusExt.setModelKind(modelKind);
  291. //统计点位数量
  292. scenePlusExt.setShootCount(this.getShootCount(scenePlusExt));
  293. scenePlusExtService.updateById(scenePlusExt);
  294. }
  295. private Integer getShootCount(ScenePlusExt scenePlusExt){
  296. Integer shootCount = null;
  297. String homePath = SceneUtil.getHomePath(scenePlusExt.getDataSource());
  298. JSONObject dataFdageObj = JSON.parseObject(fYunFileService.getFileContent(homePath.concat("data.fdage")));
  299. if(Objects.nonNull(dataFdageObj)){
  300. JSONArray points = dataFdageObj.getJSONArray("points");
  301. if(CollUtil.isNotEmpty(points)){
  302. shootCount = points.size();
  303. }
  304. }
  305. if(Objects.nonNull(shootCount) && shootCount > 0){
  306. return shootCount;
  307. }
  308. String slamDataStr = fYunFileService.getFileContent(homePath.concat("slam_data.json"));
  309. JSONObject slamDataObj = JSON.parseObject(slamDataStr);
  310. if(Objects.nonNull(slamDataObj)){
  311. JSONArray viewsInfo = slamDataObj.getJSONArray("views_info");
  312. if(CollUtil.isNotEmpty(viewsInfo)){
  313. shootCount = viewsInfo.stream().mapToInt(info -> {
  314. return ((JSONObject) info).getJSONArray("list_pose").size();
  315. }).sum();
  316. }
  317. }
  318. return shootCount;
  319. }
  320. }