BuildSceneServiceImpl.java 41 KB

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