ZengTao
2024-03-18 f4a9924f1d945978ac264786b7e008b2985d42b5
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
package com.example.springboot.component;
 
import cn.hutool.json.JSONObject;
import com.example.springboot.entity.device.PlcBitObject;
import static com.example.springboot.component.InitUtil.readAndUpdateBitValues;
import static com.example.springboot.component.InitUtil.readAndUpdateWordValues;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class PLCManualJogReview extends Thread {
 
 
    // 用于存储应用程序的配置信息
    private Configuration config;
 
    private static InitUtil initUtil;
    // 创建一个自定义的 S7 控制器消息处理器对象
    MessageHandler customS7Control = new MessageHandler();
 
    // 单例实例
    private static PLCManualJogReview instance;
 
 
 
    public PLCManualJogReview() throws IOException {
        config = new Configuration("config.properties");
        initUtil = new InitUtil();
    }
 
    // 获取单例实例
    public static synchronized PLCManualJogReview getInstance() throws IOException {
        if (instance == null) {
            instance = new PLCManualJogReview();
        }
        return instance;
    }
    String jsonFilePath = PLCManualJogReview.class.getResource("/JsonFile/PlcTest.json").getPath();
 
    PlcBitObject plcTestObject = initUtil.initbit(jsonFilePath);
 
    @Override
    public void run() {
        while (this != null) {
            try {
                Thread.sleep(500);
 
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
 
 
 
         //  List<String> addressList21 = Arrays.asList(config.getProperty("PLCManualJog.button").split(","));
      // List<Boolean> arraylist = S7control.getinstance().readBits(addressList21);
 
 
            readAndUpdateBitValues(plcTestObject);
 
            List<String> codeidlist = Arrays.asList(
                    "D01VFDconveyor", "D02VFDconveyor", "D03VFDconveyor", "D04VFDconveyor",
                    "D05VFDconveyor", "D06VFDconveyor","A01VFDconveyor", "A01VFDconveyorreverse", "A01SERVETRAVEL",
                    "A01SERVETURN", "A01oilpump", "A02VFDconveyor", "A02VFDconveyorreverse",
                    "A02SERVETRAVEL", "A02SERVETURN", "A02oilpump", "B01VFDconveyor", "B01SERVETRAVEL",
                    "B01YVTURN", "B01YVUPDOWN", "B01YVGassing", "B02VFDconveyor", "B02SERVETRAVEL",
                    "B02YVTURN", "B02YVUPDOWN", "B02YVGassing", "A01SERVETRAVELhome", "A02SERVETRAVELhome"
            );
 
 
 
//PLCManualJog.button=DB101.0.0,DB101.0.1,DB101.0.2,DB101.0.3,DB101.0.4,DB101.0.5,DB101.0.6,DB101.3.4,DB101.1.4,DB101.1.2,DB101.5.2,DB101.0.7,DB101.3.5,DB101.1.5,DB101.1.3,DB101.5.3,DB101.1.0,DB101.1.6,DB101.2.6,DB101.2.7,DB101.3.2,DB101.1.1,DB101.1.7,DB101.3.0,DB101.3.1,DB101.3.3,DB101.10.5,DB101.10.6
 
            List<String> address =  plcTestObject.getAddressListByCodeId(codeidlist);
         System.out.println(address);
 
//
//            int index2 = plcTestObject.getPlcBit("B01YVUPDOWN").getAddressIndex();
//            System.out.println(index2);
//
//            System.out.println(plcTestObject.getPlcBit("B01YVUPDOWN").getAddress(index2));
            List<Boolean> arraylist = plcTestObject.getPlcBitValues(codeidlist);
 
 
            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);
 
 
 
            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) {
 
 
                        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<>();
 
 
                            for (int i = 0; i < 28 && i < messageBooleans.size(); i++) {
                                bolList.add(messageBooleans.get(i));
                            }
 
 
                            if (!bolList.isEmpty()) {
                         S7control.getinstance().WriteBit(address, bolList);
//
 
                               System.out.println("messageValue:" + bolList + " written to PLC at address " + address);
                            }
 
                            webserver.clearMessages();
                        }
                    }
                }
 
            }
 
        }
    }
}