1
zhang
7 天以前 62107c34d3bb40649c6af1385a5f61d81104cf47
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package com.zy.core.thread;
 
import HslCommunication.Core.Types.OperateResult;
import HslCommunication.Core.Types.OperateResultExOne;
import HslCommunication.Profinet.Siemens.SiemensPLCS;
import HslCommunication.Profinet.Siemens.SiemensS7Net;
import com.alibaba.fastjson.JSON;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.framework.common.DateUtils;
import com.zy.asrs.entity.Devp;
import com.zy.asrs.service.DevpService;
import com.zy.asrs.utils.SpringContextUtil;
import com.zy.common.utils.News;
import com.zy.core.DevpThread;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.OutputQueue;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.constant.*;
import com.zy.core.enums.SlaveType;
import com.zy.core.enums.TaskType;
import com.zy.core.model.DevpSlave;
import com.zy.core.model.Task;
import com.zy.core.model.protocol.StaProtocol;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
 
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * 输送线线程
 * Created by vincent on 2020/8/4
 */
@Data
@Slf4j
public class SiemensDevpThread implements Runnable, DevpThread {
 
    private DevpSlave slave;
 
    private SiemensS7Net siemensS7Net;
 
    private Map<Integer, StaProtocol> station = new ConcurrentHashMap<>();
 
    private volatile boolean connected = false;
 
    private static final int WRITE_RETRY_MAX = 5;
    private static final int WRITE_RETRY_INTERVAL_MS = 200;
    private static final int READ_INTERVAL_MS = 100;
    private static final int DB_UPDATE_INTERVAL_MS = 1000; // 数据库更新间隔
 
    private long lastDbUpdateTime = 0;
 
 
    public SiemensDevpThread(DevpSlave slave) {
        this.slave = slave;
    }
 
 
    @Override
    @SuppressWarnings("InfiniteLoopStatement")
    public void run() {
        connect();
        while (!Thread.currentThread().isInterrupted()) {
            try {
                TaskType step = TaskType.READ;
                Task task = MessageQueue.poll(SlaveType.Devp, slave.getId());
                if (task != null) {
                    step = task.getStep();
                }
                switch (step) {
                    // 读数据
                    case READ:
                        read();
                        break;
                    // 写数据 ID+目标站
                    case WRITE:
                        write((StaProtocol) task.getData());
                        break;
                    default:
                        break;
                }
                Thread.sleep(READ_INTERVAL_MS);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                log.warn("SiemensDevp线程被中断 [id:{}]", slave.getId());
                break;
            } catch (Exception e) {
                log.error("SiemensDevp线程运行异常 [id:{}]", slave.getId(), e);
                // 发生异常时尝试重连
                reconnect();
            }
        }
        // 线程退出时关闭连接
        close();
        log.info("SiemensDevp线程已退出 [id:{}]", slave.getId());
    }
 
    /**
     * 初始化或重置站点状态
     */
    private void initSite() {
        List<Integer> staNos = slave.getStaNos();
        for (Integer siteId : staNos) {
            StaProtocol staProtocol = getOrCreateStaProtocol(siteId);
            resetStaProtocol(staProtocol);
        }
    }
 
    /**
     * 获取或创建站点协议对象
     */
    private StaProtocol getOrCreateStaProtocol(Integer siteId) {
        return station.computeIfAbsent(siteId, id -> {
            StaProtocol protocol = new StaProtocol();
            protocol.setSiteId(id);
            return protocol;
        });
    }
 
    /**
     * 重置站点协议状态
     */
    private void resetStaProtocol(StaProtocol staProtocol) {
        staProtocol.setWorkNo(0);
        staProtocol.setAutoing(false);
        staProtocol.setLoading(false);
        staProtocol.setInEnable(false);
        staProtocol.setOutEnable(false);
        staProtocol.setEmptyMk(false);
        staProtocol.setStaNo(0);
 
        if (!staProtocol.isPakMk() && !staProtocol.isLoading()) {
            staProtocol.setPakMk(true);
        }
    }
 
    @Override
    public boolean connect() {
        // 如果已连接,先关闭旧连接
        if (siemensS7Net != null) {
            try {
                siemensS7Net.ConnectClose();
            } catch (Exception e) {
                log.warn("关闭旧PLC连接异常 [id:{}]", slave.getId(), e);
            }
        }
 
        siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200, slave.getIp());
        siemensS7Net.setRack(slave.getRack().byteValue());
        siemensS7Net.setSlot(slave.getSlot().byteValue());
        
