BuildSceneServiceImpl.java 36 KB

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