UnityServiceImpl.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package com.fdkankan.jp.xspace.service.impl;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.alibaba.fastjson.JSON;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  7. import com.fdkankan.common.constant.CommonStatus;
  8. import com.fdkankan.common.util.CmdUtils;
  9. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  10. import com.fdkankan.jp.xspace.common.constant.NasPathConstant;
  11. import com.fdkankan.jp.xspace.common.constant.OSSPathConstant;
  12. import com.fdkankan.jp.xspace.common.constant.UnityConstant;
  13. import com.fdkankan.jp.xspace.common.exception.PackException;
  14. import com.fdkankan.jp.xspace.entity.SceneXspace;
  15. import com.fdkankan.jp.xspace.entity.UnityConfig;
  16. import com.fdkankan.jp.xspace.service.IUnityConfigService;
  17. import com.fdkankan.jp.xspace.service.IUnityService;
  18. import com.fdkankan.jp.xspace.vo.SceneEditControlsVO;
  19. import com.fdkankan.jp.xspace.vo.SceneInfoVO;
  20. import com.fdkankan.redis.constant.RedisKey;
  21. import com.fdkankan.redis.util.RedisUtil;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.stereotype.Service;
  25. import javax.annotation.Resource;
  26. import java.io.File;
  27. import java.nio.charset.StandardCharsets;
  28. import java.util.List;
  29. import java.util.Objects;
  30. @Slf4j
  31. @Service
  32. public class UnityServiceImpl implements IUnityService {
  33. @Resource
  34. private FYunFileServiceInterface fYunFileService;
  35. @Autowired
  36. private IUnityConfigService unityConfigService;
  37. @Resource
  38. private RedisUtil redisUtil;
  39. @Override
  40. public void packXspace(SceneXspace bean) throws Exception {
  41. //准备资源
  42. String workPath = this.pre(bean);
  43. //调用unity打包
  44. this.callUnity(workPath);
  45. //文件处理/上传
  46. this.dealFileAndUpload(bean, workPath);
  47. //删除本地资源
  48. FileUtil.del(workPath);
  49. }
  50. private String pre(SceneXspace bean){
  51. String num = bean.getNum();
  52. String workPath = NasPathConstant.UNITY_WORK_PATH + bean.getNum() + "/" + bean.getSerial() + "/";
  53. String localMeshPath = workPath + "mesh/";
  54. List<String> fileList = fYunFileService.listRemoteFiles(String.format(OSSPathConstant.SCENE_VIEW_DATA_DATA, num) + "mesh/");
  55. fileList.stream().forEach(v->{
  56. fYunFileService.downloadFile(v, localMeshPath);
  57. });
  58. return workPath;
  59. }
  60. private void callUnity(String workPath) throws Exception {
  61. UnityConfig unityConfig = unityConfigService.getOne(new LambdaQueryWrapper<>());
  62. String cmdStr = String.format(UnityConstant.EXEC_UNITY_FORMAT, workPath, unityConfig.getSerial(), unityConfig.getAccount(), unityConfig.getPassword());
  63. try {
  64. CmdUtils.callLine(cmdStr);
  65. }catch (Exception e){
  66. log.error("unity执行报错,workPath:{}", workPath, e);
  67. throw new PackException("unity执行报错");
  68. }
  69. String resultFilePath = workPath + "result.txt";
  70. boolean completed = this.checkComputeCompleted(resultFilePath, 3, 300);
  71. if(!completed){
  72. throw new PackException("unity异常,没有生成result.txt");
  73. }
  74. String resultStr = FileUtil.readUtf8String(resultFilePath);
  75. JSONObject resultObj = JSON.parseObject(resultStr);
  76. boolean success = resultObj.getBooleanValue("success");
  77. if(!success){
  78. throw new PackException(resultObj.getString("reason"));
  79. }
  80. }
  81. private boolean checkComputeCompleted(String uploadJsonPath, int maxCheckTimes, long waitTime) throws Exception{
  82. int checkTimes = 1;
  83. boolean exist = false;
  84. do {
  85. if(new File(uploadJsonPath).exists()){
  86. exist = true;
  87. break;
  88. }
  89. Thread.sleep(waitTime);
  90. ++checkTimes;
  91. }while (checkTimes <= maxCheckTimes);
  92. return exist;
  93. }
  94. private void dealFileAndUpload(SceneXspace bean, String workPath){
  95. String xspaceSceneOssPath = String.format(OSSPathConstant.XSPACE_SCENE_FORMAT,bean.getNum(), bean.getSerial());
  96. //上传mesh
  97. List<File> fileList = FileUtil.loopFiles(workPath);
  98. fileList.parallelStream().forEach(v->{
  99. fYunFileService.uploadFile(v.getAbsolutePath(), v.getAbsolutePath().replace(workPath, xspaceSceneOssPath));
  100. });
  101. //上传文件映射json
  102. fYunFileService.uploadFile(workPath + "xxx.json", xspaceSceneOssPath + "xxx.json");
  103. //复制skybox图
  104. String sourceImagesPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_IMAGES, bean.getNum()) + "tiles/4k/";
  105. String targetImagesPath = xspaceSceneOssPath + "images/";
  106. fYunFileService.copyFileInBucket(sourceImagesPath, targetImagesPath);
  107. //复制user
  108. String sourceUserPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_USER, bean.getNum());
  109. String targetUserPath = xspaceSceneOssPath + "user/";
  110. fYunFileService.copyFileInBucket(sourceUserPath, targetUserPath);
  111. //上传getInfo.json
  112. String getInfoKey = xspaceSceneOssPath + "getInfo.json";
  113. SceneInfoVO getInfoJson = this.getSceneInfo4View(bean.getNum());
  114. fYunFileService.uploadFile(JSON.toJSONString(getInfoJson).toString().getBytes(StandardCharsets.UTF_8), getInfoKey);
  115. //上传floorplan.json
  116. String floorplanPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_DATA, bean.getNum()) + "floorplan.json";
  117. if(getInfoJson.getFloorPlanUser() == 1){
  118. floorplanPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_USER, bean.getNum()) + "floorplan.json";
  119. }
  120. String xspaceFloorplanPath = xspaceSceneOssPath + "floorplan.json";
  121. fYunFileService.copyFileInBucket(floorplanPath, xspaceFloorplanPath);
  122. //复制vision.modeldata
  123. String sourceVisionPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_IMAGES, bean.getNum()) + "vision.modeldata";
  124. String tagetVisionPath = xspaceFloorplanPath + "vision.modeldata";
  125. fYunFileService.copyFileInBucket(sourceVisionPath, tagetVisionPath);
  126. }
  127. private SceneInfoVO getSceneInfo4View(String num){
  128. String key = String.format(RedisKey.SCENE_JSON, num);
  129. String sceneJson = redisUtil.get(key);
  130. SceneInfoVO sceneInfoVO = null;
  131. //先查询redis
  132. if(StrUtil.isEmpty(sceneJson)) {
  133. String objectName = String.format(OSSPathConstant.SCENE_VIEW_DATA, num) + "scene.json";
  134. sceneJson = fYunFileService.getFileContent(objectName);
  135. }
  136. sceneInfoVO = JSON.parseObject(sceneJson, SceneInfoVO.class);
  137. sceneInfoVO.setScenePassword(null);
  138. if(Objects.isNull(sceneInfoVO.getFloorPlanAngle())){
  139. sceneInfoVO.setFloorPlanAngle(0f);
  140. }
  141. if(Objects.isNull(sceneInfoVO.getFloorPlanCompass())){
  142. sceneInfoVO.setFloorPlanCompass(0f);
  143. }
  144. SceneEditControlsVO controls = sceneInfoVO.getControls();
  145. if(Objects.isNull(controls.getShowShare())){
  146. controls.setShowShare(CommonStatus.YES.code().intValue());
  147. }
  148. if(Objects.isNull(controls.getShowCapture())){
  149. controls.setShowCapture(CommonStatus.YES.code().intValue());
  150. }
  151. if(Objects.isNull(controls.getShowBillboardTitle())){
  152. controls.setShowBillboardTitle(CommonStatus.YES.code().intValue());
  153. }
  154. return sceneInfoVO;
  155. }
  156. }