wuyouming666
2023-12-11 7ae7bcc0245afa4d94516feddc2b8bd60e4253c6
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package com.example.springboot.component;
 
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.google.common.primitives.Bytes;
import org.apache.commons.io.FileUtils;
 
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class PlcServoManualone extends Thread {
 
 
 
  private Configuration config;
 
  public PlcServoManualone() throws IOException {
    config = new Configuration("config.properties");
  }
 
  public static class PLCBooleanConverter {
 
    public static short[] convertListsToBinaryShortArray(List<Boolean>... lists) {
      // 计算所有列表中元素的总数
      int totalSize = 0;
      for (List<Boolean> list : lists) {
        totalSize += list.size();
      }
 
      // 创建一个足够大的数组来存放所有的二进制值
      short[] binaryShortArray = new short[totalSize];
 
      // 当前数组填充位置指针
      int currentIndex = 0;
 
      // 遍历所有的列表
      for (List<Boolean> list : lists) {
        // 将每个布尔值转换为 0 或 1 并填入数组
        for (Boolean value : list) {
          binaryShortArray[currentIndex++] = (short) (value ? 1 : 0);
        }
      }
 
      return binaryShortArray;
    }
  }
 
//读取id
  public StringBuilder queGlassid(String address,int count) {
    StringBuilder writedstrIdOut = new StringBuilder();
    byte[] writedglassidbytesOut = S7control.getinstance().ReadByte(address, count);
    if (writedglassidbytesOut != null) {
      // 获取玻璃id
      for (byte iditem : writedglassidbytesOut) {
        writedstrIdOut.append((char) iditem);
      }
    }
    return writedstrIdOut;
  }
//写入id
  public void outmesid(String glassid,String address) {
    //System.out.println("outmesid:" + glassid);
    List<Byte> glassidlist = new ArrayList();
    char ds[]=glassid.toCharArray();
    for (char iditem : ds) {
      glassidlist.add((byte)iditem);
    }
    byte[] bytes = Bytes.toArray(glassidlist);
    System.out.println("outmesidbytes:" + bytes.length);
    S7control.getinstance().WriteByte(address, bytes);
  }
 
  @Override
  public void run() {
    while (this != null) {
      try {
        Thread.sleep(1000);
 
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
 
//A01组合中地址表集合
      List<String> addresses = Arrays.asList(config.getProperty("A01.addresses").split(","));
      //System.out.println(addresses);
 
      List<Short> arraylist = S7control.getinstance().readWords(addresses);
 
//A02组合中地址表集合
      List<String> addresses2 = Arrays.asList(config.getProperty("A02.addresses").split(","));
      List<Short> arraylist2 = S7control.getinstance().readWords(addresses2);
 
      //A01翻转地址
      List<String> addresses3 = Arrays.asList(config.getProperty("A01.flipAddress"));
      List<Short> arraylist3 = S7control.getinstance().readWords(addresses3);
 
      //A02翻转地址
      List<String> addresses4 = Arrays.asList(config.getProperty("A02.flipAddress"));
 
      List<Short> arraylist4 = S7control.getinstance().readWords(addresses4);
     //B01组合中地址
      String b01Address = config.getProperty("B01.address");
      List<Short> arraylist5 = S7control.getinstance().ReadWord(b01Address, 2);
      //B02组合地址
      String b02Address = config.getProperty("B02.address");
      List<Short> arraylist6 = S7control.getinstance().ReadWord(b02Address, 2);
//      System.out.println(b02Address);
     // 所有故障按钮地址
      List<String> addresses7 = Arrays.asList(config.getProperty("faultButtonAddresses").split(","));
      List<Boolean> arraylist7 = S7control.getinstance().readBits(addresses7);
 
      //A01启动地址
      List<String> A01start = Arrays.asList(config.getProperty("A01start"));
      //A02启动地址
      List<String> A02start = Arrays.asList(config.getProperty("A02start"));
//A01半自动地址
      List<String> A01Bstart = Arrays.asList(config.getProperty("A01Bstart"));
      //A02半自动地址
      List<String> A02Bstart = Arrays.asList(config.getProperty("A02Bstart"));
      //
      List<String> abortresumeTasks = Arrays.asList(config.getProperty("abortresumeTasks"));
      //回零按钮地址
      List<String> waddresses4 = Arrays.asList(config.getProperty("HomedButtonAddresses").split(","));
      // 所有复位按钮地址
      List<String> Reset = Arrays.asList(config.getProperty("resetButtonAddresses").split(","));
//      System.out.println(Reset);
      // 所有未回零按钮地址
      List<String> addresses8 = Arrays.asList(config.getProperty("notHomedButtonAddresses").split(","));
      // 进片id地址
      String inputGlassIdAddress = config.getProperty("inputGlassIdAddress");
      String outputGlassIdAddress = config.getProperty("outputGlassIdAddress");
      StringBuilder queueid1 = queGlassid(inputGlassIdAddress, 14);
      // 存片id地址
      StringBuilder queueid2 = queGlassid(outputGlassIdAddress, 14);
 
      List<Boolean> A01readstart = S7control.getinstance().readBits(A01start);
      List<Boolean> A02readstart = S7control.getinstance().readBits(A02start);
      List<Boolean> A01readBstart = S7control.getinstance().readBits(A01Bstart);
      List<Boolean> A02readsBtart = S7control.getinstance().readBits(A02Bstart);
 
      List<Boolean> resumeTasks = S7control.getinstance().readBits(abortresumeTasks);
 
      short[] combinedBinaryShortArray = PLCBooleanConverter.convertListsToBinaryShortArray(
              A01readstart, A01readBstart, A02readstart, A02readsBtart
      );
 
 
// 所有故障按钮转换为1和0
      short[] params2 = new short[arraylist7.size()];
      for (int i = 0; i < arraylist7.size(); i++) {
        boolean value = arraylist7.get(i);
        params2[i] = value ? (short) 1 : (short) 0;
      }
 
//急停中止按钮
      short[] resumeTask = new short[resumeTasks.size()];
      for (int i = 0; i < resumeTasks.size(); i++) {
        boolean value = resumeTasks.get(i);
        resumeTask[i] = value ? (short) 1 : (short) 0;
      }
 
      List<Boolean> Reset1 = S7control.getinstance().readBits(Reset);
    //  System.out.println(Reset1);
// 所有故障按钮转换为1和0
      short[] rReset = new short[Reset1.size()];
      for (int i = 0; i < Reset1.size(); i++) {
        boolean value = Reset1.get(i);
        rReset[i] = value ? (short) 1 : (short) 0;
      }
 
 
 
      List<Boolean> arraylist8 = S7control.getinstance().readBits(addresses8);
      short[] params = new short[arraylist8.size()];
      for (int i = 0; i < arraylist8.size(); i++) {
        boolean value = arraylist8.get(i);
        params[i] = value ? (short) 1 : (short) 0;
      }
 
 
 
 
 
//      Short[] values1 = { 1, 2, 3, 4, 5, 6, 7};
//      List<Short> arraylist = new ArrayList<>(Arrays.asList(values1));
//      Short[] values2 = { 1, 2, 3, 4, 5};
//      List<Short> arraylist2 = new ArrayList<>(Arrays.asList(values2));
//
//      Short[] values3 = {1};
//      List<Short> arraylist3 = new ArrayList<>(Arrays.asList(values3));
//      Short[] values4 = {2};
//      List<Short> arraylist4 = new ArrayList<>(Arrays.asList(values4));
//      Short[] values5 = {3,3};
//      List<Short> arraylist5 = new ArrayList<>(Arrays.asList(values5));
//      Short[] values6 = { 4,4};
//      List<Short> arraylist6 = new ArrayList<>(Arrays.asList(values6));
//      Boolean[] values7 = { false, true, true, true, true, true};
//     List<Boolean> arraylist7 = new ArrayList<>(Arrays.asList(values7));
//      Boolean[] values8 = { false, true, true, true, true, true};
//      List<Boolean> arraylist8 = new ArrayList<>(Arrays.asList(values8));
//
//      String queueid1 = "x1234567890123";
//      String queueid2 = "x1234567890123";
//
//      short[] params2 = new short[arraylist7.size()];
//      for (int i = 0; i < arraylist7.size(); i++) {
//        boolean value = arraylist7.get(i);
//        params2[i] = value ? (short) 1 : (short) 0;
//      }
//
//
//      short[] params = new short[arraylist8.size()];
//      for (int i = 0; i < arraylist8.size(); i++) {
//        boolean value = arraylist8.get(i);
//        params[i] = value ? (short) 1 : (short) 0;
//      }
 
      JSONObject jsonObject = new JSONObject();
 
      jsonObject.append("zuhe1", arraylist);
      jsonObject.append("zuhe2", arraylist2);
      jsonObject.append("zuhe3", arraylist3);
      jsonObject.append("zuhe4", arraylist4);
      jsonObject.append("zuhe5", arraylist5);
      jsonObject.append("zuhe6", arraylist6);
      jsonObject.append("guzhang", params2);
   jsonObject.append("weihuiling", params);
      jsonObject.append("jinpianid", queueid1);
      jsonObject.append("qupianid", queueid2);
      jsonObject.append("fuwei", rReset);
      jsonObject.append("qidong", combinedBinaryShortArray);
      jsonObject.append("resumeTasks", resumeTask);
 
 
 
//      WebSocketServer sendwServer = WebSocketServer.sessionMap.get("ServoManualone");
//      if (sendwServer != null) {
//        sendwServer.sendMessage(jsonObject.toString());
//      }
 
      ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("ServoManualone");
      if (sendwServer != null) {
        for (WebSocketServer webserver : sendwServer) {
          webserver.sendMessage(jsonObject.toString());
 
 
          if (webserver != null) {
            List<String> messages = webserver.getMessages();
 
 
            if (!messages.isEmpty()) {
              // 将最后一个消息转换为 short 类型的列表
              String lastMessage = messages.get(messages.size() - 1);
//        System.out.println("lastMessage:" + lastMessage);
              JSONArray messageArray = new JSONArray(lastMessage);
 
 
              // 写入AO1组合
              if (messageArray.getJSONArray(0).size() > 0) {
                JSONArray jsonArray = messageArray.getJSONArray(0);
                List<Short> sValue = new ArrayList<>();
                for (int i = 0; i < jsonArray.size(); i++) {
                  Object value = jsonArray.get(i);
                  if (value != null && !value.toString().equals("null")) {
                    try {
                      String cleanedValue = value.toString().replaceAll("[^0-9-]", "");
                      short val = Short.parseShort(cleanedValue.trim());
                      sValue.add(val);
                      System.out.println("messageValue:" + Arrays.asList(val) + " added to the list");
                    } catch (NumberFormatException e) {
                      // 如果无法解析为 short 类型,则忽略该部分
                      System.err.println("Could not parse value: " + value);
                    }
                  }
                }
                if (!sValue.isEmpty()) {
 
                  S7control.getinstance().WriteWord(addresses, sValue);
                  // 检查是否读取到了位值,并且列表不为空
                  if (A01readstart != null && !A01readstart.isEmpty()) {
                    // 创建一个新的列表用于写入新值
                    List<Boolean> a01startval = new ArrayList<>();
                    // 遍历所有读取到的位值
                    for (Boolean bit : A01readstart) {
                      // 取反当前位的值并添加到新的列表中
                      a01startval.add(!bit);
                    }
                    // 写入新的位值列表
                    S7control.getinstance().WriteBit(A01start, a01startval);
                  }
                  System.out.println("Values " + sValue + " written to PLC at address " + addresses);
                }
              }
 
 
              //// 写入AO2组合
              if (messageArray.getJSONArray(1).size() > 0) {
                JSONArray jsonArray2 = messageArray.getJSONArray(1);
                List<Short> sValue2 = new ArrayList<>();
                for (int i = 0; i < jsonArray2.size(); i++) {
                  Object value = jsonArray2.get(i);
                  if (value != null && !value.toString().equals("null")) {
                    try {
                      String cleanedValue = value.toString().replaceAll("[^0-9-]", "");
                      short val = Short.parseShort(cleanedValue.trim());
                      sValue2.add(val);
                      System.out.println("messageValue:" + Arrays.asList(val) + " added to the list");
                    } catch (NumberFormatException e) {
                      // 如果无法解析为 short 类型,则忽略该部分
                      System.err.println("Could not parse value: " + value);
                    }
                  }
                }
                if (!sValue2.isEmpty()) {
                  S7control.getinstance().WriteWord(addresses2, sValue2);
 
                  if (A02readstart != null && !A02readstart.isEmpty()) {
                    // 创建一个新的列表用于写入新值
                    List<Boolean> a02startval = new ArrayList<>();
                    // 遍历所有读取到的位值
                    for (Boolean bit : A02readstart) {
                      // 取反当前位的值并添加到新的列表中
                      a02startval.add(!bit);
                    }
                    // 写入新的位值列表
                    S7control.getinstance().WriteBit(A02start, a02startval);
                  }
                  System.out.println("Values " + sValue2 + " written to PLC at address " + addresses2);
                }
              }
 
 
              // 复位按钮写入
              if (messageArray.getJSONArray(2).size() > 0) {
                JSONArray jsonArray3 = messageArray.getJSONArray(2);
                List<Boolean> sValue3 = new ArrayList<>();
                for (int i = 0; i < jsonArray3.size(); i++) {
                  Object value = jsonArray3.get(i);
                  if (value != null && !value.toString().equals("null")) {
                    try {
                      String cleanedValue = value.toString().replaceAll("[^0-9-]", "");
                      boolean val = "1".equals(cleanedValue.trim());
                      sValue3.add(val);
                      System.out.println("messageValue: " + Arrays.asList(val) + " added to the list");
                    } catch (NumberFormatException e) {
                      // 如果无法解析为 boolean 类型,则忽略该部分
                      System.err.println("Could not parse value: " + value);
                    }
                  }
                }
                if (!sValue3.isEmpty()) {
                  S7control.getinstance().WriteBit(Reset, sValue3);
                  System.out.println("Values " + sValue3 + " written to PLC at address " + Reset);
                }
              }
 
              //回零按钮写入
              if (messageArray.getJSONArray(3).size() > 0) {
                JSONArray jsonArray4 = messageArray.getJSONArray(3);
                List<Boolean> sValue4 = new ArrayList<>();
                for (int i = 0; i < jsonArray4.size(); i++) {
                  Object value = jsonArray4.get(i);
                  if (value != null && !value.toString().equals("null")) {
                    try {
                      String cleanedValue = value.toString().replaceAll("[^0-9-]", "");
                      boolean val = "1".equals(cleanedValue.trim());
                      sValue4.add(val);
                      System.out.println("messageValue: " + Arrays.asList(val) + " added to the list");
                    } catch (NumberFormatException e) {
                      // 如果无法解析为 boolean 类型,则忽略该部分
                      System.err.println("Could not parse value: " + value);
                    }
                  }
                }
                if (!sValue4.isEmpty()) {
                  S7control.getinstance().WriteBit(waddresses4, sValue4);
                  System.out.println("Values " + sValue4 + " written to PLC at address " + waddresses4);
                }
              }
 
 
              //取片ID写入
              if (messageArray.getJSONArray(5).size() > 0) {
//            JSONArray jsonArray5 = messageArray.getJSONArray(4);
                String jsonArray5 = String.valueOf(messageArray.getJSONArray(5));
                if (!jsonArray5.isEmpty()) {
//              String value = (String) jsonArray5.get(0);
                  jsonArray5 = jsonArray5.replaceAll("[\\[\\]\"]", ""); // 去除方括号和双引号
                  outmesid(jsonArray5, outputGlassIdAddress);
                  System.out.println("Values1 " + jsonArray5 + " written to PLC at address " + outputGlassIdAddress);
                }
              }
              // 进片ID写入
              if (messageArray.getJSONArray(4).size() > 0) {
                JSONArray jsonArray6 = messageArray.getJSONArray(4);
 
                if (!jsonArray6.isEmpty()) {
                  String value2 = (String) jsonArray6.get(0);
                  outmesid(value2.trim(), inputGlassIdAddress);
                  System.out.println("Values2 " + value2 + " written to PLC at address " + inputGlassIdAddress);
                }
              }
 
 
              if (messageArray.getJSONArray(6).size() > 0) {
                JSONArray jsonArray4 = messageArray.getJSONArray(6);
                List<Boolean> sValueb4 = new ArrayList<>();
                for (int i = 0; i < jsonArray4.size(); i++) {
                  Object value = jsonArray4.get(i);
                  if (value != null && !value.toString().equals("null")) {
                    try {
                      String cleanedValue = value.toString().replaceAll("[^0-9-]", "");
                      boolean val = "1".equals(cleanedValue.trim());
                      sValueb4.add(val);
                      System.out.println("messageValue: " + Arrays.asList(val) + " added to the list");
                    } catch (NumberFormatException e) {
                      // 如果无法解析为 boolean 类型,则忽略该部分
                      System.err.println("Could not parse value: " + value);
                    }
                  }
                }
                if (!sValueb4.isEmpty()) {
 
                  S7control.getinstance().WriteBit(A01Bstart, sValueb4);
                  System.out.println("Values " + sValueb4 + " written to PLC at address " + A01Bstart);
                }
              }
 
 
              if (messageArray.getJSONArray(7).size() > 0 && !messageArray.isNull(7)) {
                JSONArray jsonArray4 = messageArray.getJSONArray(7);
                List<Boolean> sValueb5 = new ArrayList<>();
                for (int i = 0; i < jsonArray4.size(); i++) {
                  Object value = jsonArray4.get(i);
                  if (value != null && !value.toString().equals("null")) {
                    try {
                      String cleanedValue = value.toString().replaceAll("[^0-9-]", "");
                      boolean val = "1".equals(cleanedValue.trim());
                      sValueb5.add(val);
                      System.out.println("messageValue: " + Arrays.asList(val) + " added to the list");
                    } catch (NumberFormatException e) {
                      // 如果无法解析为 boolean 类型,则忽略该部分
                      System.err.println("Could not parse value: " + value);
                    }
                  }
                }
                if (!sValueb5.isEmpty()) {
 
                  S7control.getinstance().WriteBit(A02Bstart, sValueb5);
                  System.out.println("Values " + sValueb5 + " written to PLC at address " + A02Bstart);
                }
              }
 
 
 
 
              // 清空消息列表
              webserver.clearMessages();
            }
          }
        }
      }
    }
  }
}