huang
2025-11-26 792236ef78c2cdd3a989fb40a7f2e2487c4e17b6
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
package com.mes.device.service.impl;
 
import com.mes.device.entity.DeviceConfig;
import com.mes.interaction.DeviceLogicHandler;
import com.mes.interaction.DeviceLogicHandlerFactory;
import com.mes.device.request.DeviceGlassFeedRequest;
import com.mes.device.service.DeviceConfigService;
import com.mes.device.service.DeviceControlProfileService;
import com.mes.device.service.DeviceInteractionService;
import com.mes.device.service.DevicePlcOperationService;
import com.mes.device.vo.DeviceControlProfile;
import com.mes.device.vo.DevicePlcVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 设备交互逻辑实现
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DeviceInteractionServiceImpl implements DeviceInteractionService {
 
    private final DeviceControlProfileService controlProfileService;
    private final DevicePlcOperationService devicePlcOperationService;
    private final DeviceConfigService deviceConfigService;
    private final DeviceLogicHandlerFactory handlerFactory;
 
    /**
     * 执行玻璃上料写入(兼容旧接口,保留原有逻辑)
     */
    @Override
    public DevicePlcVO.OperationResult feedGlass(DeviceGlassFeedRequest request) {
        // 优先使用新的处理器架构
        DeviceConfig deviceConfig = deviceConfigService.getDeviceById(request.getDeviceId());
        if (deviceConfig != null) {
            DeviceLogicHandler handler = handlerFactory.getHandler(deviceConfig.getDeviceType());
            if (handler != null) {
                // 使用新架构执行
                Map<String, Object> params = new HashMap<>();
                params.put("glassIds", request.getGlassIds());
                params.put("positionCode", request.getPositionCode());
                params.put("positionValue", request.getPositionValue());
                params.put("triggerRequest", request.getTriggerRequest());
                return handler.execute(deviceConfig, "feedGlass", params);
            }
        }
 
        // 降级到原有逻辑(兼容旧代码)
        DeviceControlProfile profile = controlProfileService.getProfile(request.getDeviceId());
        Map<String, Object> payload = buildGlassPayload(profile, request);
        String opName = "玻璃上料";
        if (request.getPositionCode() != null) {
            opName = opName + "(" + request.getPositionCode() + ")";
        }
        return devicePlcOperationService.writeFields(request.getDeviceId(), payload, opName);
    }
 
    /**
     * 执行设备逻辑操作(新接口,使用处理器架构)
     */
    @Override
    public DevicePlcVO.OperationResult executeOperation(Long deviceId, String operation, Map<String, Object> params) {
        // 获取设备配置
        DeviceConfig deviceConfig = deviceConfigService.getDeviceById(deviceId);
        if (deviceConfig == null) {
            return DevicePlcVO.OperationResult.builder()
                    .success(false)
                    .message("设备不存在: " + deviceId)
                    .build();
        }
 
        // 获取对应的处理器
        DeviceLogicHandler handler = handlerFactory.getHandler(deviceConfig.getDeviceType());
        if (handler == null) {
            return DevicePlcVO.OperationResult.builder()
                    .success(false)
                    .message("不支持的设备类型: " + deviceConfig.getDeviceType())
                    .build();
        }
 
        // 执行操作
        return handler.execute(deviceConfig, operation, params != null ? params : new HashMap<>());
    }
 
    /**
     * 构建玻璃上料数据(兼容旧逻辑)
     */
    private Map<String, Object> buildGlassPayload(DeviceControlProfile profile, DeviceGlassFeedRequest request) {
        if (CollectionUtils.isEmpty(profile.getGlassSlots())) {
            throw new IllegalStateException("设备未配置玻璃槽位信息");
        }
        List<String> glassIds = request.getGlassIds();
        Map<String, Object> payload = new HashMap<>();
 
        // 写入玻璃ID
        for (int i = 0; i < profile.getGlassSlots().size(); i++) {
            DeviceControlProfile.GlassSlot slot = profile.getGlassSlots().get(i);
            String value = (glassIds != null && i < glassIds.size()) ? glassIds.get(i) : "";
            payload.put(slot.getField(), value);
        }
 
        // 写入玻璃数量
        if (profile.getGlassCountField() != null) {
            int count = glassIds != null ? glassIds.size() : 0;
            payload.put(profile.getGlassCountField(), count);
        }
 
        // 写入位置
        if (profile.getPositionField() != null) {
            Integer positionValue = request.getPositionValue();
            if (positionValue == null && request.getPositionCode() != null
                    && profile.getPositionMappings() != null) {
                positionValue = profile.getPositionMappings().get(request.getPositionCode());
            }
            if (positionValue != null) {
                payload.put(profile.getPositionField(), positionValue);
            }
        }
 
        // 自动触发请求字
        boolean trigger = request.getTriggerRequest() != null ? request.getTriggerRequest()
                : Boolean.TRUE.equals(profile.getAutoRequest());
        if (trigger && profile.getRequestField() != null) {
            payload.put(profile.getRequestField(), 1);
        }
 
        return payload;
    }
}