mes-processes/mes-plcSend/src/main/java/com/mes/service/PlcTestWriteService.java
@@ -70,11 +70,16 @@
                // 使用新的PLC客户端读取数据
                Map<String, Object> currentData = plcClient.readAllData();
                if (currentData != null && !currentData.isEmpty()) {
                    // 检查联机状态
                    Integer onlineState = parseInteger(currentData.get("onlineState"));
                    if (onlineState != null && onlineState == OFF) {
                    // 检查联机状态(仅当配置中存在该字段时)
                    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;
                            }
                        }
                    }
                    // 检查汇报字,如果为1则重置为0
@@ -126,7 +131,8 @@
                return false;
            }
            // 检查联机状态
            // 检查联机状态(仅当配置中存在该字段时)
            if (hasFieldInConfig(device, "onlineState")) {
            Object onlineStateObj = currentData.get("onlineState");
            Integer onlineState = null;
            if (onlineStateObj != null) {
@@ -147,6 +153,7 @@
            if (onlineState != null && onlineState == OFF) {
                log.info("当前PLC联机模式为0,停止联机: deviceId={}", device.getId());
                return false;
                }
            }
            // 检查汇报字,如果为1则重置为0
@@ -281,14 +288,32 @@
            // 尝试使用新的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);
@@ -321,14 +346,32 @@
                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);
@@ -572,4 +615,44 @@
        
        throw new IllegalStateException("无法解析设备的PLC项目标识, 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;
    }
}