BuildSceneServiceImpl.java 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. package com.fdkankan.contro.mq.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.CollUtil;
  4. import cn.hutool.core.io.FileUtil;
  5. import cn.hutool.core.io.file.FileNameUtil;
  6. import cn.hutool.core.util.StrUtil;
  7. import cn.hutool.core.util.ZipUtil;
  8. import com.alibaba.fastjson.JSON;
  9. import com.alibaba.fastjson.JSONArray;
  10. import com.alibaba.fastjson.JSONObject;
  11. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  12. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  13. import com.fdkankan.common.constant.*;
  14. import com.fdkankan.common.util.FileUtils;
  15. import com.fdkankan.contro.bean.SceneJsonBean;
  16. import com.fdkankan.contro.entity.*;
  17. import com.fdkankan.contro.mq.service.IBuildSceneService;
  18. import com.fdkankan.contro.service.*;
  19. import com.fdkankan.contro.service.impl.FdkkV4Service;
  20. import com.fdkankan.contro.vo.SceneEditControlsVO;
  21. import com.fdkankan.fyun.config.FYunFileConfig;
  22. import com.fdkankan.fyun.constant.FYunTypeEnum;
  23. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  24. import com.fdkankan.image.MatrixToImageWriterUtil;
  25. import com.fdkankan.model.constants.ConstantFileName;
  26. import com.fdkankan.model.constants.ConstantFilePath;
  27. import com.fdkankan.model.constants.UploadFilePath;
  28. import com.fdkankan.model.utils.CreateHouseJsonUtil;
  29. import com.fdkankan.model.utils.CreateObjUtil;
  30. import com.fdkankan.model.utils.SceneUtil;
  31. import com.fdkankan.push.config.PushMessageConfig;
  32. import com.fdkankan.push.utils.PushMsgUtil;
  33. import com.fdkankan.rabbitmq.bean.BuildSceneCallMessage;
  34. import com.fdkankan.rabbitmq.bean.BuildSceneResultMqMessage;
  35. import com.fdkankan.rabbitmq.util.RabbitMqProducer;
  36. import com.fdkankan.redis.constant.RedisKey;
  37. import com.fdkankan.redis.util.RedisUtil;
  38. import lombok.extern.slf4j.Slf4j;
  39. import org.apache.commons.lang3.ObjectUtils;
  40. import org.apache.commons.lang3.StringUtils;
  41. import org.springframework.beans.factory.annotation.Autowired;
  42. import org.springframework.beans.factory.annotation.Value;
  43. import org.springframework.stereotype.Service;
  44. import java.io.File;
  45. import java.io.IOException;
  46. import java.util.*;
  47. import java.util.Map.Entry;
  48. import java.util.stream.Collectors;
  49. /**
  50. * <p>
  51. * TODO
  52. * </p>
  53. *
  54. * @author dengsixing
  55. * @since 2022/4/20
  56. **/
  57. @Slf4j
  58. @Service
  59. public class BuildSceneServiceImpl implements IBuildSceneService {
  60. @Value("${main.url}")
  61. private String mainUrl;
  62. @Value("${scene.pro.new.url}")
  63. private String sceneProNewUrl;
  64. @Value("${queue.modeling.modeling-call}")
  65. private String queueModelingCall;
  66. @Autowired
  67. private RabbitMqProducer mqProducer;
  68. @Autowired
  69. private FYunFileServiceInterface fYunFileService;
  70. @Autowired
  71. private ISceneFileBuildService sceneFileBuildService;
  72. @Autowired
  73. private ICameraDetailService cameraDetailService;
  74. @Autowired
  75. private ISceneEditInfoService sceneEditInfoService;
  76. @Autowired
  77. private ISceneEditControlsService sceneEditControlsService;
  78. @Autowired
  79. private FYunFileConfig fYunFileConfig;
  80. @Autowired
  81. private RedisUtil redisUtil;
  82. @Autowired
  83. private IScenePlusService scenePlusService;
  84. @Autowired
  85. private IScenePlusExtService scenePlusExtService;
  86. @Autowired
  87. private ISceneEditInfoExtService sceneEditInfoExtService;
  88. @Autowired
  89. private IUserIncrementService userIncrementService;
  90. @Autowired
  91. private IFdkkLaserService fdkkLaserService;
  92. @Autowired
  93. private FdkkV4Service fdkkV4Service;
  94. @Override
  95. public void buildScenePre(BuildSceneCallMessage message) {
  96. boolean success = false;
  97. try {
  98. //根据相机类型,组装资源路径
  99. String path = SceneUtil.getPath(message.getPath(), message.getCameraName(),
  100. message.getFileId(), Integer.parseInt(message.getCameraType()), message.getUnicode());
  101. //下载资源到本地
  102. this.downLoadSource(message, path);
  103. //发送mq,就进行计算
  104. mqProducer.sendByWorkQueue(queueModelingCall, message);
  105. success = true;
  106. log.info("场景计算资源准备结束,场景码:{}", message.getSceneNum());
  107. }catch (Exception e){
  108. log.error("场景计算前置处理出错", e);
  109. }finally {
  110. //如果前置处理失败,发送钉钉消息
  111. if(!success){
  112. String serverPath = message.getPath().substring(0,message.getPath().lastIndexOf("/")+1).concat(message.getPrefix());
  113. // buildSceneDTService.handFail("场景计算前置处理出错", serverPath, message.getSceneNum(), BuildScenePreServiceImpl.hostName, null);
  114. }
  115. }
  116. }
  117. @Override
  118. public void downLoadSource(BuildSceneCallMessage buildSceneMqMessage,String path) throws Exception{
  119. String cameraName = buildSceneMqMessage.getCameraName();
  120. String fileId = buildSceneMqMessage.getFileId();
  121. String unicode = buildSceneMqMessage.getUnicode();
  122. int cameraType = Integer.parseInt(buildSceneMqMessage.getCameraType());
  123. String prefix = buildSceneMqMessage.getPrefix();
  124. String imgsName = buildSceneMqMessage.getImgsName();
  125. if(cameraType < 3){
  126. for(int i = 0;i<5;++i){
  127. try{
  128. FileUtils.downLoadFromUrl(prefix+imgsName+"?m="+new Date().getTime(), imgsName,
  129. path + File.separator + "capture");
  130. FileUtils.decompress(path + File.separator + "capture" +File.separator+imgsName,
  131. path + File.separator + "capture") ;
  132. break;
  133. }catch(Exception e){
  134. log.error(String.format("第%d次下载并解压资源失败", i+1), e);
  135. if(i<4)
  136. {
  137. FileUtils.deleteFile(path + File.separator + "capture" +File.separator+imgsName);
  138. FileUtils.delFolder(path + File.separator + "capture" +File.separator+"images");
  139. }
  140. Thread.sleep(10000);
  141. }
  142. }
  143. FileUtils.deleteFile(path + File.separator + "capture" +File.separator+"zip.Zip");
  144. }else{
  145. String ossPath = ConstantFilePath.OSS_PREFIX + cameraName.replace("4DKKPRO_", "")
  146. .replace("-fdage", "").toLowerCase() + File.separator + fileId + File.separator
  147. + unicode + File.separator;
  148. if(cameraType == 5 || cameraType == 6){
  149. path = ConstantFilePath.BUILD_MODEL_PATH + unicode;
  150. //下载zip包
  151. FileUtils.downLoadFromUrl(prefix + "/" + imgsName + "?m=" + System.currentTimeMillis(), imgsName, path + File.separator + "capture");
  152. //解压
  153. FileUtils.decompress(path + File.separator + "capture" + File.separator + imgsName,
  154. path + File.separator + "capture") ;
  155. //删除压缩包
  156. FileUtils.delFile(path + File.separator + "capture" + File.separator + imgsName);
  157. }else if(cameraType == 14) {
  158. fYunFileService.downloadFileByCommand(path + File.separator + "capture", ossPath);
  159. } else{
  160. fYunFileService.downloadFileByCommand(path + File.separator + "capture", ossPath);
  161. }
  162. }
  163. }
  164. @Override
  165. public void buildScenePost(BuildSceneResultMqMessage message) throws Exception {
  166. Integer cameraType = Integer.parseInt(message.getCameraType());
  167. String sceneCode = message.getSceneCode();
  168. String fileId = message.getFileId();
  169. Integer payStatus = message.getPayStatus();
  170. Long computeTime = message.getComputeTime();
  171. String path = message.getPath();
  172. Boolean buildSuccess = message.getBuildSuccess();
  173. JSONObject fdageData = getFdageData(path + File.separator + "capture" +File.separator+"data.fdage");
  174. Integer videoVersion = fdageData.getInteger("videoVersion");
  175. Integer pushChannel = message.getPushChannel();
  176. String pushToken = message.getPushToken();
  177. String prefix = message.getPrefix();
  178. try {
  179. // 上传计算日志
  180. //如果是重复计算,没有走到计算逻辑,不需要上传日志文件
  181. log.info("开始上传计算日志");
  182. String buildLogPath = String.format(UploadFilePath.BUILD_LOG_PATH, sceneCode);
  183. fYunFileService.uploadFile(path + File.separator + "console.log", buildLogPath + "console.log");
  184. log.info("计算日志上传完成");
  185. if (!buildSuccess) {
  186. log.error("建模失败,修改状态为失败状态");
  187. scenePlusService.update(new LambdaUpdateWrapper<ScenePlus>()
  188. .set(ScenePlus::getSceneStatus, SceneStatus.FAILD.code())
  189. .eq(ScenePlus::getNum, sceneCode));
  190. SceneFileBuild sceneFileBuild = sceneFileBuildService.findByFileId(fileId);
  191. if (Objects.nonNull(sceneFileBuild)) {
  192. sceneFileBuild.setBuildStatus(BuildStatus.fail.code());
  193. sceneFileBuildService.updateById(sceneFileBuild);
  194. }
  195. throw new RuntimeException("建模失败!");
  196. }
  197. Map<String, String> uploadFiles = getUploadFiles(path);
  198. //建模成功走以下逻辑
  199. log.info("cameraType:{}",cameraType);
  200. //计算场景消耗磁盘空间
  201. long space = this.calUseSpace(uploadFiles);
  202. if(cameraType < 3){
  203. this.updateDb4Sm(sceneCode, space);
  204. fYunFileService.uploadMulFiles(uploadFiles);
  205. return;
  206. }
  207. //读取计算结果文件生成videosJson
  208. JSONObject videosJson = this.getVideosJson(path, videoVersion, sceneCode, cameraType);
  209. //写入数据库
  210. ScenePlus scenePlus = this.updateDbPlus(sceneCode, space, payStatus, videosJson.toJSONString(), computeTime, fileId,fdageData.containsKey("exportMeshObj") && fdageData.getIntValue("exportMeshObj") == 1);
  211. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  212. //变更容量,arrearCap为true时,代表容量不足
  213. boolean arrearCap = this.updateSpace(scenePlus.getCameraId(), space);
  214. //如果相机容量不足,需要把场景的paystatus改为容量不足状态
  215. this.sealScene(arrearCap, scenePlus.getId(), scenePlus.getId());
  216. Object[] editInfoArr = this.updateEditInfo(scenePlus);
  217. SceneEditInfo sceneEditInfo = (SceneEditInfo)editInfoArr[0];
  218. SceneEditInfoExt sceneEditInfoExt = (SceneEditInfoExt)editInfoArr[1];
  219. SceneEditControls sceneEditControls = (SceneEditControls)editInfoArr[2];
  220. String sceneUrl = mainUrl + "/" + sceneProNewUrl;
  221. String outPathZh = ConstantFilePath.BASE_PATH + File.separator + "sceneQRcode/"+sceneCode+".png";
  222. String outPathEn = ConstantFilePath.BASE_PATH + File.separator + "sceneQRcode/"+sceneCode+"_en.png";
  223. //生成新的分享的二维码-中文版本
  224. MatrixToImageWriterUtil.createQRCode(sceneUrl + sceneCode, outPathZh, false,null);
  225. //生成新的分享的二维码-英文版本
  226. MatrixToImageWriterUtil.createQRCode(sceneUrl + sceneCode + "&lang=en", outPathEn, false, null);
  227. //上传二维码
  228. fYunFileService.uploadFile(outPathZh, String.format(UploadFilePath.DOWNLOADS_QRCODE, sceneCode) + sceneCode + ".png");
  229. fYunFileService.uploadFile(outPathEn, String.format(UploadFilePath.DOWNLOADS_QRCODE, sceneCode) + sceneCode + "_en.png");
  230. //上传全景图俯视图
  231. this.uploadFloorCad(path, sceneCode, uploadFiles);
  232. //上传文件
  233. log.info("开始上传场景计算结果数据,num:{}", sceneCode);
  234. fYunFileService.uploadMulFiles(uploadFiles);
  235. Map<String,String> damFileHeaders = new HashMap<>();
  236. damFileHeaders.put("Content-Encoding","gzip");
  237. if (!fYunFileService.getFyunType().equals(FYunTypeEnum.LOCAL.code())) {
  238. // dam 文件设置请求头
  239. uploadFiles.entrySet().stream().filter(entry -> FileNameUtil.extName(entry.getKey()).equals("dam"))
  240. .forEach(entry -> {
  241. // gzip压缩
  242. FileUtil.writeBytes(ZipUtil.gzip(new File(entry.getKey())), entry.getKey() + ".gzip");
  243. // 重命名
  244. FileUtil.rename(new File(entry.getKey() + ".gzip"), entry.getKey(), true);
  245. fYunFileService.uploadFile(entry.getKey(), entry.getValue(), damFileHeaders);
  246. });
  247. }
  248. //拷贝部分文件到编辑目录,用于用户编辑
  249. this.copyToEditDir(sceneCode);
  250. //生成houseTypejson并上传
  251. uploadFiles.entrySet().stream().filter(entry-> FileNameUtil.getName(entry.getKey()).equals("floorplan_cad.json"))
  252. .forEach(entry-> uploadHouseTypeJson(sceneCode,entry.getKey()));
  253. //写scene.json
  254. log.info("生成scene.json上传oss并设置缓存,num:{}", sceneCode);
  255. this.writeSceneJson(sceneCode, videosJson,sceneEditInfo, sceneEditInfoExt, sceneEditControls, scenePlus,
  256. scenePlusExt, arrearCap);
  257. //计算成功,通知APP
  258. this.pushMsgToApp(pushChannel,pushToken, cameraType, scenePlus.getTitle(), scenePlusExt.getWebSite());
  259. if (cameraType == 14) {
  260. //计算成功 激光转台相机 同步 请求
  261. fdkkLaserService.syncBuildResult(scenePlus, scenePlusExt);
  262. }
  263. CreateObjUtil.deleteFile(path.replace(ConstantFilePath.BUILD_MODEL_PATH, "/") + "/capture");
  264. log.info("场景计算结果处理结束,场景码:{}", message.getSceneCode());
  265. }catch (Exception e){
  266. String errorMsg = "场景计算结果处理出错!";
  267. log.error(errorMsg);
  268. e.printStackTrace();
  269. if(!buildSuccess){
  270. errorMsg = "场景构建失败!";
  271. }
  272. //发送钉钉
  273. String serverPath = message.getPath().substring(0,message.getPath().lastIndexOf("/")+1).concat(prefix);
  274. // buildSceneDTService.handFail(errorMsg, serverPath, sceneCode, BuildScenePreServiceImpl.hostName, BuildScenePreServiceImpl.contentExt);
  275. }
  276. }
  277. private Map<String, String> getUploadFiles(String path) throws Exception {
  278. ScenePlus scenePlus = scenePlusService.getByFileId(path);
  279. if (ObjectUtils.isEmpty(scenePlus)) {
  280. throw new Exception("未找到场景信息:" + path);
  281. }
  282. String projectNum = scenePlus.getNum();
  283. String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, projectNum);
  284. String imagesPath = String.format(UploadFilePath.IMG_VIEW_PATH, projectNum);
  285. String videoPath = String.format(UploadFilePath.VIDEOS_VIEW_PATH, projectNum);
  286. String resultsPath = path + File.separator + "results" + File.separator;
  287. String uploadJsonPath = resultsPath + "upload.json";
  288. String uploadData = FileUtils.readFile(uploadJsonPath);
  289. log.info("upload.json 文件路径:{}, 内容:{}", uploadJsonPath, uploadData);
  290. JSONArray array = JSONObject.parseObject(uploadData).getJSONArray("upload");
  291. JSONObject fileJson = null;
  292. String fileName = "";
  293. Map<String, String> map = new HashMap();
  294. for (int i = 0; i < array.size(); ++i) {
  295. fileJson = array.getJSONObject(i);
  296. fileName = fileJson.getString("file");
  297. String filePath = resultsPath + fileName;
  298. if (!(new File(filePath)).exists()) {
  299. throw new Exception(filePath + "文件不存在");
  300. }
  301. if ("vision2.txt".equals(fileName)) {
  302. CreateObjUtil.convertTxtToVisionmodeldata(resultsPath + "vision2.txt", resultsPath + "vision2.modeldata");
  303. map.put(resultsPath + "vision2.modeldata", imagesPath + "vision2.modeldata");
  304. map.put(resultsPath + "vision2.txt", imagesPath + "vision2.txt");
  305. }
  306. if (fileJson.getIntValue("clazz") == 2) {
  307. map.put(filePath, imagesPath + ConstantFileName.modelUUID + "_50k_texture_jpg_high1/" + fileName.replace("tex/", ""));
  308. } else if (fileJson.getIntValue("clazz") == 3) {
  309. map.put(filePath, imagesPath + "pan/high/" + fileName.replace("high/", ""));
  310. } else if (fileJson.getIntValue("clazz") == 4) {
  311. map.put(filePath, imagesPath + "pan/low/" + fileName.replace("low/", ""));
  312. } else if (fileJson.getIntValue("clazz") == 5) {
  313. map.put(filePath, imagesPath + fileName);
  314. } else if (fileJson.getIntValue("clazz") == 7) {
  315. map.put(filePath, imagesPath + fileName);
  316. } else if (fileJson.getIntValue("clazz") == 10) {
  317. String updown = FileUtils.readFile(filePath);
  318. JSONObject updownJson = JSONObject.parseObject(updown);
  319. String mappingOssPath = String.format("scene_edit_data/%s/data/", projectNum) + fileName.replace("updown", "mapping");
  320. map.put(filePath, mappingOssPath);
  321. } else {
  322. if (fileJson.getIntValue("clazz") == 11 || fileJson.getIntValue("clazz") == 12) {
  323. map.put(filePath, videoPath + fileName.replace("videos/", ""));
  324. if (fileName.contains(".mp4")) {
  325. map.put(resultsPath + fileName.replace("mp4", "flv"), videoPath + fileName.replace("videos/", "").replace("mp4", "flv"));
  326. }
  327. }
  328. if (fileJson.getIntValue("clazz") == 16) {
  329. map.put(filePath, dataViewPath + fileName);
  330. }
  331. if (fileJson.getIntValue("clazz") == 18) {
  332. map.put(filePath, imagesPath + fileName);
  333. }
  334. }
  335. }
  336. CreateObjUtil.convertTxtToDam(resultsPath + "tex" + File.separator + "modeldata.txt", resultsPath + ConstantFileName.modelUUID + "_50k.dam");
  337. CreateObjUtil.convertDamToLzma(resultsPath);
  338. CreateObjUtil.convertTxtToDam(resultsPath + "tex" + File.separator + "modeldata.txt", resultsPath + ConstantFileName.modelUUID + "_50k.dam");
  339. CreateObjUtil.convertTxtToVisionmodeldata(resultsPath + "vision.txt", resultsPath + "vision.modeldata");
  340. map.put(resultsPath + "vision.txt", imagesPath + "vision.txt");
  341. map.put(resultsPath + "vision.modeldata", imagesPath + "vision.modeldata");
  342. log.info("数据转换完成:" + projectNum);
  343. File file = new File(resultsPath + ConstantFileName.modelUUID + "_50k.dam.lzma");
  344. while (!file.exists()) {
  345. Thread.sleep(60000L);
  346. }
  347. map.put(resultsPath + ConstantFileName.modelUUID + "_50k.dam.lzma", imagesPath + ConstantFileName.modelUUID + "_50k.dam.lzma");
  348. map.put(resultsPath + ConstantFileName.modelUUID + "_50k.dam", imagesPath + ConstantFileName.modelUUID + "_50k.dam");
  349. FileUtil.touch("/mnt/4Dkankan/scene/data" + File.separator + "data" + projectNum);
  350. map.put(resultsPath + "floorplan.json", dataViewPath + "floor.json");
  351. map.put(resultsPath + "floorplan_cad.json", dataViewPath + "floorplan_cad.json");
  352. map.put(path + File.separator + "capture/stitch_params.txt", dataViewPath + "stitch_params.txt");
  353. map.put(path + File.separator + "capture/Up.xml", dataViewPath + "Up.xml");
  354. map.put(path + File.separator + "capture/Up2.xml", dataViewPath + "Up2.xml");
  355. map.put(path + File.separator + "capture/Up.txt", dataViewPath + "Up.txt");
  356. map.put(path + File.separator + "capture/Up2.txt", dataViewPath + "Up2.txt");
  357. return map;
  358. }
  359. private JSONObject getFdageData(String dataFdagePath) {
  360. log.info("dataFdagePath 文件路径 :{}", dataFdagePath);
  361. String data = FileUtils.readFile(dataFdagePath);
  362. //获取data.fdage的内容
  363. JSONObject dataJson = new JSONObject();
  364. if(data!=null){
  365. dataJson = JSONObject.parseObject(data);
  366. }
  367. return dataJson;
  368. }
  369. private void uploadFloorCad(String path, String num, Map<String, String> uploadFiles){
  370. //户型图上传
  371. String userEditPath = UploadFilePath.USER_EDIT_PATH + "floor-cad-%s.%s";
  372. String userViewPath = UploadFilePath.USER_VIEW_PATH + "floor-cad-%s.%s";
  373. String floorCadPath = path + "/results/floorplan_cad";
  374. List<String> floorCadList = FileUtils.getFileList(floorCadPath);
  375. if(CollUtil.isNotEmpty(floorCadList)){
  376. floorCadList.stream().forEach(str->{
  377. String substring = str.substring(str.lastIndexOf(File.separator) + 1);
  378. String[] arr = substring.split("floor");
  379. String[] arr2 = arr[1].split("\\.");
  380. uploadFiles.put(str, String.format(userEditPath, num, arr2[0], arr2[1]));
  381. uploadFiles.put(str, String.format(userViewPath, num, arr2[0], arr2[1]));
  382. });
  383. }
  384. }
  385. private void writeSceneJson(String num, JSONObject videosJson, SceneEditInfo sceneEditInfo, SceneEditInfoExt sceneEditInfoExt,
  386. SceneEditControls sceneEditControls, ScenePlus scenePlus, ScenePlusExt scenePlusExt, boolean arrearCap) throws Exception{
  387. String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, num);
  388. String sceneJsonKey = dataViewPath + "scene.json";
  389. boolean exist = fYunFileService.fileExist(sceneJsonKey);
  390. //如果云端没有scene.json文件,生成一份
  391. if(!exist){
  392. SceneJsonBean sceneJson = new SceneJsonBean();
  393. BeanUtil.copyProperties(sceneEditInfoExt, sceneJson);
  394. BeanUtil.copyProperties(sceneEditInfo, sceneJson);
  395. SceneEditControlsVO sceneEditControlsVO = BeanUtil.copyProperties(sceneEditControls, SceneEditControlsVO.class);
  396. sceneJson.setControls(sceneEditControlsVO);
  397. sceneJson.setNum(num);
  398. sceneJson.setCreateTime(scenePlus.getCreateTime());
  399. sceneJson.setSceneResolution(scenePlusExt.getSceneResolution());
  400. sceneJson.setSceneFrom(scenePlusExt.getSceneFrom());
  401. sceneJson.setVideos(JSON.toJSONString(videosJson));
  402. if(arrearCap) {
  403. sceneJson.setPayStatus(PayStatus.NO_CAPACITY.code());
  404. }
  405. String sceneJsonStr = JSON.toJSONString(sceneJson);
  406. //上传sceneJson文件
  407. fYunFileService.uploadFile(sceneJsonStr.getBytes(), sceneJsonKey);
  408. //scenejson写入缓存
  409. redisUtil.set(String.format(RedisKey.SCENE_JSON, num), sceneJsonStr);
  410. }
  411. // 上传status JSON.
  412. JSONObject statusJson = new JSONObject();
  413. //临时将-2改成1,app还没完全更新
  414. statusJson.put("status", 1);
  415. statusJson.put("webSite", scenePlusExt.getWebSite());
  416. statusJson.put("sceneNum", num);
  417. statusJson.put("thumb", scenePlusExt.getThumb());
  418. statusJson.put("payStatus", 1);
  419. FileUtils.writeFile(ConstantFilePath.SCENE_PATH + "data/data" + num + File.separator + "status.json", statusJson.toString());
  420. fYunFileService.uploadFile(ConstantFilePath.SCENE_PATH + "data/data" + num + File.separator + "status.json",
  421. dataViewPath + "status.json");
  422. }
  423. private void pushMsgToApp(Integer pushChannel, String pushToken, int cameraType, String sceneName, String webSite){
  424. log.info("推送消息,渠道是 {}, 手机token是 {}", pushChannel, pushToken);
  425. if(Objects.isNull(pushChannel) && StrUtil.isBlank(pushToken)){
  426. return;
  427. }
  428. String title = sceneName + "计算完成";
  429. String body = "您上传的" + sceneName + "计算完成,点击查看";
  430. try{
  431. if(FYunTypeEnum.AWS.code().equals(fYunFileService.getFyunType())){
  432. PushMsgUtil.googlePushMsg(ConstantFilePath.BASE_PATH + "/refreshToken.json", pushToken,
  433. title, body , webSite);
  434. return;
  435. }
  436. PushMessageConfig pushConfig = null;
  437. if(pushChannel == 0){
  438. if(cameraType == 10 || cameraType == 13){
  439. //ios
  440. pushConfig = new PushMessageConfig(PushMessageConfig.IOS_KEY_Z, PushMessageConfig.IOS_SECRET_Z);
  441. pushConfig.sendIOSUnicast(pushToken, "四维看看Minion",title, body, webSite);
  442. }else {
  443. //ios
  444. pushConfig = new PushMessageConfig(PushMessageConfig.IOS_KEY, PushMessageConfig.IOS_SECRET);
  445. pushConfig.sendIOSUnicast(pushToken, "四维看看Pro",title, body, webSite);
  446. }
  447. }else {
  448. if(cameraType == 10 || cameraType == 13){
  449. //ios
  450. //安卓
  451. pushConfig = new PushMessageConfig(PushMessageConfig.ANDROID_KEY_Z, PushMessageConfig.ANDROID_SECRET_Z);
  452. pushConfig.sendAndroidUnicast2(pushToken, "四维看看Minion",title, body, webSite);
  453. }else {
  454. //安卓
  455. pushConfig = new PushMessageConfig(PushMessageConfig.ANDROID_KEY, PushMessageConfig.ANDROID_SECRET);
  456. pushConfig.sendAndroidUnicast(pushToken, "四维看看Pro",title, body, webSite);
  457. }
  458. }
  459. log.info("消息推送结束!");
  460. }catch (Exception e){
  461. log.error("推送消息失败:", e);
  462. }
  463. }
  464. private void copyToEditDir(String num) throws IOException {
  465. String editImagesPath = String.format(UploadFilePath.IMG_EDIT_PATH, num);
  466. String viewImagesPath = String.format(UploadFilePath.IMG_VIEW_PATH, num);
  467. String editDataPath = String.format(UploadFilePath.DATA_EDIT_PATH, num);
  468. String viewDataPath = String.format(UploadFilePath.DATA_VIEW_PATH, num);
  469. Map<String, String> map = new HashMap<>();
  470. map.put(editImagesPath + "vision.modeldata", viewImagesPath + "vision.modeldata");
  471. map.put(editImagesPath + "vision2.modeldata", viewImagesPath + "vision2.modeldata");
  472. map.put(editDataPath + "floorplan_cad.json", viewDataPath + "floorplan_cad.json");
  473. for (Entry<String, String> entry : map.entrySet()) {
  474. fYunFileService.copyFileInBucket(entry.getValue(), entry.getKey());
  475. }
  476. }
  477. private JSONObject getVideosJson(String path, Integer videoVersion, String projectNum, int cameraType) throws Exception {
  478. //读取videos_hdr_param.json, 保存点位视频的value
  479. Map<String, Object> videoMap = new HashMap<>();
  480. String videosHdr = FileUtils.readFile(path + File.separator + "results/videos/videos_hdr_param.json");
  481. JSONArray videoArray = null;
  482. if(StringUtils.isNotEmpty(videosHdr)){
  483. videoArray = JSONObject.parseObject(videosHdr).getJSONArray("hdr_param");
  484. }
  485. if(videoArray != null){
  486. for(int i = 0, len = videoArray.size(); i < len; i++) {
  487. videoMap.put(videoArray.getJSONObject(i).getString("name"), videoArray.getJSONObject(i).getString("value"));
  488. if(videoArray.getJSONObject(i).containsKey("fov")){
  489. videoMap.put(videoArray.getJSONObject(i).getString("name") + "_fov", videoArray.getJSONObject(i).getString("fov"));
  490. }
  491. }
  492. }
  493. //获取upload中的video视频名称
  494. String uploadData = FileUtils.readFile(path + File.separator + "results" +File.separator+"upload.json");
  495. JSONObject uploadJson = null;
  496. JSONArray array = null;
  497. if(uploadData!=null) {
  498. uploadJson = JSONObject.parseObject(uploadData);
  499. array = uploadJson.getJSONArray("upload");
  500. }
  501. JSONObject fileJson = null;
  502. String fileName = "";
  503. //计算ts文件的大小,并拼接成json格式
  504. JSONArray jsonArray = new JSONArray();
  505. JSONObject videoJson = null;
  506. JSONObject videosJson = new JSONObject();
  507. long videoSize = 0L;
  508. for(int i = 0, len = array.size(); i < len; i++) {
  509. fileJson = array.getJSONObject(i);
  510. fileName = fileJson.getString("file");
  511. if(fileJson.getIntValue("clazz") == 11 && fileName.contains(".mp4") && !fileName.contains("-ios.mp4")){
  512. videoJson = new JSONObject();
  513. videoJson.put("id", fileName.substring(
  514. 0, fileName.lastIndexOf(".")).replace("videos/", ""));
  515. //如果ts文件存在,就计算ts大小
  516. if(new File(path + File.separator + "results" +File.separator+ fileName.replace(".mp4", ".ts")).exists()){
  517. videoSize = new File(path + File.separator + "results" +File.separator+ fileName.replace(".mp4", ".ts")).length();
  518. videoJson.put("tsSize", videoSize);
  519. }
  520. if(videoMap.containsKey(videoJson.get("id"))){
  521. videoJson.put("value", videoMap.get(videoJson.get("id")));
  522. }
  523. if(videoMap.containsKey(videoJson.get("id") + "_fov")){
  524. videoJson.put("blend_fov", videoMap.get(videoJson.get("id") + "_fov"));
  525. }else {
  526. videoJson.put("blend_fov", 7);
  527. }
  528. jsonArray.add(videoJson);
  529. }
  530. }
  531. videosJson.put("data", jsonArray);
  532. if(Objects.nonNull(videoVersion) && videoVersion >= 4){
  533. videosJson.put("version", 3);
  534. videosJson.put("upPath", fYunFileConfig.getHost() + String.format(UploadFilePath.DATA_VIEW_PATH, projectNum) + "Up.xml");
  535. if(cameraType == 13){
  536. //转台相机
  537. videosJson.put("upPath", videosJson.getString("upPath").replace(".xml", ".txt"));
  538. }
  539. }else {
  540. videosJson.put("version", 1);
  541. videosJson.put("upPath", fYunFileConfig.getHost() + String.format(UploadFilePath.DATA_VIEW_PATH, projectNum) + "Up2.xml");
  542. if(cameraType == 13){
  543. //转台相机
  544. videosJson.put("upPath", videosJson.getString("upPath").replace(".xml", ".txt"));
  545. }
  546. }
  547. if(cameraType == 5 || cameraType == 6){
  548. videosJson.put("version", 1);
  549. videosJson.put("upPath", fYunFileConfig.getHost() + String.format(UploadFilePath.DATA_VIEW_PATH, projectNum) + "stitch_params.txt");
  550. }
  551. return videosJson;
  552. }
  553. private Long calUseSpace(Map<String, String> uploadFile) {
  554. return uploadFile.keySet().stream().map(File::new).filter(File::exists).mapToLong(File::length).sum();
  555. }
  556. private void sealScene(boolean arrearCap, Long scenePlusId, Long sceneProId){
  557. if(!arrearCap){
  558. return;
  559. }
  560. scenePlusService.update(
  561. new LambdaUpdateWrapper<ScenePlus>()
  562. .set(ScenePlus::getPayStatus, PayStatus.NO_CAPACITY.code())
  563. .eq(ScenePlus::getId, scenePlusId));
  564. }
  565. /**
  566. * <p>
  567. 双目场景更新数据库
  568. * </p>
  569. * @author dengsixing
  570. * @date 2022/3/21
  571. * @param num
  572. * @param space
  573. **/
  574. private void updateDb4Sm(String num, long space){
  575. List<ScenePlus> ScenePlusList = scenePlusService.list(
  576. new LambdaQueryWrapper<ScenePlus>().select(ScenePlus::getId).eq(ScenePlus::getNum, num));
  577. if(CollUtil.isEmpty(ScenePlusList)){
  578. return ;
  579. }
  580. List<Long> sceneIds = ScenePlusList.stream().map(ScenePlus::getId).collect(Collectors.toList());
  581. //更新场景创建时间
  582. scenePlusService.update(new LambdaUpdateWrapper<ScenePlus>().in(ScenePlus::getId, sceneIds)
  583. .set(ScenePlus::getSceneStatus, SceneStatus.NO_DISPLAY.code()));
  584. //更新使用容量
  585. scenePlusExtService.update(new LambdaUpdateWrapper<ScenePlusExt>().in(ScenePlusExt::getPlusId, sceneIds)
  586. .set(ScenePlusExt::getSpace, space)
  587. .set(ScenePlusExt::getAlgorithmTime, Calendar.getInstance().getTime()));
  588. }
  589. private ScenePlus updateDbPlus(String num, Long space, Integer payStatus, String videosJson, Long computeTime, String fileId,boolean isObj){
  590. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  591. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  592. List<ScenePlus> list = scenePlusService.list(
  593. new LambdaQueryWrapper<ScenePlus>().select(ScenePlus::getId).eq(ScenePlus::getNum, num));
  594. if (CollUtil.isEmpty(list)) {
  595. return null;
  596. }
  597. //修改场景状态 空间 支付状态 计算时间
  598. List<Long> scenePlusIds = list.stream().map(ScenePlus::getId).collect(Collectors.toList());
  599. scenePlusService.update(new LambdaUpdateWrapper<ScenePlus>()
  600. .in(ScenePlus::getId, scenePlusIds)
  601. .set(ScenePlus::getSceneStatus, SceneStatus.NO_DISPLAY.code())
  602. .set(ScenePlus::getCreateTime, Calendar.getInstance().getTime())
  603. .set(ScenePlus::getPayStatus, payStatus));
  604. scenePlusExtService.update(new LambdaUpdateWrapper<ScenePlusExt>()
  605. .in(ScenePlusExt::getPlusId, scenePlusIds)
  606. .set(ScenePlusExt::getSpace, space)
  607. .set(ScenePlusExt::getComputeTime, computeTime)
  608. .set(ScenePlusExt::getVideos, videosJson).set(ScenePlusExt::getIsObj, isObj ? 1 : 0));
  609. SceneFileBuild sceneFileBuild = sceneFileBuildService.findByFileId(fileId);
  610. if(sceneFileBuild != null){
  611. sceneFileBuild.setBuildStatus(BuildStatus.success.code());
  612. sceneFileBuildService.updateById(sceneFileBuild);
  613. }
  614. SceneSource sceneSource = SceneSource.get(scenePlus.getSceneSource());
  615. switch (sceneSource){
  616. case BM:
  617. scenePlusExt.setSceneResolution(SceneResolution.TILES_2K.code());
  618. scenePlusExt.setSceneFrom(SceneFrom.PRO.code());
  619. break;
  620. case SM:
  621. scenePlusExt.setSceneResolution(SceneResolution.TILES_1K.code());
  622. scenePlusExt.setSceneFrom(SceneFrom.LITE.code());
  623. break;
  624. case ZT:
  625. scenePlusExt.setSceneResolution(SceneResolution.TILES_4K.code());
  626. scenePlusExt.setSceneFrom(SceneFrom.MINION.code());
  627. break;
  628. case JG:
  629. scenePlusExt.setSceneResolution(SceneResolution.TILES_4K.code());
  630. scenePlusExt.setSceneFrom(SceneFrom.LASER.code());
  631. break;
  632. }
  633. scenePlusExtService.updateById(scenePlusExt);
  634. return scenePlus;
  635. }
  636. private boolean updateSpace(Long cameraId, Long space) throws Exception{
  637. //更新相机使用用量
  638. CameraDetail cameraDetail = cameraDetailService.updateCameraDetailByCameraIdAndSpace(cameraId, space);
  639. UserIncrement userIncrement = userIncrementService.getByCameraId(cameraId);
  640. if( userIncrement == null || userIncrement.getIsExpired() == ExpiredStatus.Expired.code()) {
  641. // 新上传的场景,如果总容量小于使用容量,则该大场景保留在临时存储空间30天
  642. if (cameraDetail.getTotalSpace().compareTo(cameraDetail.getUsedSpace()) == -1){
  643. return true;
  644. }
  645. }
  646. return false;
  647. }
  648. private Object[] updateEditInfo(ScenePlus scenePlus){
  649. SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  650. SceneEditControls sceneEditControls = null;
  651. SceneEditInfoExt sceneEditInfoExt = null;
  652. if(sceneEditInfo == null){
  653. sceneEditInfo = new SceneEditInfo();
  654. sceneEditInfo.setScenePlusId(scenePlus.getId());
  655. sceneEditInfo.setDescription(scenePlus.getDescription());
  656. sceneEditInfo.setTitle(scenePlus.getTitle());
  657. sceneEditInfoService.save(sceneEditInfo);
  658. }else{
  659. sceneEditControls = sceneEditControlsService.getBySceneEditId(sceneEditInfo.getId());
  660. sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfo.getId());
  661. sceneEditInfo.setVersion(sceneEditInfo.getVersion() + 1);
  662. sceneEditInfoService.updateById(sceneEditInfo);
  663. }
  664. if(sceneEditControls == null){
  665. sceneEditControls = new SceneEditControls();
  666. sceneEditControls.setEditInfoId(sceneEditInfo.getId());
  667. sceneEditControlsService.save(sceneEditControls);
  668. }
  669. if(sceneEditInfoExt == null){
  670. sceneEditInfoExt = new SceneEditInfoExt();
  671. sceneEditInfoExt.setScenePlusId(scenePlus.getId());
  672. sceneEditInfoExt.setEditInfoId(sceneEditInfo.getId());
  673. sceneEditInfoExtService.save(sceneEditInfoExt);
  674. }
  675. return new Object[]{sceneEditInfo, sceneEditInfoExt, sceneEditControls};
  676. }
  677. public void uploadHouseTypeJson(String num, String floorPlanCardFilePath) {
  678. if (!new File(floorPlanCardFilePath).exists()) {
  679. log.error("floorplan_cad.json 文件不存在,文件路径:{}", floorPlanCardFilePath);
  680. return;
  681. }
  682. JSONObject json = CreateHouseJsonUtil.createHouseTypeJsonByCad(floorPlanCardFilePath);
  683. String hourseTypeJsonPath = String.format(UploadFilePath.DATA_VIEW_PATH, num) + "houseType.json";
  684. fYunFileService.uploadFile(json.toJSONString().getBytes(), hourseTypeJsonPath);
  685. hourseTypeJsonPath = String.format(UploadFilePath.DATA_EDIT_PATH, num) + "houseType.json";
  686. fYunFileService.uploadFile(json.toJSONString().getBytes(), hourseTypeJsonPath);
  687. }
  688. }