huang
2025-11-20 366ba040d2447bacd3455299425e3166f1f992bb
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package com.mes.interaction.impl;
 
import com.mes.device.entity.DeviceConfig;
import com.mes.interaction.BaseDeviceLogicHandler;
import com.mes.device.service.DevicePlcOperationService;
import com.mes.device.vo.DevicePlcVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 玻璃存储设备逻辑处理器
 * 
 * @author mes
 * @since 2025-01-XX
 */
@Slf4j
@Component
public class GlassStorageLogicHandler extends BaseDeviceLogicHandler {
 
    public GlassStorageLogicHandler(DevicePlcOperationService devicePlcOperationService) {
        super(devicePlcOperationService);
    }
 
    @Override
    public String getDeviceType() {
        return DeviceConfig.DeviceType.GLASS_STORAGE;
    }
 
    @Override
    protected DevicePlcVO.OperationResult doExecute(
            DeviceConfig deviceConfig,
            String operation,
            Map<String, Object> params,
            Map<String, Object> logicParams) {
 
        log.info("执行玻璃存储设备操作: deviceId={}, operation={}", deviceConfig.getId(), operation);
 
        switch (operation) {
            case "storeGlass":
                return handleStoreGlass(deviceConfig, params, logicParams);
            case "retrieveGlass":
                return handleRetrieveGlass(deviceConfig, params, logicParams);
            case "triggerRequest":
                return handleTriggerRequest(deviceConfig, params, logicParams);
            case "triggerReport":
                return handleTriggerReport(deviceConfig, params, logicParams);
            case "reset":
                return handleReset(deviceConfig, params, logicParams);
            default:
                log.warn("不支持的操作类型: {}", operation);
                return DevicePlcVO.OperationResult.builder()
                        .success(false)
                        .message("不支持的操作: " + operation)
                        .build();
        }
    }
 
    /**
     * 处理存储玻璃操作
     */
    private DevicePlcVO.OperationResult handleStoreGlass(
            DeviceConfig deviceConfig,
            Map<String, Object> params,
            Map<String, Object> logicParams) {
 
        // 从逻辑参数中获取配置
        Integer storageCapacity = getLogicParam(logicParams, "storageCapacity", 100);
        String retrievalMode = getLogicParam(logicParams, "retrievalMode", "FIFO");
        Boolean autoStore = getLogicParam(logicParams, "autoStore", true);
 
        // 从运行时参数中获取数据
        String glassId = (String) params.get("glassId");
        Integer storagePosition = (Integer) params.get("storagePosition");
        Boolean triggerRequest = (Boolean) params.getOrDefault("triggerRequest", autoStore);
 
        // 构建写入数据
        Map<String, Object> payload = new HashMap<>();
        
        if (glassId != null) {
            payload.put("plcGlassId", glassId);
        }
        
        if (storagePosition != null) {
            payload.put("storagePosition", storagePosition);
        }
 
        // 自动触发请求
        if (triggerRequest != null && triggerRequest) {
            payload.put("plcRequest", 1);
        }
 
        log.info("玻璃存储: deviceId={}, glassId={}, position={}", 
                deviceConfig.getId(), glassId, storagePosition);
 
        return devicePlcOperationService.writeFields(
                deviceConfig.getId(), 
                payload, 
                "玻璃存储-存储玻璃"
        );
    }
 
    /**
     * 处理取货操作
     */
    private DevicePlcVO.OperationResult handleRetrieveGlass(
            DeviceConfig deviceConfig,
            Map<String, Object> params,
            Map<String, Object> logicParams) {
 
        // 从逻辑参数中获取配置
        String retrievalMode = getLogicParam(logicParams, "retrievalMode", "FIFO");
        Boolean autoRetrieve = getLogicParam(logicParams, "autoRetrieve", true);
 
        // 从运行时参数中获取数据
        Integer storagePosition = (Integer) params.get("storagePosition");
        String glassId = (String) params.get("glassId");
        Boolean triggerRequest = (Boolean) params.getOrDefault("triggerRequest", autoRetrieve);
 
        // 构建写入数据
        Map<String, Object> payload = new HashMap<>();
        
        if (storagePosition != null) {
            payload.put("retrievePosition", storagePosition);
        }
        
        if (glassId != null) {
            payload.put("retrieveGlassId", glassId);
        }
 
        // 自动触发请求
        if (triggerRequest != null && triggerRequest) {
            payload.put("plcRequest", 1);
        }
 
        log.info("玻璃取货: deviceId={}, position={}, glassId={}", 
                deviceConfig.getId(), storagePosition, glassId);
 
        return devicePlcOperationService.writeFields(
                deviceConfig.getId(), 
                payload, 
                "玻璃存储-取货"
        );
    }
 
    /**
     * 处理触发请求操作
     */
    private DevicePlcVO.OperationResult handleTriggerRequest(
            DeviceConfig deviceConfig,
            Map<String, Object> params,
            Map<String, Object> logicParams) {
 
        Map<String, Object> payload = new HashMap<>();
        payload.put("plcRequest", 1);
        
        log.info("玻璃存储触发请求: deviceId={}", deviceConfig.getId());
        return devicePlcOperationService.writeFields(
                deviceConfig.getId(),
                payload,
                "玻璃存储-触发请求"
        );
    }
 
    /**
     * 处理触发汇报操作
     */
    private DevicePlcVO.OperationResult handleTriggerReport(
            DeviceConfig deviceConfig,
            Map<String, Object> params,
            Map<String, Object> logicParams) {
 
        Map<String, Object> payload = new HashMap<>();
        payload.put("plcReport", 1);
        
        log.info("玻璃存储触发汇报: deviceId={}", deviceConfig.getId());
        return devicePlcOperationService.writeFields(
                deviceConfig.getId(),
                payload,
                "玻璃存储-触发汇报"
        );
    }
 
    /**
     * 处理重置操作
     */
    private DevicePlcVO.OperationResult handleReset(
            DeviceConfig deviceConfig,
            Map<String, Object> params,
            Map<String, Object> logicParams) {
 
        Map<String, Object> payload = new HashMap<>();
        payload.put("plcRequest", 0);
        payload.put("plcReport", 0);
        
        log.info("玻璃存储重置: deviceId={}", deviceConfig.getId());
        return devicePlcOperationService.writeFields(
                deviceConfig.getId(),
                payload,
                "玻璃存储-重置"
        );
    }
 
    @Override
    public String validateLogicParams(DeviceConfig deviceConfig) {
        Map<String, Object> logicParams = parseLogicParams(deviceConfig);
        
        // 验证必填参数
        Integer storageCapacity = getLogicParam(logicParams, "storageCapacity", null);
        if (storageCapacity != null && storageCapacity <= 0) {
            return "存储容量(storageCapacity)必须大于0";
        }
 
        String retrievalMode = getLogicParam(logicParams, "retrievalMode", null);
        if (retrievalMode != null && !retrievalMode.matches("FIFO|LIFO|RANDOM")) {
            return "取货模式(retrievalMode)必须是FIFO、LIFO或RANDOM";
        }
 
        return null; // 验证通过
    }
 
    @Override
    public String getDefaultLogicParams() {
        Map<String, Object> defaultParams = new HashMap<>();
        defaultParams.put("storageCapacity", 100);
        defaultParams.put("retrievalMode", "FIFO");
        defaultParams.put("autoStore", true);
        defaultParams.put("autoRetrieve", true);
        defaultParams.put("maxRetryCount", 3);
        
        try {
            return objectMapper.writeValueAsString(defaultParams);
        } catch (Exception e) {
            log.error("生成默认逻辑参数失败", e);
            return "{}";
        }
    }
}