Browse Source

传感器

lchao 2 years ago
parent
commit
89fb717207

+ 4 - 2
ruoyi-admin/src/main/java/com/ruoyi/RuoYiApplication.java

@@ -4,9 +4,11 @@ import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 
+import java.io.IOException;
+
 /**
  * 启动程序
- * 
+ *
  * @author ruoyi
  */
 @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@@ -27,4 +29,4 @@ public class RuoYiApplication
                 " |  |  \\    /  \\      /           \n" +
                 " ''-'   `'-'    `-..-'              ");
     }
-}
+}

+ 5 - 5
ruoyi-admin/src/main/resources/application.yml

@@ -17,10 +17,10 @@ ruoyi:
 server:
   # 服务器的HTTP端口,默认为80
   port: 81
-  ssl:
-    key-store: classpath:keystore.jks
-    key-store-password: 123456
-    key-password: 123456
+#  ssl:
+#    key-store: classpath:keystore.jks
+#    key-store-password: 123456
+#    key-password: 123456
 ## 开发环境配置(短信服务配置)
 #server:
 #  # 服务器的HTTP端口,默认为80
@@ -111,7 +111,7 @@ shiro:
     # 首页地址
     indexUrl: /index
     # 验证码开关
-    captchaEnabled: true
+    captchaEnabled: false
     # 验证码类型 math 数组计算 char 字符
     captchaType: math
   cookie:

+ 60 - 0
ruoyi-system/src/main/java/com/ruoyi/system/EchoThread.java

@@ -0,0 +1,60 @@
+package com.ruoyi.system;
+
+import com.alibaba.fastjson.JSONObject;
+import java.io.PrintWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.Socket;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+public class EchoThread implements Runnable{
+    private Socket client;
+    PrintWriter out = null;
+    InputStream in = null;
+    private String equid = null;
+
+    public EchoThread(Socket client){
+        this.client = client;
+    }
+
+    @Override
+    public void run() {
+        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD hh:mm:ss");
+        try {
+            out = new PrintWriter(client.getOutputStream(),true);
+            in = client.getInputStream();
+         while(true){
+                String inputLine="";
+                byte[] temp = new byte[1024];
+                int length = in.read(temp);
+                inputLine+=new String(temp,0,length);
+                JSONObject object = JSONObject.parseObject(inputLine);
+                String st = object.get("ST").toString();
+                String serial = object.get("SERIAL").toString();
+                if(".".equals(inputLine))
+                {
+                    out.println("bye");
+                    break;
+                }
+                //传感器上报的数据===>inputLine
+                System.out.println("===="+inputLine);
+                //do sth
+
+                //do sth
+                JSONObject res = new JSONObject();
+                res.put("ST",st);
+                res.put("SERIAL",serial);
+                res.put("MARK","1");
+                res.put("TM ", sdf.format(new Date()));
+                out.println(res);
+            }
+            in.close();
+            out.close();
+            client.close();
+        } catch (IOException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+    }
+}

+ 89 - 0
ruoyi-system/src/main/java/com/ruoyi/system/TcpClientHandler.java

@@ -0,0 +1,89 @@
+package com.ruoyi.system;
+
+
+
+import com.alibaba.fastjson.JSONObject;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+@Component
+public class TcpClientHandler  extends Thread implements CommandLineRunner {
+
+    private Socket clientSocket;
+    private ServerSocket serverSocket;
+    private PrintWriter out;
+    private InputStream in;
+
+
+
+    @Override
+    public void run(String... args) throws Exception
+    {
+
+        try{
+            ServerSocket server = new ServerSocket(8802);
+            Socket client = null;
+            boolean flag = true;
+            while(flag){
+                System.out.println("start...");
+                client = server.accept();
+                new Thread(new EchoThread(client)).start();
+            }
+            client.close();
+            server.close();
+        }catch(Exception ex){
+            // TODO Auto-generated catch block
+            ex.printStackTrace();
+        }
+//        try {
+//            serverSocket = new ServerSocket(8802);
+//            clientSocket = serverSocket.accept();
+//            SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD hh:mm:ss");
+//            out = new PrintWriter(clientSocket.getOutputStream(),true);
+//            in = clientSocket.getInputStream();
+//            while(true)
+//            {
+//                String inputLine="";
+//                byte[] temp = new byte[1024];
+//                int length = in.read(temp);
+//                inputLine+=new String(temp,0,length);
+//                JSONObject object = JSONObject.parseObject(inputLine);
+//                String st = object.get("ST").toString();
+//                String serial = object.get("SERIAL").toString();
+//                if(".".equals(inputLine))
+//                {
+//                    out.println("bye");
+//                    break;
+//                }
+//                //传感器上报的数据===>inputLine
+//                System.out.println("===="+inputLine);
+//                //do sth
+//
+//                //do sth
+//                JSONObject res = new JSONObject();
+//                res.put("ST",st);
+//                res.put("SERIAL",serial);
+//                res.put("MARK","1");
+//                res.put("TM ", sdf.format(new Date()));
+//                out.println(res);
+//            }
+//            in.close();
+//            out.close();
+//            clientSocket.close();
+//        } catch (IOException e) {
+//            // TODO Auto-generated catch block
+//            e.printStackTrace();
+//        }
+    }
+
+}
+
+