        OperateResult connect = siemensS7Net.ConnectServer();
        if (connect.IsSuccess) {
            connected = true;
            News.info("SiemensDevp - 1 - 输送线plc连接成功 ===>> [id:{}] [ip:{}] [port:{}]", 
                    slave.getId(), slave.getIp(), slave.getPort());
            log.info("输送线plc连接成功 [id:{}] [ip:{}] [rack:{}] [slot:{}]", 
                    slave.getId(), slave.getIp(), slave.getRack(), slave.getSlot());
        } else {
            connected = false;
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】输送线plc连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", 
                    DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
            News.error("SiemensDevp - 2 - 输送线plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", 
                    slave.getId(), slave.getIp(), slave.getPort());
            log.error("输送线plc连接失败 [id:{}] [ip:{}] [error:{}]", 
                    slave.getId(), slave.getIp(), connect.Message);
        }
        
        initSite();
        return connected;
    }
 
    /**
     * 重连机制
     */
    private void reconnect() {
        log.warn("尝试重新连接PLC [id:{}]", slave.getId());
        try {
            Thread.sleep(1000); // 重连前等待1秒
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return;
        }
        connect();
    }
 
    /**
     * 读取状态 ====> 整块plc
     */
    private void read() throws InterruptedException {
        if (!connected || siemensS7Net == null) {
            log.warn("PLC未连接,跳过读取 [id:{}]", slave.getId());
            return;
        }
 
        List<Integer> staNos = slave.getStaNos();
        int staNoSize = staNos.size();
        
        // 读取站点状态
        OperateResultExOne<byte[]> result = siemensS7Net.Read(
                StationStatusField.ALL.buildAddress(), 
                (short) (staNoSize * StationStatusField.ALL.getByteLength()));
        
        if (!result.IsSuccess) {
            log.error("读取站点状态失败 [id:{}] [error:{}]", slave.getId(), result.Message);
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】读取输送线plc状态信息失败 ===>> [id:{1}] [ip:{2}]", 
                    DateUtils.convert(new Date()), slave.getId(), slave.getIp()));
            connected = false;
            return;
        }
 
        byte[] content = result.Content;
        for (int i = 0; i < staNoSize; i++) {
            Integer siteId = staNos.get(i);
            StaProtocol staProtocol = getOrCreateStaProtocol(siteId);
            parseStationStatus(content, i, staProtocol);
        }
 
        // 读取条码
        readBarcodes();
 
        // 读取外形检测错误
        readDimensionErrors();
 
        // 读取PLC故障
        readPlcAlarms(staNos, staNoSize);
 
        // 定期更新数据库(降低频率)
        updateDatabaseIfNeeded();
    }
 
    /**
     * 解析单个站点状态
     */
    private void parseStationStatus(byte[] content, int index, StaProtocol staProtocol) {
        int offset = index * StationStatusField.ALL.getByteLength();
        staProtocol.setWorkNo(siemensS7Net.getByteTransform().TransInt32(content, offset));
        staProtocol.setStaNo((int) siemensS7Net.getByteTransform().TransInt16(
                content, offset + StationStatusField.FINAL_TARGET.getOffset()));
        
        boolean[] status = siemensS7Net.getByteTransform().TransBool(
                content, offset + StationStatusField.STATUS_WORD.getOffset(), 
                StationStatusField.STATUS_WORD.getByteLength());
        
        staProtocol.setAutoing(status[0]);
        staProtocol.setLoading(status[1]);
        staProtocol.setInEnable(status[2]);
        staProtocol.setOutEnable(status[3]);
        staProtocol.setEmptyMk(status[4]);
        staProtocol.setFullPlt(status[5]);
        staProtocol.setHigh(status[6]);
        staProtocol.setLow(status[7]);
 
        if (!staProtocol.isPakMk() && !staProtocol.isLoading()) {
            staProtocol.setPakMk(true);
        }
    }
 
    /**
     * 读取条码信息
     */
    private void readBarcodes() {
        List<Integer> barcodeArr = slave.getBarcodeArr();
        if (barcodeArr == null || barcodeArr.isEmpty()) {
            return;
        }
 
        OperateResultExOne<byte[]> result = siemensS7Net.Read(
                DeviceField.BARCODE.buildAddress(), 
                (short) (barcodeArr.size() * DeviceField.BARCODE.getByteLength()));
        
        if (!result.IsSuccess) {
            log.warn("读取条码失败 [id:{}]", slave.getId());
            return;
        }
 
        byte[] content = result.Content;
        for (int i = 0; i < barcodeArr.size(); i++) {
            String barcode = siemensS7Net.getByteTransform().TransString(
                    content, i * DeviceField.BARCODE.getByteLength(), 
                    DeviceField.BARCODE.getByteLength(), "UTF-8");
            
            BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(
                    SlaveType.Barcode, barcodeArr.get(i));
            
            if (Cools.isEmpty(barcode)) {
                if (barcodeThread != null) {
                    barcodeThread.clearBarcode();
                }
            } else {
                if (barcodeThread != null && !barcode.equals(barcodeThread.getBarcode())) {
                    barcodeThread.setBarcode(barcode);
                    log.info("料箱码:{}", barcode);
                }
            }
        }
    }
 
    /**
     * 读取外形检测错误
     */
    private void readDimensionErrors() {
        List<Integer> staNosError = slave.getStaNosError();
        if (staNosError == null || staNosError.isEmpty()) {
            return;
        }
 
        OperateResultExOne<byte[]> result = siemensS7Net.Read(
                DeviceField.DIMENSION_WORD.buildAddress(), 
                (short) (staNosError.size() * DeviceField.DIMENSION_WORD.getByteLength()));
        
        if (!result.IsSuccess) {
            log.warn("读取外形检测错误失败 [id:{}]", slave.getId());
            return;
        }
 
        byte[] content = result.Content;
        for (int i = 0; i < staNosError.size(); i++) {
            Integer siteId = staNosError.get(i);
            StaProtocol staProtocol = getOrCreateStaProtocol(siteId);
            
            boolean[] status = siemensS7Net.getByteTransform().TransBool(
                    content, i * DeviceField.DIMENSION_WORD.getByteLength(), 
                    DeviceField.DIMENSION_WORD.getByteLength());
            
            staProtocol.setFrontErr(status[0]);
            staProtocol.setBackErr(status[1]);
            staProtocol.setHighErr(status[2]);
            staProtocol.setLeftErr(status[3]);
            staProtocol.setRightErr(status[4]);
            staProtocol.setWeightErr(status[5]);
            staProtocol.setBarcodeErr(status[6]);
        }
    }
 
    /**
     * 读取PLC故障信息
     */
    private void readPlcAlarms(List<Integer> staNos, int staNoSize) {
        OperateResultExOne<byte[]> result = siemensS7Net.Read(
                PlcAlarmDefinition.ALL.buildAddress(), 
                (short) (staNoSize * PlcAlarmDefinition.ALL.getByteLength()));
        
        if (!result.IsSuccess) {
            log.warn("读取PLC故障信息失败 [id:{}]", slave.getId());
            return;
        }
 
        byte[] content = result.Content;
        for (int i = 0; i < staNoSize; i++) {
            Integer siteId = staNos.get(i);
            StaProtocol staProtocol = station.get(siteId);
            if (staProtocol == null) {
                continue;
            }
 
            boolean[] status = siemensS7Net.getByteTransform().TransBool(
                    content, i * PlcAlarmDefinition.ALL.getByteLength(), 1);
            
            staProtocol.setBreakerErr(status[0]);
            staProtocol.setInfraredErr(status[1]);
            staProtocol.setOutTimeErr(status[2]);
            staProtocol.setSeizeSeatErr(status[3]);
            staProtocol.setWrkYgoodsN(status[4]);
            staProtocol.setInverterErr(status[5]);
            staProtocol.setContactErr(status[6]);
            staProtocol.setUpcontactErr(status[7]);
        }
    }
 
    /**
     * 按需更新数据库(降低更新频率)
     */
    private void updateDatabaseIfNeeded() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastDbUpdateTime < DB_UPDATE_INTERVAL_MS) {
            return;
        }
 
        try {
            List<Integer> staNos = slave.getStaNos();
            List<Devp> devps = new ArrayList<>(staNos.size());
            for (Integer siteId : staNos) {
                StaProtocol staProtocol = station.get(siteId);
                if (staProtocol != null) {
                    devps.add(staProtocol.toSqlModel());
                }
            }
 
            if (devps.isEmpty()) {
                return;
            }
 
            DevpService devpService = SpringContextUtil.getBean(DevpService.class);
            if (devpService != null) {
                devpService.updateBatchByDevpNo(devps);
                lastDbUpdateTime = currentTime;
                log.debug("批量更新数据库成功 [id:{}] [count:{}]", slave.getId(), devps.size());
            } else {
                log.error("DevpService未找到,无法更新数据库 [id:{}]", slave.getId());
            }
        } catch (Exception e) {
            log.error("更新数据库数据失败 [id:{}]", slave.getId(), e);
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】更新数据库数据失败 ===>> [id:{1}]", 
                    DateUtils.convert(new Date()), slave.getId()));
            News.error("SiemensDevp - 3 - 更新数据库数据失败 ===>> [id:{}]", slave.getId());
            // 更新失败时重置站点状态
            initSite();
        }
    }
 
    /**
     * 写入 ID+目标站 =====> 单站点写入
     */
    private void write(StaProtocol staProtocol) throws InterruptedException {
        if (staProtocol == null) {
            log.warn("写入数据为空,跳过 [id:{}]", slave.getId());
            return;
        }
 
        if (!connected || siemensS7Net == null) {
            log.error("PLC未连接,无法写入 [id:{}]", slave.getId());
            return;
        }
 
        List<Integer> staNos = slave.getStaNos();
        int index = staNos.indexOf(staProtocol.getSiteId());
        if (index < 0) {
            log.error("站点编号不存在于配置中 [id:{}] [siteId:{}]", slave.getId(), staProtocol.getSiteId());
            return;
        }
 
        int baseOffset = index * TaskField.ALL.getByteLength();
        String workNoAddr = TaskField.TASK_NUMBER.getAddressPattern() + PlcConstant.ADDRESS_CONCATENATION 
                + (baseOffset + TaskField.TASK_NUMBER.getOffset());
        String destStaAddr = TaskField.DEST_STATION.getAddressPattern() + PlcConstant.ADDRESS_CONCATENATION 
                + (baseOffset + TaskField.DEST_STATION.getOffset());
 
        // 任务下发重试机制
        int writeCount = 0;
        boolean success = false;
        
        while (writeCount < WRITE_RETRY_MAX) {
            OperateResult writeResult = siemensS7Net.Write(workNoAddr, staProtocol.getWorkNo().shortValue());
            Thread.sleep(WRITE_RETRY_INTERVAL_MS);
            
            OperateResult writeResult1 = siemensS7Net.Write(destStaAddr, staProtocol.getStaNo().shortValue());
            
            if (writeResult.IsSuccess && writeResult1.IsSuccess) {
                log.info("写入输送线命令成功 [id:{}] [siteId:{}] [workNo:{}] [destSta:{}] [retry:{}]", 
                        slave.getId(), staProtocol.getSiteId(), staProtocol.getWorkNo(), 
                        staProtocol.getStaNo(), writeCount);
                success = true;
                break;
            }
            
            writeCount++;
            log.error("写入输送线命令失败 [id:{}] [siteId:{}] [retry:{}] [workErr:{}] [destErr:{}]", 
                    slave.getId(), staProtocol.getSiteId(), writeCount, 
                    writeResult.Message, writeResult1.Message);
            
            if (writeCount < WRITE_RETRY_MAX) {
                Thread.sleep(WRITE_RETRY_INTERVAL_MS);
            }
        }
 
        if (!success) {
            StaProtocol currentSta = station.get(staProtocol.getSiteId());
            if (currentSta != null && currentSta.getWorkNo() == 0 && currentSta.getStaNo() == 0) {
                currentSta.setPakMk(true);
            }
            
            String errorMsg = MessageFormat.format("【{0}】写入输送线站点数据失败。输送线plc编号={1},站点数据={2}", 
                    slave.getId(), JSON.toJSON(staProtocol));
            OutputQueue.DEVP.offer(errorMsg);
            News.error("SiemensDevp - 4 - 写入输送线站点数据失败。输送线plc编号={},站点数据={}", 
                    slave.getId(), JSON.toJSON(staProtocol));
            log.error(errorMsg);
        }
    }
 
 
    /**
     * 设置入库标记
     */
    @Override
    public void setPakMk(Integer siteId, boolean pakMk) {
        StaProtocol staProtocol = station.get(siteId);
        if (null != staProtocol) {
            staProtocol.setPakMk(pakMk);
        }
    }
 
    @Override
    public void close() {
        if (siemensS7Net != null) {
            try {
                siemensS7Net.ConnectClose();
                log.info("PLC连接已关闭 [id:{}]", slave.getId());
            } catch (Exception e) {
                log.error("关闭PLC连接异常 [id:{}]", slave.getId(), e);
            } finally {
                connected = false;
            }
        }
    }
 
 
}