wuyouming666
2023-12-11 a487e1560cddf1d8b3e7372d9791b9589e9af96c
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package com.example.springboot.component;
 
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import org.apache.commons.io.FileUtils;
 
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class PLCManualJog 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/address.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<String> addressList0 = new ArrayList<>();
      addressList0.add("DB101.2.6");
      addressList0.add("DB101.2.7");
      addressList0.add("DB101.3.0");
      addressList0.add("DB101.3.1");
      addressList0.add("DB101.3.2");
      addressList0.add("DB101.3.3");
      addressList0.add("DB101.3.4");
      addressList0.add("DB101.3.5");
 
      List<String> addressList21 = new ArrayList<>();
      addressList21.add("DB101.0.0");
      addressList21.add("DB101.0.1");
      addressList21.add("DB101.0.2");
      addressList21.add("DB101.0.3");
      addressList21.add("DB101.0.4");
      addressList21.add("DB101.0.5");
      addressList21.add("DB101.0.6");
      addressList21.add("DB101.0.7");
      addressList21.add("DB101.1.0");
      addressList21.add("DB101.1.1");
      addressList21.add("DB101.1.2");
      addressList21.add("DB101.1.3");
      addressList21.add("DB101.1.4");
      addressList21.add("DB101.1.5");
      addressList21.add("DB101.1.6");
      addressList21.add("DB101.1.7");
      List<Boolean> arraylist = S7control.getinstance().readBits(addressList21);
      List<Boolean> arraylist2 = S7control.getinstance().readBits(addressList0);
      List<Boolean> arraylist3 = S7control.getinstance().ReadBits("DB101.5.2", 2);
      // Boolean[] values1 = { false, true, true, true, false, false, true, false,
      // false, true, true, true, false, false,
      // true, false };
      // List<Boolean> arraylist = new ArrayList<>(Arrays.asList(values1));
      // Boolean[] values2 = { false, true, true, true, false, false, true, false };
      // List<Boolean> arraylist2 = new ArrayList<>(Arrays.asList(values2));
      // Boolean[] values3 = { false, false };
      // List<Boolean> arraylist3 = new ArrayList<>(Arrays.asList(values3));
 
      arraylist.addAll(arraylist2);
      arraylist.addAll(arraylist3);
 
      JSONObject jsonObject = new JSONObject();
 
      short[] params = new short[arraylist.size()];
      for (int i = 0; i < arraylist.size(); i++) {
        boolean value = arraylist.get(i);
        params[i] = value ? (short) 1 : (short) 0;
      }
 
      jsonObject.append("params", params);
 
//      System.out.println("messageValue:" + arraylist +"messageValue2:" + arraylist2 );
//      WebSocketServer sendwServer = WebSocketServer.sessionMap.get("ManualJog");
//      if (sendwServer != null) {
//        sendwServer.sendMessage(jsonObject.toString());
//      }
 
      ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("ManualJog");
      if (sendwServer != null) {
        for (WebSocketServer webserver : sendwServer) {
          webserver.sendMessage(jsonObject.toString());
 
//      WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("ManualJog");
 
          if (webserver != null) {
            //       String addressList1 = "DB101.0.0";
//        String addressList2 = "DB101.2.6";
            String addressList3 = "DB101.5.2";
 
 
            List<String> addressList2 = new ArrayList<>();
            addressList2.add("DB101.2.6");
            addressList2.add("DB101.2.7");
            addressList2.add("DB101.3.0");
            addressList2.add("DB101.3.1");
            addressList2.add("DB101.3.2");
            addressList2.add("DB101.3.3");
            addressList2.add("DB101.3.4");
            addressList2.add("DB101.3.5");
 
 
            List<String> addressList211 = new ArrayList<>();
            addressList211.add("DB101.0.0");
            addressList211.add("DB101.0.1");
            addressList211.add("DB101.0.2");
            addressList211.add("DB101.0.3");
            addressList211.add("DB101.0.4");
            addressList211.add("DB101.0.5");
            addressList211.add("DB101.0.6");
            addressList211.add("DB101.0.7");
            addressList211.add("DB101.1.0");
            addressList211.add("DB101.1.1");
            addressList211.add("DB101.1.2");
            addressList211.add("DB101.1.3");
            addressList211.add("DB101.1.4");
            addressList211.add("DB101.1.5");
            addressList211.add("DB101.1.6");
            addressList211.add("DB101.1.7");
 
            List<String> addressList311 = new ArrayList<>();
            addressList311.add("DB101.5.2");
            addressList311.add("DB101.5.3");
 
            List<String> messages = webserver.getMessages();
            if (!messages.isEmpty()) {
              // 将最后一个消息转换为整数类型的列表
              String lastMessage = messages.get(messages.size() - 1);
              // System.out.println("messages:" + messages);
              String[] parts = lastMessage.split(",");
              List<Integer> messageValues = new ArrayList<>();
              for (String part : parts) {
                try {
                  // 使用正则表达式清除非数字字符
                  String cleanedPart = part.replaceAll("[^0-9-]", "");
                  Integer value = Integer.parseInt(cleanedPart.trim());
                  messageValues.add(value);
                } catch (NumberFormatException e) {
                  // 如果无法解析为整数类型,则忽略该部分
                  // e.printStackTrace();
                }
 
              }
              // 将消息值转换为布尔列表
              List<Boolean> messageBooleans = new ArrayList<>();
              for (Integer value : messageValues) {
                messageBooleans.add(value == 1 ? true : false);
              }
              // System.out.println(messageBooleans);
              List<Boolean> bolList = new ArrayList<>();
              List<Boolean> bolList2 = new ArrayList<>();
              List<Boolean> bolList3 = new ArrayList<>();
 
              for (int i = 0; i < 16 && i < messageBooleans.size(); i++) {
                bolList.add(messageBooleans.get(i));
              }
 
              for (int i = 16; i < 24 && i < messageBooleans.size(); i++) {
                bolList2.add(messageBooleans.get(i));
              }
 
              for (int i = 24; i < 26 && i < messageBooleans.size(); i++) {
                bolList3.add(messageBooleans.get(i));
              }
 
              // System.out.println(bolList);
              // System.out.println(bolList2);
              // System.out.println(bolList3);
              if (!bolList.isEmpty()) {
                S7control.getinstance().WriteBit(addressList211, bolList);
                System.out.println("messageValue:" + bolList + " written to PLC at address " + addressList211);
              }
              if (!bolList2.isEmpty()) {
                S7control.getinstance().WriteBit(addressList2, bolList2);
                System.out.println("messageValue:" + bolList2 + " written to PLC at address " + addressList2);
              }
              if (!bolList3.isEmpty()) {
                S7control.getinstance().WriteBit(addressList311, bolList3);
                System.out.println("messageValue:" + bolList3 + " written to PLC at address " + addressList311);
              }
              webserver.clearMessages();
            }
          }
        }
 
      }
 
    }
  }
}