严智鑫
2025-06-13 d14cdaf28222bfef468185e34de7c823f1436b19
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
package com.mes.connect.Thread;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mes.common.JsonConversion;
import com.mes.common.ReadFile;
import com.mes.connect.IndustrialInterface.Api;
import com.mes.connect.IndustrialInterface.IndustrialClient;
import com.mes.connect.entity.*;
import com.mes.connect.modbus.ModbusIpClient;
import com.mes.connect.modbus.ModbusTcpClient;
import com.mes.connect.protocol.ProtocolType;
import com.mes.connect.s7.S7Client;
import com.mes.connect.s7.S7ClientOld;
import com.mes.model.entity.Machine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
 
@Slf4j
public class MachineThread extends Thread {
    //当前设备的参数
    private Machine machine;
    private ProtocolType protocolType;
    private IndustrialClient client;
    private PlcParameters plcParameters;
    private LogicConfig logicConfig;
    private Api api;
 
    // 存储所有逻辑线程
    private Map<String, Thread> logicThreads = new ConcurrentHashMap<>();
    // 控制逻辑线程运行的标志
    private Map<String, Boolean> logicRunningFlags = new ConcurrentHashMap<>();
    // 主线程运行标志
    private boolean running = true;
    // 主线程执行间隔
    private int mainThreadInterval = 1000;
 
    public MachineThread(Machine machine, Api api) throws IOException {
        this.machine = machine;
        this.api = api;
        this.logicConfig = JsonConversion.jsonToObjectByJackson(ReadFile.readJson(machine.getLogicFile()).toString(), LogicConfig.class);
        this.plcParameters = JsonConversion.jsonToObjectByJackson(ReadFile.readJson(machine.getMachineFile()).toString(), PlcParameters.class);
        switch (machine.getProtocolType().getName()) {
            case "ModbusTcp":
                client = new ModbusTcpClient(machine.getIp(), machine.getPort(), 1);
                break;
            case "ModbusIp":
                client = new ModbusIpClient(machine.getIp(), machine.getPort());
                break;
            case "S7":
                client = new S7Client(machine.getIp(), machine.getPort(), 0, 1);
                break;
            case "S7Old":
                client = new S7ClientOld(machine.getPlcType().getName(),machine.getIp(), machine.getPort(), 0, 1);;
                break;
            default:
                log.error("无效的协议类型: {}", protocolType);
                throw new IllegalArgumentException("无效的协议类型: " + protocolType);
        }
        if (client != null) {
            client.connect();
            boolean connected = client.isConnected();
            if (!connected) {
                log.error("连接PLC失败: {}", machine.getIp());
            }
        }
    }
 
    @Override
    public void run() {
        log.info("MachineThread启动,设备IP: {}", machine.getIp());
        if (client == null || !client.isConnected()) {
            log.error("PLC客户端未连接,线程退出");
            return;
        }
 
        plcParameters.Initialization();
 
        try {
            // 初始化读取PLC参数
            readPlcParameter();
        } catch (Exception e) {
            log.error("初始化读取PLC参数失败: {}", e.getMessage(), e);
            return;
        }
        // 为每个逻辑项创建并启动子线程
        //startLogicThread(logicConfig.getLogics().get(0));
        for (LogicItem logicItem : logicConfig.getLogics()) {
            startLogicThread(logicItem);
            //startLogicThread(logicConfig.getLogics().get(0));
        }
 
        // 主线程持续运行,定期读取PLC参数并监控连接状态
        while (running) {
            try {
                // 读取PLC参数,为逻辑处理提供最新数据
                readPlcParameter();
 
                // 检查PLC连接状态
                if (!client.isConnected()) {
                    log.warn("PLC连接断开,尝试重新连接");
                    tryReconnect();
                }
 
                // 检查逻辑线程状态,重启已终止的线程
                checkLogicThreadsStatus();
 
                // 等待下一个执行周期
                Thread.sleep(mainThreadInterval);
            } catch (InterruptedException e) {
                log.info("主线程被中断,准备退出");
                running = false;
                Thread.currentThread().interrupt();
            } catch (Exception e) {
                log.error("执行过程中发生错误: {}", e.getMessage(), e);
                // 尝试重新连接
                tryReconnect();
                // 等待一段时间再继续执行
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException ie) {
                    log.info("主线程被中断,准备退出");
                    running = false;
                    Thread.currentThread().interrupt();
                }
            }
        }
 
