Browse Source

优化runbuild

zhujinghui 4 năm trước cách đây
mục cha
commit
47f0d05736
1 tập tin đã thay đổi với 127 bổ sung83 xóa
  1. 127 83
      src/main/java/com/example/demo/listener/RunBuild.java

+ 127 - 83
src/main/java/com/example/demo/listener/RunBuild.java

@@ -19,9 +19,11 @@ import org.springframework.amqp.rabbit.annotation.RabbitListener;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
 import tk.mybatis.mapper.entity.Condition;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.*;
 
 /**
@@ -184,8 +186,12 @@ public class RunBuild {
                 for(int i = 0, len = checkArray.size(); i < len; i++){
                     JSONObject objectJson = checkArray.getJSONObject(i);
 
-                    if(uploadObj && objectJson.containsKey("simpleModel") && "done".equals(objectJson.getString("simpleModel"))){
-                        log.info("vrnum-{},进入simpleModel方法", vrNum);
+                    if(isCanDownLoad(uploadObj, objectJson, vrNum)){
+
+                        log.info("下载前先睡眠5秒-{}", vrNum);
+                        // 由于阿里云资源上传有延时问题,所以需要延时一点时间再下载
+                        Thread.sleep(5000L);
+
                         uploadObjData(vrNum, sceneNum);
                         uploadObj = false;
                     }
@@ -281,6 +287,33 @@ public class RunBuild {
     }
 
     /**
+     * 是否可以下载
+     * @param uploadObj
+     * @param objectJson
+     * @return
+     */
+    private boolean isCanDownLoad(boolean uploadObj, JSONObject objectJson, String vrNum) {
+        JSONArray checkArray = objectJson.getJSONArray("params");
+        if (CollectionUtils.isEmpty(checkArray)) {
+            return false;
+        }
+        if (!uploadObj || !objectJson.containsKey("simpleModel")
+            || !"done".equals(objectJson.getString("simpleModel"))) {
+            return false;
+        }
+        for(int i = 0, len = checkArray.size(); i < len; i++){
+            JSONObject params = checkArray.getJSONObject(i);
+            JSONObject simpleModel = params.getJSONObject("simpleModel");
+            Object name = simpleModel.get("name");
+            Object inSimple = simpleModel.get("simpleModel");
+            if (vrNum.equals(name) && "done".equals(inSimple)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
      * 转换obj,并且上传
      * @param vrNum
      * @param sceneNum
@@ -679,73 +712,11 @@ public class RunBuild {
             jsonObject.put("postfix", modelType);
 //            String buildResult = OkHttpUtils.httpPostJson(buildUrl + "pro", jsonObject.toJSONString());
 
-
-            while (true){
-                String buildResult = OkHttpUtils.httpPostJson(modelBuildUrl + "pro", jsonObject.toJSONString());
-                JSONObject buildJson = JSONObject.parseObject(buildResult);
-
-                if("isBusy".equals(buildJson.getString("msg"))){
-                    Thread.sleep(2000L);
-                    continue;
-                }
-
-                if("error".equals(buildJson.getString("state"))){
-                    FileUtils.writeFile(filePath + "/error-pro-model.json", buildJson.toJSONString());
-                    throw new RuntimeException(fileId + ":datafactory方式pro接口返回error错误");
-                }
-
-                break;
-            }
+            postJson(fileId, filePath, jsonObject);
 
             Thread.sleep(1000L);
-            boolean over = false;
-            while (true) {
-                String checkResult = OkHttpUtils.httpPostJson(modelBuildUrl + "check", jsonObject.toJSONString());
-                JSONObject checkJson = JSONObject.parseObject(checkResult);
-
-                JSONArray checkArray = checkJson.getJSONArray("msg");
-
-                if (checkArray.size() == 0) {
-                    FileUtils.writeFile(filePath + "/error-check-model.json", checkJson.toJSONString());
-                    throw new RuntimeException(fileId + ":datafactory方式check接口返回error错误");
-                }
-
-                for(int i = 0, len = checkArray.size(); i < len; i++){
-                    JSONObject objectJson = checkArray.getJSONObject(i);
-
-                    if("error".equals(objectJson.getString("state"))){
-                        FileUtils.writeFile(filePath + "/error-check-model.json", checkJson.toJSONString());
-                        throw new RuntimeException(fileId + ":datafactory方式check接口返回error错误");
-                    }
-
-                    if(objectJson.containsKey("state") && ("".equals(objectJson.getString("state")) ||
-                            "done".equals(objectJson.getString("state")) )){
-                        over = true;
-                    }
-
-                    if(objectJson.containsKey("progress")){
-                        modelUploadEntity = new ModelUploadEntity();
-                        modelUploadEntity.setId(Long.valueOf(modelUploadId));
-                        modelUploadEntity.setProgress(objectJson.getInteger("progress"));
-                        modelUploadService.update(modelUploadEntity);
-                    }
-
-                }
-
-                if(over){
-                    break;
-                }
-
-                Thread.sleep(1000L);
-            }
-
-//            List<String> list = uploadToOssUtil.fileFolderTo4dTjw("domain/eHome/furniture/InitialData/" + modelType + "/" + fileId);
-//            for (String s : list) {
-//                if(s.toLowerCase().endsWith(".zip") || s.toLowerCase().equals(".rar")){
-//                    continue;
-//                }
-//                uploadToOssUtil.downloadFileTo4dTjw(s, buildPath + "upload" + File.separator + fileId + File.separator + s.substring(s.lastIndexOf("/") + 1));
-//            }
+            
+            checkResultJson(fileId, filePath, jsonObject, modelUploadId);
 
             Integer facesNum = 0;
             if("fbx".equals(modelType)){
@@ -761,15 +732,6 @@ public class RunBuild {
 //                uploadToOssUtil.downloadFileTo4dTjw("domain/eHome/furniture/ue4data/" + fileId + "/" + fileId + "_preview.jpg", filePath + fileId + "_preview.jpg");
                 CreateObjUtil.ossCommonUtilCp("domain/eHome/furniture/ue4data/" + fileId, filePath);
 
-//                if(new File(filePath + fileId + ".fbx").exists()){
-//                    OkHttpUtils.httpGet(maxObjUrl + "fbxToObjNoImg?uuid=" + fileId +"&fbx=" + fileId + ".fbx" +
-//                            "&obj=" + fileId + "-base.obj&r=100");
-//
-//                    facesNum = ObjProcess.findFacesNum(filePath + fileId + "-base.obj");
-//
-//                    OkHttpUtils.httpGet(maxObjUrl + "objToObj?uuid=" + fileId +"&inObj=" + fileId +
-//                            "-base.obj&outObj=" + fileId + ".obj&r=3000");
-//                }
 
                 if(new File(filePath + fileId + "-base.obj").exists()){
                     facesNum = ObjProcess.findFacesNum(filePath + fileId + "-base.obj");
@@ -789,13 +751,6 @@ public class RunBuild {
                 modelUploadEntity.setThumPath((filePath + fileId + "_preview.jpg").replace(buildPath, ""));
             }
 
-//            FileUtils.writeFile(filePath + "mesh.mtl", ObjProcess.createMtlFile(fileId + ".jpg"));
-//
-//            ObjProcess.addObjMsg(objPath, objPath.replace(".obj", "-new.obj"), "mtllib mesh.mtl");
-//            File objFile = new File(objPath);
-//            File newObjFile = new File(objPath.replace(".obj", "-new.obj"));
-//            objFile.delete();
-//            newObjFile.renameTo(objFile);
 
             modelUploadEntity.setStatus(1);
             modelUploadEntity.setObjPath(objPath.replace(buildPath, ""));
@@ -811,6 +766,95 @@ public class RunBuild {
 
     }
 
+    /**
+     * 循环检查3d渲染
+     * @param fileId
+     * @param filePath
+     * @param jsonObject
+     * @param modelUploadId
+     * @throws InterruptedException
+     * @throws IOException
+     */
+    private void checkResultJson(String fileId, String filePath, JSONObject jsonObject, String modelUploadId) throws InterruptedException, IOException {
+        // 循环检查渲染结果的次数
+        int times = 0;
+        boolean over = false;
+        while (true) {
+            String checkResult = OkHttpUtils.httpPostJson(modelBuildUrl + "check", jsonObject.toJSONString());
+            JSONObject checkJson = JSONObject.parseObject(checkResult);
+
+            JSONArray checkArray = checkJson.getJSONArray("msg");
+
+            log.info("请求检查3d场景渲染接口获取接口数据-{}", checkJson);
+
+            if (checkArray.size() == 0) {
+                FileUtils.writeFile(filePath + "/error-check-model.json", checkJson.toJSONString());
+                throw new RuntimeException(fileId + ":datafactory方式check接口返回error错误");
+            }
+
+            for(int i = 0, len = checkArray.size(); i < len; i++){
+                JSONObject objectJson = checkArray.getJSONObject(i);
+
+                if("error".equals(objectJson.getString("state"))){
+                    FileUtils.writeFile(filePath + "/error-check-model.json", checkJson.toJSONString());
+                    throw new RuntimeException(fileId + ":datafactory方式check接口返回error错误");
+                }
+
+                if(objectJson.containsKey("state") && ("".equals(objectJson.getString("state")) ||
+                        "done".equals(objectJson.getString("state")) )){
+                    over = true;
+                }
+
+                if(objectJson.containsKey("progress")){
+                    Integer progress = objectJson.getInteger("progress");
+                    log.info("第{}次获取上传进度,进度为{}%", times++, progress);
+                    ModelUploadEntity modelUploadEntity = new ModelUploadEntity();
+                    modelUploadEntity.setId(Long.valueOf(modelUploadId));
+                    modelUploadEntity.setProgress(progress);
+                    modelUploadService.update(modelUploadEntity);
+                }
+
+            }
+
+            if(over){
+                break;
+            }
+
+            Thread.sleep(1000L);
+        }
+    }
+
+    /**
+     * 通知3D渲染业务端发送渲染请求,并不断请求获取渲染结果
+     * isBusy:忙碌状态,继续请求获取渲染结果
+     * error: 获取结果失败,直接上传失败
+     * @param fileId
+     * @param filePath
+     * @param jsonObject
+     * @throws InterruptedException
+     * @throws IOException
+     */
+    private void postJson(String fileId, String filePath, JSONObject jsonObject) throws InterruptedException, IOException {
+        while (true){
+            String buildResult = OkHttpUtils.httpPostJson(modelBuildUrl + "pro", jsonObject.toJSONString());
+            JSONObject buildJson = JSONObject.parseObject(buildResult);
+
+            log.info("请求3d场景渲染接口获取接口数据-{}", buildJson);
+
+            if("isBusy".equals(buildJson.getString("msg"))){
+                Thread.sleep(2000L);
+                continue;
+            }
+
+            if("error".equals(buildJson.getString("state"))){
+                FileUtils.writeFile(filePath + "/error-pro-model.json", buildJson.toJSONString());
+                throw new RuntimeException(fileId + ":datafactory方式pro接口返回error错误");
+            }
+
+            break;
+        }
+    }
+
     private void modelCreatThum(String data){
         String[] datas = data.split(":;");
         String fileId = datas[0];