1
13693261870
2024-09-30 b2b9e959447664bceb4381630358230a2ee5a1ab
src/main/java/com/se/simu/service/UwService.java
@@ -4,14 +4,13 @@
import com.se.simu.config.PropertiesConfig;
import com.se.simu.domain.dto.ConfigDto;
import com.se.simu.domain.po.DataPo;
import com.se.simu.helper.WebHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.se.simu.Rainfall;
import javax.annotation.Resource;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
/**
 * 内涝求解器服务类
@@ -26,13 +25,14 @@
    @Resource
    PropertiesConfig config;
    public void createRainFile() {
    public void createRainFile() throws Exception {
        Rainfall rainfall = new Rainfall();
        rainfall.rainfall("D:/simu/in/RainGage.dat", "Tongzhou", "2024-09-29 00:00:00", 60, 0.5, 10);
    }
    public void createConfig(DataPo data) throws IOException {
        ConfigDto dto = new ConfigDto();
        dto.setProperties(data.getInPath(), data.getDuration(), config);
        dto.setProperties(data.getInPath(), data.getStartTime(), data.getDuration(), config);
        String json = JSONUtil.toJsonPrettyStr(dto);
        String filePath = config.getInPath() + File.separator + data.getInPath() + ".json";
@@ -44,8 +44,64 @@
        fw.close();
    }
    public void callExe() {
        //
    public String callExe(DataPo data) throws Exception {
        String cmd = String.format("%s %d %s", config.getSolverBat(), WebHelper.getCpuCores(), data.getInPath() + ".json");
        return exec(cmd);
    }
    private String exec(String cmd) throws Exception {
        Process process = null;
        BufferedReader nr = null;
        BufferedReader er = null;
        try {
            // new String[] { "/bin/sh", "-c", cmd }
            process = Runtime.getRuntime().exec(cmd);
            nr = new BufferedReader(new InputStreamReader(process.getInputStream()));
            er = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String errorLine;
            while ((errorLine = er.readLine()) != null) {
                log.warn(errorLine);
            }
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = nr.readLine()) != null) {
                sb.append(line);
            }
            // 等待程序执行结束并输出状态
            int exitCode = process.waitFor();
            return sb.toString();
        } catch (Exception ex) {
            throw ex;
        } finally {
            closeReader(er);
            closeReader(nr);
            closeProcess(process);
        }
    }
    private static void closeReader(Reader reader) {
        if (null != reader) {
            try {
                reader.close();
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            }
        }
    }
    private static void closeProcess(Process process) {
        if (null != process) {
            try {
                process.destroy();
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            }
        }
    }
    public void copeWaterFiles() {