BuildSceneServiceImpl.java 43 KB

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