BuildSceneServiceImpl.java 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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.enums.ModelTypeEnums;
  28. import com.fdkankan.model.utils.CreateHouseJsonUtil;
  29. import com.fdkankan.model.utils.CreateObjUtil;
  30. import com.fdkankan.push.config.PushMessageConfig;
  31. import com.fdkankan.push.utils.PushMsgUtil;
  32. import com.fdkankan.rabbitmq.bean.BuildSceneCallMessage;
  33. import com.fdkankan.rabbitmq.bean.BuildSceneResultMqMessage;
  34. import com.fdkankan.rabbitmq.util.RabbitMqProducer;
  35. import com.fdkankan.redis.constant.RedisKey;
  36. import com.fdkankan.redis.util.RedisUtil;
  37. import lombok.extern.slf4j.Slf4j;
  38. import org.apache.commons.lang3.ObjectUtils;
  39. import org.apache.commons.lang3.StringUtils;
  40. import org.springframework.beans.factory.annotation.Autowired;
  41. import org.springframework.beans.factory.annotation.Value;
  42. import org.springframework.stereotype.Service;
  43. import java.io.File;
  44. import java.io.IOException;
  45. import java.util.*;
  46. import java.util.Map.Entry;
  47. import java.util.stream.Collectors;
  48. /**
  49. * <p>
  50. * TODO
  51. * </p>
  52. *
  53. * @author dengsixing
  54. * @since 2022/4/20
  55. **/
  56. @Slf4j
  57. @Service
  58. public class BuildSceneServiceImpl implements IBuildSceneService {
  59. @Value("${queue.modeling.modeling-call}")
  60. private String queueModelingCall;
  61. @Value("${model.type}")
  62. private String modelType;
  63. @Autowired
  64. private RabbitMqProducer mqProducer;
  65. @Autowired
  66. private FYunFileServiceInterface fYunFileService;
  67. @Autowired
  68. private ICameraDetailService cameraDetailService;
  69. @Autowired
  70. private ISceneEditInfoService sceneEditInfoService;
  71. @Autowired
  72. private ISceneEditControlsService sceneEditControlsService;
  73. @Autowired
  74. private FYunFileConfig fYunFileConfig;
  75. @Autowired
  76. private RedisUtil redisUtil;
  77. @Autowired
  78. private IScenePlusService scenePlusService;
  79. @Autowired
  80. private IScenePlusExtService scenePlusExtService;
  81. @Autowired
  82. private ISceneEditInfoExtService sceneEditInfoExtService;
  83. @Autowired
  84. private IUserIncrementService userIncrementService;
  85. @Autowired
  86. private IFdkkLaserService fdkkLaserService;
  87. @Autowired
  88. private IBuildSceneDTService buildSceneDTService;
  89. @Override
  90. public void buildScenePre(BuildSceneCallMessage message) {
  91. boolean success = false;
  92. try {
  93. //根据相机类型,组装资源路径
  94. //下载资源到本地
  95. this.downLoadSource(message, message.getPath());
  96. if (!ObjectUtils.isEmpty(modelType)) {
  97. // 修改dataFdage文件
  98. JSONObject fdageJson = JSONObject.parseObject(FileUtils.readFile(message.getPath().concat("/capture/data.fdage")));
  99. fdageJson.put("modelType", modelType);
  100. FileUtils.writeFile(message.getPath().concat("/capture/data.fdage"), fdageJson.toJSONString());
  101. }
  102. message.getBuildContext().put("cameraType",message.getCameraType());
  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. buildSceneDTService.handFail("场景计算资源准备异常!", message.getPath(), message.getSceneNum(), "计算控制服务器", null);
  113. }
  114. }
  115. }
  116. @Override
  117. public void downLoadSource(BuildSceneCallMessage buildSceneMqMessage,String path){
  118. String ossPath = ConstantFilePath.OSS_PREFIX
  119. + path.replace(ConstantFilePath.BUILD_MODEL_PATH, "")
  120. .replace(ConstantFilePath.BUILD_MODEL_LASER_PATH, "");
  121. if (!ossPath.endsWith("/")) {
  122. ossPath = ossPath.concat("/");
  123. }
  124. fYunFileService.downloadFileByCommand(path + File.separator + "capture", ossPath);
  125. }
  126. @Override
  127. public void buildScenePost(BuildSceneResultMqMessage message) throws Exception {
  128. Integer cameraType = Integer.parseInt(message.getBuildContext().get("cameraType").toString());
  129. String sceneCode = message.getBuildContext().get("sceneNum").toString();
  130. Long computeTime = message.getComputeTime();
  131. String path = message.getPath();
  132. Boolean buildSuccess = message.getBuildSuccess();
  133. JSONObject fdageData = getFdageData(path + File.separator + "capture" +File.separator+"data.fdage");
  134. Integer videoVersion = fdageData.getInteger("videoVersion");
  135. try {
  136. // 上传计算日志
  137. //如果是重复计算,没有走到计算逻辑,不需要上传日志文件
  138. log.info("开始上传计算日志");
  139. String buildLogPath = String.format(UploadFilePath.BUILD_LOG_PATH, sceneCode);
  140. fYunFileService.uploadFile(path + File.separator + "console.log", buildLogPath + "console.log");
  141. log.info("计算日志上传完成");
  142. if (!buildSuccess) {
  143. log.error("建模失败,修改状态为失败状态");
  144. scenePlusService.update(new LambdaUpdateWrapper<ScenePlus>()
  145. .set(ScenePlus::getSceneStatus, SceneStatus.FAILD.code())
  146. .eq(ScenePlus::getNum, sceneCode));
  147. throw new RuntimeException("建模失败!");
  148. }
  149. Map<String, String> uploadFiles = getUploadFiles(path,cameraType,fdageData);
  150. //建模成功走以下逻辑
  151. log.info("cameraType:{}",cameraType);
  152. //计算场景消耗磁盘空间
  153. long space = this.calUseSpace(uploadFiles);
  154. if(cameraType < 3){
  155. this.updateDb4Sm(sceneCode, space);
  156. fYunFileService.uploadMulFiles(uploadFiles);
  157. return;
  158. }
  159. //读取计算结果文件生成videosJson
  160. JSONObject videosJson = this.getVideosJson(path, videoVersion, sceneCode, cameraType);
  161. //写入数据库
  162. ScenePlus scenePlus = this.updateDbPlus(sceneCode, space, videosJson.toJSONString(), computeTime,fdageData.containsKey("exportMeshObj") && fdageData.getIntValue("exportMeshObj") == 1);
  163. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  164. //变更容量,arrearCap为true时,代表容量不足
  165. boolean arrearCap = this.updateSpace(scenePlus.getCameraId(), space);
  166. //如果相机容量不足,需要把场景的paystatus改为容量不足状态
  167. if (arrearCap && cameraType != 14) {
  168. this.sealScene(arrearCap, scenePlus.getId(), scenePlus.getId());
  169. }
  170. Object[] editInfoArr = this.updateEditInfo(scenePlus);
  171. SceneEditInfo sceneEditInfo = (SceneEditInfo)editInfoArr[0];
  172. SceneEditInfoExt sceneEditInfoExt = (SceneEditInfoExt)editInfoArr[1];
  173. SceneEditControls sceneEditControls = (SceneEditControls)editInfoArr[2];
  174. String outPathZh = ConstantFilePath.BASE_PATH + File.separator + "sceneQRcode/"+sceneCode+".png";
  175. String outPathEn = ConstantFilePath.BASE_PATH + File.separator + "sceneQRcode/"+sceneCode+"_en.png";
  176. //生成新的分享的二维码-中文版本
  177. MatrixToImageWriterUtil.createQRCode(scenePlusExt.getWebSite(), outPathZh, false,null);
  178. //生成新的分享的二维码-英文版本
  179. MatrixToImageWriterUtil.createQRCode(scenePlusExt.getWebSite() + "&lang=en", outPathEn, false, null);
  180. //上传二维码
  181. fYunFileService.uploadFile(outPathZh, String.format(UploadFilePath.DOWNLOADS_QRCODE, sceneCode) + sceneCode + ".png");
  182. fYunFileService.uploadFile(outPathEn, String.format(UploadFilePath.DOWNLOADS_QRCODE, sceneCode) + sceneCode + "_en.png");
  183. //上传全景图俯视图
  184. this.uploadFloorCad(path, sceneCode, uploadFiles);
  185. //上传文件
  186. log.info("开始上传场景计算结果数据,num:{}", sceneCode);
  187. fYunFileService.uploadMulFiles(uploadFiles);
  188. Map<String,String> damFileHeaders = new HashMap<>();
  189. damFileHeaders.put("Content-Encoding","gzip");
  190. if (!fYunFileService.getFyunType().equals(FYunTypeEnum.LOCAL.code())) {
  191. // dam 文件设置请求头
  192. uploadFiles.entrySet().stream().filter(entry -> FileNameUtil.extName(entry.getKey()).equals("dam"))
  193. .filter(entry -> new File(entry.getKey()).exists())
  194. .forEach(entry -> {
  195. // gzip压缩
  196. FileUtil.writeBytes(ZipUtil.gzip(new File(entry.getKey())), entry.getKey() + ".gzip");
  197. // 重命名
  198. FileUtil.rename(new File(entry.getKey() + ".gzip"), entry.getKey(), true);
  199. fYunFileService.uploadFile(entry.getKey(), entry.getValue(), damFileHeaders);
  200. });
  201. }
  202. //拷贝部分文件到编辑目录,用于用户编辑
  203. this.copyToEditDir(sceneCode);
  204. //生成houseTypejson并上传
  205. uploadFiles.entrySet().stream().filter(entry-> FileNameUtil.getName(entry.getKey()).equals("floorplan_cad.json"))
  206. .forEach(entry-> uploadHouseTypeJson(sceneCode,entry.getKey()));
  207. //写scene.json
  208. log.info("生成scene.json上传oss并设置缓存,num:{}", sceneCode);
  209. this.writeSceneJson(sceneCode, videosJson,sceneEditInfo, sceneEditInfoExt, sceneEditControls, scenePlus,
  210. scenePlusExt, arrearCap);
  211. //计算成功,通知APP
  212. Integer pushChannel = fdageData.getInteger("pushChannel");
  213. String pushToken = fdageData.getString("pushToken");
  214. this.pushMsgToApp(pushChannel,pushToken, cameraType, scenePlus.getTitle(), scenePlusExt.getWebSite());
  215. if (cameraType == 14) {
  216. //计算成功 激光转台相机 同步 请求
  217. fdkkLaserService.syncBuildResult(scenePlus.getNum(), scenePlusExt.getDataSource(),scenePlus.getCreateTime());
  218. }
  219. CreateObjUtil.deleteFile(path.replace(ConstantFilePath.BUILD_MODEL_PATH, "/") + "/capture");
  220. log.info("场景计算结果处理结束,场景码:{}", sceneCode);
  221. }catch (Exception e){
  222. String errorMsg = "场景计算结果处理出错!";
  223. String hostName = "计算控制服务";
  224. log.error(errorMsg);
  225. e.printStackTrace();
  226. if(!buildSuccess){
  227. errorMsg = "场景构建失败!";
  228. hostName = message.getHostName();
  229. }
  230. //发送钉钉
  231. String serverPath = message.getPath();
  232. buildSceneDTService.handFail(errorMsg, serverPath, sceneCode, hostName, null);
  233. }
  234. }
  235. private Map<String, String> getUploadFiles(String path,Integer cameraType,JSONObject fdageData) throws Exception {
  236. ScenePlus scenePlus = scenePlusService.getByFileId(path);
  237. if (ObjectUtils.isEmpty(scenePlus)) {
  238. throw new Exception("未找到场景信息:" + path);
  239. }
  240. String projectNum = scenePlus.getNum();
  241. String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, projectNum);
  242. String imagesPath = String.format(UploadFilePath.IMG_VIEW_PATH, projectNum);
  243. String videoPath = String.format(UploadFilePath.VIDEOS_VIEW_PATH, projectNum);
  244. String resultsPath = path + File.separator + "results" + File.separator;
  245. String uploadJsonPath = resultsPath + "upload.json";
  246. String uploadData = FileUtils.readFile(uploadJsonPath);
  247. log.info("upload.json 文件路径:{}, 内容:{}", uploadJsonPath, uploadData);
  248. JSONArray array = JSONObject.parseObject(uploadData).getJSONArray("upload");
  249. JSONObject fileJson = null;
  250. String fileName = "";
  251. Map<String, String> map = new HashMap();
  252. for (int i = 0; i < array.size(); ++i) {
  253. fileJson = array.getJSONObject(i);
  254. fileName = fileJson.getString("file");
  255. String filePath = resultsPath + fileName;
  256. if (!(new File(filePath)).exists()) {
  257. throw new Exception(filePath + "文件不存在");
  258. }
  259. if ("vision2.txt".equals(fileName)) {
  260. CreateObjUtil.convertTxtToVisionmodeldata(resultsPath + "vision2.txt", resultsPath + "vision2.modeldata");
  261. map.put(resultsPath + "vision2.modeldata", imagesPath + "vision2.modeldata");
  262. map.put(resultsPath + "vision2.txt", imagesPath + "vision2.txt");
  263. }
  264. if (fileJson.getIntValue("clazz") == 2) {
  265. map.put(filePath, imagesPath + ConstantFileName.modelUUID + "_50k_texture_jpg_high1/" + fileName.replace("tex/", ""));
  266. } else if (fileJson.getIntValue("clazz") == 3) {
  267. map.put(filePath, imagesPath + "pan/high/" + fileName.replace("high/", ""));
  268. } else if (fileJson.getIntValue("clazz") == 4) {
  269. map.put(filePath, imagesPath + "pan/low/" + fileName.replace("low/", ""));
  270. } else if (fileJson.getIntValue("clazz") == 5) {
  271. map.put(filePath, imagesPath + fileName);
  272. } else if (fileJson.getIntValue("clazz") == 7) {
  273. map.put(filePath, imagesPath + fileName);
  274. } else if (fileJson.getIntValue("clazz") == 10) {
  275. String updown = FileUtils.readFile(filePath);
  276. JSONObject updownJson = JSONObject.parseObject(updown);
  277. String mappingOssPath = String.format("scene_edit_data/%s/data/", projectNum) + fileName.replace("updown", "mapping");
  278. map.put(filePath, mappingOssPath);
  279. } else {
  280. if (fileJson.getIntValue("clazz") == 11 || fileJson.getIntValue("clazz") == 12) {
  281. map.put(filePath, videoPath + fileName.replace("videos/", ""));
  282. if (fileName.contains(".mp4")) {
  283. map.put(resultsPath + fileName.replace("mp4", "flv"), videoPath + fileName.replace("videos/", "").replace("mp4", "flv"));
  284. }
  285. }
  286. if (fileJson.getIntValue("clazz") == 16) {
  287. map.put(filePath, dataViewPath + fileName);
  288. }
  289. if (fileJson.getIntValue("clazz") == 18) {
  290. map.put(filePath, imagesPath + fileName);
  291. }
  292. }
  293. }
  294. if ((fdageData.containsKey("exportMeshObj") && fdageData.getIntValue("exportMeshObj") == 1)
  295. || (!ObjectUtils.isEmpty(cameraType) && (!cameraType.equals(14)))) {
  296. CreateObjUtil.convertTxtToDam(path + File.separator + "results" + File.separator + "tex" + File.separator + "modeldata.txt", path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam");
  297. CreateObjUtil.convertDamToLzma(path + File.separator + "results/");
  298. CreateObjUtil.convertTxtToDam(path + File.separator + "results" + File.separator + "tex" + File.separator + "modeldata.txt", path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam");
  299. map.put(path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam.lzma", imagesPath + ConstantFileName.modelUUID + "_50k.dam.lzma");
  300. map.put(path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam", imagesPath + ConstantFileName.modelUUID + "_50k.dam");
  301. }
  302. CreateObjUtil.convertTxtToVisionmodeldata(resultsPath + "vision.txt", resultsPath + "vision.modeldata");
  303. map.put(resultsPath + "vision.txt", imagesPath + "vision.txt");
  304. map.put(resultsPath + "vision.modeldata", imagesPath + "vision.modeldata");
  305. log.info("数据转换完成:" + projectNum);
  306. FileUtil.touch("/mnt/4Dkankan/scene/data" + File.separator + "data" + projectNum);
  307. map.put(resultsPath + "floorplan.json", dataViewPath + "floor.json");
  308. map.put(resultsPath + "floorplan_cad.json", dataViewPath + "floorplan_cad.json");
  309. map.put(path + File.separator + "capture/stitch_params.txt", dataViewPath + "stitch_params.txt");
  310. map.put(path + File.separator + "capture/Up.xml", dataViewPath + "Up.xml");
  311. map.put(path + File.separator + "capture/Up2.xml", dataViewPath + "Up2.xml");
  312. map.put(path + File.separator + "capture/Up.txt", dataViewPath + "Up.txt");
  313. map.put(path + File.separator + "capture/Up2.txt", dataViewPath + "Up2.txt");
  314. return map;
  315. }
  316. private JSONObject getFdageData(String dataFdagePath) {
  317. log.info("dataFdagePath 文件路径 :{}", dataFdagePath);
  318. String data = FileUtils.readFile(dataFdagePath);
  319. //获取data.fdage的内容
  320. JSONObject dataJson = new JSONObject();
  321. if(data!=null){
  322. dataJson = JSONObject.parseObject(data);
  323. }
  324. return dataJson;
  325. }
  326. private void uploadFloorCad(String path, String num, Map<String, String> uploadFiles){
  327. //户型图上传
  328. String userEditPath = UploadFilePath.USER_EDIT_PATH + "floor-cad-%s.%s";
  329. String userViewPath = UploadFilePath.USER_VIEW_PATH + "floor-cad-%s.%s";
  330. String floorCadPath = path + "/results/floorplan_cad";
  331. List<String> floorCadList = FileUtils.getFileList(floorCadPath);
  332. if(CollUtil.isNotEmpty(floorCadList)){
  333. floorCadList.stream().forEach(str->{
  334. String substring = str.substring(str.lastIndexOf(File.separator) + 1);
  335. String[] arr = substring.split("floor");
  336. String[] arr2 = arr[1].split("\\.");
  337. uploadFiles.put(str, String.format(userEditPath, num, arr2[0], arr2[1]));
  338. uploadFiles.put(str, String.format(userViewPath, num, arr2[0], arr2[1]));
  339. });
  340. }
  341. }
  342. private void writeSceneJson(String num, JSONObject videosJson, SceneEditInfo sceneEditInfo, SceneEditInfoExt sceneEditInfoExt,
  343. SceneEditControls sceneEditControls, ScenePlus scenePlus, ScenePlusExt scenePlusExt, boolean arrearCap) throws Exception{
  344. String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, num);
  345. String sceneJsonKey = dataViewPath + "scene.json";
  346. boolean exist = fYunFileService.fileExist(sceneJsonKey);
  347. //如果云端没有scene.json文件,生成一份
  348. if(!exist){
  349. SceneJsonBean sceneJson = new SceneJsonBean();
  350. BeanUtil.copyProperties(sceneEditInfoExt, sceneJson);
  351. BeanUtil.copyProperties(sceneEditInfo, sceneJson);
  352. SceneEditControlsVO sceneEditControlsVO = BeanUtil.copyProperties(sceneEditControls, SceneEditControlsVO.class);
  353. sceneJson.setControls(sceneEditControlsVO);
  354. sceneJson.setNum(num);
  355. sceneJson.setCreateTime(scenePlus.getCreateTime());
  356. sceneJson.setSceneResolution(scenePlusExt.getSceneResolution());
  357. sceneJson.setSceneFrom(scenePlusExt.getSceneFrom());
  358. sceneJson.setVideos(JSON.toJSONString(videosJson));
  359. if(arrearCap) {
  360. sceneJson.setPayStatus(PayStatus.NO_CAPACITY.code());
  361. }
  362. String sceneJsonStr = JSON.toJSONString(sceneJson);
  363. //上传sceneJson文件
  364. fYunFileService.uploadFile(sceneJsonStr.getBytes(), sceneJsonKey);
  365. //scenejson写入缓存
  366. redisUtil.set(String.format(RedisKey.SCENE_JSON, num), sceneJsonStr);
  367. }
  368. // 上传status JSON.
  369. JSONObject statusJson = new JSONObject();
  370. //临时将-2改成1,app还没完全更新
  371. statusJson.put("status", 1);
  372. statusJson.put("webSite", scenePlusExt.getWebSite());
  373. statusJson.put("sceneNum", num);
  374. statusJson.put("thumb", scenePlusExt.getThumb());
  375. statusJson.put("payStatus", 1);
  376. statusJson.put("sceneScheme", scenePlusExt.getSceneScheme());
  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. scenePlusService.update(
  516. new LambdaUpdateWrapper<ScenePlus>()
  517. .set(ScenePlus::getPayStatus, PayStatus.NO_CAPACITY.code())
  518. .eq(ScenePlus::getId, scenePlusId));
  519. }
  520. /**
  521. * <p>
  522. 双目场景更新数据库
  523. * </p>
  524. * @author dengsixing
  525. * @date 2022/3/21
  526. * @param num
  527. * @param space
  528. **/
  529. private void updateDb4Sm(String num, long space){
  530. List<ScenePlus> ScenePlusList = scenePlusService.list(
  531. new LambdaQueryWrapper<ScenePlus>().select(ScenePlus::getId).eq(ScenePlus::getNum, num));
  532. if(CollUtil.isEmpty(ScenePlusList)){
  533. return ;
  534. }
  535. List<Long> sceneIds = ScenePlusList.stream().map(ScenePlus::getId).collect(Collectors.toList());
  536. //更新场景创建时间
  537. scenePlusService.update(new LambdaUpdateWrapper<ScenePlus>().in(ScenePlus::getId, sceneIds)
  538. .set(ScenePlus::getSceneStatus, SceneStatus.NO_DISPLAY.code()));
  539. //更新使用容量
  540. scenePlusExtService.update(new LambdaUpdateWrapper<ScenePlusExt>().in(ScenePlusExt::getPlusId, sceneIds)
  541. .set(ScenePlusExt::getSpace, space)
  542. .set(ScenePlusExt::getAlgorithmTime, Calendar.getInstance().getTime()));
  543. }
  544. private ScenePlus updateDbPlus(String num, Long space, String videosJson, Long computeTime,boolean isObj){
  545. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  546. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  547. List<ScenePlus> list = scenePlusService.list(new LambdaQueryWrapper<ScenePlus>()
  548. .select(ScenePlus::getId).eq(ScenePlus::getNum, num));
  549. if (CollUtil.isEmpty(list)) {
  550. return null;
  551. }
  552. //修改场景状态 空间 支付状态 计算时间
  553. List<Long> scenePlusIds = list.stream().map(ScenePlus::getId).collect(Collectors.toList());
  554. scenePlusService.update(new LambdaUpdateWrapper<ScenePlus>()
  555. .in(ScenePlus::getId, scenePlusIds)
  556. .set(ScenePlus::getSceneStatus, SceneStatus.NO_DISPLAY.code())
  557. .set(ScenePlus::getUpdateTime, new Date())
  558. .set(ScenePlus::getPayStatus, PayStatus.PAY.code()));
  559. scenePlusExt.setSpace(space);
  560. scenePlusExt.setComputeTime(computeTime.toString());
  561. scenePlusExt.setAlgorithmTime(new Date());
  562. scenePlusExt.setVideos(videosJson);
  563. scenePlusExt.setIsObj(isObj ? 1 : 0);
  564. if(ModelTypeEnums.TILE_CODE.equals(modelType)){
  565. scenePlusExt.setSceneScheme(3);
  566. }
  567. SceneSource sceneSource = SceneSource.get(scenePlus.getSceneSource());
  568. switch (sceneSource){
  569. case BM:
  570. scenePlusExt.setSceneResolution(SceneResolution.TILES_2K.code());
  571. scenePlusExt.setSceneFrom(SceneFrom.PRO.code());
  572. break;
  573. case SM:
  574. scenePlusExt.setSceneResolution(SceneResolution.TILES_1K.code());
  575. scenePlusExt.setSceneFrom(SceneFrom.LITE.code());
  576. break;
  577. case ZT:
  578. scenePlusExt.setSceneResolution(SceneResolution.TILES_4K.code());
  579. scenePlusExt.setSceneFrom(SceneFrom.MINION.code());
  580. break;
  581. case JG:
  582. scenePlusExt.setSceneResolution(SceneResolution.TILES_4K.code());
  583. scenePlusExt.setSceneFrom(SceneFrom.LASER.code());
  584. break;
  585. }
  586. scenePlusExtService.updateById(scenePlusExt);
  587. return scenePlus;
  588. }
  589. private boolean updateSpace(Long cameraId, Long space) throws Exception{
  590. //更新相机使用用量
  591. CameraDetail cameraDetail = cameraDetailService.updateCameraDetailByCameraIdAndSpace(cameraId, space);
  592. UserIncrement userIncrement = userIncrementService.getByCameraId(cameraId);
  593. if( userIncrement == null || userIncrement.getIsExpired() == ExpiredStatus.Expired.code()) {
  594. // 新上传的场景,如果总容量小于使用容量,则该大场景保留在临时存储空间30天
  595. if (cameraDetail.getTotalSpace().compareTo(cameraDetail.getUsedSpace()) == -1){
  596. return true;
  597. }
  598. }
  599. return false;
  600. }
  601. private Object[] updateEditInfo(ScenePlus scenePlus){
  602. SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  603. SceneEditControls sceneEditControls = null;
  604. SceneEditInfoExt sceneEditInfoExt = null;
  605. if(sceneEditInfo == null){
  606. sceneEditInfo = new SceneEditInfo();
  607. sceneEditInfo.setScenePlusId(scenePlus.getId());
  608. sceneEditInfo.setDescription(scenePlus.getDescription());
  609. sceneEditInfo.setTitle(scenePlus.getTitle());
  610. sceneEditInfoService.save(sceneEditInfo);
  611. }else{
  612. sceneEditControls = sceneEditControlsService.getBySceneEditId(sceneEditInfo.getId());
  613. sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfo.getId());
  614. sceneEditInfo.setVersion(sceneEditInfo.getVersion() + 1);
  615. sceneEditInfoService.updateById(sceneEditInfo);
  616. }
  617. if(sceneEditControls == null){
  618. sceneEditControls = new SceneEditControls();
  619. sceneEditControls.setEditInfoId(sceneEditInfo.getId());
  620. sceneEditControlsService.save(sceneEditControls);
  621. }
  622. if(sceneEditInfoExt == null){
  623. sceneEditInfoExt = new SceneEditInfoExt();
  624. sceneEditInfoExt.setScenePlusId(scenePlus.getId());
  625. sceneEditInfoExt.setEditInfoId(sceneEditInfo.getId());
  626. sceneEditInfoExtService.save(sceneEditInfoExt);
  627. }
  628. return new Object[]{sceneEditInfo, sceneEditInfoExt, sceneEditControls};
  629. }
  630. public void uploadHouseTypeJson(String num, String floorPlanCardFilePath) {
  631. if (!new File(floorPlanCardFilePath).exists()) {
  632. log.error("floorplan_cad.json 文件不存在,文件路径:{}", floorPlanCardFilePath);
  633. return;
  634. }
  635. JSONObject json = CreateHouseJsonUtil.createHouseTypeJsonByCad(floorPlanCardFilePath);
  636. String hourseTypeJsonPath = String.format(UploadFilePath.DATA_VIEW_PATH, num) + "houseType.json";
  637. fYunFileService.uploadFile(json.toJSONString().getBytes(), hourseTypeJsonPath);
  638. hourseTypeJsonPath = String.format(UploadFilePath.DATA_EDIT_PATH, num) + "houseType.json";
  639. fYunFileService.uploadFile(json.toJSONString().getBytes(), hourseTypeJsonPath);
  640. }
  641. }