BuildSceneServiceImpl.java 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. package com.fdkankan.contro.mq.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.thread.ThreadUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import cn.hutool.core.util.ZipUtil;
  7. import cn.hutool.extra.qrcode.QrCodeUtil;
  8. import cn.hutool.extra.qrcode.QrConfig;
  9. import cn.hutool.http.ContentType;
  10. import cn.hutool.http.HttpUtil;
  11. import com.alibaba.fastjson.JSON;
  12. import com.alibaba.fastjson.JSONArray;
  13. import com.alibaba.fastjson.JSONObject;
  14. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  15. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  16. import com.fdkankan.common.constant.*;
  17. import com.fdkankan.common.util.FileUtils;
  18. import com.fdkankan.contro.bean.SceneJsonBean;
  19. import com.fdkankan.contro.constant.UserEditDataType;
  20. import com.fdkankan.contro.entity.*;
  21. import com.fdkankan.contro.mq.service.IBuildSceneService;
  22. import com.fdkankan.contro.service.*;
  23. import com.fdkankan.fyun.config.FYunFileConfig;
  24. import com.fdkankan.fyun.constant.FYunTypeEnum;
  25. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  26. import com.fdkankan.model.constants.ConstantFileName;
  27. import com.fdkankan.model.constants.ConstantFilePath;
  28. import com.fdkankan.model.constants.UploadFilePath;
  29. import com.fdkankan.model.enums.ModelTypeEnums;
  30. import com.fdkankan.model.utils.CreateHouseJsonUtil;
  31. import com.fdkankan.model.utils.CreateObjUtil;
  32. import com.fdkankan.model.utils.SceneUtil;
  33. import com.fdkankan.push.config.PushMessageConfig;
  34. import com.fdkankan.push.utils.PushMsgUtil;
  35. import com.fdkankan.rabbitmq.bean.BuildSceneCallMessage;
  36. import com.fdkankan.rabbitmq.bean.BuildSceneResultMqMessage;
  37. import com.fdkankan.rabbitmq.util.RabbitMqProducer;
  38. import com.fdkankan.redis.constant.RedisKey;
  39. import com.fdkankan.redis.util.RedisUtil;
  40. import lombok.extern.slf4j.Slf4j;
  41. import org.apache.commons.lang3.ObjectUtils;
  42. import org.apache.commons.lang3.StringUtils;
  43. import org.apache.http.HttpHeaders;
  44. import org.springframework.beans.factory.annotation.Autowired;
  45. import org.springframework.beans.factory.annotation.Value;
  46. import org.springframework.cloud.context.config.annotation.RefreshScope;
  47. import org.springframework.stereotype.Service;
  48. import javax.annotation.Resource;
  49. import java.io.File;
  50. import java.io.IOException;
  51. import java.nio.charset.StandardCharsets;
  52. import java.util.*;
  53. import java.util.Map.Entry;
  54. import java.util.stream.Collectors;
  55. /**
  56. * <p>
  57. * </p>
  58. *
  59. * @author dengsixing
  60. * @since 2022/4/20
  61. **/
  62. @Slf4j
  63. @Service
  64. @RefreshScope
  65. public class BuildSceneServiceImpl implements IBuildSceneService {
  66. @Value("${queue.modeling.modeling-call}")
  67. private String queueModelingCall;
  68. @Value("${queue.modeling.single.modeling-call}")
  69. private String singleModelingCall;
  70. @Value("${model.type:#{null}}")
  71. private String modelType;
  72. @Value("${model.modelKind:3dtiles}")
  73. private String modelKind;
  74. @Value("#{'${model.3dtiles.sceneSource:}'.split(',')}")
  75. private List<Integer> sdTilesSceneSourceList;
  76. @Value("${env:gn}")
  77. private String env;
  78. @Value("#{'${build.scene.post.not-delete-nas-nums:}'.split(',')}")
  79. private List<String> notDeleteNasNumList;
  80. @Value("4dkk.laserService.bucket")
  81. private String laserBucket;
  82. @Autowired
  83. private RabbitMqProducer mqProducer;
  84. @Resource
  85. private FYunFileServiceInterface fYunFileService;
  86. @Autowired
  87. private ICameraDetailService cameraDetailService;
  88. @Autowired
  89. private ISceneEditInfoService sceneEditInfoService;
  90. @Autowired
  91. private ISceneEditControlsService sceneEditControlsService;
  92. @Autowired
  93. private FYunFileConfig fYunFileConfig;
  94. @Autowired
  95. private RedisUtil redisUtil;
  96. @Autowired
  97. private IScenePlusService scenePlusService;
  98. @Autowired
  99. private IScenePlusExtService scenePlusExtService;
  100. @Autowired
  101. private ISceneEditInfoExtService sceneEditInfoExtService;
  102. @Autowired
  103. private IUserIncrementService userIncrementService;
  104. @Autowired
  105. private IFdkkLaserService fdkkLaserService;
  106. // @Autowired
  107. // private IBuildSceneDTService buildSceneDTService;
  108. @Autowired
  109. private IIncrementTypeService incrementTypeService;
  110. @Autowired
  111. private ICompanyService companyService;
  112. @Autowired
  113. private ISceneAsynOperLogService sceneAsynOperLogService;
  114. @Autowired
  115. private ICommonService commonService;
  116. @Autowired
  117. private ISceneBuildProcessLogService sceneBuildProcessLogService;
  118. @Autowired
  119. private ISceneColdStorageService sceneColdStorageService;
  120. @Autowired
  121. private IOrigFileUploadBatchService origFileUploadBatchService;
  122. @Autowired
  123. private IOrigFileUploadService origFileUploadService;
  124. public static void main(String[] args) {
  125. SceneJsonBean sceneJson = new SceneJsonBean();
  126. sceneJson.setNum("123123123");
  127. String jsonString = JSON.toJSONString(sceneJson);
  128. System.out.println(111);
  129. }
  130. @Override
  131. public void buildScenePre(BuildSceneCallMessage message) throws Exception{
  132. boolean success = false;
  133. String num = message.getSceneNum();
  134. String batchId = (String) message.getExt().get("batchId");
  135. try {
  136. //如果场景原始资源上传批次id不为空,则需要下载批次文件上传到oss目录
  137. this.downloadOrigFile(batchId, message.getPath());
  138. //重新计算时需要删除文件夹,否知使用缓存
  139. if(new File(message.getPath() + File.separator + "results").exists()){
  140. FileUtils.deleteDirectory(message.getPath() + File.separator + "results");
  141. }
  142. //由于刘强说caches会影响计算结果,所以这里删除caches
  143. if(new File(message.getPath() + File.separator + "caches").exists()){
  144. FileUtils.deleteDirectory(message.getPath() + File.separator + "caches");
  145. }
  146. //删除project.json文件
  147. FileUtil.del(message.getPath().concat(File.separator).concat("project.json"));
  148. //删除点位校准数据
  149. if (Objects.nonNull(message.getExt())
  150. && message.getExt().containsKey("deleteExtras")
  151. && (Boolean) message.getExt().get("deleteExtras")) {
  152. String extras = String.format(UploadFilePath.scene_result_data_path, num).concat("extras");
  153. if(CollUtil.isNotEmpty(fYunFileService.listRemoteFiles(extras))){
  154. fYunFileService.deleteFolder(extras);
  155. }
  156. }
  157. // //用户相机重新全量上传,需要解冻结
  158. // sceneColdStorageService.unfreeze(num, "用户相机重新全量上传", message.getPath());
  159. //根据相机类型,组装资源路径
  160. //下载资源到本地
  161. this.downLoadSource(message, message.getPath());
  162. JSONObject fdageJson = JSONObject.parseObject(FileUtils.readFile(message.getPath().concat("/capture/data.fdage")));
  163. boolean rewrite = false;
  164. // 兼容旧的数据,防止OnlyExportMeshObj标志未删除掉
  165. if (fdageJson.containsKey("OnlyExportMeshObj")) {
  166. log.info("data.fdage 包含 OnlyExportMeshObj,进行去除!");
  167. // 写入data.fdage 防止重算
  168. fdageJson.remove("OnlyExportMeshObj");
  169. String ossPath = getOssPath(message.getPath());
  170. fYunFileService.uploadFile(fdageJson.toJSONString().getBytes(), ossPath + "data.fdage");
  171. rewrite = true;
  172. }
  173. if (!ObjectUtils.isEmpty(modelType)) {
  174. // 修改dataFdage文件
  175. fdageJson.put("modelType", modelType);
  176. rewrite = true;
  177. }
  178. if (rewrite) {
  179. FileUtils.writeFile(message.getPath().concat("/capture/data.fdage"), fdageJson.toJSONString());
  180. }
  181. message.getBuildContext().put("cameraType",message.getCameraType());
  182. //查询是否超过比例50%,如果超过,则启动128G服务器弹性伸缩
  183. Float maxRate = fdageJson.getFloat("maxRate");
  184. if(env.equals("gn") && Objects.nonNull(maxRate) && maxRate > 50){
  185. Map<String, Object> ext = message.getExt();
  186. if(Objects.isNull(ext)){
  187. ext = new HashMap<>();
  188. message.setExt(ext);
  189. }
  190. ext.put("128G", 1);
  191. }
  192. mqProducer.sendByWorkQueue(queueModelingCall, message);
  193. log.info("场景计算资源准备结束,场景码:{}", message.getSceneNum());
  194. }catch (Exception e){
  195. log.error("场景计算前置处理出错,num"+num, e);
  196. // buildSceneDTService.handBaseFail("场景计算资源准备异常!", message.getPath(), message.getSceneNum(), "计算控制服务器");
  197. throw e;
  198. }
  199. }
  200. private void downloadOrigFile(String batchId, String dataSource){
  201. if(StrUtil.isEmpty(batchId)){
  202. return;
  203. }
  204. List<OrigFileUpload> fileList = origFileUploadService.getByBatchId(batchId);
  205. if(CollUtil.isEmpty(fileList)){
  206. return;
  207. }
  208. String homePath = "/oss/4dkankan/" + SceneUtil.getHomePath(dataSource);
  209. for (OrigFileUpload origFileUpload : fileList) {
  210. int times = 0;
  211. String filePath = homePath.concat(origFileUpload.getFileName());
  212. do{
  213. try {
  214. ++times;
  215. HttpUtil.downloadFile(origFileUpload.getFileUrl(), new File(filePath), 10 * 60 * 1000);
  216. if(FileUtil.exist(filePath)){
  217. break;
  218. }
  219. }catch (Exception e){
  220. log.info("原始文件第{}次下载失败,fileUrl:{}, filePath:{}", origFileUpload.getFileUrl(), filePath);
  221. }
  222. }while (times < 4);
  223. if(!FileUtil.exist(filePath)){
  224. throw new RuntimeException("原始文件下载失败,fileUrl:" + origFileUpload.getFileUrl() + ", filePath:" + filePath);
  225. }
  226. }
  227. }
  228. private String getOssPath(String path) {
  229. String ossPath = ConstantFilePath.OSS_PREFIX
  230. + path.replace(ConstantFilePath.BUILD_MODEL_PATH, "")
  231. .replace(ConstantFilePath.BUILD_MODEL_LASER_PATH, "");
  232. if (!ossPath.endsWith("/")) {
  233. ossPath = ossPath.concat("/");
  234. }
  235. return ossPath;
  236. }
  237. @Override
  238. public void downLoadSource(BuildSceneCallMessage buildSceneMqMessage,String path){
  239. String ossPath = getOssPath(path);
  240. fYunFileService.downloadFileByCommand(path + File.separator + "capture", ossPath);
  241. }
  242. @Override
  243. public void buildScenePost(BuildSceneResultMqMessage message) throws Exception {
  244. String sceneCode = message.getBuildContext().get("sceneNum").toString();
  245. String path = message.getPath();
  246. String batchId = (String) message.getExt().get("batchId");
  247. try {
  248. // 上传计算日志
  249. //如果是重复计算,没有走到计算逻辑,不需要上传日志文件
  250. log.info("开始上传计算日志");
  251. String buildLogPath = String.format(UploadFilePath.BUILD_LOG_PATH, sceneCode);
  252. fYunFileService.uploadFile(path + File.separator + "console.log", buildLogPath + "console.log");
  253. log.info("计算日志上传完成");
  254. JSONObject fdageData = getFdageData(path + File.separator + "capture" +File.separator+"data.fdage");
  255. String uuid = fdageData.getString("creator") + "_" + fdageData.getString("uuidtime");
  256. OrigFileUploadBatch origFileUploadBatch = null;
  257. if(StrUtil.isNotEmpty(batchId)){
  258. OrigFileUploadBatch condition = new OrigFileUploadBatch();
  259. condition.setBatchId(batchId);
  260. origFileUploadBatch = origFileUploadBatchService.getByCondition(condition);
  261. }
  262. if (!message.getBuildSuccess()) {
  263. log.error("建模失败,修改状态为失败状态");
  264. scenePlusService.update(new LambdaUpdateWrapper<ScenePlus>()
  265. .set(ScenePlus::getSceneStatus, SceneStatus.FAILD.code())
  266. .eq(ScenePlus::getNum, sceneCode));
  267. redisUtil.set(String.format(RedisKey.SCENE_BUILD_FINISH_NUM, sceneCode), "-1");
  268. try {
  269. commonService.sendSceneStatus(sceneCode, uuid, batchId, 4);
  270. }catch (Exception e){
  271. log.info("通知计算失败状态失败, num:{}", sceneCode, e);
  272. }
  273. return;
  274. }
  275. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(sceneCode);
  276. Integer cameraType = Integer.parseInt(message.getBuildContext().get("cameraType").toString());
  277. Map<String, String> uploadFiles = getUploadFiles(scenePlus,path,cameraType,fdageData);
  278. scenePlus.setPayStatus(PayStatus.PAY.code());
  279. scenePlus.setUpdateTime(new Date());
  280. scenePlus.setSceneStatus(SceneStatus.NO_DISPLAY.code());
  281. Integer videoVersion = fdageData.getInteger("videoVersion");
  282. //读取计算结果文件生成videosJson
  283. JSONObject videosJson = this.getVideosJson(path, videoVersion, sceneCode, cameraType);
  284. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  285. boolean isObj = fdageData.containsKey("exportMeshObj") && fdageData.getIntValue("exportMeshObj") == 1;
  286. //上传全景图俯视图
  287. this.uploadFloorCad(path, sceneCode, uploadFiles);
  288. log.info("开始上传场景计算结果数据,num:{}", sceneCode);
  289. //由于3dtiles算法mesh文件发生变化,所以这里需要先清除一下oss的mesh目录,避免存在旧算法obj文件
  290. fYunFileService.deleteFolder(String.format(UploadFilePath.DATA_VIEW_PATH, sceneCode) + "mesh");
  291. fYunFileService.deleteFolder(String.format(UploadFilePath.IMG_VIEW_PATH, sceneCode) + ModelKind.THREE_D_TILE.code());
  292. //上传文件
  293. fYunFileService.uploadMulFiles(uploadFiles);
  294. //修改oss上dam的内容编码
  295. // Map<String,String> damFileHeaders = new HashMap<>();
  296. // damFileHeaders.put("Content-Encoding","gzip");
  297. // String damPath = path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam";
  298. // fYunFileService.uploadFile(damPath, String.format(UploadFilePath.IMG_VIEW_PATH, sceneCode) + ConstantFileName.modelUUID + "_50k.dam", damFileHeaders);
  299. //拷贝部分文件到编辑目录,用于用户编辑
  300. this.copyToEditDir(sceneCode);
  301. //计算完毕后,同步全景图到缓存目录
  302. // this.cachePanorama(path, sceneCode);
  303. //生成houseTypejson并上传
  304. boolean existHouseType = this.uploadHouseTypeJson(sceneCode, path);
  305. scenePlus.setHouseType(existHouseType ? CommonStatus.YES.code().intValue() : CommonStatus.NO.code().intValue());
  306. //生成floorpan.json
  307. commonService.uploadFloorplanJson(sceneCode, path);
  308. //重置异步操作记录
  309. commonService.removeSceneAsynOperLog(sceneCode);
  310. //清除用户编辑业务数据
  311. Set<String> bizs = new HashSet<>();
  312. bizs.add(UserEditDataType.BOX_MODEL.message());
  313. bizs.add(UserEditDataType.FLOORPLAN.message());
  314. bizs.add(UserEditDataType.FILTERS.message());
  315. commonService.initUserEditData(sceneCode, bizs, null);
  316. //上传计算结果文件
  317. commonService.uploadBuildResultData(sceneCode, path, SceneVersionType.V4.code());
  318. //容量统计
  319. Long space = commonService.getSpace(sceneCode);
  320. //写入数据库
  321. this.updateDbPlus(scenePlus.getSceneSource(), space, videosJson.toJSONString(), message.getComputeTime(),isObj,scenePlusExt);
  322. Object[] editInfoArr = commonService.updateEditInfo(scenePlus);
  323. SceneEditInfo sceneEditInfo = (SceneEditInfo)editInfoArr[0];
  324. SceneEditInfoExt sceneEditInfoExt = (SceneEditInfoExt)editInfoArr[1];
  325. SceneEditControls sceneEditControls = (SceneEditControls)editInfoArr[2];
  326. //更新场景主表
  327. //如果相机容量不足,需要把场景的paystatus改为容量不足状态
  328. scenePlus.setPayStatus(commonService.getPayStatus(scenePlus.getCameraId(), space));
  329. //统计原始资源大小
  330. scenePlusExt.setOrigSpace(FileUtil.size(new File(path.concat(File.separator).concat("capture"))));
  331. if (cameraType == 14) {
  332. //计算成功 激光转台相机 同步 请求
  333. fdkkLaserService.syncBuildResult(scenePlus.getNum(), path, scenePlus.getCreateTime(), Calendar.getInstance().getTime(), scenePlusExt.getShootCount(), scenePlus.getPayStatus());
  334. sceneEditControlsService.update(new LambdaUpdateWrapper<SceneEditControls>().set(SceneEditControls::getShowMap,0)
  335. .eq(SceneEditControls::getEditInfoId,sceneEditInfo.getId()));
  336. sceneEditControls.setShowMap(0);
  337. } else if (new File(path + "/results/laserData/vision_edit.txt").exists()) {
  338. fdkkLaserService.cloudPointBuild(sceneCode,path);
  339. }
  340. log.info("生成scene.json上传oss并设置缓存,num:{}", sceneCode);
  341. CameraDetail cameraDetail = cameraDetailService.getByCameraId(scenePlus.getCameraId());
  342. Company company = !ObjectUtils.isEmpty(cameraDetail.getCompanyId()) ? companyService.getById(cameraDetail.getCompanyId()) : null;
  343. //写scene.json
  344. commonService.writeSceneJson(sceneCode, videosJson,sceneEditInfo, sceneEditInfoExt, sceneEditControls, scenePlus,scenePlusExt,company);
  345. String qrLogo = !ObjectUtils.isEmpty(company) && !ObjectUtils.isEmpty(company.getQrLogo()) ? company.getQrLogo() : null;
  346. qrLogo = ObjectUtils.isEmpty(qrLogo) && !ObjectUtils.isEmpty(sceneEditInfoExt.getShareLogoImg()) ? fYunFileConfig.getHost().concat(sceneEditInfoExt.getShareLogoImg()) : null;
  347. createQrCode(sceneCode, scenePlusExt, qrLogo);
  348. //计算成功,通知APP
  349. Integer pushChannel = fdageData.getInteger("pushChannel");
  350. String pushToken = fdageData.getString("pushToken");
  351. this.pushMsgToApp(pushChannel,pushToken, cameraType, scenePlus.getTitle(), scenePlusExt.getWebSite());
  352. //删除计算目录
  353. // if(CollUtil.isEmpty(notDeleteNasNumList) || !notDeleteNasNumList.contains(sceneCode)){
  354. // CreateObjUtil.deleteFile(path.replace(ConstantFilePath.BUILD_MODEL_PATH, "/"));
  355. // }
  356. //更新场景主表
  357. //如果相机容量不足,需要把场景的paystatus改为容量不足状态
  358. if (cameraType != 14) {
  359. scenePlus.setPayStatus(commonService.getPayStatus(scenePlus.getCameraId(), space));
  360. }
  361. this.uploadStatusJson(scenePlus, scenePlusExt);
  362. scenePlusService.updateById(scenePlus);
  363. scenePlusExtService.updateById(scenePlusExt);
  364. if(Objects.isNull(origFileUploadBatch)){
  365. origFileUploadBatch.setStatus(3);
  366. origFileUploadBatchService.updateById(origFileUploadBatch);
  367. }
  368. //国际环境需要发邮件通知
  369. if("eur".equals(env)){
  370. commonService.sendEmail(sceneCode);
  371. }
  372. redisUtil.set(String.format(RedisKey.SCENE_BUILD_FINISH_NUM, sceneCode), "1");
  373. try {
  374. commonService.sendSceneStatus(sceneCode, uuid, batchId, 4);
  375. }catch (Exception e){
  376. log.info("通知计算成功状态失败, num:{}", sceneCode, e);
  377. }
  378. log.info("场景计算结果处理结束,场景码:{}", sceneCode);
  379. }catch (Exception e){
  380. log.error("场景计算结果处理出错,num"+sceneCode, e);
  381. // buildSceneDTService.handBaseFail("场景计算结果处理出错!", message.getPath(), sceneCode, "计算控制服务器");
  382. throw e;
  383. } finally {
  384. Map<String, Object> sceneStatusParam = new HashMap<>();
  385. sceneStatusParam.put("num", sceneCode);
  386. sceneStatusParam.put("status", message.getBuildSuccess() ? 1 : -1);
  387. // commonService.sendUpdateSceneStatusMqToQueues(sceneStatusParam);
  388. }
  389. }
  390. // private void cachePanorama(String dataSource, String num){
  391. // String cachedImagesPath = String.format(ConstantFilePath.SCENE_CACHE_IMAGES, num);
  392. // //将全景图缓存到缓存目录
  393. // List<String> imagesList = FileUtil.listFileNames(dataSource + "/caches/images");
  394. // //先清除旧的全景图
  395. // cn.hutool.core.io.FileUtil.del(cachedImagesPath);
  396. // String visionPath = dataSource + "/results/vision.txt";
  397. // List<String> panoramaImageList = SceneUtil.getPanoramaImageList(visionPath);
  398. // imagesList.stream().forEach(fileName -> {
  399. // if (panoramaImageList.contains(fileName)) {
  400. // String srcPath = dataSource + "/caches/images/" + fileName;
  401. // String targetPath = cachedImagesPath + fileName;
  402. // log.info("源文件:{}, 目标文件:{}", srcPath, targetPath);
  403. // cn.hutool.core.io.FileUtil.copy(srcPath, targetPath, true);
  404. // }
  405. // });
  406. // }
  407. private Map<String, String> getUploadFiles(ScenePlus scenePlus,String path,Integer cameraType,JSONObject fdageData) throws Exception {
  408. if (ObjectUtils.isEmpty(scenePlus)) {
  409. throw new Exception("未找到场景信息:" + path);
  410. }
  411. String projectNum = scenePlus.getNum();
  412. String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, projectNum);
  413. String imagesPath = String.format(UploadFilePath.IMG_VIEW_PATH, projectNum);
  414. String videoPath = String.format(UploadFilePath.VIDEOS_VIEW_PATH, projectNum);
  415. String resultsPath = path + File.separator + "results" + File.separator;
  416. String uploadData = FileUtils.readFile(resultsPath + "upload.json");
  417. JSONArray array = JSONObject.parseObject(uploadData).getJSONArray("upload");
  418. JSONObject fileJson = null;
  419. String fileName = "";
  420. Map<String, String> map = new HashMap();
  421. for (int i = 0; i < array.size(); ++i) {
  422. fileJson = array.getJSONObject(i);
  423. fileName = fileJson.getString("file");
  424. String filePath = resultsPath + fileName;
  425. if (!(new File(filePath)).exists()) {
  426. throw new Exception(filePath + "文件不存在");
  427. }
  428. if ("vision2.txt".equals(fileName)) {
  429. CreateObjUtil.convertTxtToVisionmodeldata(resultsPath + "vision2.txt", resultsPath + "vision2.modeldata");
  430. map.put(resultsPath + "vision2.modeldata", imagesPath + "vision2.modeldata");
  431. map.put(resultsPath + "vision2.txt", imagesPath + "vision2.txt");
  432. }
  433. if (fileJson.getIntValue("clazz") == 2) {
  434. map.put(filePath, imagesPath + ConstantFileName.modelUUID + "_50k_texture_jpg_high1/" + fileName.replace("tex/", ""));
  435. } else if (fileJson.getIntValue("clazz") == 3) {
  436. map.put(filePath, imagesPath + "pan/high/" + fileName.replace("high/", ""));
  437. } else if (fileJson.getIntValue("clazz") == 4) {
  438. map.put(filePath, imagesPath + "pan/low/" + fileName.replace("low/", ""));
  439. } else if (fileJson.getIntValue("clazz") == 5) {
  440. map.put(filePath, imagesPath + fileName);
  441. } else if (fileJson.getIntValue("clazz") == 7) {
  442. map.put(filePath, imagesPath + fileName);
  443. } else if (fileJson.getIntValue("clazz") == 10) {
  444. String updown = FileUtils.readFile(filePath);
  445. JSONObject updownJson = JSONObject.parseObject(updown);
  446. String mappingOssPath = String.format("scene_edit_data/%s/data/", projectNum) + fileName.replace("updown", "mapping");
  447. map.put(filePath, mappingOssPath);
  448. } else {
  449. if (fileJson.getIntValue("clazz") == 11 || fileJson.getIntValue("clazz") == 12) {
  450. map.put(filePath, videoPath + fileName.replace("videos/", ""));
  451. if (fileName.contains(".mp4")) {
  452. map.put(resultsPath + fileName.replace("mp4", "flv"), videoPath + fileName.replace("videos/", "").replace("mp4", "flv"));
  453. }
  454. }
  455. if (fileJson.getIntValue("clazz") == 16) {
  456. map.put(filePath, dataViewPath + fileName);
  457. }
  458. if (fileJson.getIntValue("clazz") == 18) {
  459. map.put(filePath, imagesPath + fileName);
  460. }
  461. }
  462. }
  463. //exportMeshObj这个是字段由app写入的
  464. boolean genModel = true;//是否生成模型 默认生成,深时场景要根据 exportMeshObj判断是否生成
  465. if(!ObjectUtils.isEmpty(cameraType)
  466. && cameraType == 14
  467. && (!fdageData.containsKey("exportMeshObj") || fdageData.getIntValue("exportMeshObj") != 1)){
  468. genModel = false;
  469. }
  470. boolean gen3dTiles = true;//是否生成3dtiles模型 默认生成
  471. if(!ModelKind.THREE_D_TILE.code().equals(modelKind)
  472. || CollUtil.isEmpty(sdTilesSceneSourceList)
  473. || !sdTilesSceneSourceList.contains(scenePlus.getSceneSource())){
  474. gen3dTiles = false;
  475. }
  476. if(genModel){
  477. if (!gen3dTiles) {
  478. String damPath = path + File.separator + "results" + File.separator + ConstantFileName.modelUUID + "_50k.dam";
  479. CreateObjUtil.convertTxtToDam(path + File.separator + "results" + File.separator + "tex" + File.separator + "modeldata.txt", damPath);
  480. // FileUtil.writeBytes(ZipUtil.gzip(new File(damPath)), damPath);
  481. map.put(damPath, imagesPath + ConstantFileName.modelUUID + "_50k.dam");
  482. }else{
  483. List<String> list = FileUtils.list(new File(path + File.separator + "results" + File.separator + ModelKind.THREE_D_TILE.code()));
  484. if(CollUtil.isEmpty(list)){
  485. log.error("3dtiles目录异常,3dtiles地址:{}", new File(path + File.separator + "results" + File.separator + ModelKind.THREE_D_TILE.code()));
  486. throw new Exception("3dtiles目录异常");
  487. }
  488. list.stream().forEach(str->{
  489. map.put(str, str.replace(path + File.separator + "results" + File.separator, imagesPath));
  490. });
  491. }
  492. }
  493. CreateObjUtil.convertTxtToVisionmodeldata(resultsPath + "vision.txt", resultsPath + "vision.modeldata");
  494. map.put(resultsPath + "vision.txt", imagesPath + "vision.txt");
  495. map.put(resultsPath + "vision.modeldata", imagesPath + "vision.modeldata");
  496. log.info("数据转换完成:" + projectNum);
  497. if(!new File("/mnt/4Dkankan/scene/data" + File.separator + "data" + projectNum).exists()){
  498. FileUtil.mkdir("/mnt/4Dkankan/scene/data" + File.separator + "data" + projectNum);
  499. }
  500. // map.put(resultsPath + "floorplan.json", dataViewPath + "floor.json");
  501. map.put(resultsPath + "floorplan_cad.json", dataViewPath + "floorplan_cad.json");
  502. map.put(path + File.separator + "capture/stitch_params.txt", dataViewPath + "stitch_params.txt");
  503. map.put(path + File.separator + "capture/Up.xml", dataViewPath + "Up.xml");
  504. map.put(path + File.separator + "capture/Up2.xml", dataViewPath + "Up2.xml");
  505. map.put(path + File.separator + "capture/Up.txt", dataViewPath + "Up.txt");
  506. map.put(path + File.separator + "capture/Up2.txt", dataViewPath + "Up2.txt");
  507. return map;
  508. }
  509. private JSONObject getFdageData(String dataFdagePath) {
  510. log.info("dataFdagePath 文件路径 :{}", dataFdagePath);
  511. String data = FileUtils.readFile(dataFdagePath);
  512. //获取data.fdage的内容
  513. JSONObject dataJson = new JSONObject();
  514. if(data!=null){
  515. dataJson = JSONObject.parseObject(data);
  516. }
  517. return dataJson;
  518. }
  519. private void uploadFloorCad(String path, String num, Map<String, String> uploadFiles){
  520. //户型图上传
  521. String dataViewPath = UploadFilePath.DATA_VIEW_PATH + "floor-cad-%s.%s";
  522. String floorCadPath = path + "/results/floorplan_cad";
  523. List<String> floorCadList = FileUtils.getFileList(floorCadPath);
  524. if(CollUtil.isNotEmpty(floorCadList)){
  525. floorCadList.stream().forEach(str->{
  526. String substring = str.substring(str.lastIndexOf(File.separator) + 1);
  527. String[] arr = substring.split("floor");
  528. String[] arr2 = arr[1].split("\\.");
  529. uploadFiles.put(str, String.format(dataViewPath, num, arr2[0], arr2[1]));
  530. });
  531. }
  532. }
  533. private void uploadStatusJson(ScenePlus scenePlus, ScenePlusExt scenePlusExt){
  534. String num = scenePlus.getNum();
  535. String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, num);
  536. Integer status = 1;
  537. if(scenePlus.getSceneSource() == 4 || scenePlus.getSceneSource() == 5){//如果是激光场景,需要激光系统那边完全处理好之后再发mq通知更新状态
  538. status = 0;
  539. }
  540. // 上传status JSON.
  541. JSONObject statusJson = new JSONObject();
  542. //临时将-2改成1,app还没完全更新
  543. statusJson.put("status", status);
  544. statusJson.put("webSite", scenePlusExt.getWebSite());
  545. statusJson.put("sceneNum", num);
  546. statusJson.put("thumb", scenePlusExt.getThumb());
  547. statusJson.put("payStatus", scenePlus.getPayStatus());
  548. statusJson.put("sceneScheme", scenePlusExt.getSceneScheme());
  549. FileUtils.writeFile(ConstantFilePath.SCENE_PATH + "data/data" + num + File.separator + "status.json", statusJson.toString());
  550. Map<String,String> headers = new HashMap<>();
  551. headers.put(HttpHeaders.CONTENT_TYPE, ContentType.JSON.getValue());
  552. fYunFileService.uploadFile(ConstantFilePath.SCENE_PATH + "data/data" + num + File.separator + "status.json", dataViewPath + "status.json", headers);
  553. }
  554. private void createQrCode(String num, ScenePlusExt scenePlusExt, String qrLogo) {
  555. String localLogoPath = null;
  556. if (!ObjectUtils.isEmpty(qrLogo)) {
  557. try {
  558. localLogoPath = ConstantFilePath.AGENT_PATH + qrLogo.substring(qrLogo.lastIndexOf("//") + 1);
  559. HttpUtil.downloadFile(qrLogo, localLogoPath);
  560. } catch (Exception e) {
  561. log.error("公司logo下载失败:{}", qrLogo);
  562. localLogoPath = null;
  563. }
  564. }
  565. //生成二维码
  566. String outPathZh = ConstantFilePath.BASE_PATH + File.separator + "sceneQRcode/"+ num +".png";
  567. String outPathEn = ConstantFilePath.BASE_PATH + File.separator + "sceneQRcode/"+ num +"_en.png";
  568. QrConfig qrConfig = QrConfig.create();
  569. qrConfig.setWidth(1024);
  570. qrConfig.setHeight(1024);
  571. if(StrUtil.isNotEmpty(localLogoPath)){
  572. qrConfig.setImg(localLogoPath);
  573. }
  574. QrCodeUtil.generate(scenePlusExt.getWebSite(), qrConfig, FileUtil.file(outPathZh));
  575. QrCodeUtil.generate(scenePlusExt.getWebSite() + "&lang=en", qrConfig, FileUtil.file(outPathEn));
  576. //上传二维码
  577. fYunFileService.uploadFile(outPathZh, String.format(UploadFilePath.DOWNLOADS_QRCODE, num) + num + ".png");
  578. fYunFileService.uploadFile(outPathEn, String.format(UploadFilePath.DOWNLOADS_QRCODE, num) + num + "_en.png");
  579. if(!ObjectUtils.isEmpty(localLogoPath)){
  580. FileUtils.deleteFile(localLogoPath);
  581. }
  582. }
  583. private void pushMsgToApp(Integer pushChannel, String pushToken, int cameraType, String sceneName, String webSite){
  584. log.info("推送消息,渠道是 {}, 手机token是 {}", pushChannel, pushToken);
  585. if(Objects.isNull(pushChannel) || StrUtil.isBlank(pushToken)){
  586. return;
  587. }
  588. String title = sceneName + "计算完成";
  589. String body = "您上传的" + sceneName + "计算完成,点击查看";
  590. try{
  591. if(FYunTypeEnum.AWS.code().equals(fYunFileService.getFyunType())){
  592. PushMsgUtil.googlePushMsg(ConstantFilePath.BASE_PATH + "/refreshToken.json", pushToken,
  593. title, body , webSite);
  594. return;
  595. }
  596. PushMessageConfig pushConfig = null;
  597. if(pushChannel == 0){
  598. if(cameraType == 10 || cameraType == 13){
  599. //ios
  600. pushConfig = new PushMessageConfig(PushMessageConfig.IOS_KEY_Z, PushMessageConfig.IOS_SECRET_Z);
  601. pushConfig.sendIOSUnicast(pushToken, "四维看看Minion",title, body, webSite);
  602. }else {
  603. //ios
  604. pushConfig = new PushMessageConfig(PushMessageConfig.IOS_KEY, PushMessageConfig.IOS_SECRET);
  605. pushConfig.sendIOSUnicast(pushToken, "四维看看Pro",title, body, webSite);
  606. }
  607. }else {
  608. if(cameraType == 10 || cameraType == 13){
  609. //ios
  610. //安卓
  611. pushConfig = new PushMessageConfig(PushMessageConfig.ANDROID_KEY_Z, PushMessageConfig.ANDROID_SECRET_Z);
  612. pushConfig.sendAndroidUnicast2(pushToken, "四维看看Minion",title, body, webSite);
  613. }else {
  614. //安卓
  615. pushConfig = new PushMessageConfig(PushMessageConfig.ANDROID_KEY, PushMessageConfig.ANDROID_SECRET);
  616. pushConfig.sendAndroidUnicast(pushToken, "四维看看Pro",title, body, webSite);
  617. }
  618. }
  619. log.info("消息推送结束!");
  620. }catch (Exception e){
  621. log.error("推送消息失败:", e);
  622. }
  623. }
  624. private void copyToEditDir(String num) throws IOException {
  625. String editImagesPath = String.format(UploadFilePath.IMG_EDIT_PATH, num);
  626. String viewImagesPath = String.format(UploadFilePath.IMG_VIEW_PATH, num);
  627. String editDataPath = String.format(UploadFilePath.DATA_EDIT_PATH, num);
  628. String viewDataPath = String.format(UploadFilePath.DATA_VIEW_PATH, num);
  629. Map<String, String> map = new HashMap<>();
  630. map.put(editImagesPath + "vision.modeldata", viewImagesPath + "vision.modeldata");
  631. map.put(editImagesPath + "vision2.modeldata", viewImagesPath + "vision2.modeldata");
  632. map.put(editDataPath + "floorplan_cad.json", viewDataPath + "floorplan_cad.json");
  633. for (Entry<String, String> entry : map.entrySet()) {
  634. fYunFileService.copyFileInBucket(entry.getValue(), entry.getKey());
  635. }
  636. }
  637. private JSONObject getVideosJson(String path, Integer videoVersion, String projectNum, int cameraType) throws Exception {
  638. //读取videos_hdr_param.json, 保存点位视频的value
  639. Map<String, Object> videoMap = new HashMap<>();
  640. String videosHdr = FileUtils.readFile(path + File.separator + "results/videos/videos_hdr_param.json");
  641. JSONArray videoArray = null;
  642. if(StringUtils.isNotEmpty(videosHdr)){
  643. videoArray = JSONObject.parseObject(videosHdr).getJSONArray("hdr_param");
  644. }
  645. if(videoArray != null){
  646. for(int i = 0, len = videoArray.size(); i < len; i++) {
  647. videoMap.put(videoArray.getJSONObject(i).getString("name"), videoArray.getJSONObject(i).getString("value"));
  648. if(videoArray.getJSONObject(i).containsKey("fov")){
  649. videoMap.put(videoArray.getJSONObject(i).getString("name") + "_fov", videoArray.getJSONObject(i).getString("fov"));
  650. }
  651. }
  652. }
  653. //获取upload中的video视频名称
  654. String uploadData = FileUtils.readFile(path + File.separator + "results" +File.separator+"upload.json");
  655. JSONObject uploadJson = null;
  656. JSONArray array = null;
  657. if(uploadData!=null) {
  658. uploadJson = JSONObject.parseObject(uploadData);
  659. array = uploadJson.getJSONArray("upload");
  660. }
  661. JSONObject fileJson = null;
  662. String fileName = "";
  663. //计算ts文件的大小,并拼接成json格式
  664. JSONArray jsonArray = new JSONArray();
  665. JSONObject videoJson = null;
  666. JSONObject videosJson = new JSONObject();
  667. long videoSize = 0L;
  668. for(int i = 0, len = array.size(); i < len; i++) {
  669. fileJson = array.getJSONObject(i);
  670. fileName = fileJson.getString("file");
  671. if(fileJson.getIntValue("clazz") == 11 && fileName.contains(".mp4") && !fileName.contains("-ios.mp4")){
  672. videoJson = new JSONObject();
  673. videoJson.put("id", fileName.substring(
  674. 0, fileName.lastIndexOf(".")).replace("videos/", ""));
  675. //如果ts文件存在,就计算ts大小
  676. if(new File(path + File.separator + "results" +File.separator+ fileName.replace(".mp4", ".ts")).exists()){
  677. videoSize = new File(path + File.separator + "results" +File.separator+ fileName.replace(".mp4", ".ts")).length();
  678. videoJson.put("tsSize", videoSize);
  679. }
  680. if(videoMap.containsKey(videoJson.get("id"))){
  681. videoJson.put("value", videoMap.get(videoJson.get("id")));
  682. }
  683. if(videoMap.containsKey(videoJson.get("id") + "_fov")){
  684. videoJson.put("blend_fov", videoMap.get(videoJson.get("id") + "_fov"));
  685. }else {
  686. videoJson.put("blend_fov", 7);
  687. }
  688. jsonArray.add(videoJson);
  689. }
  690. }
  691. videosJson.put("data", jsonArray);
  692. if(Objects.nonNull(videoVersion) && videoVersion >= 4){
  693. videosJson.put("version", 3);
  694. videosJson.put("upPath", String.format(UploadFilePath.DATA_VIEW_PATH, projectNum) + "Up.xml");
  695. if(cameraType == 13){
  696. //转台相机
  697. videosJson.put("upPath", videosJson.getString("upPath").replace(".xml", ".txt"));
  698. }
  699. }else {
  700. videosJson.put("version", 1);
  701. videosJson.put("upPath", String.format(UploadFilePath.DATA_VIEW_PATH, projectNum) + "Up2.xml");
  702. if(cameraType == 13){
  703. //转台相机
  704. videosJson.put("upPath", videosJson.getString("upPath").replace(".xml", ".txt"));
  705. }
  706. }
  707. if(cameraType == 5 || cameraType == 6){
  708. videosJson.put("version", 1);
  709. videosJson.put("upPath", String.format(UploadFilePath.DATA_VIEW_PATH, projectNum) + "stitch_params.txt");
  710. }
  711. return videosJson;
  712. }
  713. private void updateDbPlus(int sceneSource,Long space,String videosJson, Long computeTime,boolean isObj,ScenePlusExt scenePlusExt){
  714. scenePlusExt.setSpace(space);
  715. scenePlusExt.setComputeTime(computeTime.toString());
  716. scenePlusExt.setAlgorithmTime(new Date());
  717. scenePlusExt.setVideos(videosJson);
  718. scenePlusExt.setIsObj(isObj ? 1 : 0);
  719. if(ModelTypeEnums.TILE_CODE.equals(modelType)){
  720. scenePlusExt.setSceneScheme(3);
  721. }
  722. switch (SceneSource.get(sceneSource)){
  723. case BM:
  724. scenePlusExt.setSceneResolution(SceneResolution.two_K.code());
  725. scenePlusExt.setSceneFrom(SceneFrom.PRO.code());
  726. break;
  727. case SM:
  728. scenePlusExt.setSceneResolution(SceneResolution.one_k.code());
  729. scenePlusExt.setSceneFrom(SceneFrom.LITE.code());
  730. break;
  731. case ZT:
  732. scenePlusExt.setSceneResolution(SceneResolution.four_K.code());
  733. scenePlusExt.setSceneFrom(SceneFrom.MINION.code());
  734. break;
  735. case JG:
  736. scenePlusExt.setSceneResolution(SceneResolution.four_K.code());
  737. scenePlusExt.setSceneFrom(SceneFrom.LASER.code());
  738. break;
  739. case SG:
  740. scenePlusExt.setSceneResolution(SceneResolution.four_K.code());
  741. scenePlusExt.setSceneFrom(SceneFrom.LASER.code());
  742. break;
  743. }
  744. String sceneKind = scenePlusExt.getSceneScheme() == 3 ? SceneKind.FACE.code():SceneKind.TILES.code();
  745. scenePlusExt.setSceneKind(sceneKind);
  746. // scenePlusExt.setModelKind(modelKind);
  747. //统计点位数量
  748. scenePlusExt.setShootCount(this.getShootCount(scenePlusExt));
  749. scenePlusExtService.updateById(scenePlusExt);
  750. }
  751. private Integer getShootCount(ScenePlusExt scenePlusExt){
  752. Integer shootCount = null;
  753. String homePath = SceneUtil.getHomePath(scenePlusExt.getDataSource());
  754. JSONObject dataFdageObj = JSON.parseObject(fYunFileService.getFileContent(homePath.concat("data.fdage")));
  755. if(Objects.nonNull(dataFdageObj)){
  756. JSONArray points = dataFdageObj.getJSONArray("points");
  757. if(CollUtil.isNotEmpty(points)){
  758. shootCount = points.size();
  759. }
  760. }
  761. if(Objects.nonNull(shootCount) && shootCount > 0){
  762. return shootCount;
  763. }
  764. String slamDataStr = fYunFileService.getFileContent(homePath.concat("slam_data.json"));
  765. JSONObject slamDataObj = JSON.parseObject(slamDataStr);
  766. if(Objects.nonNull(slamDataObj)){
  767. JSONArray viewsInfo = slamDataObj.getJSONArray("views_info");
  768. if(CollUtil.isNotEmpty(viewsInfo)){
  769. shootCount = viewsInfo.stream().mapToInt(info -> {
  770. return ((JSONObject) info).getJSONArray("list_pose").size();
  771. }).sum();
  772. }
  773. }
  774. return shootCount;
  775. }
  776. public boolean uploadHouseTypeJson(String num, String dataSource) {
  777. String floorPlanCardFilePath = dataSource + File.separator + "results/floorplan_cad.json";
  778. if (!new File(floorPlanCardFilePath).exists()) {
  779. log.warn("floorplan_cad.json 文件不存在,文件路径:{}", floorPlanCardFilePath);
  780. return false;
  781. }
  782. JSONObject json = CreateHouseJsonUtil.createHouseTypeJsonByCad(floorPlanCardFilePath);
  783. if(Objects.isNull(json)){
  784. return false;
  785. }
  786. String hourseTypeJsonPath = String.format(UploadFilePath.USER_VIEW_PATH, num) + "houseType.json";
  787. fYunFileService.uploadFile(json.toJSONString().getBytes(), hourseTypeJsonPath);
  788. hourseTypeJsonPath = String.format(UploadFilePath.USER_EDIT_PATH, num) + "houseType.json";
  789. fYunFileService.uploadFile(json.toJSONString().getBytes(), hourseTypeJsonPath);
  790. return true;
  791. }
  792. }