BuildSceneReceiver.java 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. package com.fdkankan.modeling.receiver;
  2. import cn.hutool.core.date.DateUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.fdkankan.utils.constant.ConstantFilePath;
  6. import com.fdkankan.utils.constant.ConstantUrl;
  7. import com.fdkankan.mq.message.BuildSceneMqMessage;
  8. import com.fdkankan.utils.utils.CreateObjUtil;
  9. import com.fdkankan.utils.utils.FileUtil;
  10. import com.fdkankan.utils.utils.FileUtils;
  11. import com.fdkankan.modeling.constants.RedisKey;
  12. import com.fdkankan.modeling.constants.SysConstants;
  13. import com.fdkankan.modeling.entity.SceneFileBuild;
  14. import com.fdkankan.modeling.entity.ScenePro;
  15. import com.fdkankan.modeling.entity.SceneProExt;
  16. import com.fdkankan.modeling.entity.User;
  17. import com.fdkankan.modeling.push.PushMessageConfig;
  18. import com.fdkankan.modeling.service.*;
  19. import com.fdkankan.modeling.utils.ComputerUtil;
  20. import com.fdkankan.modeling.utils.DingDingUtils;
  21. import com.fdkankan.modeling.utils.PushMsgUtil;
  22. import com.fdkankan.oss.UploadUtils;
  23. import com.taobao.api.ApiException;
  24. import lombok.extern.log4j.Log4j2;
  25. import org.apache.commons.lang3.StringUtils;
  26. import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
  27. import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
  28. import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
  29. import org.apache.rocketmq.common.message.MessageExt;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.beans.factory.annotation.Value;
  32. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  33. import org.springframework.data.redis.core.RedisTemplate;
  34. import org.springframework.stereotype.Component;
  35. import org.springframework.util.CollectionUtils;
  36. import org.springframework.util.ObjectUtils;
  37. import org.springframework.web.client.RestTemplate;
  38. import java.io.*;
  39. import java.security.InvalidKeyException;
  40. import java.security.NoSuchAlgorithmException;
  41. import java.time.Duration;
  42. import java.time.temporal.ChronoUnit;
  43. import java.util.Date;
  44. import java.util.HashMap;
  45. import java.util.List;
  46. import java.util.Map;
  47. import java.util.concurrent.*;
  48. import static com.fdkankan.modeling.push.PushMessageConfig.*;
  49. @Log4j2
  50. @Component
  51. @ConditionalOnProperty(name = "rabbitmq.queue.enable",havingValue = "true")
  52. public class BuildSceneReceiver implements MessageListenerConcurrently {
  53. @Autowired
  54. private ISceneService sceneService;
  55. @Autowired
  56. private ISceneProService sceneProService;
  57. @Autowired
  58. private IUserService userService;
  59. @Autowired
  60. private ISceneFileBuildService sceneFileBuildService;
  61. @Autowired
  62. private ISceneProExtService sceneProExtService;
  63. @Autowired
  64. private RedisTemplate<String,String> redisTemplate;
  65. private RestTemplate restTemplate = new RestTemplate();
  66. @Value("${4dkk.laserService.host}")
  67. private String laserHost;
  68. @Value("${oss.type}")
  69. private String ossType;
  70. @Value("${oss.url.prefix}")
  71. private String prefixAli;
  72. @Autowired
  73. private UploadUtils uploadUtils;
  74. @Override
  75. public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgExt,
  76. ConsumeConcurrentlyContext consumeConcurrentlyContext) {
  77. if (CollectionUtils.isEmpty(msgExt)) {
  78. log.error("消息内容为空,退出构建,当前服务器id:{}", SysConstants.hostName);
  79. return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
  80. }
  81. MessageExt messageExt = msgExt.get(0);
  82. BuildSceneMqMessage message = JSONObject.parseObject(messageExt.getBody(), BuildSceneMqMessage.class);
  83. log.info("开始处理消息,消息队列:{},消息内容:{}", consumeConcurrentlyContext.getMessageQueue().getTopic(),
  84. JSONObject.toJSONString(message));
  85. process(message);
  86. return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
  87. }
  88. public void process(BuildSceneMqMessage message) {
  89. final ExecutorService exec = Executors.newFixedThreadPool(1);
  90. Callable<String> call = (Callable<String>) () -> {
  91. //开始执行耗时操作
  92. try {
  93. String key = RedisKey.SCENE_BUILDING + message.getSceneNum();
  94. // 获取缓存锁,防止重复消费
  95. Long building = redisTemplate.opsForValue().increment(key, 1);
  96. if (building.compareTo(1L) != 0) {
  97. log.error("场景正在构建中,退出构建,当前服务器id:{},参数:{}", SysConstants.hostName, JSONObject.toJSONString(message));
  98. } else {
  99. redisTemplate.expire(key, Duration.of(SysConstants.modelTimeOut, ChronoUnit.HOURS));
  100. }
  101. //休眠2秒等待数据入库
  102. Thread.sleep(2000L);
  103. try {
  104. FileUtils.writeFile("/opt/hosts/running.txt", DateUtil.formatDateTime(new Date()));
  105. String tomcatLog = "scenenum:" + message.getSceneNum() + "\ntime:" + DateUtil.formatDateTime(new Date()) +
  106. " action:create hostname:" + SysConstants.hostName;
  107. //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
  108. FileWriter writer = new FileWriter("/mnt/elastic_log/tomcat" + "_" + message.getSceneNum() + ".log", true);
  109. writer.write(tomcatLog);
  110. writer.close();
  111. } catch (Exception e) {
  112. e.printStackTrace();
  113. }
  114. buildScene(message);
  115. redisTemplate.delete(key);
  116. try {
  117. FileUtils.deleteFile("/opt/hosts/running.txt");
  118. String tomcatLog = "scenenum:" + message.getSceneNum() + "\ntime:" + DateUtil.formatDateTime(new Date()) +
  119. " action:delete hostname:" + SysConstants.hostName;
  120. //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
  121. FileWriter writer = new FileWriter("/mnt/elastic_log/tomcat" + "_" + message.getSceneNum() + "log", true);
  122. writer.write(tomcatLog);
  123. writer.close();
  124. } catch (Exception e) {
  125. e.printStackTrace();
  126. }
  127. } catch (Exception e) {
  128. e.printStackTrace();
  129. sceneProService.updateStatus(message.getSceneNum(), -1);
  130. BuildSceneReceiver.this.handFail("计算失败", message);
  131. }
  132. log.info("场景生成好了***");
  133. return "success";
  134. };
  135. Future<String> future = exec.submit(call);
  136. try {
  137. future.get(SysConstants.modelTimeOut, TimeUnit.HOURS); //任务处理超时时间设为 24个小时
  138. } catch (TimeoutException ex) {
  139. ex.printStackTrace();
  140. StringWriter trace=new StringWriter();
  141. ex.printStackTrace(new PrintWriter(trace));
  142. log.error("超时了");
  143. log.error(trace.toString());
  144. FileUtils.deleteFile("/opt/hosts/running.txt");
  145. future.cancel(true);
  146. handFail("计算超时",message);
  147. } catch (Exception e) {
  148. e.printStackTrace();
  149. StringWriter trace=new StringWriter();
  150. e.printStackTrace(new PrintWriter(trace));
  151. log.error(trace.toString());
  152. FileUtils.deleteFile("/opt/hosts/running.txt");
  153. future.cancel(true);
  154. handFail("计算失败",message);
  155. }
  156. }
  157. private void buildScene(BuildSceneMqMessage message){
  158. String projectNum = null;
  159. SceneFileBuild sceneFileBuildEntity = null;
  160. ScenePro scene = null;
  161. SceneProExt sceneProExt = null;
  162. try{
  163. if(StringUtils.equals(message.getIsStandardization(),"1")){
  164. //表示标定算法
  165. ComputerUtil.computerCalibration(message.getPath());
  166. return;
  167. }
  168. String unicode = message.getUnicode();
  169. String path = message.getPath();
  170. String prefix = message.getPrefix();
  171. String imgsName = message.getImgsName();
  172. projectNum = message.getSceneNum();
  173. String userName = message.getUserName();
  174. //不同的相机不同的方法
  175. String cameraType = message.getCameraType();
  176. String algorithm = message.getAlgorithm();
  177. String fileId = message.getFileId();
  178. String cameraName = message.getCameraName();
  179. //0表示有4k图,1表示没有
  180. String resolution = message.getResolution();
  181. //判断调用V2还是V3版本的算法
  182. String buildType = ObjectUtils.isEmpty(message.getBuildType()) ? "V2" : message.getBuildType();
  183. log.info("执行数据库操作--前");
  184. sceneFileBuildEntity = sceneFileBuildService.findByFileId(fileId);
  185. log.info("执行数据库操作--后");
  186. Map<String, String> dataMap = ComputerUtil.getTypeString(cameraType, algorithm,
  187. resolution);
  188. String splitType = dataMap.get("splitType");
  189. String skyboxType = dataMap.get("skyboxType");
  190. String dataDescribe = dataMap.get("dataDescribe");
  191. log.info("用的算法是:"+algorithm);
  192. log.info("用的相机是:"+ (Integer.parseInt(cameraType) < 4 ? "单球目" : "双球目(八目)"));
  193. Map<String,String> map = new HashMap<>();
  194. //该场景使用的容量
  195. Long space = 0L;
  196. //支付状态
  197. int payStatus = 0;
  198. //4表示硬件部研发的双球目相机,其余为旧版本相机
  199. if(Integer.parseInt(cameraType) < 3){
  200. for(int i = 0;i<5;++i){
  201. try{
  202. FileUtils.downLoadFromUrl(prefix+imgsName+"?m="+new Date().getTime(), imgsName,
  203. path + File.separator + "capture");
  204. FileUtils.decompress(path + File.separator + "capture" +File.separator+imgsName,
  205. path + File.separator + "capture") ;
  206. break;
  207. }
  208. catch(Exception e){
  209. e.printStackTrace();
  210. StringWriter trace=new StringWriter();
  211. e.printStackTrace(new PrintWriter(trace));
  212. log.error(trace.toString());
  213. if(i<4)
  214. {
  215. FileUtils.deleteFile(path + File.separator + "capture" +File.separator+imgsName);
  216. FileUtils.delFolder(path + File.separator + "capture" +File.separator+"images");
  217. }
  218. Thread.sleep(10000);
  219. }
  220. }
  221. FileUtils.deleteFile(path + File.separator + "capture" +File.separator+"zip.Zip");
  222. String data = FileUtils.readFile(path + File.separator + "capture" +File.separator+"data.fdage");
  223. JSONObject dataJson = new JSONObject();
  224. if(data!=null){
  225. dataJson = JSONObject.parseObject(data);
  226. if(dataJson.containsKey("imgs")){
  227. splitType = "SPLIT_V4";
  228. }
  229. }
  230. //生成project.json和data.json供算法部使用
  231. ComputerUtil.createJson(path, splitType, skyboxType, dataDescribe, projectNum, path);
  232. //计算模型并返回需要上传oss的文件集合
  233. map = ComputerUtil.computer(projectNum, path, buildType);
  234. uploadUtils.uploadMulFiles(map);
  235. log.info("双目上完oss结束修改数据:"+projectNum);
  236. sceneService.updateTime(projectNum, space, payStatus);
  237. } else {
  238. //休眠1秒,否则可能获取不到场景资源
  239. Thread.sleep(1000);
  240. scene = sceneProService.findBySceneNum(projectNum);
  241. sceneProExt = sceneProExtService.lambdaQuery().eq(SceneProExt::getSceneProId, scene.getId()).getEntity();
  242. if(scene == null){
  243. log.info(projectNum + ":场景不存在");
  244. handFail("场景不存在",message);
  245. return;
  246. }
  247. long start = System.currentTimeMillis();
  248. //cameraType=5为新版本双目, cameraType=6为小红屋新版本双目
  249. if(Integer.parseInt(cameraType) == 5 || Integer.parseInt(cameraType) == 6){
  250. path = ConstantFilePath.BUILD_MODEL_PATH + unicode;
  251. //下载zip包,并解压
  252. FileUtils.downLoadFromUrl(prefix + "/" + imgsName + "?m=" + System.currentTimeMillis(), imgsName, path + File.separator + "capture");
  253. FileUtils.decompress(path + File.separator + "capture" + File.separator + imgsName,
  254. path + File.separator + "capture") ;
  255. FileUtil.delFile(path + File.separator + "capture" + File.separator + imgsName);
  256. }else if(Integer.parseInt(cameraType) == 14 ) {
  257. CreateObjUtil.ossUtilCp(ConstantFilePath.OSS_PREFIX + cameraName.replace("4DKKPRO_", "")
  258. .replace("-fdage", "").toLowerCase() + File.separator + fileId + File.separator
  259. + unicode + File.separator,
  260. ConstantFilePath.BUILD_MODEL_LASER_PATH + cameraName.replace("4DKKPRO_", "")
  261. .replace("-fdage", "").toLowerCase() + File.separator + fileId + File.separator
  262. + unicode + File.separator + "capture");
  263. path = ConstantFilePath.BUILD_MODEL_LASER_PATH + cameraName.replace("4DKKPRO_", "").replace("-fdage", "").toLowerCase() + File.separator + fileId + File.separator + unicode;
  264. } else if(Integer.parseInt(cameraType) >= 4 ) {
  265. CreateObjUtil.ossUtilCp(ConstantFilePath.OSS_PREFIX + cameraName.replace("4DKKPRO_", "")
  266. .replace("-fdage", "").toLowerCase() + File.separator + fileId + File.separator
  267. + unicode + File.separator,
  268. ConstantFilePath.BUILD_MODEL_PATH + cameraName.replace("4DKKPRO_", "")
  269. .replace("-fdage", "").toLowerCase() + File.separator + fileId + File.separator
  270. + unicode + File.separator + "capture");
  271. path = ConstantFilePath.BUILD_MODEL_PATH + cameraName.replace("4DKKPRO_", "").replace("-fdage", "").toLowerCase() + File.separator + fileId + File.separator + unicode;
  272. }
  273. String data = FileUtils.readFile(path + File.separator + "capture" +File.separator+"data.fdage");
  274. //获取data.fdage的内容
  275. JSONObject dataJson = new JSONObject();
  276. if(data!=null){
  277. dataJson = JSONObject.parseObject(data);
  278. }
  279. //判断是否计算过资源,若计算过删除缓存
  280. File caches = new File(path + File.separator + "caches");
  281. if(caches.exists()){
  282. for(File deleteFile : caches.listFiles()){
  283. if(new File(path + "_images").exists()){
  284. if(deleteFile.isDirectory()){
  285. FileUtils.delAllFile(deleteFile.getAbsolutePath());
  286. }else {
  287. FileUtils.deleteFile(deleteFile.getAbsolutePath());
  288. }
  289. }
  290. else if(!deleteFile.getAbsolutePath().contains("images")){
  291. if(deleteFile.isDirectory()){
  292. FileUtils.delAllFile(deleteFile.getAbsolutePath());
  293. }else {
  294. FileUtils.deleteFile(deleteFile.getAbsolutePath());
  295. }
  296. }
  297. }
  298. }
  299. if(new File(path + File.separator + "results").exists()){
  300. FileUtils.delAllFile(path + File.separator + "results");
  301. }
  302. //生成project.json和data.json供算法部使用
  303. log.info("path:" + path);
  304. if(dataJson.containsKey("videoVersion") && StringUtils.isNotEmpty(dataJson.getString("videoVersion")) && Integer.parseInt(dataJson.getString("videoVersion")) < 4){
  305. //v2版本使用4k算法
  306. skyboxType = "SKYBOX_V6";
  307. }
  308. ComputerUtil.createJson(path, splitType, skyboxType, dataDescribe, projectNum, path);
  309. //计算模型并返回需要上传oss的文件集合
  310. map = ComputerUtil.computer(projectNum, path, buildType);
  311. if(Integer.parseInt(cameraType) == 5 || Integer.parseInt(cameraType) == 6){
  312. map.put(path + File.separator + "capture/stitch_params.txt", "data/data" + projectNum + "/stitch_params.txt");
  313. }
  314. map.put(path + File.separator + "capture/Up.xml", "data/data" + projectNum + "/Up.xml");
  315. map.put(path + File.separator + "capture/Up2.xml", "data/data" + projectNum + "/Up2.xml");
  316. if(Integer.parseInt(cameraType) == 13){
  317. //转台相机
  318. map.put(path + File.separator + "capture/Up.txt", "data/data" + projectNum + "/Up.txt");
  319. map.put(path + File.separator + "capture/Up2.txt", "data/data" + projectNum + "/Up2.txt");
  320. }
  321. uploadUtils.uploadMulFiles(map);
  322. payStatus = 1;
  323. File spaceFile = null;
  324. for (String key : map.keySet()) {
  325. spaceFile = new File(key);
  326. if(spaceFile.exists()){
  327. space += spaceFile.length();
  328. }
  329. }
  330. log.info("八目上完oss结束修改数据:"+projectNum);
  331. //获取upload中的video视频名称
  332. String uploadData = FileUtils.readFile(path + File.separator + "results" +File.separator+"upload.json");
  333. com.alibaba.fastjson.JSONObject uploadJson = null;
  334. JSONArray array = null;
  335. if(uploadData!=null) {
  336. uploadJson = com.alibaba.fastjson.JSONObject.parseObject(uploadData);
  337. array = uploadJson.getJSONArray("upload");
  338. }
  339. com.alibaba.fastjson.JSONObject fileJson = null;
  340. String fileName = "";
  341. //读取videos_hdr_param.json, 保存点位视频的value
  342. Map<String, Object> videoMap = new HashMap<>();
  343. String videosHdr = FileUtils.readFile(path + File.separator + "results/videos/videos_hdr_param.json");
  344. JSONArray videoArray = null;
  345. if(StringUtils.isNotEmpty(videosHdr)){
  346. videoArray = com.alibaba.fastjson.JSONObject.parseObject(videosHdr).getJSONArray("hdr_param");
  347. }
  348. if(videoArray != null){
  349. for(int i = 0, len = videoArray.size(); i < len; i++) {
  350. videoMap.put(videoArray.getJSONObject(i).getString("name"), videoArray.getJSONObject(i).getString("value"));
  351. if(videoArray.getJSONObject(i).containsKey("fov")){
  352. videoMap.put(videoArray.getJSONObject(i).getString("name") + "_fov", videoArray.getJSONObject(i).getString("fov"));
  353. }
  354. }
  355. }
  356. //计算ts文件的大小,并拼接成json格式
  357. JSONArray jsonArray = new JSONArray();
  358. com.alibaba.fastjson.JSONObject videoJson = null;
  359. com.alibaba.fastjson.JSONObject videosJson = new com.alibaba.fastjson.JSONObject();
  360. long videoSize = 0L;
  361. for(int i = 0, len = array.size(); i < len; i++) {
  362. fileJson = array.getJSONObject(i);
  363. fileName = fileJson.getString("file");
  364. if(fileJson.getIntValue("clazz") == 11 && fileName.contains(".mp4") && !fileName.contains("-ios.mp4")){
  365. videoJson = new com.alibaba.fastjson.JSONObject();
  366. videoJson.put("id", fileName.substring(
  367. 0, fileName.lastIndexOf(".")).replace("videos/", ""));
  368. //如果ts文件存在,就计算ts大小
  369. if(new File(path + File.separator + "results" +File.separator+ fileName.replace(".mp4", ".ts")).exists()){
  370. videoSize = new File(path + File.separator + "results" +File.separator+ fileName.replace(".mp4", ".ts")).length();
  371. videoJson.put("tsSize", videoSize);
  372. }
  373. if(videoMap.containsKey(videoJson.get("id"))){
  374. videoJson.put("value", videoMap.get(videoJson.get("id")));
  375. }
  376. if(videoMap.containsKey(videoJson.get("id") + "_fov")){
  377. videoJson.put("blend_fov", videoMap.get(videoJson.get("id") + "_fov"));
  378. }else {
  379. videoJson.put("blend_fov", 7);
  380. }
  381. jsonArray.add(videoJson);
  382. }
  383. }
  384. videosJson.put("data", jsonArray);
  385. if(dataJson.containsKey("videoVersion") && StringUtils.isNotEmpty(dataJson.getString("videoVersion")) && Integer.parseInt(dataJson.getString("videoVersion")) >= 4){
  386. videosJson.put("version", 3);
  387. if("oss".equals(ossType)){
  388. videosJson.put("upPath", prefixAli + "data/data" + projectNum + "/Up.xml");
  389. }
  390. if("s3".equals(ossType)){
  391. videosJson.put("upPath", ConstantUrl.PREFIX_AWS + "data/data" + projectNum + "/Up.xml");
  392. }
  393. if(Integer.parseInt(cameraType) == 13){
  394. //转台相机
  395. videosJson.put("upPath", videosJson.getString("upPath").replace(".xml", ".txt"));
  396. }
  397. }else {
  398. videosJson.put("version", 1);
  399. if("oss".equals(ossType)){
  400. videosJson.put("upPath", prefixAli + "data/data" + projectNum + "/Up2.xml");
  401. }
  402. if("s3".equals(ossType)){
  403. videosJson.put("upPath", ConstantUrl.PREFIX_AWS + "data/data" + projectNum + "/Up2.xml");
  404. }
  405. if(Integer.parseInt(cameraType) == 13){
  406. //转台相机
  407. videosJson.put("upPath", videosJson.getString("upPath").replace(".xml", ".txt"));
  408. }
  409. }
  410. if(Integer.parseInt(cameraType) == 5 || Integer.parseInt(cameraType) == 6){
  411. videosJson.put("version", 1);
  412. if("oss".equals(ossType)){
  413. videosJson.put("upPath", prefixAli + "data/data" + projectNum + "/stitch_params.txt");
  414. }
  415. if("s3".equals(ossType)){
  416. videosJson.put("upPath", ConstantUrl.PREFIX_AWS + "data/data" + projectNum + "/stitch_params.txt");
  417. }
  418. }
  419. long computeTime = (System.currentTimeMillis() - start) / 1000;
  420. sceneProService.updateTime(projectNum, space, payStatus, videosJson.toJSONString(), computeTime);
  421. //更新scene.json里面的video数据
  422. StringBuffer dataBuf = new StringBuffer()
  423. .append("data").append(File.separator)
  424. .append("data").append(projectNum)
  425. .append(File.separator);
  426. StringBuffer dataBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(dataBuf.toString());
  427. String strsceneInfos = FileUtils.readFile(dataBuffer.toString() + "scene.json");
  428. com.alibaba.fastjson.JSONObject scenejson = new com.alibaba.fastjson.JSONObject();
  429. if(strsceneInfos!=null){
  430. scenejson = com.alibaba.fastjson.JSONObject.parseObject(strsceneInfos);
  431. }
  432. scenejson.put("videos", videosJson.toJSONString());
  433. FileUtils.writeFile(dataBuffer.toString() + "scene.json", scenejson.toString());
  434. User userEntity = userService.findByUserName(userName);
  435. if(userEntity == null){
  436. userService.updateUserUsedSpaceBySceneNum(null, projectNum);
  437. }else {
  438. userService.updateUserUsedSpaceBySceneNum(userEntity.getId(), projectNum);
  439. }
  440. if (sceneFileBuildEntity != null){
  441. sceneFileBuildEntity.setBuildStatus(3);
  442. sceneFileBuildService.update(sceneFileBuildEntity);
  443. }
  444. //根据data.fdage推送计算完成的消息
  445. log.info("推送消息,渠道是 {}, 手机token是 {}", dataJson.get("pushChannel"), dataJson.get("pushToken"));
  446. if(dataJson.containsKey("pushChannel") && dataJson.containsKey("pushToken")){
  447. try{
  448. if(!"s3".equals(ossType)){
  449. PushMessageConfig demo = null;
  450. if(dataJson.getIntValue("pushChannel") == 0){
  451. if(Integer.parseInt(cameraType) == 10 || Integer.parseInt(cameraType) == 13){
  452. //ios
  453. log.info("IOS_KEY:{}, IOS_SECRET:{}", IOS_KEY_Z, IOS_SECRET_Z);
  454. demo = new PushMessageConfig(IOS_KEY_Z, IOS_SECRET_Z);
  455. demo.sendIOSUnicast(dataJson.getString("pushToken"),
  456. "四维看看Minion", scene.getSceneName() + "计算完成", "您上传的" + scene.getSceneName() + "计算完成,点击查看",
  457. scene.getWebSite());
  458. }else {
  459. //ios
  460. log.info("IOS_KEY:{}, IOS_SECRET:{}", IOS_KEY, IOS_SECRET);
  461. demo = new PushMessageConfig(IOS_KEY, IOS_SECRET);
  462. demo.sendIOSUnicast(dataJson.getString("pushToken"),
  463. "四维看看Pro", scene.getSceneName() + "计算完成", "您上传的" + scene.getSceneName() + "计算完成,点击查看",
  464. scene.getWebSite());
  465. }
  466. }else {
  467. if(Integer.parseInt(cameraType) == 10 || Integer.parseInt(cameraType) == 13){
  468. //ios
  469. //安卓
  470. log.info("ANDROID_KEY:{}, ANDROID_SECRET:{}", ANDROID_KEY_Z, ANDROID_SECRET_Z);
  471. demo = new PushMessageConfig(ANDROID_KEY_Z, ANDROID_SECRET_Z);
  472. demo.sendAndroidUnicast2(dataJson.getString("pushToken"),
  473. "四维看看Minion", scene.getSceneName() + "计算完成", "您上传的" + scene.getSceneName() + "计算完成,点击查看",
  474. scene.getWebSite());
  475. }else {
  476. //安卓
  477. log.info("ANDROID_KEY:{}, ANDROID_SECRET:{}", ANDROID_KEY, ANDROID_SECRET);
  478. demo = new PushMessageConfig(ANDROID_KEY, ANDROID_SECRET);
  479. demo.sendAndroidUnicast(dataJson.getString("pushToken"),
  480. "四维看看Pro", scene.getSceneName() + "计算完成", "您上传的" + scene.getSceneName() + "计算完成,点击查看",
  481. scene.getWebSite());
  482. }
  483. }
  484. }else {
  485. PushMsgUtil.googlePushMsg(dataJson.getString("pushToken"),
  486. scene.getSceneName() + "计算完成", "您上传的" + scene.getSceneName() + "计算完成,点击查看",
  487. scene.getWebSite());
  488. }
  489. log.info("消息推送结束!");
  490. }catch (Exception e){
  491. log.info("推送消息失败:");
  492. e.printStackTrace();
  493. }
  494. }
  495. //计算成功 激光转台相机推送
  496. log.info("激光转台相机 同步 请求 ");
  497. if(Integer.parseInt(cameraType) == 14){
  498. try {
  499. String title = "";
  500. if(StringUtils.isNotEmpty(scene.getSceneName())){
  501. title = scene.getSceneName();
  502. }
  503. String dataSource = sceneProExt.getDataSource();
  504. String jgPath = dataSource;
  505. //创建目录
  506. if(dataSource.lastIndexOf("/")!=-1){
  507. jgPath = jgPath + "_laserData";
  508. }else{
  509. jgPath = jgPath.substring(0,jgPath.length()-1) + "_laserData";
  510. }
  511. FileUtils.createDir(jgPath+"/extras");
  512. log.info("生成 激光相机目录 " + jgPath);
  513. //生成data.json
  514. JSONObject jgDataJson = new JSONObject();
  515. jgDataJson.put("split_type", "SPLIT_V15");
  516. jgDataJson.put("skybox_type", "SKYBOX_V5");
  517. jgDataJson.put("extras", null);
  518. FileUtils.writeFile(jgPath + File.separator + "data.json", jgDataJson.toString());
  519. CreateObjUtil.cpfile(dataSource + "/results/laserData/cover", jgPath+"/extras/");
  520. CreateObjUtil.cplaserfile(dataSource + "/results/laserData", jgPath+File.separator);
  521. //激光相机
  522. String url = laserHost+"/indoor/{sceneCode}/api/init?path="
  523. + jgPath + File.separator + "laserData" + "&title="+ title
  524. + "&childName=" + cameraName + "&createTime=" + scene.getCreateTime()
  525. + "&snCode="+ cameraName;
  526. if(scene.getUserId()!=null){
  527. url = url + "&userId=" + scene.getUserId();
  528. }
  529. if(StringUtils.isNotEmpty(userName)){
  530. url = url + "&phone=" + userName;
  531. }
  532. url = url.replace("{sceneCode}",scene.getSceneCode());
  533. log.info("激光转台相机 同步 :" + url);
  534. com.alibaba.fastjson.JSONObject hotListJson =
  535. com.alibaba.fastjson.JSONObject.parseObject(restTemplate.getForObject(url,String.class));
  536. log.info("激光转台相机 同步结束 :" + hotListJson);
  537. }catch (Exception e){
  538. e.printStackTrace();
  539. handFail("激光转台相机同步失败",message);
  540. }
  541. }
  542. }
  543. CreateObjUtil.deleteFile(path.replace(ConstantFilePath.BUILD_MODEL_PATH, "/") + "/capture");
  544. } catch(Exception e){
  545. log.error("计算大场景失败"+projectNum);
  546. sceneService.updateStatus(projectNum, -1);
  547. sceneProService.updateStatus(projectNum, -1);
  548. if (sceneFileBuildEntity != null){
  549. sceneFileBuildEntity.setBuildStatus(-1);
  550. sceneFileBuildService.update(sceneFileBuildEntity);
  551. }
  552. e.printStackTrace();
  553. StringWriter trace=new StringWriter();
  554. e.printStackTrace(new PrintWriter(trace));
  555. log.error(trace.toString());
  556. handFail("计算失败",message);
  557. }finally {
  558. try{
  559. scene = sceneProService.findBySceneNum(projectNum);
  560. if(scene != null){
  561. com.alibaba.fastjson.JSONObject statusJson = new com.alibaba.fastjson.JSONObject();
  562. //临时将-2改成1,app还没完全更新
  563. int status = sceneProExt.getSceneStatus();
  564. if(status == -2){
  565. status = 1;
  566. }else if(status == -1){
  567. // 失败状态不予显示到客户端
  568. status = 0;
  569. }
  570. statusJson.put("status", status);
  571. statusJson.put("webSite", scene.getWebSite());
  572. statusJson.put("sceneNum", scene.getSceneCode());
  573. statusJson.put("thumb", scene.getThumb());
  574. statusJson.put("payStatus", sceneProExt.getPayStatus());
  575. FileUtils.writeFile(ConstantFilePath.SCENE_PATH+"data/data"+projectNum+File.separator+"status.json", statusJson.toString());
  576. uploadUtils.upload(ConstantFilePath.SCENE_PATH+"data/data"+projectNum+File.separator+"status.json",
  577. "data/data"+projectNum+File.separator+"status.json");
  578. //上送日志
  579. uploadUtils.upload(sceneProExt.getDataSource()+File.separator+"console.log",
  580. "build_log/"+scene.getSceneCode()+File.separator+"console.log");
  581. }
  582. }catch (Exception e){
  583. e.printStackTrace();
  584. }
  585. }
  586. }
  587. private void handFail(String reason,BuildSceneMqMessage message){
  588. String serverPath = message.getPath().substring(0,message.getPath().lastIndexOf("/")+1).concat(message.getPrefix());
  589. handFail(reason,serverPath,message.getSceneNum());
  590. }
  591. private void handFail(String reason,String serverPath,String num){
  592. // 释放缓存锁
  593. redisTemplate.delete(RedisKey.SCENE_BUILDING + num);
  594. CompletableFuture.runAsync(() -> {
  595. try {
  596. DingDingUtils.sendMsgToDingRobot(reason,serverPath,num);
  597. } catch (ApiException | UnsupportedEncodingException | NoSuchAlgorithmException | InvalidKeyException apiException) {
  598. apiException.printStackTrace();
  599. }
  600. });
  601. }
  602. }