BuildSceneProgressServiceImpl.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. String projectJsonPath = path.concat(File.separator).concat("project.json");
  59. File file = FileUtil.file(projectJsonPath);
  60. WatchMonitor watchMonitor = WatchMonitor.create(file);
  61. watchMonitor.setWatcher(new Watcher() {
  62. boolean complete = false;
  63. int mainProgress = 0;
  64. Long totalTime = null;//buildProgressTime
  65. @Override
  66. public void onCreate(WatchEvent<?> event, Path currentPath) {
  67. // log.info("project.json文件创建完毕");
  68. // String projectJsonStr = FileUtil.readUtf8String(file);
  69. // JSONObject projectJson = JSON.parseObject(projectJsonStr);
  70. // JSONObject state = projectJson.getJSONObject("state");
  71. // Long expectTime = state.getLong("expect_time");
  72. // totalTime += expectTime;
  73. // redisUtil.set(String.format(RedisKey.SCENE_BUILD_EXPECT_TOTAL_TIME_NUM, num), String.valueOf(totalTime), RedisKey.CAMERA_EXPIRE_7_TIME);
  74. // Map<String, Object> params = new HashMap<>();
  75. // params.put("website", website);
  76. // params.put("title", title);
  77. // params.put("customUserId",customUserId);
  78. // params.put("gps", gps);
  79. // params.put("totalTime", totalTime);
  80. // params.put("progress", mainProgress);
  81. // HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000);
  82. }
  83. @Override
  84. public void onModify(WatchEvent<?> event, Path currentPath) {
  85. log.info("发生了变化,小飞棍来惹。。。。。");
  86. String projectJsonStr = FileUtil.readUtf8String(file);
  87. JSONObject projectJson = JSON.parseObject(projectJsonStr);
  88. JSONObject state = projectJson.getJSONObject("state");
  89. complete = state.getBoolean("done");
  90. log.info("计算完成状态:{}", complete);
  91. if (Objects.isNull(totalTime)) {
  92. try {
  93. log.info("计算总时间开始。。。。");
  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. log.info("计算总时间结束。。。。");
  97. }catch (Exception e){
  98. log.info("这里报错了,你麻痹。。。。。");
  99. }
  100. }
  101. if (complete) {
  102. mainProgress = 90;
  103. Map<String, Object> params = new HashMap<>();
  104. params.put("website", website);
  105. params.put("title", title);
  106. params.put("customUserId", customUserId);
  107. params.put("gps", gps);
  108. params.put("totalTime", totalTime);
  109. params.put("progress", mainProgress);
  110. log.info("算法完成发送请求,url:{}, param:{}", buildProgressUrl, JSON.toJSONString(params));
  111. HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000);
  112. watchMonitor.interrupt();
  113. } else {
  114. int progress = new BigDecimal(projectJson.getDouble("progress")).multiply(new BigDecimal(100)).intValue();
  115. log.info("当前进度为,progress:{}",progress);
  116. if (progress - mainProgress >= 10) {
  117. mainProgress = progress;
  118. Map<String, Object> params = new HashMap<>();
  119. params.put("website", website);
  120. params.put("title", title);
  121. params.put("customUserId", customUserId);
  122. params.put("gps", gps);
  123. params.put("totalTime", totalTime);
  124. params.put("progress", progress);
  125. log.info("算法进行中发送请求,url:{}, param:{}", buildProgressUrl, JSON.toJSONString(params));
  126. HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000);
  127. }
  128. }
  129. }
  130. @Override
  131. public void onDelete(WatchEvent<?> event, Path currentPath) {
  132. watchMonitor.interrupt();
  133. }
  134. @Override
  135. public void onOverflow(WatchEvent<?> event, Path currentPath) {
  136. }
  137. });
  138. watchMonitor.start();
  139. }
  140. public static void main(String[] args) {
  141. File file = new File("D:\\test\\111.txt");
  142. WatchMonitor watchMonitor = WatchMonitor.create(file);
  143. watchMonitor.setWatcher(new Watcher(){
  144. boolean complete = false;
  145. int mainProgress = 0;
  146. Long totalTime = null;//buildProgressTime
  147. @Override
  148. public void onCreate(WatchEvent<?> event, Path currentPath) {
  149. // log.info("project.json文件创建完毕");
  150. // String projectJsonStr = FileUtil.readUtf8String(file);
  151. // JSONObject projectJson = JSON.parseObject(projectJsonStr);
  152. // JSONObject state = projectJson.getJSONObject("state");
  153. // Long expectTime = state.getLong("expect_time");
  154. // totalTime += expectTime;
  155. // redisUtil.set(String.format(RedisKey.SCENE_BUILD_EXPECT_TOTAL_TIME_NUM, num), String.valueOf(totalTime), RedisKey.CAMERA_EXPIRE_7_TIME);
  156. // Map<String, Object> params = new HashMap<>();
  157. // params.put("website", website);
  158. // params.put("title", title);
  159. // params.put("customUserId",customUserId);
  160. // params.put("gps", gps);
  161. // params.put("totalTime", totalTime);
  162. // params.put("progress", mainProgress);
  163. // HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000);
  164. }
  165. @Override
  166. public void onModify(WatchEvent<?> event, Path currentPath) {
  167. System.out.println("文件修改了");
  168. }
  169. @Override
  170. public void onDelete(WatchEvent<?> event, Path currentPath) {
  171. System.out.println("文件删除了");
  172. watchMonitor.interrupt();
  173. }
  174. @Override
  175. public void onOverflow(WatchEvent<?> event, Path currentPath) {
  176. }
  177. });
  178. watchMonitor.start();
  179. }
  180. }