From f7371a4a3413ec37931b26a95b7b0290a46219f2 Mon Sep 17 00:00:00 2001
From: zhang <zc857179121@qq.com>
Date: 星期五, 10 四月 2026 15:18:56 +0800
Subject: [PATCH] Merge branch 'rcs_master' into jdxaj
---
zy-asc-conveyor/src/main/java/com/zy/acs/conveyor/core/thread/SiemensDevpThread.java | 283 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 283 insertions(+), 0 deletions(-)
diff --git a/zy-asc-conveyor/src/main/java/com/zy/acs/conveyor/core/thread/SiemensDevpThread.java b/zy-asc-conveyor/src/main/java/com/zy/acs/conveyor/core/thread/SiemensDevpThread.java
new file mode 100644
index 0000000..dbc7d9a
--- /dev/null
+++ b/zy-asc-conveyor/src/main/java/com/zy/acs/conveyor/core/thread/SiemensDevpThread.java
@@ -0,0 +1,283 @@
+package com.zy.acs.conveyor.core.thread;
+
+import HslCommunication.Core.Types.OperateResultExOne;
+import HslCommunication.Profinet.Siemens.SiemensS7Net;
+import com.zy.acs.common.utils.News;
+import com.zy.acs.conveyor.core.constant.DeviceField;
+import com.zy.acs.conveyor.core.constant.PlcAlarmDefinition;
+import com.zy.acs.conveyor.core.constant.StationStatusField;
+import com.zy.acs.conveyor.core.model.StaProtocol;
+import com.zy.acs.conveyor.core.properties.DevpSlave;
+import com.zy.acs.conveyor.entity.Devp;
+import com.zy.acs.conveyor.service.DevpService;
+import com.zy.acs.conveyor.utils.SpringContextUtil;
+import com.zy.acs.framework.common.Cools;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 杈撻�佺嚎绾跨▼
+ * Created by vincent on 2020/8/4
+ */
+@Data
+@Slf4j
+public class SiemensDevpThread implements Runnable {
+
+ private DevpSlave slave;
+
+ private SiemensS7Net siemensS7Net;
+
+ private Map<Integer, StaProtocol> station;
+
+ 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 = 2000; // 鏁版嵁搴撴洿鏂伴棿闅�
+
+ private long lastDbUpdateTime = 0;
+
+
+ public SiemensDevpThread(DevpSlave slave, SiemensS7Net siemensS7Net, Map<Integer, StaProtocol> station) {
+ this.slave = slave;
+ this.siemensS7Net = siemensS7Net;
+ this.station = station;
+ }
+
+
+ @Override
+ @SuppressWarnings("InfiniteLoopStatement")
+ public void run() {
+ while (!Thread.currentThread().isInterrupted()) {
+ try {
+ read();
+ 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);
+ }
+ }
+ log.info("SiemensDevp璇荤嚎绋嬪凡閫�鍑� [id:{}]", slave.getId());
+ }
+
+ /**
+ * 璇诲彇鐘舵�� ====> 鏁村潡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);
+ connected = false;
+ return;
+ }
+
+ byte[] content = result.Content;
+ for (int i = 0; i < staNoSize; i++) {
+ StaProtocol staProtocol = station.get(staNos.get(staNoSize));
+ parseStationStatus(content, i, staProtocol);
+ }
+
+ // 璇诲彇鏉$爜
+ readBarcodes();
+
+ // 璇诲彇澶栧舰妫�娴嬮敊璇�
+ readDimensionErrors();
+
+ // 璇诲彇PLC鏁呴殰
+ readPlcAlarms(staNos, staNoSize);
+
+ // 瀹氭湡鏇存柊鏁版嵁搴擄紙闄嶄綆棰戠巼锛�
+ updateDatabaseIfNeeded();
+ }
+
+ /**
+ * 瑙f瀽鍗曚釜绔欑偣鐘舵��
+ */
+ 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]);
+
+
+ }
+
+ /**
+ * 璇诲彇鏉$爜淇℃伅
+ */
+ 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");
+
+ if (!Cools.isEmpty(barcode)) {
+ StaProtocol staProtocol = station.get(barcodeArr.get(i));
+ staProtocol.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 = station.get(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);
+ News.error("SiemensDevp - 3 - 鏇存柊鏁版嵁搴撴暟鎹け璐� ===>> [id:{}]", slave.getId());
+ }
+ }
+
+
+}
--
Gitblit v1.9.1