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
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 LargeGlassLogicHandler extends BaseDeviceLogicHandler {
 
    public LargeGlassLogicHandler(DevicePlcOperationService devicePlcOperationService) {
        super(devicePlcOperationService);
    }
 
    @Override
    public String getDeviceType() {
        return DeviceConfig.DeviceType.LARGE_GLASS;
    }
 
    @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 "processGlass":
                return handleProcessGlass(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 handleProcessGlass(
            DeviceConfig deviceConfig,
            Map<String, Object> params,
            Map<String, Object> logicParams) {
 
        // 从逻辑参数中获取配置
        Integer glassSize = getLogicParam(logicParams, "glassSize", 2000);
        Integer processingTime = getLogicParam(logicParams, "processingTime", 5000);
        Boolean autoProcess = getLogicParam(logicParams, "autoProcess", true);
 
        // 从运行时参数中获取数据
        String glassId = (String) params.get("glassId");
        Integer processType = (Integer) params.get("processType");
        Boolean triggerRequest = (Boolean) params.getOrDefault("triggerRequest", autoProcess);
 
        // 构建写入数据
        Map<String, Object> payload = new HashMap<>();
        
        if (glassId != null) {
            payload.put("plcGlassId", glassId);
        }
        
        if (processType != null) {
            payload.put("processType", processType);
        }
 
        // 自动触发请求
        if (triggerRequest != null && triggerRequest) {
            payload.put("plcRequest", 1);
        }
 
        log.info("大理片玻璃加工: deviceId={}, glassId={}, processType={}", 
                deviceConfig.getId(), glassId, processType);
 
        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 glassSize = getLogicParam(logicParams, "glassSize", null);
        if (glassSize != null && glassSize <= 0) {
            return "玻璃尺寸(glassSize)必须大于0";
        }
 
        Integer processingTime = getLogicParam(logicParams, "processingTime", null);
        if (processingTime != null && processingTime < 0) {
            return "处理时间(processingTime)不能为负数";
        }
 
        return null; // 验证通过
    }
 
    @Override
    public String getDefaultLogicParams() {
        Map<String, Object> defaultParams = new HashMap<>();
        defaultParams.put("glassSize", 2000);
        defaultParams.put("processingTime", 5000);
        defaultParams.put("autoProcess", true);
        defaultParams.put("maxRetryCount", 3);
        
        try {
            return objectMapper.writeValueAsString(defaultParams);
        } catch (Exception e) {
            log.error("生成默认逻辑参数失败", e);
            return "{}";
        }
    }
}