BuildSceneServiceImpl.java 39 KB

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