From 97629da6a002b534da10f828187fd6cd65941aa4 Mon Sep 17 00:00:00 2001 From: dcb <xgybdcb@163.com> Date: 星期一, 07 七月 2025 17:14:13 +0800 Subject: [PATCH] 模拟间隔支持毫秒级 --- src/main/java/com/se/nsl/service/RealTimeSimulationService.java | 203 +++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 183 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/se/nsl/service/RealTimeSimulationService.java b/src/main/java/com/se/nsl/service/RealTimeSimulationService.java index c835224..308b480 100644 --- a/src/main/java/com/se/nsl/service/RealTimeSimulationService.java +++ b/src/main/java/com/se/nsl/service/RealTimeSimulationService.java @@ -3,7 +3,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.se.nsl.config.GaugeServer; +import com.se.nsl.config.RealTimeSimulationConfig; import com.se.nsl.config.PropertiesConfig; import com.se.nsl.domain.dto.*; import com.se.nsl.domain.po.RainGauge; @@ -47,10 +47,10 @@ private final ObjectMapper mapper = new ObjectMapper(); @Resource - private GaugeServer gaugeServer; + private RealTimeSimulationConfig rtsConfig; private static final long MILLIS_OF_ONE_DAY = 86400000; - public String realTimeSimulate(Simu simu) throws IOException { + public String startSimulation(Simu simu) throws IOException { InputStream stream = RealTimeSimulationService.class.getResourceAsStream("/device_info.json"); List<DeviceInfo> deviceInfos = mapper.readValue(stream, mapper.getTypeFactory().constructCollectionType(List.class, DeviceInfo.class)); @@ -71,7 +71,7 @@ } private long getCurrentTime() { - double offsetDays = gaugeServer.getOffsetDays(); + double offsetDays = rtsConfig.getOffsetDays(); long millis = (long) (offsetDays * MILLIS_OF_ONE_DAY); return System.currentTimeMillis() - millis; } @@ -181,18 +181,19 @@ String serviceNameDirPath = serviceNameDir.getAbsolutePath(); configVo.setTerrain(serviceNameDirPath + File.separator + DEM_TIF); configVo.setLanduse(serviceNameDirPath + File.separator + "Landuse.tif"); + configVo.setEvaporation(config.getEvaporation()); File stationFile = new File(serviceNameDir, "Station.tif"); if (stationFile.exists()) { configVo.setStation(stationFile.getAbsolutePath()); } int realTimeSimulateTime = 300; //妯℃嫙鏃堕棿锛岄粯璁や负5min锛屽嵆300s - int realTimeInterval = 60; //姣忓抚鐨勯棿闅旀椂闂达紝榛樿涓�60s,60s鐢熸垚涓�甯� + Double realTimeInterval = rtsConfig.getFrameInterval(); //姣忓抚鐨勯棿闅旀椂闂� List<String> rainGauge = new ArrayList<>(); rainGauge.add(newDatFile.getAbsolutePath());//raingage file rainGauge.add("mm/min"); configVo.setRaingage(rainGauge); String saveName = serviceNameDir + File.separator + RESULT_ZARR; - ResultVo result = new ResultVo(saveName, realTimeSimulateTime / realTimeInterval, + ResultVo result = new ResultVo(saveName, (int) (realTimeSimulateTime / realTimeInterval), realTimeInterval,"continue", null); result.setSave_interval(realTimeInterval); result.setSave_timestamp0(TimeFormatUtil.formatTime(currentTime, YYYY_MM_DD_HH_MM_SS)); @@ -211,7 +212,7 @@ configVo.setStation(stationFile.getAbsolutePath()); } int realTimeSimulateTime = 300; //妯℃嫙鏃堕棿锛岄粯璁や负5min锛屽嵆300s - int realTimeInterval = 60; //姣忓抚鐨勯棿闅旀椂闂达紝榛樿涓�60s,60s鐢熸垚涓�甯� + Double realTimeInterval = rtsConfig.getFrameInterval(); //姣忓抚鐨勯棿闅旀椂闂� configVo.getRaingage().set(0, newDatFile.getAbsolutePath()); //raingage file ResultVo result = configVo.getResult(); Integer lastFrames = result.getLastFrames(); @@ -219,10 +220,10 @@ lastFrames = 0; } Integer saveStart = result.getSave_start(); - Integer saveInterval = result.getSave_interval(); + Double saveInterval = result.getSave_interval(); Integer saveFrames = result.getSave_frames(); - int newStartPoint = saveStart + (saveFrames - lastFrames) * saveInterval; - int newSaveFrames = saveFrames + (realTimeSimulateTime / realTimeInterval); + int newStartPoint = (int) (saveStart + (saveFrames - lastFrames) * saveInterval); + int newSaveFrames = (int) (saveFrames + (realTimeSimulateTime / realTimeInterval)); result.setSave_start(newStartPoint); //璧峰鏃堕棿瑕佸湪涓婃鏃堕棿鐨勫熀纭�涓婂紑濮� result.setLastFrames(saveFrames); result.setSave_frames(newSaveFrames); //淇濈暀5甯э紝鍦ㄥ師鏉ョ殑鍩虹涓婂鍔�5甯� @@ -237,11 +238,16 @@ List<String> newLines = new ArrayList<>(); newLines.add(title); Map<String, Integer> deviceInfoMap = deviceInfos.stream().collect(Collectors.toMap(DeviceInfo::getId, DeviceInfo::getMappingId)); + List<RainRecord> rainRecords = new ArrayList<>(); for (RainGauge gauge : gauges) { String id = gauge.getId(); Integer mappingId = deviceInfoMap.getOrDefault(id, 0); - String l = getRainGaugeResult(gauge, mappingId, currentTime); - newLines.add(l); + RainRecord rr = getRainGaugeResult(gauge, mappingId, currentTime); + rainRecords.add(rr); + } + fillEmptyValue(rainRecords); //濉厖娌℃湁鍊肩殑闆ㄩ噺璁� + for (RainRecord rr : rainRecords) { + newLines.add(rr.toString()); } File newDatFile = new File(serviceNameDir, "rainfall_" + currentTime + ".dat"); if (!newDatFile.exists()) newDatFile.createNewFile(); @@ -249,7 +255,52 @@ return newDatFile; } - private String getRainGaugeResult(RainGauge gauge,int stationId, long currentTime) throws JsonProcessingException { + //灏嗛洦閲忚涓┖鍊硷紙intensity涓�-1锛夌殑閮ㄥ垎濉厖鍊� + private void fillEmptyValue(List<RainRecord> rainRecords) { + double[] rainValues = rainRecords.stream().mapToDouble(r -> r.getIntensity()).toArray(); + double[] forwardValues = forwardFill(rainValues); + double[] backwordValues = backwordFill(rainValues); + for (int i = 0; i < rainRecords.size(); i++) { + RainRecord rr = rainRecords.get(i); + double filledValue = forwardValues[i] != -1 ? forwardValues[i] : backwordValues[i]; + rr.setIntensity(filledValue); + } + //濡傛灉閮芥槸-1锛屽垯璁剧疆涓�0 + for (RainRecord rr : rainRecords) { + double intensity = rr.getIntensity(); + if (intensity == -1) { + rr.setIntensity(0); + } + } + } + + private double[] forwardFill(double[] rainValues) { + double[] result = Arrays.copyOf(rainValues, rainValues.length); + double lastValid = -1D; + for (int i = 0; i < result.length; i++) { + if (result[i] != -1) { + lastValid = result[i]; + } else if (lastValid != -1) { + result[i] = lastValid; + } + } + return result; + } + + private double[] backwordFill(double[] rainValues) { + double[] result = Arrays.copyOf(rainValues, rainValues.length); + double lastValid = -1D; + for (int i = result.length - 1; i >= 0; i--) { + if (result[i] != -1) { + lastValid = result[i]; + } else if (lastValid != -1) { + result[i] = lastValid; + } + } + return result; + } + + private RainRecord getRainGaugeResult(RainGauge gauge,int stationId, long currentTime) throws JsonProcessingException { double lon = gauge.getX(); double lat = gauge.getY(); LocalDateTime dateTime = TimeFormatUtil.toDate(currentTime); @@ -259,12 +310,13 @@ int hour = dateTime.getHour(); int minute = dateTime.getMinute(); String id = gauge.getId(); - long someMinutesAgo = currentTime - TimeUnit.MINUTES.toMillis(gaugeServer.getRequestOffsetMinutes()); + long someMinutesAgo = currentTime - TimeUnit.MINUTES.toMillis(rtsConfig.getRequestOffsetMinutes()); String startTime = TimeFormatUtil.formatTime(someMinutesAgo, YYYY_MM_DD_HH_MM_SS); String endTime = TimeFormatUtil.formatTime(currentTime, YYYY_MM_DD_HH_MM_SS); double intensity = getIntensityByDeviceId(id, startTime, endTime); //淇濈暀鎸囧畾浣嶆暟灏忔暟 - return String.format("%s %s %s %s %s %s %s %s %s", - stationId, lon, lat, year, month, day, hour, minute, String.format("%.6f", intensity)); +// return String.format("%s %s %s %s %s %s %s %s %s", +// stationId, lon, lat, year, month, day, hour, minute, String.format("%.6f", intensity)); + return new RainRecord(stationId, lon, lat, year, month, day, hour, minute, intensity); } //鏍规嵁闆ㄩ噺璁℃爣璇嗘煡璇㈤洦閲忔暟鎹� @@ -276,8 +328,8 @@ filterObject.setDeviceCode(deviceId); filterObject.setSendTimeList(Arrays.asList(startTime, endTime)); input.setFilterObject(filterObject); - String url = gaugeServer.getUrl(); - String token = gaugeServer.getToken(); + String url = rtsConfig.getUrl(); + String token = rtsConfig.getToken(); ResponseEntity<String> post = HttpRequestUtil.post(url, input, String.class, token); String body = post.getBody(); JsonNode jsonNode = mapper.readTree(body); @@ -285,9 +337,8 @@ if (!pageData.isEmpty()) { return pageData.get(0).get("value").asDouble(); } - return 0; + return -1D; } - private String callBat(String cmd) { try { @@ -309,4 +360,116 @@ } } + static class RainRecord { + private int stationId; + private double lon; + private double lat; + private int year; + private int month; + private int day; + private int hour; + private int minute; + private double intensity; //-1浠h〃娌℃湁鏁板�� + + public RainRecord() {} + + public RainRecord(int stationId, double lon, double lat, int year, int month, + int day, int hour, int minute, double intensity) { + this.stationId = stationId; + this.lon = lon; + this.lat = lat; + this.year = year; + this.month = month; + this.day = day; + this.hour = hour; + this.minute = minute; + this.intensity = intensity; + } + + public int getStationId() { + return stationId; + } + + public void setStationId(int stationId) { + this.stationId = stationId; + } + + public double getLon() { + return lon; + } + + public void setLon(double lon) { + this.lon = lon; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } + + public int getYear() { + return year; + } + + public void setYear(int year) { + this.year = year; + } + + public int getMonth() { + return month; + } + + public void setMonth(int month) { + this.month = month; + } + + public int getDay() { + return day; + } + + public void setDay(int day) { + this.day = day; + } + + public int getHour() { + return hour; + } + + public void setHour(int hour) { + this.hour = hour; + } + + public int getMinute() { + return minute; + } + + public void setMinute(int minute) { + this.minute = minute; + } + + public double getIntensity() { + return intensity; + } + + public void setIntensity(double intensity) { + this.intensity = intensity; + } + + @Override + public String toString() { + return "" + stationId + + " " + lon + + " " + lat + + " " + year + + " " + month + + " " + day + + " " + hour + + " " + minute + + " " + String.format("%.6f", intensity); + } + } + } -- Gitblit v1.9.3