UnityServiceImpl.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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){
  41. //准备资源
  42. String workPath = this.pre(bean);
  43. //调用unity打包
  44. this.callUnity(workPath);
  45. //文件处理/上传
  46. this.dealFileAndUpload(bean, workPath);
  47. }
  48. private String pre(SceneXspace bean){
  49. String num = bean.getNum();
  50. String workPath = NasPathConstant.UNITY_WORK_PATH + bean.getNum() + "/" + bean.getSerial() + "/";
  51. String localMeshPath = workPath + "mesh/";
  52. FileUtil.mkdir(localMeshPath);
  53. List<String> fileList = fYunFileService.listRemoteFiles(String.format(OSSPathConstant.SCENE_VIEW_DATA_DATA, num) + "mesh/");
  54. fileList.stream().forEach(v->{
  55. fYunFileService.downloadFile(v, localMeshPath);
  56. });
  57. return workPath;
  58. }
  59. private void callUnity(String workPath){
  60. UnityConfig unityConfig = unityConfigService.getOne(new LambdaQueryWrapper<>());
  61. String cmdStr = UnityConstant.EXEC_UNITY_FORMAT.replace("@workPath@",workPath).replace("@serial@", unityConfig.getSerial()).replace("@account@", unityConfig.getAccount()).replace("@pwd@", unityConfig.getPassword());
  62. // 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.json";
  70. boolean completed = this.checkComputeCompleted(resultFilePath, 3, 300);
  71. if(!completed){
  72. throw new PackException("unity异常,没有生成result.json");
  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("message"));
  79. }
  80. }
  81. private boolean checkComputeCompleted(String uploadJsonPath, int maxCheckTimes, long waitTime){
  82. int checkTimes = 1;
  83. boolean exist = false;
  84. do {
  85. if(new File(uploadJsonPath).exists()){
  86. exist = true;
  87. break;
  88. }
  89. try {
  90. Thread.sleep(waitTime);
  91. } catch (InterruptedException e) {
  92. throw new RuntimeException(e);
  93. }
  94. ++checkTimes;
  95. }while (checkTimes <= maxCheckTimes);
  96. return exist;
  97. }
  98. private void dealFileAndUpload(SceneXspace bean, String workPath){
  99. String xspaceSceneOssPath = String.format(OSSPathConstant.XSPACE_SCENE_FORMAT,bean.getNum(), bean.getSerial());
  100. //上传mesh
  101. List<File> fileList = FileUtil.loopFiles(workPath);
  102. fileList.parallelStream().forEach(v->{
  103. fYunFileService.uploadFile(v.getAbsolutePath(), v.getAbsolutePath().replace(workPath, xspaceSceneOssPath));
  104. });
  105. //上传文件映射json
  106. fYunFileService.uploadFile(workPath + "xxx.json", xspaceSceneOssPath + "xxx.json");
  107. //复制skybox图
  108. String sourceImagesPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_IMAGES, bean.getNum()) + "tiles/4k/";
  109. String targetImagesPath = xspaceSceneOssPath + "images/";
  110. fYunFileService.copyFileInBucket(sourceImagesPath, targetImagesPath);
  111. //复制user
  112. String sourceUserPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_USER, bean.getNum());
  113. String targetUserPath = xspaceSceneOssPath + "user/";
  114. fYunFileService.copyFileInBucket(sourceUserPath, targetUserPath);
  115. //上传getInfo.json
  116. String getInfoKey = xspaceSceneOssPath + "getInfo.json";
  117. SceneInfoVO getInfoJson = this.getSceneInfo4View(bean.getNum());
  118. fYunFileService.uploadFile(JSON.toJSONString(getInfoJson).toString().getBytes(StandardCharsets.UTF_8), getInfoKey);
  119. //上传floorplan.json
  120. String floorplanPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_DATA, bean.getNum()) + "floorplan.json";
  121. if(getInfoJson.getFloorPlanUser() == 1){
  122. floorplanPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_USER, bean.getNum()) + "floorplan.json";
  123. }
  124. String xspaceFloorplanPath = xspaceSceneOssPath + "floorplan.json";
  125. fYunFileService.copyFileInBucket(floorplanPath, xspaceFloorplanPath);
  126. //复制vision.modeldata
  127. String sourceVisionPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_IMAGES, bean.getNum()) + "vision.modeldata";
  128. String tagetVisionPath = xspaceFloorplanPath + "vision.modeldata";
  129. fYunFileService.copyFileInBucket(sourceVisionPath, tagetVisionPath);
  130. }
  131. private SceneInfoVO getSceneInfo4View(String num){
  132. String key = String.format(RedisKey.SCENE_JSON, num);
  133. String sceneJson = redisUtil.get(key);
  134. SceneInfoVO sceneInfoVO = null;
  135. //先查询redis
  136. if(StrUtil.isEmpty(sceneJson)) {
  137. String objectName = String.format(OSSPathConstant.SCENE_VIEW_DATA, num) + "scene.json";
  138. sceneJson = fYunFileService.getFileContent(objectName);
  139. }
  140. sceneInfoVO = JSON.parseObject(sceneJson, SceneInfoVO.class);
  141. sceneInfoVO.setScenePassword(null);
  142. if(Objects.isNull(sceneInfoVO.getFloorPlanAngle())){
  143. sceneInfoVO.setFloorPlanAngle(0f);
  144. }
  145. if(Objects.isNull(sceneInfoVO.getFloorPlanCompass())){
  146. sceneInfoVO.setFloorPlanCompass(0f);
  147. }
  148. SceneEditControlsVO controls = sceneInfoVO.getControls();
  149. if(Objects.isNull(controls.getShowShare())){
  150. controls.setShowShare(CommonStatus.YES.code().intValue());
  151. }
  152. if(Objects.isNull(controls.getShowCapture())){
  153. controls.setShowCapture(CommonStatus.YES.code().intValue());
  154. }
  155. if(Objects.isNull(controls.getShowBillboardTitle())){
  156. controls.setShowBillboardTitle(CommonStatus.YES.code().intValue());
  157. }
  158. return sceneInfoVO;
  159. }
  160. }