wuyouming666
2024-01-22 2883c25a71cb080258f5e17430ff5ee37c0061c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.example.springboot.component;
 
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
 
import org.apache.commons.io.FileUtils;
 
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 中字节并将其解码为字符
      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(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", 60);
     List<Short> state = S7control.getinstance().ReadWord("DB103.0", 10);
//      Short[] values1 = { 1231, 1, 1, 1, 1, 1, 2, 33, 2, 3, 4, 5 ,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.append("params", arraylist);
      jsonObject.append("state", state);
      // jsonObject.append("action", sholist);
//      WebSocketServer sendwServer = WebSocketServer.sessionMap.get("Parameter");
//      if (sendwServer != null) {
//        sendwServer.sendMessage(jsonObject.toString());
//      }
 
      ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("Parameter");
      if (sendwServer != null) {
        for (WebSocketServer webserver : sendwServer) {
          webserver.sendMessage(jsonObject.toString());
 
//      WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("Parameter");
          if (webserver != null) {
            List<String> messages = webserver.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 {
                  // 使用正则表达式清除非数字字符
                  String cleanedPart = part.replaceAll("[^0-9-]", "");
                  short value = Short.parseShort(cleanedPart.trim());
                  messageValues.add(value);
                } catch (NumberFormatException e) {
                  // 如果无法解析为 short 类型,则忽略该部分
 
                  e.printStackTrace();
                }
              }
 
              // 将消息值写入 PLC
              S7control.getinstance().WriteWord(addressList, messageValues);
              System.out.println("messageValues:" + messageValues);
              System.out.println("addressList:" + addressList);
              // 清空消息列表
              webserver.clearMessages();
            }
          }
        }
      }
    }
  }
}