1
13693261870
2024-09-30 59c16d2a5b1d46e5ed88e43a2065ec39fe649bcf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.se.simu.service;
 
import cn.hutool.json.JSONUtil;
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.StringHelper;
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.*;
 
/**
 * 内涝求解器服务类
 *
 * @author WWW
 * @date   2024-09-29
 */
@Slf4j
@Service
@SuppressWarnings("ALL")
public class UwService {
    @Resource
    PropertiesConfig config;
 
    public void createRainFile(DataPo data) throws Exception {
        String filePath = config.getInPath() + File.separator + data.getInPath() + File.separator + config.getRaingage();
        String startTime = StringHelper.YMDHMS_FORMAT.format(data.getStartTime());
 
        Rainfall rainfall = new Rainfall();
        rainfall.rainfall(filePath, config.getRainStation(), startTime, data.getDuration(), 0.5, config.getRainPeriod());
    }
 
    public void createConfig(DataPo data) throws IOException {
        ConfigDto dto = new ConfigDto();
        dto.setProperties(data.getInPath(), data.getStartTime(), data.getDuration(), config);
 
        String json = JSONUtil.toJsonPrettyStr(dto);
        String filePath = config.getInPath() + File.separator + data.getInPath() + ".json";
 
        FileWriter fw = new FileWriter(filePath);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(json);
        bw.close();
        fw.close();
    }
 
    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() {
        //
    }
 
    public void copeDrainFiles() {
        //
    }
}