UnityServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package com.fdkankan.jp.xspace.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.alibaba.fastjson.JSON;
  6. import com.alibaba.fastjson.JSONArray;
  7. import com.alibaba.fastjson.JSONObject;
  8. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  9. import com.fdkankan.common.constant.CommonStatus;
  10. import com.fdkankan.common.util.CmdUtils;
  11. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  12. import com.fdkankan.jp.xspace.common.constant.NasPathConstant;
  13. import com.fdkankan.jp.xspace.common.constant.OSSPathConstant;
  14. import com.fdkankan.jp.xspace.common.constant.UnityConstant;
  15. import com.fdkankan.jp.xspace.common.exception.PackException;
  16. import com.fdkankan.jp.xspace.entity.SceneXspace;
  17. import com.fdkankan.jp.xspace.entity.UnityConfig;
  18. import com.fdkankan.jp.xspace.service.IUnityConfigService;
  19. import com.fdkankan.jp.xspace.service.IUnityService;
  20. import com.fdkankan.jp.xspace.vo.SceneEditControlsVO;
  21. import com.fdkankan.jp.xspace.vo.SceneInfoVO;
  22. import com.fdkankan.redis.constant.RedisKey;
  23. import com.fdkankan.redis.util.RedisUtil;
  24. import lombok.extern.slf4j.Slf4j;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.stereotype.Service;
  27. import javax.annotation.Resource;
  28. import java.io.File;
  29. import java.nio.charset.StandardCharsets;
  30. import java.util.List;
  31. import java.util.Objects;
  32. @Slf4j
  33. @Service
  34. public class UnityServiceImpl implements IUnityService {
  35. @Resource
  36. private FYunFileServiceInterface fYunFileService;
  37. @Autowired
  38. private IUnityConfigService unityConfigService;
  39. @Resource
  40. private RedisUtil redisUtil;
  41. @Override
  42. public void packXspace(SceneXspace bean){
  43. //准备资源
  44. String workPath = this.pre(bean);
  45. //调用unity打包
  46. this.callUnity(workPath);
  47. //文件处理/上传
  48. this.dealFileAndUpload(bean, 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. FileUtil.mkdir(localMeshPath);
  55. String meshKey = String.format(OSSPathConstant.SCENE_VIEW_DATA_DATA, num) + "mesh/";
  56. List<String> fileList = fYunFileService.listRemoteFiles(meshKey);
  57. fileList.stream().forEach(v->{
  58. fYunFileService.downloadFile(v, localMeshPath + v.replace(meshKey, ""));
  59. });
  60. //如果有三维模型,需要下载三维模型原始文件
  61. String sceneJsonKey = String.format(OSSPathConstant.SCENE_VIEW_DATA_DATA, bean.getNum()) + "scene.json";
  62. String sceneJsonStr = fYunFileService.getFileContent(sceneJsonKey);
  63. JSONObject sceneJsonObj = JSON.parseObject(sceneJsonStr);
  64. String boxModelsStr = sceneJsonObj.getString("boxModels");
  65. if(StrUtil.isNotEmpty(boxModelsStr)){
  66. List<JSONObject> jsonObjects = JSON.parseArray(boxModelsStr, JSONObject.class);
  67. if(CollUtil.isNotEmpty(jsonObjects)){
  68. String userViewPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_USER, bean.getNum());
  69. jsonObjects.stream().forEach(item->{
  70. String sid = item.getString("sid");
  71. String origObjPath = userViewPath + "boxModels/" + sid + "/";
  72. List<String> objFileList = fYunFileService.listRemoteFiles(origObjPath);
  73. if(CollUtil.isNotEmpty(objFileList)){
  74. objFileList.stream().forEach(objKey->{
  75. fYunFileService.downloadFile(objKey, workPath + objKey.replace(OSSPathConstant.SCENE_VIEW_DATA + bean.getNum() + "/", ""));
  76. });
  77. }
  78. });
  79. }
  80. }
  81. return workPath;
  82. }
  83. private void callUnity(String workPath){
  84. UnityConfig unityConfig = unityConfigService.getOne(new LambdaQueryWrapper<>());
  85. String cmdStr = UnityConstant.EXEC_UNITY_FORMAT.replaceAll("@workPath@",workPath).replace("@serial@", unityConfig.getSerial()).replace("@account@", unityConfig.getAccount()).replace("@pwd@", unityConfig.getPassword());
  86. // String.format(UnityConstant.EXEC_UNITY_FORMAT, workPath, unityConfig.getSerial(), unityConfig.getAccount(), unityConfig.getPassword());
  87. try {
  88. CmdUtils.callLine(cmdStr);
  89. }catch (Exception e){
  90. log.error("unity执行报错,workPath:{}", workPath, e);
  91. throw new PackException("unity执行报错");
  92. }
  93. String resultFilePath = workPath + "result.json";
  94. boolean completed = this.checkComputeCompleted(resultFilePath, 3, 300);
  95. if(!completed){
  96. throw new PackException("unity异常,没有生成result.json");
  97. }
  98. String resultStr = FileUtil.readUtf8String(resultFilePath);
  99. JSONObject resultObj = JSON.parseObject(resultStr);
  100. boolean success = resultObj.getBooleanValue("success");
  101. if(!success){
  102. throw new PackException(resultObj.getString("message"));
  103. }
  104. }
  105. private boolean checkComputeCompleted(String uploadJsonPath, int maxCheckTimes, long waitTime){
  106. int checkTimes = 1;
  107. boolean exist = false;
  108. do {
  109. if(new File(uploadJsonPath).exists()){
  110. exist = true;
  111. break;
  112. }
  113. try {
  114. Thread.sleep(waitTime);
  115. } catch (InterruptedException e) {
  116. throw new RuntimeException(e);
  117. }
  118. ++checkTimes;
  119. }while (checkTimes <= maxCheckTimes);
  120. return exist;
  121. }
  122. private void dealFileAndUpload(SceneXspace bean, String workPath){
  123. String xspaceSceneOssPath = String.format(OSSPathConstant.XSPACE_SCENE_FORMAT,bean.getNum(), bean.getSerial());
  124. //复制user
  125. String sourceUserPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_USER, bean.getNum());
  126. String targetUserPath = xspaceSceneOssPath + "user/";
  127. fYunFileService.copyFileInBucket(sourceUserPath, targetUserPath);
  128. //复制data
  129. String sourceDataPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_DATA, bean.getNum());
  130. String targetDataPath = xspaceSceneOssPath + "data/";
  131. fYunFileService.copyFileInBucket(sourceDataPath, targetDataPath);
  132. //删除mesh
  133. fYunFileService.deleteFolder(targetDataPath + "mesh");
  134. //复制images
  135. String sourceImagesPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_IMAGES, bean.getNum());
  136. String targetImagesPath = xspaceSceneOssPath + "images/";
  137. fYunFileService.copyFileInBucket(sourceImagesPath, targetImagesPath);
  138. //复制video
  139. String sourceVideoPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_VIDEO, bean.getNum());
  140. String targetVideoPath = xspaceSceneOssPath + "video/";
  141. fYunFileService.copyFileInBucket(sourceVideoPath, targetVideoPath);
  142. //复制vioce
  143. String sourceVoicePath = String.format(OSSPathConstant.SCENE_VIEW_DATA_VOICE, bean.getNum());
  144. String targetVoicePath = xspaceSceneOssPath + "vioce/";
  145. fYunFileService.copyFileInBucket(sourceVoicePath, targetVoicePath);
  146. //上传mesh
  147. List<File> fileList = FileUtil.loopFiles(workPath);
  148. fileList.parallelStream().forEach(v->{
  149. fYunFileService.uploadFile(v.getAbsolutePath(), v.getAbsolutePath().replace(workPath, targetDataPath));
  150. });
  151. //上传文件映射json
  152. // fYunFileService.uploadFile(workPath + "xxx.json", xspaceSceneOssPath + "xxx.json");
  153. // //复制skybox图
  154. // String sourceImagesPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_IMAGES, bean.getNum()) + "tiles/4k/";
  155. // String targetImagesPath = xspaceSceneOssPath + "images/";
  156. // fYunFileService.copyFileInBucket(sourceImagesPath, targetImagesPath);
  157. //
  158. // //复制user
  159. // String sourceUserPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_USER, bean.getNum());
  160. // String targetUserPath = xspaceSceneOssPath + "user/";
  161. // fYunFileService.copyFileInBucket(sourceUserPath, targetUserPath);
  162. //
  163. // //上传getInfo.json
  164. // String getInfoKey = xspaceSceneOssPath + "getInfo.json";
  165. // SceneInfoVO getInfoJson = this.getSceneInfo4View(bean.getNum());
  166. // fYunFileService.uploadFile(JSON.toJSONString(getInfoJson).toString().getBytes(StandardCharsets.UTF_8), getInfoKey);
  167. //
  168. // //上传floorplan.json
  169. // String floorplanPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_DATA, bean.getNum()) + "floorplan.json";
  170. // if(getInfoJson.getFloorPlanUser() == 1){
  171. // floorplanPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_USER, bean.getNum()) + "floorplan.json";
  172. // }
  173. // String xspaceFloorplanPath = xspaceSceneOssPath + "floorplan.json";
  174. // fYunFileService.copyFileInBucket(floorplanPath, xspaceFloorplanPath);
  175. //
  176. // //复制vision.modeldata
  177. // String sourceVisionPath = String.format(OSSPathConstant.SCENE_VIEW_DATA_IMAGES, bean.getNum()) + "vision.modeldata";
  178. // String tagetVisionPath = xspaceSceneOssPath + "vision.modeldata";
  179. // fYunFileService.copyFileInBucket(sourceVisionPath, tagetVisionPath);
  180. }
  181. private SceneInfoVO getSceneInfo4View(String num){
  182. String key = String.format(RedisKey.SCENE_JSON, num);
  183. String sceneJson = redisUtil.get(key);
  184. SceneInfoVO sceneInfoVO = null;
  185. //先查询redis
  186. if(StrUtil.isEmpty(sceneJson)) {
  187. String objectName = String.format(OSSPathConstant.SCENE_VIEW_DATA_DATA, num) + "scene.json";
  188. sceneJson = fYunFileService.getFileContent(objectName);
  189. }
  190. sceneInfoVO = JSON.parseObject(sceneJson, SceneInfoVO.class);
  191. sceneInfoVO.setScenePassword(null);
  192. if(Objects.isNull(sceneInfoVO.getFloorPlanAngle())){
  193. sceneInfoVO.setFloorPlanAngle(0f);
  194. }
  195. if(Objects.isNull(sceneInfoVO.getFloorPlanCompass())){
  196. sceneInfoVO.setFloorPlanCompass(0f);
  197. }
  198. SceneEditControlsVO controls = sceneInfoVO.getControls();
  199. if(Objects.isNull(controls.getShowShare())){
  200. controls.setShowShare(CommonStatus.YES.code().intValue());
  201. }
  202. if(Objects.isNull(controls.getShowCapture())){
  203. controls.setShowCapture(CommonStatus.YES.code().intValue());
  204. }
  205. if(Objects.isNull(controls.getShowBillboardTitle())){
  206. controls.setShowBillboardTitle(CommonStatus.YES.code().intValue());
  207. }
  208. return sceneInfoVO;
  209. }
  210. }