BuildSceneProgressServiceImpl.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.fdkankan.contro.mq.service.impl;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.hutool.core.io.watch.WatchMonitor;
  4. import cn.hutool.core.io.watch.Watcher;
  5. import cn.hutool.http.HttpUtil;
  6. import com.alibaba.fastjson.JSON;
  7. import com.alibaba.fastjson.JSONObject;
  8. import com.fdkankan.contro.entity.ScenePlus;
  9. import com.fdkankan.contro.entity.ScenePlusExt;
  10. import com.fdkankan.contro.mq.service.IBuildSceneProgressService;
  11. import com.fdkankan.contro.service.IScenePlusExtService;
  12. import com.fdkankan.contro.service.IScenePlusService;
  13. import com.fdkankan.rabbitmq.bean.BuildSceneCallMessage;
  14. import com.fdkankan.redis.constant.RedisKey;
  15. import com.fdkankan.redis.util.RedisUtil;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.beans.factory.annotation.Value;
  19. import org.springframework.cloud.context.config.annotation.RefreshScope;
  20. import org.springframework.stereotype.Service;
  21. import java.io.File;
  22. import java.math.BigDecimal;
  23. import java.nio.file.Path;
  24. import java.nio.file.WatchEvent;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. @RefreshScope
  28. @Slf4j
  29. @Service
  30. public class BuildSceneProgressServiceImpl implements IBuildSceneProgressService {
  31. @Value("${build.progress.time:300}")
  32. public Long buildProgressTime;
  33. @Value("${build.progress.url}")
  34. public String buildProgressUrl;
  35. @Autowired
  36. private RedisUtil redisUtil;
  37. @Autowired
  38. private IScenePlusService scenePlusService;
  39. @Autowired
  40. private IScenePlusExtService scenePlusExtService;
  41. @Override
  42. public void monitorProgress(BuildSceneCallMessage buildSceneCallMessage) {
  43. String num = buildSceneCallMessage.getSceneNum();
  44. String customUserId = (String)buildSceneCallMessage.getExt().get("customUserId");
  45. String gps = (String)buildSceneCallMessage.getExt().get("gps");
  46. String path = buildSceneCallMessage.getPath();
  47. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  48. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  49. String website = scenePlusExt.getWebSite();
  50. String title = scenePlus.getTitle();
  51. String projectJsonPath = path.concat(File.separator).concat("project.json");
  52. File file = FileUtil.file(projectJsonPath);
  53. WatchMonitor watchMonitor = WatchMonitor.create(file);
  54. watchMonitor.setWatcher(new Watcher(){
  55. boolean complete = false;
  56. int mainProgress = 0;
  57. Long totalTime = buildProgressTime;
  58. @Override
  59. public void onCreate(WatchEvent<?> event, Path currentPath) {
  60. log.info("project.json文件创建完毕");
  61. String projectJsonStr = FileUtil.readUtf8String(file);
  62. JSONObject projectJson = JSON.parseObject(projectJsonStr);
  63. JSONObject state = projectJson.getJSONObject("state");
  64. Long expectTime = state.getLong("expect_time");
  65. totalTime += expectTime;
  66. redisUtil.set(String.format(RedisKey.SCENE_BUILD_EXPECT_TOTAL_TIME_NUM, num), String.valueOf(totalTime), RedisKey.CAMERA_EXPIRE_7_TIME);
  67. Map<String, Object> params = new HashMap<>();
  68. params.put("website", website);
  69. params.put("title", title);
  70. params.put("customUserId",customUserId);
  71. params.put("gps", gps);
  72. params.put("totalTime", totalTime);
  73. params.put("progress", mainProgress);
  74. HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000);
  75. }
  76. @Override
  77. public void onModify(WatchEvent<?> event, Path currentPath) {
  78. log.info("project.json文件发生了变化");
  79. String projectJsonStr = FileUtil.readUtf8String(file);
  80. JSONObject projectJson = JSON.parseObject(projectJsonStr);
  81. JSONObject state = projectJson.getJSONObject("state");
  82. complete = state.getBoolean("done");
  83. if(complete){
  84. mainProgress = 90;
  85. Map<String, Object> params = new HashMap<>();
  86. params.put("website", website);
  87. params.put("title", title);
  88. params.put("customUserId",customUserId);
  89. params.put("gps", gps);
  90. params.put("totalTime", totalTime);
  91. params.put("progress", mainProgress);
  92. HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000);
  93. watchMonitor.interrupt();
  94. }else{
  95. int progress = new BigDecimal(projectJson.getDouble("expect_time")).multiply(new BigDecimal(100)).intValue();
  96. if(progress - mainProgress >= 10){
  97. mainProgress = progress;
  98. Map<String, Object> params = new HashMap<>();
  99. params.put("website", website);
  100. params.put("title", title);
  101. params.put("customUserId",customUserId);
  102. params.put("gps", gps);
  103. params.put("totalTime", totalTime);
  104. params.put("progress", progress);
  105. HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000);
  106. }
  107. }
  108. }
  109. @Override
  110. public void onDelete(WatchEvent<?> event, Path currentPath) {
  111. }
  112. @Override
  113. public void onOverflow(WatchEvent<?> event, Path currentPath) {
  114. }
  115. });
  116. watchMonitor.start();
  117. }
  118. }