From 09fe50d59e2f1e2261d376eb49ad6c40301f25b4 Mon Sep 17 00:00:00 2001
From: ZengTao <2773468879@qq.com>
Date: 星期五, 10 五月 2024 16:08:59 +0800
Subject: [PATCH] 修改IP与数据库连接

---
 springboot-vue3/src/main/java/com/example/springboot/component/PlcParameter.java |  149 +++++++++++++------------------------------------
 1 files changed, 41 insertions(+), 108 deletions(-)

diff --git a/springboot-vue3/src/main/java/com/example/springboot/component/PlcParameter.java b/springboot-vue3/src/main/java/com/example/springboot/component/PlcParameter.java
index fc93c23..54f3223 100644
--- a/springboot-vue3/src/main/java/com/example/springboot/component/PlcParameter.java
+++ b/springboot-vue3/src/main/java/com/example/springboot/component/PlcParameter.java
@@ -14,135 +14,68 @@
 import java.io.InputStreamReader;
 import java.io.UnsupportedEncodingException;
 
+
 import org.apache.commons.io.FileUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
+import org.springframework.stereotype.Component;
 
+@Component
 public class PlcParameter extends Thread {
-  String name = "";
-  Integer count = 0;
 
-  public static String readFileToString(String filePath) throws IOException {
-    File file = new File(filePath);
-    return FileUtils.readFileToString(file, "UTF-8");
-  }
-
-  public void readValue() {
-    String str = "";
-    BufferedReader bufferedReader = null;
-    FileInputStream fileInputStream;
-    try {
-      // 浠庢枃浠朵腑璇诲彇瀛楄妭鏁版嵁瀛樺叆 fileInputStream
-      fileInputStream = new FileInputStream("CanadaMes-ui/src/configuration/Parameter.json");
-      // 璇诲彇 fileInputStream 涓瓧鑺傚苟灏嗗叾瑙g爜涓哄瓧绗�
-      InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");
-      // 鎻愰珮璇诲彇鏁堢巼锛屽湪 BufferedReader 鍐呭寘瑁� InputStreamReader
-      bufferedReader = new BufferedReader(inputStreamReader);
-      String line = null;
-      // 灏� bufferedReader 鍐呭涓�琛屼竴琛岃祴鍊肩粰str
-      while ((line = bufferedReader.readLine()) != null) {
-        str += line;
-      }
-
-      // 灏唖tr瀛楃涓叉牸寮忚浆涓簀son
-      JSONObject jsonObject = new JSONObject(str);
-
-      // 鑾峰彇json涓殑鍊�
-      JSONArray address = jsonObject.getJSONArray("address");
-      for (int i = 0; i < address.size(); i++) {
-        JSONObject ress = (JSONObject) address.get(i);
-
-        this.name = ress.getStr("name");
-        this.count = ress.getInt("count");
-
-      }
-
-    } catch (FileNotFoundException e) {
-      e.printStackTrace();
-    } catch (UnsupportedEncodingException e) {
-      e.printStackTrace();
-    } catch (IOException e) {
-      e.printStackTrace();
-    }
-  }
+//  private final MessageSender messageSender;
+//
+//  public PlcParameter(MessageSender messageSender) {
+//    this.messageSender = messageSender;
+//  }
 
   @Override
   public void run() {
     while (this != null) {
       try {
+//        try {
+//          RabbitMQUtils.sendMessage("Hello, world!", "module_queue");
+//        } catch (Exception e) {
+//          e.printStackTrace();
+//        }
+//        try {
+//          RabbitMQUtils.receiveMessage("module_queue");
+//        } catch (Exception e) {
+//          e.printStackTrace();
+//        }
+//        String message = "Hello, RabbitMQ!";
+//        messageSender.sendMessage(message);
         Thread.sleep(500);
 
       } catch (InterruptedException e) {
         e.printStackTrace();
       }
-      this.readValue();
-      String PlcAddress = this.name;
-      Integer Plccount = this.count;
 
-      // System.out.println(stt);
-      // List<Short> arraylist = S7control.getinstance().ReadWord("DB100.0", 12);
-      // List<Short> state = S7control.getinstance().ReadWord("DB103.0", 10);
-      Short[] values1 = { 1231, 1, 1, 1, 1, 1, 2, 33, 2, 3, 4, 5 };
-      List<Short> arraylist = new ArrayList<>(Arrays.asList(values1));
-
-      Short[] values2 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
-      List<Short> state = new ArrayList<>(Arrays.asList(values2));
-
-      Boolean[] values = { false, true, true, true, true, true, true, true, true,
-          true, true, true, true, true, true, true, true, true, true, true, true, true, true,
-          true, false, true };
-      List<Boolean> paramlist = new ArrayList<>(Arrays.asList(values));
-
-      short[] sholist = new short[paramlist.size()];
-
-      for (int i = 0; i < paramlist.size(); i++) {
-        boolean value = paramlist.get(i);
-        sholist[i] = value ? (short) 1 : (short) 0;
-      }
 
       JSONObject jsonObject = new JSONObject();
-      // new short[]{1,1, 1, 1, 1, 1, 2, 33, 2,3, 4, 5}
-      // new short[]{0,0, 0, 0, 0, 0, 0, 0, 0, 0}
+      jsonObject.put("key1", "value1");
+      jsonObject.put("key2", 123);
+      JSONArray jsonArray = new JSONArray();
+      jsonArray.add("item1");
+      jsonArray.add("item2");
+      jsonObject.put("key3", jsonArray);
 
-      jsonObject.append("params", arraylist);
-      jsonObject.append("state", state);
-      jsonObject.append("action", sholist);
-      WebSocketServer sendwServer = WebSocketServer.sessionMap.get("Parameter");
-      if (sendwServer != null) {
-        sendwServer.sendMessage(jsonObject.toString());
+      try {
+        // 灏� JSON 瀵硅薄杞崲涓哄瓧绗︿覆骞跺彂閫佸埌闃熷垪//RabbitMQUtils.sendJsonMessage(jsonObject, "json_queue");
+      } catch (Exception e) {
+        e.printStackTrace();
       }
 
-      WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("Parameter");
-      if (webSocketServer != null) {
-        List<String> messages = webSocketServer.getMessages();
-
-        String addressList = "DB100.0";
-
-        if (!messages.isEmpty()) {
-          // 灏嗘渶鍚庝竴涓秷鎭浆鎹负 short 绫诲瀷鐨勫垪琛�
-          String lastMessage = messages.get(messages.size() - 1);
-          System.out.println("messages锛�" + messages);
-          String[] parts = lastMessage.split(",");
-          List<Short> messageValues = new ArrayList<>();
-          for (String part : parts) {
-            try {
-              // 浣跨敤姝e垯琛ㄨ揪寮忔竻闄ら潪鏁板瓧瀛楃
-              String cleanedPart = part.replaceAll("[^0-9-]", "");
-              short value = Short.parseShort(cleanedPart.trim());
-              messageValues.add(value);
-            } catch (NumberFormatException e) {
-              // 濡傛灉鏃犳硶瑙f瀽涓� short 绫诲瀷锛屽垯蹇界暐璇ラ儴鍒�
-
-              e.printStackTrace();
-            }
-          }
-
-          // 灏嗘秷鎭�煎啓鍏� PLC
-//          S7control.getinstance().WriteWord(addressList, messageValues);
-          System.out.println("messageValues锛�" + messageValues);
-          System.out.println("addressList锛�" + addressList);
-          // 娓呯┖娑堟伅鍒楄〃
-          webSocketServer.clearMessages();
-        }
+      try {
+        // 浠庨槦鍒楁帴鏀跺苟瑙f瀽 JSON 娑堟伅涓� JSONObject 瀵硅薄
+        JSONObject receivedJsonObject = RabbitMQUtils.receiveJsonMessage(JSONObject.class, "json_queue");
+        System.out.println(receivedJsonObject.toString());
+      } catch (Exception e) {
+        e.printStackTrace();
       }
     }
   }
+
+
 }

--
Gitblit v1.8.0