package com.fdkankan.contro.mq.service.impl; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.watch.WatchMonitor; import cn.hutool.core.io.watch.Watcher; import cn.hutool.core.thread.ThreadUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.fdkankan.common.constant.ModelingBuildStatus; import com.fdkankan.common.constant.SceneVersionType; import com.fdkankan.common.util.FileUtils; import com.fdkankan.contro.entity.ScenePlus; import com.fdkankan.contro.entity.ScenePlusExt; import com.fdkankan.contro.mq.service.IBuildSceneProgressService; import com.fdkankan.contro.service.IScenePlusExtService; import com.fdkankan.contro.service.IScenePlusService; import com.fdkankan.rabbitmq.bean.BuildSceneCallMessage; import com.fdkankan.redis.constant.RedisKey; import com.fdkankan.redis.util.RedisUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Service; import java.io.File; import java.math.BigDecimal; import java.nio.file.Path; import java.nio.file.WatchEvent; import java.util.*; import java.util.concurrent.*; @RefreshScope @Slf4j @Service public class BuildSceneProgressServiceImpl implements IBuildSceneProgressService { @Value("${build.progress.time:300}") public Long buildProgressTime; @Value("${build.progress.url}") public String buildProgressUrl; @Autowired private RedisUtil redisUtil; @Autowired private IScenePlusService scenePlusService; @Autowired private IScenePlusExtService scenePlusExtService; @Override public void monitorProgress(BuildSceneCallMessage buildSceneCallMessage) { String num = buildSceneCallMessage.getSceneNum(); String customUserId = (String)buildSceneCallMessage.getExt().get("customUserId"); String gps = (String)buildSceneCallMessage.getExt().get("gps"); String path = buildSceneCallMessage.getPath(); ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num); ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId()); String website = scenePlusExt.getWebSite(); String title = scenePlus.getTitle(); ExecutorService executor = ThreadUtil.newSingleExecutor(); Future future = executor.submit(() -> { String projectJsonPath = path.concat(File.separator).concat("project.json"); File file = FileUtil.file(projectJsonPath); WatchMonitor watchMonitor = WatchMonitor.create(file); watchMonitor.setWatcher(new Watcher() { boolean complete = false; int mainProgress = 0; Long totalTime = null;//buildProgressTime @Override public void onCreate(WatchEvent event, Path currentPath) { // log.info("project.json文件创建完毕"); // String projectJsonStr = FileUtil.readUtf8String(file); // JSONObject projectJson = JSON.parseObject(projectJsonStr); // JSONObject state = projectJson.getJSONObject("state"); // Long expectTime = state.getLong("expect_time"); // totalTime += expectTime; // redisUtil.set(String.format(RedisKey.SCENE_BUILD_EXPECT_TOTAL_TIME_NUM, num), String.valueOf(totalTime), RedisKey.CAMERA_EXPIRE_7_TIME); // Map params = new HashMap<>(); // params.put("website", website); // params.put("title", title); // params.put("customUserId",customUserId); // params.put("gps", gps); // params.put("totalTime", totalTime); // params.put("progress", mainProgress); // HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000); } @Override public void onModify(WatchEvent event, Path currentPath) { log.info("发生了变化,小飞棍来惹。。。。。"); String projectJsonStr = FileUtil.readUtf8String(file); JSONObject projectJson = JSON.parseObject(projectJsonStr); JSONObject state = projectJson.getJSONObject("state"); complete = state.getBoolean("done"); log.info("计算完成状态:{}", complete); if (Objects.isNull(totalTime)) { totalTime = state.getLong("expect_time") + buildProgressTime; redisUtil.set(String.format(RedisKey.SCENE_BUILD_EXPECT_TOTAL_TIME_NUM, num), String.valueOf(totalTime), RedisKey.CAMERA_EXPIRE_7_TIME); } if (complete) { mainProgress = 90; Map params = new HashMap<>(); params.put("website", website); params.put("title", title); params.put("customUserId", customUserId); params.put("gps", gps); params.put("totalTime", totalTime); params.put("progress", mainProgress); log.info("算法完成发送请求,url:{}, param:{}", buildProgressUrl, JSON.toJSONString(params)); HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000); watchMonitor.interrupt(); executor.shutdown(); } else { int progress = new BigDecimal(projectJson.getDouble("progress")).multiply(new BigDecimal(100)).intValue(); log.info("当前进度为,progress:{}",progress); if (progress - mainProgress >= 10) { mainProgress = progress; Map params = new HashMap<>(); params.put("website", website); params.put("title", title); params.put("customUserId", customUserId); params.put("gps", gps); params.put("totalTime", totalTime); params.put("progress", progress); log.info("算法进行中发送请求,url:{}, param:{}", buildProgressUrl, JSON.toJSONString(params)); HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000); } } } @Override public void onDelete(WatchEvent event, Path currentPath) { watchMonitor.interrupt(); executor.shutdown(); } @Override public void onOverflow(WatchEvent event, Path currentPath) { } }); watchMonitor.start(); }); try { future.get(2*24*60*60, TimeUnit.SECONDS); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } } public static void main(String[] args) { File file = new File("D:\\test\\111.txt"); WatchMonitor watchMonitor = WatchMonitor.create(file); watchMonitor.setWatcher(new Watcher(){ boolean complete = false; int mainProgress = 0; Long totalTime = null;//buildProgressTime @Override public void onCreate(WatchEvent event, Path currentPath) { // log.info("project.json文件创建完毕"); // String projectJsonStr = FileUtil.readUtf8String(file); // JSONObject projectJson = JSON.parseObject(projectJsonStr); // JSONObject state = projectJson.getJSONObject("state"); // Long expectTime = state.getLong("expect_time"); // totalTime += expectTime; // redisUtil.set(String.format(RedisKey.SCENE_BUILD_EXPECT_TOTAL_TIME_NUM, num), String.valueOf(totalTime), RedisKey.CAMERA_EXPIRE_7_TIME); // Map params = new HashMap<>(); // params.put("website", website); // params.put("title", title); // params.put("customUserId",customUserId); // params.put("gps", gps); // params.put("totalTime", totalTime); // params.put("progress", mainProgress); // HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000); } @Override public void onModify(WatchEvent event, Path currentPath) { System.out.println("文件修改了"); } @Override public void onDelete(WatchEvent event, Path currentPath) { System.out.println("文件删除了"); watchMonitor.interrupt(); } @Override public void onOverflow(WatchEvent event, Path currentPath) { } }); watchMonitor.start(); } }