Ver Fonte

增加启动时导入配置文件

xiuwei há 3 anos atrás
pai
commit
f3ea538590
3 ficheiros alterados com 67 adições e 19 exclusões
  1. 1 1
      README.md
  2. 20 15
      src/main/java/RequestController.java
  3. 46 3
      src/main/java/StartHttpServer.java

+ 1 - 1
README.md

@@ -8,7 +8,7 @@
 拉到代码后 package 打成jar包
 
 启动命令   
-java -jar 104SlaverXXXXX.jar [port1 port2]   
+java -jar 104SlaverXXXXX.jar [port1 port2 [配置文件路径]]   
 port1代表104的端口  
 port2 代表配置页面的端口  
 不输入代表默认 104:2404  页面:2405

+ 20 - 15
src/main/java/RequestController.java

@@ -1,3 +1,4 @@
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import io.netty.handler.codec.http.QueryStringDecoder;
 import io.netty.util.CharsetUtil;
@@ -111,25 +112,29 @@ public class RequestController {
         return (request) -> {
             String bodyStr = request.content().toString(CharsetUtil.UTF_8);
             JSONObject jsonObject = JSONObject.parseObject(bodyStr);
-            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
-                try {
-                    Integer key = Integer.parseInt(entry.getKey());
-                    String value = entry.getValue().toString();
-                    if ("true".equals(value) || "false".equals(value)) {
+            batchImport(jsonObject);
+            return uploadConfig().getTextResponse("yes");
+        };
+    }
+
+    public void batchImport(JSONObject jsonObject) {
+        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
+            try {
+                Integer key = Integer.parseInt(entry.getKey());
+                String value = entry.getValue().toString();
+                if ("true".equals(value) || "false".equals(value)) {
+                    PointDataContainer.getInstance().add2PointDataMap(new PointDataConstantValue(key, value));
+                } else {
+                    try {
+                        Float.parseFloat(value);
                         PointDataContainer.getInstance().add2PointDataMap(new PointDataConstantValue(key, value));
-                    } else {
-                        try {
-                            Float.parseFloat(value);
-                            PointDataContainer.getInstance().add2PointDataMap(new PointDataConstantValue(key, value));
-                        } catch (NumberFormatException e) {
-                            PointDataContainer.getInstance().add2PointDataMap(new PointDataCalculator(key, value));
-                        }
+                    } catch (NumberFormatException e) {
+                        PointDataContainer.getInstance().add2PointDataMap(new PointDataCalculator(key, value));
                     }
-                } catch (Exception e) {
                 }
+            } catch (Exception e) {
             }
-            return uploadConfig().getTextResponse("yes");
-        };
+        }
     }
 
     @RequestHandlerFunction(requestType = RequestControllerManager.RequestMethodType.GET, requestUrl = "/getValues")

+ 46 - 3
src/main/java/StartHttpServer.java

@@ -1,7 +1,13 @@
+import com.alibaba.fastjson.JSONObject;
 import io.netty.channel.Channel;
 import wei.yigulu.iec104.nettyconfig.Iec104SlaverBuilder;
 import wei.yigulu.iec104.util.SendDataFrameHelper;
 
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStreamReader;
+
 public class StartHttpServer {
     public static void main(String[] args) throws Exception {
         //104端口
@@ -10,10 +16,11 @@ public class StartHttpServer {
         Integer port2 = 2405;
         Integer tem1;
         Integer tem2;
-        if (args.length == 1 || args.length > 2) {
-            throw new RuntimeException("请输入两个端口,参数1为104Slver端口,参数2为配置页面端口");
+        File file = null;
+        if (args.length == 1 || args.length > 3) {
+            throw new RuntimeException("请输入两个端口,参数1为104Slver端口,参数2为配置页面端口,[参数3为配置文件路径]");
         }
-        if (args.length == 2) {
+        if (args.length == 2 || args.length == 3) {
             try {
                 tem1 = Integer.parseInt(args[0]);
                 if (tem1 > 65535 || tem1 < 0) {
@@ -33,6 +40,18 @@ public class StartHttpServer {
             if (tem1.intValue() == tem2.intValue()) {
                 throw new RuntimeException("参数1与参数2不能相同");
             }
+            if (args.length == 3) {
+                try {
+                    String fileUrl = args[2];
+                    File file1 = new File(fileUrl);
+                    if (file1.exists() && file1.isFile()) {
+                        file = file1;
+                    }
+                } catch (Exception e) {
+                    e.printStackTrace();
+                    throw new RuntimeException("配置文件读取失败");
+                }
+            }
             port1 = tem1;
             port2 = tem2;
         }
@@ -47,6 +66,30 @@ public class StartHttpServer {
             }
             return null;
         });
+        if (file != null) {
+            FileInputStream is = null;
+            StringBuilder stringBuilder = null;
+            try {
+                if (file.length() != 0) {
+                    /**
+                     * 文件有内容才去读文件
+                     */
+                    is = new FileInputStream(file);
+                    InputStreamReader streamReader = new InputStreamReader(is);
+                    BufferedReader reader = new BufferedReader(streamReader);
+                    String line;
+                    stringBuilder = new StringBuilder();
+                    while ((line = reader.readLine()) != null) {
+                        stringBuilder.append(line);
+                    }
+                    reader.close();
+                    is.close();
+                    RequestController.getInstance().batchImport(JSONObject.parseObject(stringBuilder.toString()));
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
         HttpServer httpServer = new HttpServer(port2);
         httpServer.start();
     }