ZengTao
2023-11-01 ce7da2e967f19e7e5543c25e91b7a759a6f360a5
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
package com.example.springboot.component;
 
import cn.hutool.json.JSONObject;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class Plcaction extends Thread {
  @Override
  public void run() {
    while (this != null) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
 
//      List<Boolean> paramlist = S7control.getinstance().ReadBits("DB100.DBW", 12);
////
//      JSONObject jsonObject = new JSONObject();
//      jsonObject.append("params", new short[]{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0});
 
 List<Boolean> paramlist = S7control.getinstance().ReadBits("DB2.0.0", 26);
//      Boolean[] values = {true, 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));
      if (paramlist == null) {
 
      } else {
        short[] params = new short[paramlist.size()];
 
        for (int i = 0; i < paramlist.size(); i++) {
          boolean value = paramlist.get(i);
          params[i] = value ? (short) 1 : (short) 0;
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.append("params", params);
        WebSocketServer sendwServer = WebSocketServer.sessionMap.get("action");
        if (sendwServer != null) {
          sendwServer.sendMessage(jsonObject.toString());
        }
      }
 
 
      WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("action");
      if (webSocketServer != null) {
 
          List<String> messages = webSocketServer.getMessages();
 
          String addressList = "DB2.0.0";
 
          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);
            }
 
            // 将布尔列表写入 PLC
            S7control.getinstance().WriteBit(addressList, messageBooleans);
            System.out.println("messageValues:" + messageBooleans);
            System.out.println("addressList:" + addressList);
          // 清空消息列表
          webSocketServer.clearMessages();
        }
      }
    }
  }
}