refactor(integration): 重构配置管理功能

- 新增通用 ConfigManager 类,实现配置管理的通用功能
- 新增 DeviceConfigManager 和 ScenicConfigManager 类,分别实现设备和景区的配置管理- 更新相关控制器和服务,使用新的配置管理器类
-调整设备和景区的配置数据结构,以适应新的管理方式
This commit is contained in:
2025-09-02 15:30:54 +08:00
parent 2dee78247e
commit 8e770a5b97
8 changed files with 424 additions and 20 deletions

View File

@@ -0,0 +1,241 @@
package com.ycwl.basic.integration.common.manager;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
* 通用配置管理器基类
* 提供配置查找、类型转换等通用功能
*
* @param <T> 配置DTO类型
*/
public abstract class ConfigManager<T> {
protected List<T> configs;
public ConfigManager(List<T> configs) {
this.configs = configs != null ? configs : new ArrayList<>();
}
/**
* 获取配置项的键
* 子类需要实现此方法来提取配置键
*/
protected abstract String getConfigKey(T config);
/**
* 获取配置项的值
* 子类需要实现此方法来提取配置值
*/
protected abstract Object getConfigValue(T config);
/**
* 根据键查找配置项
*/
protected T findConfigByKey(String key) {
if (key == null || configs == null) {
return null;
}
return configs.stream()
.filter(config -> key.equals(getConfigKey(config)))
.findFirst()
.orElse(null);
}
/**
* 获取字符串配置值
*/
public String getString(String key) {
T config = findConfigByKey(key);
if (config == null) {
return null;
}
Object value = getConfigValue(config);
return value != null ? value.toString() : null;
}
/**
* 获取字符串配置值,如果不存在则返回默认值
*/
public String getString(String key, String defaultValue) {
String value = getString(key);
return value != null ? value : defaultValue;
}
/**
* 获取整型配置值
*/
public Integer getInteger(String key) {
T config = findConfigByKey(key);
if (config == null) {
return null;
}
Object value = getConfigValue(config);
if (value == null) return null;
if (value instanceof Integer) return (Integer) value;
if (value instanceof Number) return ((Number) value).intValue();
if (value instanceof String) {
try {
return Integer.parseInt((String) value);
} catch (NumberFormatException e) {
return null;
}
}
return null;
}
/**
* 获取整型配置值,如果不存在则返回默认值
*/
public Integer getInteger(String key, Integer defaultValue) {
Integer value = getInteger(key);
return value != null ? value : defaultValue;
}
/**
* 获取布尔型配置值
*/
public Boolean getBoolean(String key) {
T config = findConfigByKey(key);
if (config == null) {
return null;
}
Object value = getConfigValue(config);
if (value == null) return null;
if (value instanceof Boolean) return (Boolean) value;
if (value instanceof String) {
String str = ((String) value).toLowerCase().trim();
return "true".equals(str) || "1".equals(str) || "yes".equals(str);
}
if (value instanceof Number) {
return ((Number) value).intValue() != 0;
}
return null;
}
/**
* 获取布尔型配置值,如果不存在则返回默认值
*/
public Boolean getBoolean(String key, Boolean defaultValue) {
Boolean value = getBoolean(key);
return value != null ? value : defaultValue;
}
/**
* 获取浮点型配置值
*/
public Float getFloat(String key) {
T config = findConfigByKey(key);
if (config == null) {
return null;
}
Object value = getConfigValue(config);
if (value == null) return null;
if (value instanceof Float) return (Float) value;
if (value instanceof Number) return ((Number) value).floatValue();
if (value instanceof String) {
try {
return Float.parseFloat((String) value);
} catch (NumberFormatException e) {
return null;
}
}
return null;
}
/**
* 获取浮点型配置值,如果不存在则返回默认值
*/
public Float getFloat(String key, Float defaultValue) {
Float value = getFloat(key);
return value != null ? value : defaultValue;
}
/**
* 获取BigDecimal配置值
*/
public BigDecimal getBigDecimal(String key) {
T config = findConfigByKey(key);
if (config == null) {
return null;
}
Object value = getConfigValue(config);
if (value == null) return null;
if (value instanceof BigDecimal) return (BigDecimal) value;
if (value instanceof Number) return new BigDecimal(value.toString());
if (value instanceof String) {
try {
return new BigDecimal((String) value);
} catch (NumberFormatException e) {
return null;
}
}
return null;
}
/**
* 获取BigDecimal配置值,如果不存在则返回默认值
*/
public BigDecimal getBigDecimal(String key, BigDecimal defaultValue) {
BigDecimal value = getBigDecimal(key);
return value != null ? value : defaultValue;
}
/**
* 获取枚举配置值
*/
public <E extends Enum<E>> E getEnum(String key, Class<E> enumClass) {
T config = findConfigByKey(key);
if (config == null) {
return null;
}
Object value = getConfigValue(config);
return parseEnum(value, enumClass);
}
/**
* 解析枚举值
*/
private <E extends Enum<E>> E parseEnum(Object value, Class<E> enumClass) {
if (value == null) return null;
try {
if (value instanceof String) {
return Enum.valueOf(enumClass, (String) value);
}
return Enum.valueOf(enumClass, value.toString());
} catch (IllegalArgumentException e) {
return null;
}
}
/**
* 获取枚举配置值,如果不存在则返回默认值
*/
public <E extends Enum<E>> E getEnum(String key, Class<E> enumClass, E defaultValue) {
E value = getEnum(key, enumClass);
return value != null ? value : defaultValue;
}
/**
* 检查指定键的配置是否存在
*/
public boolean hasConfig(String key) {
return findConfigByKey(key) != null;
}
/**
* 获取所有配置项的数量
*/
public int size() {
return configs.size();
}
/**
* 获取所有配置项
*/
public List<T> getAllConfigs() {
return new ArrayList<>(configs);
}
}

