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
package com.mes.device.service.impl;
 
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mes.device.entity.DeviceConfig;
import com.mes.device.service.DeviceConfigService;
import com.mes.device.service.DeviceControlProfileService;
import com.mes.device.vo.DeviceControlProfile;
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.Map;
 
/**
 * 设备控制参数服务实现
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DeviceControlProfileServiceImpl implements DeviceControlProfileService {
 
    private static final String CONTROL_PROFILE_KEY = "controlProfile";
 
    private final DeviceConfigService deviceConfigService;
    private final ObjectMapper objectMapper;
 
    @Override
    public DeviceControlProfile getProfile(Long deviceId) {
        DeviceConfig device = deviceConfigService.getDeviceById(deviceId);
        if (device == null) {
            throw new IllegalArgumentException("设备不存在: " + deviceId);
        }
        Map<String, Object> extraMap = readExtraMap(device);
        Object profileNode = extraMap.get(CONTROL_PROFILE_KEY);
        if (profileNode == null) {
            return DeviceControlProfile.builder().autoRequest(true).build();
        }
        return objectMapper.convertValue(profileNode, DeviceControlProfile.class);
    }
 
    @Override
    public void updateProfile(Long deviceId, DeviceControlProfile profile) {
        DeviceConfig device = deviceConfigService.getDeviceById(deviceId);
        if (device == null) {
            throw new IllegalArgumentException("设备不存在: " + deviceId);
        }
        Map<String, Object> extraMap = readExtraMap(device);
        extraMap.put(CONTROL_PROFILE_KEY, profile);
        try {
            String json = objectMapper.writeValueAsString(extraMap);
            device.setExtraParams(json);
            deviceConfigService.updateById(device);
        } catch (Exception e) {
            log.error("保存控制参数失败 deviceId={}", deviceId, e);
            throw new RuntimeException("保存控制参数失败: " + e.getMessage(), e);
        }
    }
 
    @SuppressWarnings("unchecked")
    private Map<String, Object> readExtraMap(DeviceConfig device) {
        try {
            if (device.getExtraParams() == null || device.getExtraParams().trim().isEmpty()) {
                return new HashMap<>();
            }
            return objectMapper.readValue(device.getExtraParams(),
                    new TypeReference<Map<String, Object>>() {});
        } catch (Exception e) {
            log.warn("解析设备扩展参数失败 deviceId={}", device.getId(), e);
            return new HashMap<>();
        }
    }
}