        // 线程退出前关闭所有资源
        shutdown();
        log.info("MachineThread已退出,设备IP: {}", machine.getIp());
    }
 
    // 为指定逻辑项创建并启动子线程
    private void startLogicThread(LogicItem logicItem) {
        String logicId = logicItem.getName(); // 假设每个LogicItem有唯一ID
        if (logicThreads.containsKey(logicId) && logicThreads.get(logicId).isAlive()) {
            log.warn("逻辑项线程已在运行: {}", logicId);
            return;
        }
 
        // 设置运行标志
        logicRunningFlags.put(logicId, true);
 
        // 创建并启动线程
        Thread thread = new Thread(() -> {
            log.info("逻辑项线程启动: {}", logicId);
 
            // 逻辑项子线程持续运行的循环
            while (logicRunningFlags.getOrDefault(logicId, false)) {
                try {
                    // 执行实际业务逻辑
                    basicsLogic(logicItem);
 
                    // 根据逻辑项的执行频率设置等待时间,默认1000ms
                    int logicInterval = 1000;
                    Thread.sleep(logicInterval);
                } catch (InterruptedException e) {
                    log.info("逻辑项线程被中断,准备退出: {}", logicId);
                    logicRunningFlags.put(logicId, false);
                    Thread.currentThread().interrupt();
                } catch (Exception e) {
                    log.error("执行逻辑项失败: {}, 错误: {}", logicId, e.getMessage(), e);
                    // 等待一段时间再继续执行
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException ie) {
                        log.info("逻辑项线程被中断,准备退出: {}", logicId);
                        logicRunningFlags.put(logicId, false);
                        Thread.currentThread().interrupt();
                    }
                }
            }
 
            log.info("逻辑项线程已退出: {}", logicId);
        });
 
        // 设置线程名称
        thread.setName("Logic-" + logicId);
 
        // 存储线程引用
        logicThreads.put(logicId, thread);
 
        // 启动线程
        thread.start();
        log.info("已启动逻辑项线程: {}", logicId);
    }
 
    // 检查逻辑线程状态,重启已终止的线程
    private void checkLogicThreadsStatus() {
        for (LogicItem logicItem : logicConfig.getLogics()) {
            String logicId = logicItem.getName();
            Thread thread = logicThreads.get(logicId);
 
            // 如果线程不存在或已终止且运行标志为true,则重启线程
            if ((thread == null || !thread.isAlive()) &&
                    logicRunningFlags.getOrDefault(logicId, false)) {
                log.warn("逻辑项线程已终止,准备重启: {}", logicId);
                startLogicThread(logicItem);
            }
        }
    }
 
    // 尝试重新连接PLC
    private void tryReconnect() {
        if (client != null) {
            try {
                log.info("尝试重新连接PLC: {}", machine.getIp());
                client.disconnect();
                Thread.sleep(2000);
                boolean reconnected = client.isConnected();
                if (reconnected) {
                    log.info("PLC重新连接成功: {}", machine.getIp());
                } else {
                    log.error("PLC重新连接失败: {}", machine.getIp());
                }
            } catch (Exception e) {
                log.error("重新连接PLC异常: {}", e.getMessage(), e);
            }
        }
    }
 
    // 关闭线程前的清理工作
    public void shutdown() {
        running = false;
 
        // 停止所有逻辑线程
        for (String logicId : logicRunningFlags.keySet()) {
            logicRunningFlags.put(logicId, false);
            Thread thread = logicThreads.get(logicId);
            if (thread != null && thread.isAlive()) {
                thread.interrupt();
            }
        }
 
        // 等待所有逻辑线程结束
        for (String logicId : logicThreads.keySet()) {
            Thread thread = logicThreads.get(logicId);
            if (thread != null && thread.isAlive()) {
                try {
                    thread.join(1000); // 等待最多1秒
                } catch (InterruptedException e) {
                    log.error("等待逻辑线程结束时被中断: {}", e.getMessage());
                    Thread.currentThread().interrupt();
                }
            }
        }
 
        // 清空线程映射
        logicThreads.clear();
        logicRunningFlags.clear();
 
        // 关闭PLC连接
        if (client != null) {
            try {
                client.disconnect();
                log.info("PLC连接已关闭: {}", machine.getIp());
            } catch (Exception e) {
                log.error("关闭PLC连接异常: {}", e.getMessage(), e);
            }
        }
    }
 
    //示例   解读配置 根据接口返内容给PLC
    public void basicsLogic(LogicItem logicItem) throws JSONException, IOException {
        //1.读取PLC当前参数
        try {
            //遍历逻辑
            boolean isEqual=true;
            for (Logic logic:logicItem.getLogic()){
                String plcValue=plcParameters.getMap().get(logic.getCodeId()).getReadValue().toString();
                if (!logic.getValue().contains(plcValue)){
                    isEqual=false;
                    logic.setEquals(false);
                    logicItem.setEquals(false);
                    break;
                }
            }
            if (isEqual){
                List<String> result=new ArrayList<>();
                //3.查询此逻辑下需要返回给PLC的数据 result可接收  HTTP接口,视图,存储过程 等  如下
                try{
                    String connectType=logicItem.getConnectType();
                    String connectAddress=logicItem.getConnectAddress();
                    Map map=new HashMap();
                    switch (connectType) {
                        case "Http":
                            map.put("method","POST");
                            map.put("plcParameter",plcParameters);
                            result= api.httpApi(connectAddress,map);
                            log.info("接口返回内容:{}",result);
                            break;
                        case "View": // 视图/表
                            result= api.viewApi(connectAddress,map);
                            break;
                        case "Procedure": // 存储过程
                            //result= api.procedureAPI(connectAddress,plcParameter);
                            break;
                        default:
                            log.warn("不支持的连接类型: {}", connectType);
                            return; // 不支持的方式
                    }
 
                }catch (Exception e){
                    log.error("调用接口失败: {}", e.getMessage(), e);
                }
                //4.返回PLC内容
                if (result != null&&!result.isEmpty()) {
                    basicsResult(result,logicItem.getReturnValue());
                }
            }
        }  catch (Exception e) {
            log.error("执行basicsLogic失败: {}", e.getMessage(), e);
        }
    }
 
    //传入需要发送的数据
    public boolean basicsResult(List<String> sendData, List<ReturnValue> returnValue) throws JSONException, IOException {
        for (ReturnValue itemReturnValue:returnValue){
            //需要返回PLC的值
            String values ="";
            if (itemReturnValue.isFixed()){
                //固定值
                values= itemReturnValue.getValue();
            }else{
                //按传递的参数值
                values= sendData.get(Integer.valueOf(itemReturnValue.getValue())-1);
            }
            if (values!=null && !values.equals("")){
                //还需增添  不同类型调用不同方法
                switch (itemReturnValue.getPlcDataType()) {
                    case "int":
                        client.writeRegister(itemReturnValue.getAddress(),(int)Double.parseDouble(values));
                        break;
                    case "string":
                        client.writeString(itemReturnValue.getAddress(),values);
                        break;
                    case "float":
                        client.writeFloat(itemReturnValue.getAddress(),Float.valueOf(values));
                        break;
                    default:
                        log.error("不支持的数据类型: {}", itemReturnValue.getPlcDataType());
                        return false;
                }
 
            }
        }
        return true;
    }
 
    // 按照json文件读取plc内容
    private PlcParameters readPlcParameter() throws IOException, JSONException {
        List<Parameters> parametersList=plcParameters.getParameters();
        for (int i=0;i<parametersList.size();i++){
            //根据类型读取
            switch (parametersList.get(i).getPlcDataType()) {
                case "Word":
                    int value=client.readRegister(parametersList.get(i).getAddress());
                    parametersList.get(i).setReadValue(value);
                    break;
                case "string":
                    //多寄存器    传递数量
                    String strValue = client.readString(parametersList.get(i).getAddress(), 2);
                    parametersList.get(i).setReadValue(strValue);
                    break;
                case "bit":
                    //读线圈
                    boolean bitValue = client.readBit(parametersList.get(i).getAddress());
                    parametersList.get(i).setReadValue(bitValue);
                    break;
                default:
                    log.error("不支持的数据类型: {}", parametersList.get(i).getPlcDataType());
                    return null;
            }
        }
        plcParameters.setParameters(parametersList);
        return plcParameters;
    }
}