BuildSceneServiceImpl.java 39 KB

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