huang
9 小时以前 fc4e5c458094c6bf5238d7d21291325f19a57adb
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
package com.mes.interaction;
 
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
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;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 设备逻辑处理器基类
 * 提供通用的功能实现
 * 
 * @author mes
 * @since 2025-01-XX
 */
@Slf4j
@RequiredArgsConstructor
public abstract class BaseDeviceLogicHandler implements DeviceLogicHandler {
 
    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) {
                return DevicePlcVO.OperationResult.builder()
                        .success(false)
                        .message("设备逻辑参数验证失败: " + validationError)
                        .build();
            }
 
            // 解析设备逻辑参数(从 extraParams.deviceLogic 中读取)
            Map<String, Object> logicParams = parseLogicParams(deviceConfig);
            
            // 执行具体操作(由子类实现)
            return doExecute(deviceConfig, operation, params, logicParams);
        } catch (Exception e) {
            log.error("执行设备逻辑操作失败, deviceId={}, operation={}", deviceConfig.getId(), operation, e);
            return DevicePlcVO.OperationResult.builder()
                    .success(false)
                    .message("执行失败: " + e.getMessage())
                    .build();
        }
    }
 
    /**
     * 子类实现具体的操作逻辑
     * 
     * @param deviceConfig 设备配置
     * @param operation 操作类型
     * @param params 运行时参数(动态传入)
     * @param logicParams 逻辑配置参数(从 extraParams.deviceLogic 解析)
     * @return 操作结果
     */
    protected abstract DevicePlcVO.OperationResult doExecute(
            DeviceConfig deviceConfig, 
            String operation, 
            Map<String, Object> params,
            Map<String, Object> logicParams
    );
 
    /**
     * 解析设备逻辑参数(从 extraParams 中提取 deviceLogic)
     */
    protected Map<String, Object> parseLogicParams(DeviceConfig deviceConfig) {
        String extraParams = deviceConfig.getExtraParams();
        if (extraParams == null || extraParams.trim().isEmpty()) {
            return new HashMap<>();
        }
 
        try {
            Map<String, Object> extra = objectMapper.readValue(extraParams, new TypeReference<Map<String, Object>>() {});
            @SuppressWarnings("unchecked")
            Map<String, Object> deviceLogic = (Map<String, Object>) extra.get("deviceLogic");
            return deviceLogic != null ? deviceLogic : new HashMap<>();
        } catch (Exception e) {
            log.warn("解析设备逻辑参数失败, deviceId={}", deviceConfig.getId(), e);
            return new HashMap<>();
        }
    }
 
    /**
     * 获取逻辑参数中的值(带默认值)
     */
    @SuppressWarnings("unchecked")
    protected <T> T getLogicParam(Map<String, Object> logicParams, String key, T defaultValue) {
        Object value = logicParams.get(key);
        if (value == null) {
            return defaultValue;
        }
        try {
            return (T) value;
        } catch (ClassCastException e) {
            log.warn("逻辑参数类型转换失败, key={}, value={}", key, value, e);
            return defaultValue;
        }
    }
 
    /**
     * 获取逻辑参数中的值(不带默认值)
     */
    @SuppressWarnings("unchecked")
    protected <T> T getLogicParam(Map<String, Object> logicParams, String key) {
        Object value = logicParams.get(key);
        if (value == null) {
            return null;
        }
        try {
            return (T) value;
        } catch (ClassCastException e) {
            log.warn("逻辑参数类型转换失败, key={}, value={}", key, value, e);
            return null;
        }
    }
}