BuildSceneServiceImpl.java 35 KB

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