huang
12 小时以前 fc4e5c458094c6bf5238d7d21291325f19a57adb
mes-processes/mes-plcSend/src/main/java/com/mes/interaction/BaseDeviceLogicHandler.java
@@ -5,6 +5,8 @@
import com.mes.device.entity.DeviceConfig;
import com.mes.device.service.DevicePlcOperationService;
import com.mes.device.vo.DevicePlcVO;
import com.mes.plc.client.PlcClient;
import com.mes.plc.factory.PlcClientFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -24,10 +26,52 @@
    protected final DevicePlcOperationService devicePlcOperationService;
    protected final ObjectMapper objectMapper = new ObjectMapper();
    // PlcClientFactory 可选注入,如果子类需要直接使用 PlcClient
    protected PlcClientFactory plcClientFactory;
    /**
     * 设置 PlcClientFactory(用于子类注入)
     */
    public void setPlcClientFactory(PlcClientFactory plcClientFactory) {
        this.plcClientFactory = plcClientFactory;
    }
    /**
     * 获取 PLC 客户端
     *
     * @param deviceConfig 设备配置
     * @return PLC客户端实例,如果获取失败返回null
     */
    protected PlcClient getPlcClient(DeviceConfig deviceConfig) {
        if (plcClientFactory == null) {
            log.warn("PlcClientFactory未注入,无法获取PLC客户端: deviceId={}", deviceConfig.getId());
            return null;
        }
        try {
            PlcClient client = plcClientFactory.getClient(deviceConfig);
            if (client == null) {
                log.error("获取PLC客户端失败: deviceId={}", deviceConfig.getId());
            }
            return client;
        } catch (Exception e) {
            log.error("获取PLC客户端异常: deviceId={}", deviceConfig.getId(), e);
            return null;
        }
    }
    @Override
    public DevicePlcVO.OperationResult execute(DeviceConfig deviceConfig, String operation, Map<String, Object> params) {
        try {
            // 记录参数信息(用于调试)
            if (params != null) {
                log.debug("BaseDeviceLogicHandler.execute接收参数: deviceId={}, operation={}, paramsKeys={}, params={}",
                        deviceConfig.getId(), operation, params.keySet(), params);
            } else {
                log.warn("BaseDeviceLogicHandler.execute接收参数为null: deviceId={}, operation={}",
                        deviceConfig.getId(), operation);
            }
            // 验证设备配置
            String validationError = validateLogicParams(deviceConfig);
            if (validationError != null) {