View File

@@ -0,0 +1,122 @@
package com.ycwl.basic.integration.common.manager;
import com.ycwl.basic.integration.device.dto.config.DeviceConfigV2DTO;
import java.util.List;
/**
* 设备配置管理器
* 基于通用ConfigManager实现设备配置的管理功能
*/
public class DeviceConfigManager extends ConfigManager<DeviceConfigV2DTO> {
public DeviceConfigManager(List<DeviceConfigV2DTO> configs) {
super(configs);
}
@Override
protected String getConfigKey(DeviceConfigV2DTO config) {
return config != null ? config.getConfigKey() : null;
}
@Override
protected Object getConfigValue(DeviceConfigV2DTO config) {
return config != null ? config.getConfigValue() : null;
}
/**
* 获取设备配置类型
*/
public String getConfigType(String key) {
DeviceConfigV2DTO config = findConfigByKey(key);
return config != null ? config.getConfigType() : null;
}
/**
* 获取设备配置描述
*/
public String getConfigDescription(String key) {
DeviceConfigV2DTO config = findConfigByKey(key);
return config != null ? config.getDescription() : null;
}
/**
* 获取设备配置ID
*/
public Long getConfigId(String key) {
DeviceConfigV2DTO config = findConfigByKey(key);
return config != null ? config.getId() : null;
}
// 设备配置的常用快捷方法
/**
* 获取设备IP地址
*/
public String getIpAddress() {
return getString("ip_address");
}
/**
* 获取设备分辨率
*/
public String getResolution() {
return getString("resolution");
}
/**
* 获取设备帧率
*/
public Integer getFramerate() {
return getInteger("framerate");
}
/**
* 获取设备协议
*/
public String getProtocol() {
return getString("protocol");
}
/**
* 获取设备用户名
*/
public String getUsername() {
return getString("username");
}
/**
* 获取设备密码
*/
public String getPassword() {
return getString("password");
}
/**
* 获取设备亮度
*/
public Integer getBrightness() {
return getInteger("brightness");
}
/**
* 获取设备对比度
*/
public Integer getContrast() {
return getInteger("contrast");
}
/**
* 获取设备质量
*/
public String getQuality() {
return getString("quality");
}
/**
* 检查设备是否启用录制
*/
public Boolean isRecordingEnabled() {
return getBoolean("recording_enabled");
}
}

View File

