123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- 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();
- 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<String, Object> 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)) {
- try {
- log.info("计算总时间开始。。。。");
- 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);
- log.info("计算总时间结束。。。。");
- }catch (Exception e){
- log.info("这里报错了,你麻痹。。。。。");
- }
- }
- if (complete) {
- mainProgress = 90;
- Map<String, Object> 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();
- } else {
- int progress = new BigDecimal(projectJson.getDouble("progress")).multiply(new BigDecimal(100)).intValue();
- log.info("当前进度为,progress:{}",progress);
- if (progress - mainProgress >= 10) {
- mainProgress = progress;
- Map<String, Object> 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();
- }
- @Override
- public void onOverflow(WatchEvent<?> event, Path currentPath) {
- }
- });
- watchMonitor.start();
- }
- 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<String, Object> 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();
- }
- }
|