dcb
2025-07-01 f31f0991c0d2036e563b886f57de4cf45d3c72cb
src/main/java/com/se/nsl/service/RealTimeSimulationService.java
@@ -50,7 +50,7 @@
    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));
@@ -181,6 +181,7 @@
        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());
@@ -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);
@@ -263,8 +314,9 @@
        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);
    }
    //根据雨量计标识查询雨量数据
@@ -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代表没有数值
        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);
        }
    }
}