From 16f4bb3c28fc85cffcc511718c903ada9fdab134 Mon Sep 17 00:00:00 2001
From: huang <1532065656@qq.com>
Date: 星期五, 26 十二月 2025 16:59:45 +0800
Subject: [PATCH] 调用mes导入工程参数修改,Excel表数据流程卡号一致;增加读取层号/工程号方法; 工程号一致覆盖更新
---
mes-processes/mes-plcSend/src/main/java/com/mes/service/PlcTestWriteService.java | 433 +++++++++++++++++++++++++++++++++++++++++------------
1 files changed, 333 insertions(+), 100 deletions(-)
diff --git a/mes-processes/mes-plcSend/src/main/java/com/mes/service/PlcTestWriteService.java b/mes-processes/mes-plcSend/src/main/java/com/mes/service/PlcTestWriteService.java
index 4170ef4..51d053e 100644
--- a/mes-processes/mes-plcSend/src/main/java/com/mes/service/PlcTestWriteService.java
+++ b/mes-processes/mes-plcSend/src/main/java/com/mes/service/PlcTestWriteService.java
@@ -7,7 +7,8 @@
import com.mes.device.entity.DeviceConfig;
import com.mes.device.service.DeviceConfigService;
import com.mes.device.util.ConfigJsonHelper;
-import com.mes.service.PlcDynamicDataService;
+import com.mes.plc.client.PlcClient;
+import com.mes.plc.factory.PlcClientFactory;
import com.mes.s7.enhanced.EnhancedS7Serializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@@ -21,11 +22,9 @@
/**
* PLC娴嬭瘯鍐欏叆鏈嶅姟
- *
+ *
* 鍩轰簬DeviceConfig鐨勬柊API锛岀敤浜庢ā鎷烶LC琛屼负杩涜娴嬭瘯
- *
- * 鎺ㄨ崘浣跨敤锛欴evicePlcOperationService锛堢敓浜х幆澧冿級
- *
+ *
* @author huang
* @date 2025/10/29
*/
@@ -35,24 +34,26 @@
@Resource
private DeviceConfigService deviceConfigService;
-
+
@Resource
private PlcDynamicDataService plcDynamicDataService;
-
+
+ @Resource
+ private PlcClientFactory plcClientFactory;
+
private final ObjectMapper objectMapper = new ObjectMapper();
private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<Map<String, Object>>() {};
private static final int ON = 1;
private static final int OFF = 0;
-
- // 缂撳瓨涓嶅悓璁惧鐨凷7Serializer瀹炰緥
+
+ // 缂撳瓨涓嶅悓璁惧鐨凷7Serializer瀹炰緥锛堜繚鎸佸吋瀹癸紝閫愭杩佺Щ锛�
private final ConcurrentMap<String, EnhancedS7Serializer> serializerCache = new ConcurrentHashMap<>();
- // ==================== 鍩轰簬DeviceConfig鐨勬柊API锛堟帹鑽愪娇鐢級 ====================
-
+
/**
* 鏍规嵁璁惧ID妯℃嫙PLC鍙戦�佽姹傚瓧
- *
+ *
* @param deviceId 璁惧ID
* @return 鏄惁鎴愬姛
*/
@@ -63,42 +64,98 @@
return false;
}
try {
- EnhancedS7Serializer s7Serializer = getSerializerForDevice(device);
- if (s7Serializer == null) {
- log.error("鑾峰彇S7Serializer澶辫触: deviceId={}", deviceId);
- return false;
- }
-
- // 浣跨敤PlcDynamicDataService璇诲彇鏁版嵁锛堟敮鎸乤ddressMapping锛�
- Map<String, Object> currentData = plcDynamicDataService.readAllPlcData(device, s7Serializer);
- if (currentData == null || currentData.isEmpty()) {
- log.error("璇诲彇PLC鏁版嵁澶辫触锛岃繑鍥炵┖: deviceId={}", deviceId);
- return false;
- }
-
- // 妫�鏌ヨ仈鏈虹姸鎬�
- Object onlineStateObj = currentData.get("onlineState");
- Integer onlineState = null;
- if (onlineStateObj != null) {
- if (onlineStateObj instanceof Number) {
- onlineState = ((Number) onlineStateObj).intValue();
- } else {
- try {
- String strValue = String.valueOf(onlineStateObj);
- if (!strValue.isEmpty() && !"null".equalsIgnoreCase(strValue)) {
- onlineState = Integer.parseInt(strValue);
+ // 灏濊瘯浣跨敤鏂扮殑PLC瀹㈡埛绔伐鍘�
+ PlcClient plcClient = plcClientFactory.getClient(device);
+ if (plcClient != null) {
+ // 浣跨敤鏂扮殑PLC瀹㈡埛绔鍙栨暟鎹�
+ Map<String, Object> currentData = plcClient.readAllData();
+ if (currentData != null && !currentData.isEmpty()) {
+ // 妫�鏌ヨ仈鏈虹姸鎬侊紙浠呭綋閰嶇疆涓瓨鍦ㄨ瀛楁鏃讹級
+ if (hasFieldInConfig(device, "onlineState")) {
+ Object onlineStateObj = currentData.get("onlineState");
+ if (onlineStateObj != null) {
+ Integer onlineState = parseInteger(onlineStateObj);
+ if (onlineState == OFF) {
+ log.info("褰撳墠PLC鑱旀満妯″紡涓�0锛屽仠姝㈣仈鏈�: deviceId={}", deviceId);
+ return false;
+ }
}
- } catch (NumberFormatException e) {
- log.warn("瑙f瀽onlineState澶辫触: deviceId={}, value={}", deviceId, onlineStateObj, e);
+ }
+
+ // 妫�鏌ユ眹鎶ュ瓧锛屽鏋滀负1鍒欓噸缃负0
+ Integer plcReport = parseInteger(currentData.get("plcReport"));
+ if (plcReport != null && plcReport == ON) {
+ log.info("褰撳墠涓婄墖PLC姹囨姤瀛椾负1锛岄噸缃负0: deviceId={}", deviceId);
+ currentData.put("plcReport", OFF);
+ }
+
+ // 璁剧疆璇锋眰瀛椾负1
+ currentData.put("plcRequest", ON);
+
+ // 浣跨敤鏂扮殑PLC瀹㈡埛绔啓鍏ユ暟鎹�
+ boolean success = plcClient.writeData(currentData);
+ if (success) {
+ log.info("妯℃嫙PLC鍙戦�佽姹傚瓧鎴愬姛锛歱lcRequest=1, deviceId={}", deviceId);
+ return true;
}
}
}
-
- if (onlineState != null && onlineState == OFF) {
- log.info("褰撳墠PLC鑱旀満妯″紡涓�0锛屽仠姝㈣仈鏈�: deviceId={}", deviceId);
+
+ // 濡傛灉鏂板鎴风澶辫触锛屽洖閫�鍒版棫瀹炵幇锛堜繚鎸佸吋瀹癸級
+ log.warn("鏂癙LC瀹㈡埛绔け璐ワ紝鍥為��鍒版棫瀹炵幇: deviceId={}", deviceId);
+ return simulatePlcRequestByDeviceLegacy(device);
+ } catch (Exception e) {
+ log.error("鏍规嵁璁惧妯℃嫙PLC璇锋眰瀛楀け璐�: deviceId={}", deviceId, e);
+ return false;
+ }
+ }
+
+ /**
+ * 鏃х増瀹炵幇锛氭牴鎹澶嘔D妯℃嫙PLC鍙戦�佽姹傚瓧
+ *
+ * @param device 璁惧閰嶇疆
+ * @return 鏄惁鎴愬姛
+ */
+ private boolean simulatePlcRequestByDeviceLegacy(DeviceConfig device) {
+ try {
+ EnhancedS7Serializer s7Serializer = getSerializerForDevice(device);
+ if (s7Serializer == null) {
+ log.error("鑾峰彇S7Serializer澶辫触: deviceId={}", device.getId());
return false;
}
-
+
+ // 浣跨敤PlcDynamicDataService璇诲彇鏁版嵁锛堟敮鎸乤ddressMapping锛�
+ Map<String, Object> currentData = plcDynamicDataService.readAllPlcData(device, s7Serializer);
+ if (currentData == null || currentData.isEmpty()) {
+ log.error("璇诲彇PLC鏁版嵁澶辫触锛岃繑鍥炵┖: deviceId={}", device.getId());
+ return false;
+ }
+
+ // 妫�鏌ヨ仈鏈虹姸鎬侊紙浠呭綋閰嶇疆涓瓨鍦ㄨ瀛楁鏃讹級
+ if (hasFieldInConfig(device, "onlineState")) {
+ Object onlineStateObj = currentData.get("onlineState");
+ Integer onlineState = null;
+ if (onlineStateObj != null) {
+ if (onlineStateObj instanceof Number) {
+ onlineState = ((Number) onlineStateObj).intValue();
+ } else {
+ try {
+ String strValue = String.valueOf(onlineStateObj);
+ if (!strValue.isEmpty() && !"null".equalsIgnoreCase(strValue)) {
+ onlineState = Integer.parseInt(strValue);
+ }
+ } catch (NumberFormatException e) {
+ log.warn("瑙f瀽onlineState澶辫触: deviceId={}, value={}", device.getId(), onlineStateObj, e);
+ }
+ }
+ }
+
+ if (onlineState != null && onlineState == OFF) {
+ log.info("褰撳墠PLC鑱旀満妯″紡涓�0锛屽仠姝㈣仈鏈�: deviceId={}", device.getId());
+ return false;
+ }
+ }
+
// 妫�鏌ユ眹鎶ュ瓧锛屽鏋滀负1鍒欓噸缃负0
Object plcReportObj = currentData.get("plcReport");
Integer plcReport = null;
@@ -112,33 +169,33 @@
plcReport = Integer.parseInt(strValue);
}
} catch (NumberFormatException e) {
- log.warn("瑙f瀽plcReport澶辫触: deviceId={}, value={}", deviceId, plcReportObj, e);
+ log.warn("瑙f瀽plcReport澶辫触: deviceId={}, value={}", device.getId(), plcReportObj, e);
}
}
}
-
+
if (plcReport != null && plcReport == ON) {
- log.info("褰撳墠涓婄墖PLC姹囨姤瀛椾负1锛岄噸缃负0: deviceId={}", deviceId);
+ log.info("褰撳墠涓婄墖PLC姹囨姤瀛椾负1锛岄噸缃负0: deviceId={}", device.getId());
currentData.put("plcReport", OFF);
}
-
+
// 璁剧疆璇锋眰瀛椾负1
currentData.put("plcRequest", ON);
-
+
// 浣跨敤PlcDynamicDataService鍐欏叆鏁版嵁
plcDynamicDataService.writePlcData(device, currentData, s7Serializer);
-
- log.info("妯℃嫙PLC鍙戦�佽姹傚瓧鎴愬姛锛歱lcRequest=1, deviceId={}", deviceId);
+
+ log.info("妯℃嫙PLC鍙戦�佽姹傚瓧鎴愬姛锛堟棫鐗堬級锛歱lcRequest=1, deviceId={}", device.getId());
return true;
} catch (Exception e) {
- log.error("鏍规嵁璁惧妯℃嫙PLC璇锋眰瀛楀け璐�: deviceId={}", deviceId, e);
+ log.error("鏍规嵁璁惧妯℃嫙PLC璇锋眰瀛楀け璐ワ紙鏃х増锛�: deviceId={}", device.getId(), e);
return false;
}
}
-
+
/**
* 鏍规嵁璁惧ID妯℃嫙PLC浠诲姟瀹屾垚姹囨姤
- *
+ *
* @param deviceId 璁惧ID
* @return 鏄惁鎴愬姛
*/
@@ -149,38 +206,75 @@
return false;
}
try {
- EnhancedS7Serializer s7Serializer = getSerializerForDevice(device);
- if (s7Serializer == null) {
- log.error("鑾峰彇S7Serializer澶辫触: deviceId={}", deviceId);
- return false;
+ // 灏濊瘯浣跨敤鏂扮殑PLC瀹㈡埛绔伐鍘�
+ PlcClient plcClient = plcClientFactory.getClient(device);
+ if (plcClient != null) {
+ // 浣跨敤鏂扮殑PLC瀹㈡埛绔鍙栨暟鎹�
+ Map<String, Object> currentData = plcClient.readAllData();
+ if (currentData == null) {
+ currentData = new HashMap<>();
+ }
+
+ // 璁剧疆姹囨姤瀛椾负1锛岃姹傚瓧娓�0
+ currentData.put("plcReport", ON);
+ currentData.put("plcRequest", OFF);
+
+ // 浣跨敤鏂扮殑PLC瀹㈡埛绔啓鍏ユ暟鎹�
+ boolean success = plcClient.writeData(currentData);
+ if (success) {
+ log.info("妯℃嫙PLC浠诲姟瀹屾垚姹囨姤锛歱lcReport=1, deviceId={}", deviceId);
+ return true;
+ }
}
-
- // 浣跨敤PlcDynamicDataService璇诲彇鏁版嵁
- Map<String, Object> currentData = plcDynamicDataService.readAllPlcData(device, s7Serializer);
- if (currentData == null || currentData.isEmpty()) {
- log.error("璇诲彇PLC鏁版嵁澶辫触锛岃繑鍥炵┖: deviceId={}", deviceId);
- return false;
- }
-
- // 璁剧疆姹囨姤瀛椾负1锛岃姹傚瓧娓�0
- currentData.put("plcReport", ON);
- currentData.put("plcRequest", OFF);
- currentData.put("mesGlassCount", 10);
-
- // 浣跨敤PlcDynamicDataService鍐欏叆鏁版嵁
- plcDynamicDataService.writePlcData(device, currentData, s7Serializer);
-
- log.info("妯℃嫙PLC浠诲姟瀹屾垚姹囨姤锛歱lcReport=1, mesGlassCount=10, deviceId={}", deviceId);
- return true;
+
+ // 濡傛灉鏂板鎴风澶辫触锛屽洖閫�鍒版棫瀹炵幇锛堜繚鎸佸吋瀹癸級
+ log.warn("鏂癙LC瀹㈡埛绔け璐ワ紝鍥為��鍒版棫瀹炵幇: deviceId={}", deviceId);
+ return simulatePlcReportByDeviceLegacy(device);
} catch (Exception e) {
log.error("鏍规嵁璁惧妯℃嫙PLC姹囨姤澶辫触: deviceId={}", deviceId, e);
return false;
}
}
-
+
+ /**
+ * 鏃х増瀹炵幇锛氭牴鎹澶囨ā鎷烶LC浠诲姟瀹屾垚姹囨姤
+ *
+ * @param device 璁惧閰嶇疆
+ * @return 鏄惁鎴愬姛
+ */
+ private boolean simulatePlcReportByDeviceLegacy(DeviceConfig device) {
+ try {
+ EnhancedS7Serializer s7Serializer = getSerializerForDevice(device);
+ if (s7Serializer == null) {
+ log.error("鑾峰彇S7Serializer澶辫触: deviceId={}", device.getId());
+ return false;
+ }
+
+ // 浣跨敤PlcDynamicDataService璇诲彇鏁版嵁
+ Map<String, Object> currentData = plcDynamicDataService.readAllPlcData(device, s7Serializer);
+ if (currentData == null || currentData.isEmpty()) {
+ log.error("璇诲彇PLC鏁版嵁澶辫触锛岃繑鍥炵┖: deviceId={}", device.getId());
+ return false;
+ }
+
+ // 璁剧疆姹囨姤瀛椾负1锛岃姹傚瓧娓�0
+ currentData.put("plcReport", ON);
+ currentData.put("plcRequest", OFF);
+
+ // 浣跨敤PlcDynamicDataService鍐欏叆鏁版嵁
+ plcDynamicDataService.writePlcData(device, currentData, s7Serializer);
+
+ log.info("妯℃嫙PLC浠诲姟瀹屾垚姹囨姤锛堟棫鐗堬級锛歱lcReport=1, deviceId={}", device.getId());
+ return true;
+ } catch (Exception e) {
+ log.error("鏍规嵁璁惧妯℃嫙PLC姹囨姤澶辫触锛堟棫鐗堬級: deviceId={}", device.getId(), e);
+ return false;
+ }
+ }
+
/**
* 鏍规嵁璁惧ID閲嶇疆PLC鎵�鏈夌姸鎬�
- *
+ *
* @param deviceId 璁惧ID
* @return 鏄惁鎴愬姛
*/
@@ -191,36 +285,108 @@
return false;
}
try {
- EnhancedS7Serializer s7Serializer = getSerializerForDevice(device);
- if (s7Serializer == null) {
- log.error("鑾峰彇S7Serializer澶辫触: deviceId={}", deviceId);
- return false;
+ // 灏濊瘯浣跨敤鏂扮殑PLC瀹㈡埛绔伐鍘�
+ PlcClient plcClient = plcClientFactory.getClient(device);
+ if (plcClient != null) {
+ // 鏋勫缓閲嶇疆鏁版嵁锛堝彧娣诲姞閰嶇疆涓瓨鍦ㄧ殑瀛楁锛�
+ Map<String, Object> resetData = new HashMap<>();
+ if (hasFieldInConfig(device, "plcRequest")) {
+ resetData.put("plcRequest", OFF);
+ }
+ if (hasFieldInConfig(device, "plcReport")) {
+ resetData.put("plcReport", OFF);
+ }
+ if (hasFieldInConfig(device, "mesSend")) {
+ resetData.put("mesSend", OFF);
+ }
+ if (hasFieldInConfig(device, "mesConfirm")) {
+ resetData.put("mesConfirm", OFF);
+ }
+ if (hasFieldInConfig(device, "onlineState")) {
+ resetData.put("onlineState", ON);
+ }
+ if (hasFieldInConfig(device, "alarmInfo")) {
+ resetData.put("alarmInfo", OFF);
+ }
+
+ // 妫�鏌ユ槸鍚︽湁瀛楁闇�瑕侀噸缃�
+ if (resetData.isEmpty()) {
+ log.warn("璁惧閰嶇疆涓湭鎵惧埌浠讳綍鍙噸缃殑瀛楁: deviceId={}", deviceId);
+ return false;
+ }
+
+ // 浣跨敤鏂扮殑PLC瀹㈡埛绔啓鍏ユ暟鎹�
+ boolean success = plcClient.writeData(resetData);
+ if (success) {
+ log.info("PLC鐘舵�佸凡閲嶇疆, deviceId={}", deviceId);
+ return true;
+ }
}
-
- // 鏋勫缓閲嶇疆鏁版嵁
- Map<String, Object> resetData = new HashMap<>();
- resetData.put("plcRequest", OFF);
- resetData.put("plcReport", OFF);
- resetData.put("mesSend", OFF);
- resetData.put("mesConfirm", OFF);
- resetData.put("onlineState", ON);
- resetData.put("mesGlassCount", 0);
- resetData.put("alarmInfo", OFF);
-
- // 浣跨敤PlcDynamicDataService鍐欏叆鏁版嵁
- plcDynamicDataService.writePlcData(device, resetData, s7Serializer);
-
- log.info("PLC鐘舵�佸凡閲嶇疆, deviceId={}", deviceId);
- return true;
+
+ // 濡傛灉鏂板鎴风澶辫触锛屽洖閫�鍒版棫瀹炵幇锛堜繚鎸佸吋瀹癸級
+ log.warn("鏂癙LC瀹㈡埛绔け璐ワ紝鍥為��鍒版棫瀹炵幇: deviceId={}", deviceId);
+ return resetPlcByDeviceLegacy(device);
} catch (Exception e) {
log.error("鏍规嵁璁惧閲嶇疆PLC鐘舵�佸け璐�: deviceId={}", deviceId, e);
return false;
}
}
-
+
+ /**
+ * 鏃х増瀹炵幇锛氭牴鎹澶囬噸缃甈LC鎵�鏈夌姸鎬�
+ *
+ * @param device 璁惧閰嶇疆
+ * @return 鏄惁鎴愬姛
+ */
+ private boolean resetPlcByDeviceLegacy(DeviceConfig device) {
+ try {
+ EnhancedS7Serializer s7Serializer = getSerializerForDevice(device);
+ if (s7Serializer == null) {
+ log.error("鑾峰彇S7Serializer澶辫触: deviceId={}", device.getId());
+ return false;
+ }
+
+ // 鏋勫缓閲嶇疆鏁版嵁锛堝彧娣诲姞閰嶇疆涓瓨鍦ㄧ殑瀛楁锛�
+ Map<String, Object> resetData = new HashMap<>();
+ if (hasFieldInConfig(device, "plcRequest")) {
+ resetData.put("plcRequest", OFF);
+ }
+ if (hasFieldInConfig(device, "plcReport")) {
+ resetData.put("plcReport", OFF);
+ }
+ if (hasFieldInConfig(device, "mesSend")) {
+ resetData.put("mesSend", OFF);
+ }
+ if (hasFieldInConfig(device, "mesConfirm")) {
+ resetData.put("mesConfirm", OFF);
+ }
+ if (hasFieldInConfig(device, "onlineState")) {
+ resetData.put("onlineState", ON);
+ }
+ if (hasFieldInConfig(device, "alarmInfo")) {
+ resetData.put("alarmInfo", OFF);
+ }
+
+ // 妫�鏌ユ槸鍚︽湁瀛楁闇�瑕侀噸缃�
+ if (resetData.isEmpty()) {
+ log.warn("璁惧閰嶇疆涓湭鎵惧埌浠讳綍鍙噸缃殑瀛楁: deviceId={}", device.getId());
+ return false;
+ }
+
+ // 浣跨敤PlcDynamicDataService鍐欏叆鏁版嵁
+ plcDynamicDataService.writePlcData(device, resetData, s7Serializer);
+
+ log.info("PLC鐘舵�佸凡閲嶇疆锛堟棫鐗堬級: deviceId={}", device.getId());
+ return true;
+ } catch (Exception e) {
+ log.error("鏍规嵁璁惧閲嶇疆PLC鐘舵�佸け璐ワ紙鏃х増锛�: deviceId={}", device.getId(), e);
+ return false;
+ }
+ }
+
/**
* 鏍规嵁璁惧ID璇诲彇PLC褰撳墠鐘舵��
- *
+ *
* @param deviceId 璁惧ID
* @return PLC鐘舵�佹暟鎹�
*/
@@ -231,6 +397,18 @@
return null;
}
try {
+ // 灏濊瘯浣跨敤鏂扮殑PLC瀹㈡埛绔伐鍘�
+ PlcClient plcClient = plcClientFactory.getClient(device);
+ if (plcClient != null) {
+ // 浣跨敤鏂扮殑PLC瀹㈡埛绔鍙栨墍鏈夋暟鎹�
+ Map<String, Object> data = plcClient.readAllData();
+ log.info("璇诲彇PLC鐘舵�佹垚鍔�: deviceId={}, dataSize={}", deviceId, data != null ? data.size() : 0);
+ return data;
+ }
+
+ // 濡傛灉鏂板鎴风澶辫触锛屽洖閫�鍒版棫瀹炵幇锛堜繚鎸佸吋瀹癸級
+ log.warn("鏂癙LC瀹㈡埛绔け璐ワ紝鍥為��鍒版棫瀹炵幇: deviceId={}", deviceId);
+
EnhancedS7Serializer s7Serializer = getSerializerForDevice(device);
if (s7Serializer == null) {
log.error("鑾峰彇S7Serializer澶辫触: deviceId={}", deviceId);
@@ -238,6 +416,7 @@
}
// 浣跨敤PlcDynamicDataService璇诲彇鎵�鏈夋暟鎹紙鏀寔addressMapping锛�
Map<String, Object> data = plcDynamicDataService.readAllPlcData(device, s7Serializer);
+ log.info("璇诲彇PLC鐘舵�佹垚鍔燂紙鏃х増锛�: deviceId={}, dataSize={}", deviceId, data != null ? data.size() : 0);
return data;
} catch (Exception e) {
log.error("璇诲彇璁惧PLC鐘舵�佸け璐�: deviceId={}", deviceId, e);
@@ -260,6 +439,20 @@
}
try {
+ // 灏濊瘯浣跨敤鏂扮殑PLC瀹㈡埛绔伐鍘�
+ PlcClient plcClient = plcClientFactory.getClient(device);
+ if (plcClient != null) {
+ // 浣跨敤鏂扮殑PLC瀹㈡埛绔啓鍏ユ暟鎹�
+ boolean success = plcClient.writeData(fieldValues);
+ if (success) {
+ log.info("鍐欏叆PLC瀛楁鎴愬姛: deviceId={}, fields={}", deviceId, fieldValues.keySet());
+ return true;
+ }
+ }
+
+ // 濡傛灉鏂板鎴风澶辫触锛屽洖閫�鍒版棫瀹炵幇锛堜繚鎸佸吋瀹癸級
+ log.warn("鏂癙LC瀹㈡埛绔け璐ワ紝鍥為��鍒版棫瀹炵幇: deviceId={}", deviceId);
+
// 鑾峰彇瀵瑰簲鐨凷7Serializer锛堜娇鐢ㄨ澶囬厤缃級
EnhancedS7Serializer s7Serializer = getSerializerForDevice(device);
if (s7Serializer == null) {
@@ -270,7 +463,7 @@
// 浣跨敤鍔ㄦ�佹暟鎹湇鍔″啓鍏ュ瓧娈碉紙鍩轰簬DeviceConfig锛�
plcDynamicDataService.writePlcData(device, fieldValues, s7Serializer);
- log.info("鍐欏叆PLC瀛楁鎴愬姛: deviceId={}, fields={}", deviceId, fieldValues.keySet());
+ log.info("鍐欏叆PLC瀛楁鎴愬姛锛堟棫鐗堬級: deviceId={}, fields={}", deviceId, fieldValues.keySet());
return true;
} catch (Exception e) {
log.error("鍐欏叆PLC瀛楁澶辫触: deviceId={}", deviceId, e);
@@ -329,7 +522,7 @@
EnhancedS7Serializer serializer = EnhancedS7Serializer.newInstance(s7Plc);
if (serializer == null) {
log.error("鍒涘缓EnhancedS7Serializer澶辫触: deviceId={}, plcIp={}, plcType={}",
- device.getId(), plcIp, plcType);
+ device.getId(), plcIp, plcType.name());
}
return serializer;
} catch (Exception e) {
@@ -422,4 +615,44 @@
throw new IllegalStateException("鏃犳硶瑙f瀽璁惧鐨凱LC椤圭洰鏍囪瘑, deviceId=" + device.getId());
}
+
+ /**
+ * 妫�鏌ヨ澶囬厤缃腑鏄惁瀛樺湪鎸囧畾瀛楁
+ *
+ * @param device 璁惧閰嶇疆
+ * @param fieldName 瀛楁鍚�
+ * @return 鏄惁瀛樺湪
+ */
+ private boolean hasFieldInConfig(DeviceConfig device, String fieldName) {
+ if (device == null || fieldName == null || fieldName.isEmpty()) {
+ return false;
+ }
+
+ try {
+ // 浠� configJson 涓鏌ワ紙鏂扮粨鏋勶級
+ Map<String, Object> configParams = ConfigJsonHelper.parseToMap(device.getConfigJson(), objectMapper);
+ if (configParams.containsKey(fieldName)) {
+ return true;
+ }
+
+ // 浠� extraParams.addressMapping 涓鏌ワ紙鍏煎鏃х粨鏋勶級
+ Map<String, Object> extraParams = parseExtraParams(device);
+ Object addressMapping = extraParams.get("addressMapping");
+ if (addressMapping != null) {
+ Map<String, Object> addressMappingMap;
+ if (addressMapping instanceof Map) {
+ addressMappingMap = (Map<String, Object>) addressMapping;
+ } else if (addressMapping instanceof String) {
+ addressMappingMap = objectMapper.readValue((String) addressMapping, MAP_TYPE);
+ } else {
+ return false;
+ }
+ return addressMappingMap.containsKey(fieldName);
+ }
+ } catch (Exception e) {
+ log.warn("妫�鏌ュ瓧娈垫槸鍚﹀瓨鍦ㄦ椂鍑洪敊: deviceId={}, fieldName={}", device.getId(), fieldName, e);
+ }
+
+ return false;
+ }
}
--
Gitblit v1.8.0