package com.fdkankan.contro.mq.service.impl; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.watch.WatchMonitor; import cn.hutool.core.io.watch.Watcher; import cn.hutool.http.HttpUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; 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.HashMap; import java.util.Map; @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 = 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("project.json文件发生了变化"); String projectJsonStr = FileUtil.readUtf8String(file); JSONObject projectJson = JSON.parseObject(projectJsonStr); JSONObject state = projectJson.getJSONObject("state"); complete = state.getBoolean("done"); 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); HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000); watchMonitor.interrupt(); }else{ int progress = new BigDecimal(projectJson.getDouble("expect_time")).multiply(new BigDecimal(100)).intValue(); 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); HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000); } } } @Override public void onDelete(WatchEvent event, Path currentPath) { } @Override public void onOverflow(WatchEvent event, Path currentPath) { } }); watchMonitor.start(); } }