BuildSceneProgressServiceImpl.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package com.fdkankan.contro.mq.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.io.watch.WatchMonitor;
  5. import cn.hutool.core.io.watch.Watcher;
  6. import cn.hutool.core.thread.ThreadUtil;
  7. import cn.hutool.core.util.StrUtil;
  8. import cn.hutool.http.HttpUtil;
  9. import com.alibaba.fastjson.JSON;
  10. import com.alibaba.fastjson.JSONObject;
  11. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  12. import com.fdkankan.common.constant.ModelingBuildStatus;
  13. import com.fdkankan.common.constant.SceneVersionType;
  14. import com.fdkankan.common.util.FileUtils;
  15. import com.fdkankan.contro.entity.ScenePlus;
  16. import com.fdkankan.contro.entity.ScenePlusExt;
  17. import com.fdkankan.contro.mq.service.IBuildSceneProgressService;
  18. import com.fdkankan.contro.service.IScenePlusExtService;
  19. import com.fdkankan.contro.service.IScenePlusService;
  20. import com.fdkankan.rabbitmq.bean.BuildSceneCallMessage;
  21. import com.fdkankan.redis.constant.RedisKey;
  22. import com.fdkankan.redis.util.RedisUtil;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.beans.factory.annotation.Value;
  26. import org.springframework.cloud.context.config.annotation.RefreshScope;
  27. import org.springframework.stereotype.Service;
  28. import java.io.File;
  29. import java.math.BigDecimal;
  30. import java.nio.file.Path;
  31. import java.nio.file.WatchEvent;
  32. import java.util.*;
  33. import java.util.concurrent.*;
  34. @RefreshScope
  35. @Slf4j
  36. @Service
  37. public class BuildSceneProgressServiceImpl implements IBuildSceneProgressService {
  38. @Value("${build.progress.time:300}")
  39. public Long buildProgressTime;
  40. @Value("${build.progress.url}")
  41. public String buildProgressUrl;
  42. @Autowired
  43. private RedisUtil redisUtil;
  44. @Autowired
  45. private IScenePlusService scenePlusService;
  46. @Autowired
  47. private IScenePlusExtService scenePlusExtService;
  48. @Override
  49. public void monitorProgress(BuildSceneCallMessage buildSceneCallMessage) {
  50. String num = buildSceneCallMessage.getSceneNum();
  51. String customUserId = (String)buildSceneCallMessage.getExt().get("customUserId");
  52. String gps = (String)buildSceneCallMessage.getExt().get("gps");
  53. String path = buildSceneCallMessage.getPath();
  54. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  55. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  56. String website = scenePlusExt.getWebSite();
  57. String title = scenePlus.getTitle();
  58. ExecutorService executor = ThreadUtil.newSingleExecutor();
  59. Future future = executor.submit(() -> {
  60. String projectJsonPath = path.concat(File.separator).concat("project.json");
  61. File file = FileUtil.file(projectJsonPath);
  62. WatchMonitor watchMonitor = WatchMonitor.create(file);
  63. watchMonitor.setWatcher(new Watcher() {
  64. boolean complete = false;
  65. int mainProgress = 0;
  66. Long totalTime = null;//buildProgressTime
  67. @Override
  68. public void onCreate(WatchEvent<?> event, Path currentPath) {
  69. // log.info("project.json文件创建完毕");
  70. // String projectJsonStr = FileUtil.readUtf8String(file);
  71. // JSONObject projectJson = JSON.parseObject(projectJsonStr);
  72. // JSONObject state = projectJson.getJSONObject("state");
  73. // Long expectTime = state.getLong("expect_time");
  74. // totalTime += expectTime;
  75. // redisUtil.set(String.format(RedisKey.SCENE_BUILD_EXPECT_TOTAL_TIME_NUM, num), String.valueOf(totalTime), RedisKey.CAMERA_EXPIRE_7_TIME);
  76. // Map<String, Object> params = new HashMap<>();
  77. // params.put("website", website);
  78. // params.put("title", title);
  79. // params.put("customUserId",customUserId);
  80. // params.put("gps", gps);
  81. // params.put("totalTime", totalTime);
  82. // params.put("progress", mainProgress);
  83. // HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000);
  84. }
  85. @Override
  86. public void onModify(WatchEvent<?> event, Path currentPath) {
  87. log.info("发生了变化,小飞棍来惹。。。。。");
  88. String projectJsonStr = FileUtil.readUtf8String(file);
  89. JSONObject projectJson = JSON.parseObject(projectJsonStr);
  90. JSONObject state = projectJson.getJSONObject("state");
  91. complete = state.getBoolean("done");
  92. log.info("计算完成状态:{}", complete);
  93. if (Objects.isNull(totalTime)) {
  94. totalTime = state.getLong("expect_time") + buildProgressTime;
  95. redisUtil.set(String.format(RedisKey.SCENE_BUILD_EXPECT_TOTAL_TIME_NUM, num), String.valueOf(totalTime), RedisKey.CAMERA_EXPIRE_7_TIME);
  96. }
  97. if (complete) {
  98. mainProgress = 90;
  99. Map<String, Object> params = new HashMap<>();
  100. params.put("website", website);
  101. params.put("title", title);
  102. params.put("customUserId", customUserId);
  103. params.put("gps", gps);
  104. params.put("totalTime", totalTime);
  105. params.put("progress", mainProgress);
  106. log.info("算法完成发送请求,url:{}, param:{}", buildProgressUrl, JSON.toJSONString(params));
  107. HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000);
  108. watchMonitor.interrupt();
  109. executor.shutdown();
  110. } else {
  111. int progress = new BigDecimal(projectJson.getDouble("progress")).multiply(new BigDecimal(100)).intValue();
  112. log.info("当前进度为,progress:{}",progress);
  113. if (progress - mainProgress >= 10) {
  114. mainProgress = progress;
  115. Map<String, Object> params = new HashMap<>();
  116. params.put("website", website);
  117. params.put("title", title);
  118. params.put("customUserId", customUserId);
  119. params.put("gps", gps);
  120. params.put("totalTime", totalTime);
  121. params.put("progress", progress);
  122. log.info("算法进行中发送请求,url:{}, param:{}", buildProgressUrl, JSON.toJSONString(params));
  123. HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000);
  124. }
  125. }
  126. }
  127. @Override
  128. public void onDelete(WatchEvent<?> event, Path currentPath) {
  129. watchMonitor.interrupt();
  130. executor.shutdown();
  131. }
  132. @Override
  133. public void onOverflow(WatchEvent<?> event, Path currentPath) {
  134. }
  135. });
  136. watchMonitor.start();
  137. });
  138. try {
  139. future.get(2*24*60*60, TimeUnit.SECONDS);
  140. } catch (InterruptedException e) {
  141. e.printStackTrace();
  142. } catch (ExecutionException e) {
  143. e.printStackTrace();
  144. } catch (TimeoutException e) {
  145. e.printStackTrace();
  146. }
  147. }
  148. public static void main(String[] args) {
  149. File file = new File("D:\\test\\111.txt");
  150. WatchMonitor watchMonitor = WatchMonitor.create(file);
  151. watchMonitor.setWatcher(new Watcher(){
  152. boolean complete = false;
  153. int mainProgress = 0;
  154. Long totalTime = null;//buildProgressTime
  155. @Override
  156. public void onCreate(WatchEvent<?> event, Path currentPath) {
  157. // log.info("project.json文件创建完毕");
  158. // String projectJsonStr = FileUtil.readUtf8String(file);
  159. // JSONObject projectJson = JSON.parseObject(projectJsonStr);
  160. // JSONObject state = projectJson.getJSONObject("state");
  161. // Long expectTime = state.getLong("expect_time");
  162. // totalTime += expectTime;
  163. // redisUtil.set(String.format(RedisKey.SCENE_BUILD_EXPECT_TOTAL_TIME_NUM, num), String.valueOf(totalTime), RedisKey.CAMERA_EXPIRE_7_TIME);
  164. // Map<String, Object> params = new HashMap<>();
  165. // params.put("website", website);
  166. // params.put("title", title);
  167. // params.put("customUserId",customUserId);
  168. // params.put("gps", gps);
  169. // params.put("totalTime", totalTime);
  170. // params.put("progress", mainProgress);
  171. // HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000);
  172. }
  173. @Override
  174. public void onModify(WatchEvent<?> event, Path currentPath) {
  175. System.out.println("文件修改了");
  176. }
  177. @Override
  178. public void onDelete(WatchEvent<?> event, Path currentPath) {
  179. System.out.println("文件删除了");
  180. watchMonitor.interrupt();
  181. }
  182. @Override
  183. public void onOverflow(WatchEvent<?> event, Path currentPath) {
  184. }
  185. });
  186. watchMonitor.start();
  187. }
  188. }