From 0890b7861feae74bdcfd1851e577db6b9f31d484 Mon Sep 17 00:00:00 2001
From: xingjinshuang <xingjs@qq.com>
Date: 星期四, 20 二月 2025 14:40:39 +0800
Subject: [PATCH] @xingjs@20250220@添加处理sww相关接口类

---
 src/main/java/com/se/simu/service/UwService.java |  181 +++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 165 insertions(+), 16 deletions(-)

diff --git a/src/main/java/com/se/simu/service/UwService.java b/src/main/java/com/se/simu/service/UwService.java
index 442c80c..c9ca4a6 100644
--- a/src/main/java/com/se/simu/service/UwService.java
+++ b/src/main/java/com/se/simu/service/UwService.java
@@ -4,21 +4,15 @@
 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.BufferedWriter;
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
+import java.io.*;
 
-/**
- * 鍐呮稘姹傝В鍣ㄦ湇鍔$被
- *
- * @author WWW
- * @date   2024-09-29
- */
 @Slf4j
 @Service
 @SuppressWarnings("ALL")
@@ -26,13 +20,39 @@
     @Resource
     PropertiesConfig config;
 
-    public void createRainFile() {
+    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.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,15 +64,144 @@
         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);
+        return execCmdLine(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(), "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);
+            }
+        }
+    }
+
+    private 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 void copeDrainFiles() {
-        //
+    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);
     }
 }

--
Gitblit v1.9.3