dcb
2025-06-30 59b432c883011119649c283cc9d4c1d357599802
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,7 +47,7 @@
    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 {
@@ -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;
    }
@@ -186,7 +186,7 @@
            configVo.setStation(stationFile.getAbsolutePath());
        }
        int realTimeSimulateTime = 300; //模拟时间,默认为5min,即300s
        int realTimeInterval = 60; //每帧的间隔时间,默认为60s,60s生成一帧
        int realTimeInterval = rtsConfig.getRealTimeInterval(); //每帧的间隔时间
        List<String> rainGauge = new ArrayList<>();
        rainGauge.add(newDatFile.getAbsolutePath());//raingage file
        rainGauge.add("mm/min");
@@ -211,7 +211,7 @@
            configVo.setStation(stationFile.getAbsolutePath());
        }
        int realTimeSimulateTime = 300; //模拟时间,默认为5min,即300s
        int realTimeInterval = 60; //每帧的间隔时间,默认为60s,60s生成一帧
        int realTimeInterval = rtsConfig.getRealTimeInterval(); //每帧的间隔时间
        configVo.getRaingage().set(0, newDatFile.getAbsolutePath()); //raingage file
        ResultVo result = configVo.getResult();
        Integer lastFrames = result.getLastFrames();
@@ -237,11 +237,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 +254,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 +309,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 +327,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 +336,8 @@
        if (!pageData.isEmpty()) {
            return pageData.get(0).get("value").asDouble();
        }
        return 0;
        return -1D;
    }
    private String callBat(String cmd) {
        try {
@@ -309,4 +359,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代表没有数值
        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);
        }
    }
}