wuww
2025-05-04 9db8d773af4f886cf0652b0ce303631a90692a50
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.se.nsl.service;
 
import cn.hutool.json.JSONUtil;
import com.se.nsl.config.PropertiesConfig;
import com.se.nsl.domain.dto.ConfigDto;
import com.se.nsl.domain.po.DataPo;
import com.se.nsl.helper.StringHelper;
import com.se.nsl.helper.WebHelper;
import com.se.simu.Rainfall;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.io.*;
 
@Slf4j
@Service
@SuppressWarnings("ALL")
public class UwService {
    @Resource
    PropertiesConfig config;
 
    static Rainfall _rainfall = null;
 
    public static Rainfall getTainfall() throws Exception {
        if (null == _rainfall) {
            _rainfall = new Rainfall();
        }
 
        return _rainfall;
    }
 
    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());
 
        //MWCharArray file = new MWCharArray(filePath);
        //MWCharArray station = new MWCharArray(config.getRainStation());
        //MWCharArray time = new MWCharArray(startTime);
 
        Rainfall rainfall = getTainfall();
        //rainfall('D:\simu\in\RainGage.dat','Tongzhou','2024-09-29 00:00:00',60,0.5,10)
        //Object[] rs = rainfall.rainfall(filePath, config.getRainStation(), startTime, Double.valueOf(data.getDuration()), 0.5, config.getRainPeriod());
 
        //rainfall('D:\simu\in\RainGage.dat','Tongzhou','2024-07-01 00:00:00',60,60)
        Object[] rs = rainfall.rainfall(filePath, config.getRainStation(), startTime, Double.valueOf(data.getDuration()), data.getTotal());
 
        // file.dispose();
        //station.dispose();
        //time.dispose();
    }
 
    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);
        return execCmdLine(cmd);
    }
 
    public 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(), "GBK"));
            er = new BufferedReader(new InputStreamReader(process.getErrorStream(), "GBK"));
 
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = nr.readLine()) != null) {
                sb.append(line);
                log.info(line);
            }
 
            String errorLine;
            while ((errorLine = er.readLine()) != null) {
                log.error(errorLine);
            }
 
            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 String execCmdLine(String cmd) throws IOException, InterruptedException {
        Process process = Runtime.getRuntime().exec(cmd);
 
        new Thread(() -> {
            InputStreamReader ir = null;
            BufferedReader br = null;
            try {
                ir = new InputStreamReader(process.getErrorStream(), "GBK");
                br = new BufferedReader(ir);
 
                String line;
                while ((line = br.readLine()) != null) {
                    log.error(line);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != br) br.close();
                    if (null != ir) ir.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
 
        StringBuilder sb = new StringBuilder();
        //new Thread(() -> {
            InputStreamReader ir = null;
            BufferedReader br = null;
            try {
                ir = new InputStreamReader(process.getInputStream(), "GBK");
                br = new BufferedReader(ir);
 
                String line;
                while ((line = br.readLine()) != null) {
                    log.info(line);
                    sb.append(line);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != br) br.close();
                    if (null != ir) ir.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        //}).start();
 
        process.waitFor();
        process.destroy();
 
        return sb.toString();
    }
 
    public void copeWaterFiles() {
        //
    }
 
    public String copeDrainFiles(DataPo data) throws Exception {
        String time = StringHelper.YMDHMS_FORMAT.format(data.getStartTime());
        String inPath = config.getInPath() + File.separator + data.getInPath();
        String sww = inPath + File.separator + ".save" + File.separator + data.getInPath() + ".sww";
 
        String cmd = config.getSww2tifBat() + " " + sww + " \"" + time + "\" " + data.getEpsg() + " " + inPath;
 
        return exec(cmd);
    }
    public String copeSwwDrainFiles(DataPo data) throws Exception {
        String time = StringHelper.YMDHMS_FORMAT.format(data.getStartTime());
        String inPath = "H:\\simu\\semout";
        String sww = "H:\\simu\\semout\\testsem\\.out\\" + "testsem.sww";
 
        String cmd = config.getSww2tifBat() + " " + sww + " \"" + time + "\" " + data.getEpsg() + " " + inPath;
 
        return exec(cmd);
    }
}