BuildSceneServiceImpl.java 36 KB

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