wu
2023-12-01 8a0ae6d19d78d2295a10b1cc97bfe17e651234ea
springboot-vue3/src/main/java/com/example/springboot/component/Plcstate.java
@@ -1,29 +1,88 @@
package com.example.springboot.component;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
public class Plcstate extends Thread {
  String name = "";
  Integer count = 0;
  public void readValue() {
    String str = "";
    BufferedReader bufferedReader = null;
    FileInputStream fileInputStream;
    try {
      // 从文件中读取字节数据存入 fileInputStream
      fileInputStream = new FileInputStream("CanadaMes-ui/src/configuration/State.json");
      // 读取 fileInputStream 中字节并将其解码为字符
      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;
      }
      // 将str字符串格式转为json
      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();
    }
  }
  @Override
  public void run() {
    while (this != null) {
      try {
        Thread.sleep(10000);
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      List<Short> plclist = S7control.getinstance().ReadWord("DB103.DBW0", 10);
      if (plclist == null) {
      this.readValue();
      String PlcAddress = this.name;
      Integer Plccount = this.count;
      // System.out.println(PlcAddress);
      } else {
      // List<Short> plclist = S7control.getinstance().ReadWord("DB103.0", 10);
      Short[] values = { 0, 1, 1, 0, 1, 0, 1, 0,
          1, 0, };
      List<Short> plclist = new ArrayList<>(Arrays.asList(values));
      if (plclist != null) {
        List<String> Intlist = new ArrayList<>();
        for (Short value : plclist) {
          Intlist.add(value == 1 ? "自动" : "手动");
        }
        JSONObject jsonObject = new JSONObject();
        // jsonObject.append("sta",
        // new short[] { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, });
        jsonObject.append("sta", plclist);
        jsonObject.append("sta", Intlist);
        WebSocketServer sendwServer = WebSocketServer.sessionMap.get("State");
        if (sendwServer != null) {
          sendwServer.sendMessage(jsonObject.toString());