@@ -0,0 +1,448 @@
package com.ycwl.basic.integration.common.manager;
import com.ycwl.basic.integration.common.util.ConfigValueUtil;
import com.ycwl.basic.integration.scenic.dto.config.ScenicConfigV2DTO;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
* 景区配置管理器
* 基于通用ConfigManager实现景区配置的管理功能
*
* 提供类型安全的配置值获取功能,支持多种数据类型的自动转换,
* 当类型不兼容时返回null而不是抛出异常。
*/
public class ScenicConfigManager extends ConfigManager<ScenicConfigV2DTO> {
private final Map<String, Object> configMap;
/**
* 从配置列表构造管理器
*
* @param configList 配置项列表
*/
public ScenicConfigManager(List<ScenicConfigV2DTO> configList) {
super(configList);
this.configMap = new HashMap<>();
if (configList != null) {
for (ScenicConfigV2DTO config : configList) {
if (config.getConfigKey() != null && config.getConfigValue() != null) {
this.configMap.put(config.getConfigKey(), config.getConfigValue());
}
}
}
}
/**
* 从配置Map构造管理器
*
* @param configMap 配置Map
*/
public ScenicConfigManager(Map<String, Object> configMap) {
super(null); // 使用Map构造时,父类configs为null
this.configMap = configMap != null ? new HashMap<>(configMap) : new HashMap<>();
}
@Override
protected String getConfigKey(ScenicConfigV2DTO config) {
return config != null ? config.getConfigKey() : null;
}
@Override
protected Object getConfigValue(ScenicConfigV2DTO config) {
return config != null ? config.getConfigValue() : null;
}
/**
* 获取字符串值
*
* @param key 配置键
* @return 字符串值,如果键不存在或转换失败返回null
*/
public String getString(String key) {
return ConfigValueUtil.getStringValue(configMap, key);
}
/**
* 获取字符串值,如果为null则返回默认值
*
* @param key 配置键
* @param defaultValue 默认值
* @return 字符串值或默认值
*/
public String getString(String key, String defaultValue) {
return ConfigValueUtil.getStringValue(configMap, key, defaultValue);
}
/**
* 获取整数值
*
* @param key 配置键
* @return Integer值,如果键不存在或转换失败返回null
*/
public Integer getInteger(String key) {
return ConfigValueUtil.getIntValue(configMap, key);
}
/**
* 获取整数值,如果为null则返回默认值
*
* @param key 配置键
* @param defaultValue 默认值
* @return Integer值或默认值
*/
public Integer getInteger(String key, Integer defaultValue) {
return ConfigValueUtil.getIntValue(configMap, key, defaultValue);
}
/**
* 获取长整数值
*
* @param key 配置键
* @return Long值,如果键不存在或转换失败返回null
*/
public Long getLong(String key) {
return ConfigValueUtil.getLongValue(configMap, key);
}
/**
* 获取长整数值,如果为null则返回默认值
*
* @param key 配置键
* @param defaultValue 默认值
* @return Long值或默认值
*/
public Long getLong(String key, Long defaultValue) {
Long value = ConfigValueUtil.getLongValue(configMap, key);
return value != null ? value : defaultValue;
}
/**
* 获取浮点数值
*
* @param key 配置键
* @return Float值,如果键不存在或转换失败返回null
*/
public Float getFloat(String key) {
return ConfigValueUtil.getFloatValue(configMap, key);
}
/**
* 获取浮点数值,如果为null则返回默认值
*
* @param key 配置键
* @param defaultValue 默认值
* @return Float值或默认值
*/
public Float getFloat(String key, Float defaultValue) {
Float value = ConfigValueUtil.getFloatValue(configMap, key);
return value != null ? value : defaultValue;
}
/**
* 获取双精度浮点数值
*
* @param key 配置键
* @return Double值,如果键不存在或转换失败返回null
*/
public Double getDouble(String key) {
return ConfigValueUtil.getDoubleValue(configMap, key);
}
/**
* 获取双精度浮点数值,如果为null则返回默认值
*
* @param key 配置键
* @param defaultValue 默认值
* @return Double值或默认值
*/
public Double getDouble(String key, Double defaultValue) {
Double value = ConfigValueUtil.getDoubleValue(configMap, key);
return value != null ? value : defaultValue;
}
/**
* 获取高精度小数值
*
* @param key 配置键
* @return BigDecimal值,如果键不存在或转换失败返回null
*/
public BigDecimal getBigDecimal(String key) {
return ConfigValueUtil.getBigDecimalValue(configMap, key);
}
/**
* 获取高精度小数值,如果为null则返回默认值
*
* @param key 配置键
* @param defaultValue 默认值
* @return BigDecimal值或默认值
*/
public BigDecimal getBigDecimal(String key, BigDecimal defaultValue) {
BigDecimal value = ConfigValueUtil.getBigDecimalValue(configMap, key);
return value != null ? value : defaultValue;
}
/**
* 获取布尔值
*
* @param key 配置键
* @return Boolean值,如果键不存在或转换失败返回null
*/
public Boolean getBoolean(String key) {
return ConfigValueUtil.getBooleanValue(configMap, key);
}
/**
* 获取布尔值,如果为null则返回默认值
*
* @param key 配置键
* @param defaultValue 默认值
* @return Boolean值或默认值
*/
public Boolean getBoolean(String key, Boolean defaultValue) {
return ConfigValueUtil.getBooleanValue(configMap, key, defaultValue);
}
/**
* 获取枚举值
*
* @param key 配置键
* @param enumClass 枚举类型
* @param <T> 枚举类型泛型
* @return 枚举值,如果键不存在或转换失败返回null
*/
public <T extends Enum<T>> T getEnum(String key, Class<T> enumClass) {
return ConfigValueUtil.getEnumValue(configMap, key, enumClass);
}
/**
* 获取枚举值,如果为null则返回默认值
*
* @param key 配置键
* @param enumClass 枚举类型
* @param defaultValue 默认值
* @param <T> 枚举类型泛型
* @return 枚举值或默认值
*/
public <T extends Enum<T>> T getEnum(String key, Class<T> enumClass, T defaultValue) {
T value = ConfigValueUtil.getEnumValue(configMap, key, enumClass);
return value != null ? value : defaultValue;
}
/**
* 获取原始对象值
*
* @param key 配置键
* @return 原始Object值
*/
public Object getObject(String key) {
return ConfigValueUtil.getObjectValue(configMap, key);
}
/**
* 获取并转换为指定类型的对象
*
* @param key 配置键
* @param clazz 目标类型
* @param <T> 目标类型泛型
* @return 转换后的对象,如果转换失败返回null
*/
public <T> T getObject(String key, Class<T> clazz) {
return ConfigValueUtil.getObjectValue(configMap, key, clazz);
}
/**
* 获取Map类型的值
*
* @param key 配置键
* @return Map值,如果转换失败返回null
*/
public Map<String, Object> getMap(String key) {
return ConfigValueUtil.getMapValue(configMap, key);
}
/**
* 获取List类型的值
*
* @param key 配置键
* @return List值,如果转换失败返回null
*/
public List<Object> getList(String key) {
return ConfigValueUtil.getListValue(configMap, key);
}
/**
* 获取指定元素类型的List值
*
* @param key 配置键
* @param elementClass List元素类型
* @param <T> List元素类型泛型
* @return 指定类型的List,如果转换失败返回null
*/
public <T> List<T> getList(String key, Class<T> elementClass) {
return ConfigValueUtil.getListValue(configMap, key, elementClass);
}
/**
* 检查配置键是否存在
*
* @param key 配置键
* @return true如果键存在,false如果不存在
*/
public boolean hasKey(String key) {
return ConfigValueUtil.hasKey(configMap, key);
}
/**
* 检查配置键是否存在且值不为null
*
* @param key 配置键
* @return true如果键存在且值不为null
*/
public boolean hasNonNullValue(String key) {
return ConfigValueUtil.hasNonNullValue(configMap, key);
}
/**
* 获取所有配置键
*
* @return 配置键集合
*/
public Set<String> getAllKeys() {
return new HashSet<>(configMap.keySet());
}
/**
* 获取配置项数量
*
* @return 配置项数量
*/
public int size() {
return configMap.size();
}
/**
* 检查配置是否为空
*
* @return true如果没有配置项
*/
public boolean isEmpty() {
return configMap.isEmpty();
}
/**
* 获取所有配置的拷贝
*
* @return 配置Map的拷贝
*/
public Map<String, Object> getAllConfigsAsMap() {
return new HashMap<>(configMap);
}
/**
* 根据键前缀过滤配置
*
* @param prefix 键前缀
* @return 匹配前缀的配置Map
*/
public Map<String, Object> getConfigsByPrefix(String prefix) {
if (prefix == null) {
return new HashMap<>();
}
return configMap.entrySet().stream()
.filter(entry -> entry.getKey() != null && entry.getKey().startsWith(prefix))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue
));
}
/**
* 创建新的ScenicConfigManager,包含当前配置的子集
*
* @param keys 要包含的配置键
* @return 包含指定键配置的新管理器
*/
public ScenicConfigManager subset(Set<String> keys) {
Map<String, Object> subsetMap = new HashMap<>();
if (keys != null) {
for (String key : keys) {
if (configMap.containsKey(key)) {
subsetMap.put(key, configMap.get(key));
}
}
}
return new ScenicConfigManager(subsetMap);
}
/**
* 将配置转换为扁平化的Map,键名转换为驼峰形式
*
* @return 扁平化的配置Map,键为驼峰形式
*/
public Map<String, Object> toFlatConfig() {
Map<String, Object> flatConfig = new HashMap<>();
for (Map.Entry<String, Object> entry : configMap.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (key != null) {
String camelCaseKey = toCamelCase(key);
flatConfig.put(camelCaseKey, value);
}
}
return flatConfig;
}
/**
* 将字符串转换为驼峰形式
* 支持下划线、短横线、点号分隔的字符串转换
*
* @param str 原始字符串
* @return 驼峰形式的字符串
*/
private String toCamelCase(String str) {
if (str == null || str.isEmpty()) {
return str;
}
// 支持下划线、短横线、点号作为分隔符
String[] parts = str.split("[_\\-.]");
if (parts.length <= 1) {
return str;
}
StringBuilder camelCase = new StringBuilder();
// 第一部分保持原样(全小写)
camelCase.append(parts[0].toLowerCase());
// 后续部分首字母大写
for (int i = 1; i < parts.length; i++) {
String part = parts[i];
if (!part.isEmpty()) {
camelCase.append(Character.toUpperCase(part.charAt(0)));
if (part.length() > 1) {
camelCase.append(part.substring(1).toLowerCase());
}
}
}
return camelCase.toString();
}
@Override
public String toString() {
return "ScenicConfigManager{" +
"configCount=" + configMap.size() +
", keys=" + configMap.keySet() +
'}';
}
}