GenSceneRunnerImpl.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package com.fdkankan.download.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.exceptions.ExceptionUtil;
  4. import cn.hutool.core.thread.ExecutorBuilder;
  5. import cn.hutool.core.util.ArrayUtil;
  6. import cn.hutool.system.HostInfo;
  7. import cn.hutool.system.SystemUtil;
  8. import com.fdkankan.common.constant.CommonStatus;
  9. import com.fdkankan.common.constant.CommonSuccessStatus;
  10. import com.fdkankan.common.constant.SceneSource;
  11. import com.fdkankan.download.entity.*;
  12. import com.fdkankan.download.service.*;
  13. import com.fdkankan.redis.util.RedisLockUtil;
  14. import com.fdkankan.redis.util.RedisUtil;
  15. import com.fdkankan.rabbitmq.util.RabbitMqProducer;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.boot.CommandLineRunner;
  19. import org.springframework.stereotype.Component;
  20. import java.util.Arrays;
  21. import java.util.List;
  22. import java.util.Set;
  23. import java.util.concurrent.ThreadPoolExecutor;
  24. import java.util.stream.Collectors;
  25. @Slf4j
  26. @Component
  27. public class GenSceneRunnerImpl implements CommandLineRunner {
  28. @Autowired
  29. private ISnService snService;
  30. @Autowired
  31. private IScenePlusService scenePlusService;
  32. @Autowired
  33. private ICameraService cameraService;
  34. @Autowired
  35. private IScenePlusExtService scenePlusExtService;
  36. @Autowired
  37. private IDownloadLogService downloadLogService;
  38. @Autowired
  39. private RedisUtil redisUtil;
  40. @Autowired
  41. private RedisLockUtil redisLockUtil;
  42. @Autowired
  43. private IDownloadService downloadService;
  44. @Autowired
  45. private RabbitMqProducer mqProducer;
  46. private final static ThreadPoolExecutor threadPoolExecutor = ExecutorBuilder.create().setCorePoolSize(5).setMaxPoolSize(5).build();
  47. @Override
  48. public void run(String... args) throws Exception {
  49. List<Sn> snList = snService.list();
  50. if (CollUtil.isEmpty(snList)) {
  51. return;
  52. }
  53. for (Sn sn : snList) {
  54. List<Camera> cameras = cameraService.listBySnCodeList(Arrays.asList(sn.getSn()));
  55. if (CollUtil.isEmpty(cameras)) {
  56. continue;
  57. }
  58. Camera camera = cameras.get(0);
  59. List<ScenePlus> scenePluses = scenePlusService.listByCameraIdList(Arrays.asList(camera.getId()));
  60. if (CollUtil.isEmpty(scenePluses)) {
  61. continue;
  62. }
  63. for (ScenePlus scenePlus : scenePluses) {
  64. //上锁
  65. HostInfo hostInfo = SystemUtil.getHostInfo();
  66. String key = "download:tool:" + scenePlus.getNum();
  67. String lockVal = hostInfo.getAddress();
  68. try {
  69. boolean lock = redisLockUtil.lock(key, lockVal, 24 * 60 * 60);
  70. if(!lock){
  71. continue;
  72. }
  73. List<DownloadLog> downloadLogList = downloadLogService.getByNum(scenePlus.getNum());
  74. Set<String> types = downloadLogList.stream().map(DownloadLog::getType).collect(Collectors.toSet());
  75. int isObj = CommonStatus.NO.code();
  76. //下载点云场景
  77. if (scenePlus.getSceneSource() == SceneSource.JG.code() || scenePlus.getSceneSource() == SceneSource.SG.code()) {
  78. if(!types.contains("laser")){
  79. threadPoolExecutor.submit(() -> {
  80. try {
  81. // TODO: 2024/1/3 文杰实现
  82. downloadLogService.saveLog(scenePlus.getNum(), "laser", CommonSuccessStatus.SUCCESS.code(), null);
  83. } catch (Exception e) {
  84. downloadLogService.saveLog(scenePlus.getNum(), "laser", CommonSuccessStatus.FAIL.code(), ExceptionUtil.stacktraceToString(e, 3000));
  85. log.error("点云场景打包失败,num:{}", scenePlus.getNum(), e);
  86. }
  87. });
  88. }
  89. ScenePlusExt scenePlusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
  90. isObj = scenePlusExt.getIsObj();
  91. }
  92. //下载看看、看见、mesh
  93. if (scenePlus.getSceneSource() == SceneSource.BM.code()
  94. || scenePlus.getSceneSource() == SceneSource.ZT.code()
  95. || isObj == CommonStatus.YES.code()) {
  96. if(!types.contains("kankan")) {
  97. threadPoolExecutor.submit(() -> {
  98. try {
  99. String zipPath = downloadService.downloadHandler(scenePlus.getNum());
  100. send(zipPath);
  101. downloadLogService.saveLog(scenePlus.getNum(), "kankan", CommonSuccessStatus.SUCCESS.code(), null);
  102. } catch (Exception e) {
  103. log.error("看看场景打包失败,num:{}", scenePlus.getNum(), e);
  104. downloadLogService.saveLog(scenePlus.getNum(), "kankan", CommonSuccessStatus.FAIL.code(), ExceptionUtil.stacktraceToString(e, 3000));
  105. }
  106. });
  107. }
  108. }
  109. }catch (Exception e){
  110. log.error("场景打包失败:{}", scenePlus.getNum(), e);
  111. }
  112. }
  113. }
  114. }
  115. private void send(String zipPath){
  116. mqProducer.sendByWorkQueue("rsync-scene", zipPath);
  117. }
  118. }