You've already forked FrameTour-BE
feat(device): 添加默认配置管理功能
- 新增 DefaultConfigClient接口,用于与设备微服务进行默认配置相关的操作 - 实现 DefaultConfigIntegrationService 类,提供默认配置管理的高阶服务- 添加批量配置请求构建器 BatchDefaultConfigRequestBuilder,简化批量操作 - 新增 DefaultConfigIntegrationExample 示例类,演示默认配置管理的使用方法 - 更新 CLAUDE.md 文档,增加默认配置管理的详细使用说明和示例代码
This commit is contained in:
@@ -227,10 +227,12 @@ fallbackService.clearAllFallbackCache("zt-scenic");
|
||||
#### Feign Clients
|
||||
- **DeviceV2Client**: Main device operations (CRUD, filtering, listing)
|
||||
- **DeviceConfigV2Client**: Device configuration management
|
||||
- **DefaultConfigClient**: Default configuration management
|
||||
|
||||
#### Services
|
||||
- **DeviceIntegrationService**: High-level device operations (with automatic fallback)
|
||||
- **DeviceConfigIntegrationService**: Device configuration management (with automatic fallback)
|
||||
- **DefaultConfigIntegrationService**: Default configuration management (with automatic fallback)
|
||||
|
||||
#### Configuration
|
||||
```yaml
|
||||
@@ -377,6 +379,355 @@ fallbackService.clearFallbackCache("zt-device", "device:1001");
|
||||
fallbackService.clearAllFallbackCache("zt-device");
|
||||
```
|
||||
|
||||
## Default Configuration Management (ZT-Device Microservice)
|
||||
|
||||
### Key Components
|
||||
|
||||
#### Default Configuration API
|
||||
The zt-device microservice provides a comprehensive default configuration management API at `/api/device/config/v2/defaults` that allows:
|
||||
- Creating and managing default configuration templates
|
||||
- Batch operations with conflict detection
|
||||
- Configuration type validation and enforcement
|
||||
- Usage tracking and analytics
|
||||
|
||||
#### Feign Client
|
||||
- **DefaultConfigClient**: Default configuration management operations
|
||||
|
||||
#### Service
|
||||
- **DefaultConfigIntegrationService**: High-level default configuration operations (with automatic fallback for queries)
|
||||
|
||||
### Usage Examples
|
||||
|
||||
#### Basic Default Configuration Operations (with Automatic Fallback)
|
||||
```java
|
||||
@Autowired
|
||||
private DefaultConfigIntegrationService defaultConfigService;
|
||||
|
||||
// Get default configuration list (automatically falls back to cache on failure)
|
||||
PageResponse<DefaultConfigResponse> configList = defaultConfigService.listDefaultConfigs(1, 10);
|
||||
log.info("Default configs: total={}, current page={}",
|
||||
configList.getData().getTotal(), configList.getData().getList().size());
|
||||
|
||||
// Get specific default configuration (automatically falls back to cache on failure)
|
||||
DefaultConfigResponse config = defaultConfigService.getDefaultConfig("resolution");
|
||||
if (config != null) {
|
||||
log.info("Default resolution: {}", config.getConfigValue());
|
||||
}
|
||||
|
||||
// Create new default configuration (direct operation, fails immediately on error)
|
||||
DefaultConfigRequest newConfig = new DefaultConfigRequest();
|
||||
newConfig.setConfigKey("bitrate");
|
||||
newConfig.setConfigValue("2000000");
|
||||
newConfig.setConfigType("int");
|
||||
newConfig.setDescription("Default video bitrate in bps");
|
||||
boolean created = defaultConfigService.createDefaultConfig(newConfig);
|
||||
|
||||
// Update default configuration with conflict detection (direct operation)
|
||||
Map<String, Object> updates = new HashMap<>();
|
||||
updates.put("configValue", "4000000");
|
||||
updates.put("description", "Updated default bitrate - high quality");
|
||||
DefaultConfigConflict conflict = defaultConfigService.updateDefaultConfig("bitrate", updates);
|
||||
|
||||
if (conflict != null) {
|
||||
log.warn("Configuration update conflict detected:");
|
||||
log.warn(" Key: {}, Type: {}, Affected devices: {}",
|
||||
conflict.getConfigKey(), conflict.getConflictType(), conflict.getDeviceCount());
|
||||
log.warn(" Current type: {}, Proposed type: {}",
|
||||
conflict.getCurrentType(), conflict.getProposedType());
|
||||
|
||||
// Handle conflict - show affected device IDs
|
||||
if (conflict.getConflictDevices() != null) {
|
||||
log.warn(" Conflicted device IDs: {}", conflict.getConflictDevices());
|
||||
}
|
||||
} else {
|
||||
log.info("Configuration updated successfully without conflicts");
|
||||
}
|
||||
|
||||
// Delete default configuration (direct operation, fails immediately on error)
|
||||
boolean deleted = defaultConfigService.deleteDefaultConfig("bitrate");
|
||||
```
|
||||
|
||||
#### Batch Default Configuration Operations
|
||||
```java
|
||||
// Using Builder Pattern for batch operations
|
||||
BatchDefaultConfigRequest batchRequest = defaultConfigService.createBatchConfigBuilder()
|
||||
.addVideoConfig("1920x1080", 30, "H265") // Add video configuration group
|
||||
.addNetworkConfig("192.168.1.100", 554, "RTSP") // Add network configuration group
|
||||
.addConfig("recording_enabled", "true", "bool", "Enable recording by default")
|
||||
.addConfig("storage_retention", "30", "int", "Storage retention in days")
|
||||
.addConfig("quality_profile", "high", "string", "Default video quality profile")
|
||||
.build();
|
||||
|
||||
// Execute batch update (direct operation, no fallback)
|
||||
BatchDefaultConfigResponse result = defaultConfigService.batchUpdateDefaultConfigs(batchRequest);
|
||||
|
||||
// Process batch results
|
||||
log.info("Batch operation completed: {} success, {} failed", result.getSuccess(), result.getFailed());
|
||||
|
||||
// Handle conflicts
|
||||
if (result.getConflicts() != null && !result.getConflicts().isEmpty()) {
|
||||
log.warn("Configuration conflicts detected:");
|
||||
for (DefaultConfigConflict conflict : result.getConflicts()) {
|
||||
log.warn(" Key: {}, Type: {}, Devices affected: {}",
|
||||
conflict.getConfigKey(), conflict.getConflictType(), conflict.getDeviceCount());
|
||||
}
|
||||
}
|
||||
|
||||
// Review processed items
|
||||
if (result.getProcessedItems() != null) {
|
||||
result.getProcessedItems().forEach(item -> {
|
||||
if ("success".equals(item.getStatus())) {
|
||||
log.info("✅ {} {} successfully (type: {})",
|
||||
item.getConfigKey(), item.getAction(), item.getFinalType());
|
||||
} else {
|
||||
log.warn("❌ {} failed: {}", item.getConfigKey(), item.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Handle errors
|
||||
if (result.getErrors() != null && !result.getErrors().isEmpty()) {
|
||||
result.getErrors().forEach(error -> log.error("Batch operation error: {}", error));
|
||||
}
|
||||
```
|
||||
|
||||
#### Device Type Specific Default Configuration Patterns
|
||||
```java
|
||||
// Create IPC camera default configurations
|
||||
BatchDefaultConfigRequest ipcDefaults = defaultConfigService.createBatchConfigBuilder()
|
||||
.addVideoConfig("1920x1080", 25, "H264") // Standard HD resolution
|
||||
.addConfig("night_vision", "true", "bool", "Enable night vision")
|
||||
.addConfig("motion_detection", "true", "bool", "Enable motion detection")
|
||||
.addConfig("stream_profile", "main", "string", "Default stream profile")
|
||||
.addConfig("ptz_enabled", "false", "bool", "PTZ control enabled")
|
||||
.addConfig("audio_enabled", "true", "bool", "Audio recording enabled")
|
||||
.build();
|
||||
|
||||
// Create NVR default configurations
|
||||
BatchDefaultConfigRequest nvrDefaults = defaultConfigService.createBatchConfigBuilder()
|
||||
.addConfig("max_channels", "16", "int", "Maximum supported channels")
|
||||
.addConfig("storage_mode", "continuous", "string", "Recording storage mode")
|
||||
.addConfig("backup_enabled", "true", "bool", "Enable automatic backup")
|
||||
.addConfig("raid_level", "5", "int", "Default RAID level")
|
||||
.addConfig("disk_quota", "80", "int", "Disk usage quota percentage")
|
||||
.build();
|
||||
|
||||
// Apply device-type-specific defaults
|
||||
BatchDefaultConfigResponse ipcResult = defaultConfigService.batchUpdateDefaultConfigs(ipcDefaults);
|
||||
BatchDefaultConfigResponse nvrResult = defaultConfigService.batchUpdateDefaultConfigs(nvrDefaults);
|
||||
|
||||
log.info("IPC defaults: {} success, {} failed", ipcResult.getSuccess(), ipcResult.getFailed());
|
||||
log.info("NVR defaults: {} success, {} failed", nvrResult.getSuccess(), nvrResult.getFailed());
|
||||
```
|
||||
|
||||
#### Configuration Validation and Management Patterns
|
||||
```java
|
||||
// Validate system configuration completeness
|
||||
PageResponse<DefaultConfigResponse> allDefaults = defaultConfigService.listDefaultConfigs(1, 100);
|
||||
|
||||
// Check for required default configurations
|
||||
String[] requiredDefaults = {
|
||||
"resolution", "frameRate", "codec", "bitrate",
|
||||
"protocol", "port", "username", "password"
|
||||
};
|
||||
|
||||
Map<String, Boolean> configStatus = new HashMap<>();
|
||||
for (String required : requiredDefaults) {
|
||||
boolean exists = allDefaults.getData().getList().stream()
|
||||
.anyMatch(config -> required.equals(config.getConfigKey()));
|
||||
configStatus.put(required, exists);
|
||||
|
||||
if (!exists) {
|
||||
log.warn("Missing required default configuration: {}", required);
|
||||
}
|
||||
}
|
||||
|
||||
// Generate configuration completeness report
|
||||
long totalConfigs = allDefaults.getData().getTotal();
|
||||
long completeConfigs = configStatus.values().stream().mapToLong(exists -> exists ? 1 : 0).sum();
|
||||
double completeness = (double) completeConfigs / requiredDefaults.length * 100;
|
||||
|
||||
log.info("Configuration completeness: {:.1f}% ({}/{} required configs present)",
|
||||
completeness, completeConfigs, requiredDefaults.length);
|
||||
log.info("Total default configurations in system: {}", totalConfigs);
|
||||
|
||||
// Analyze configuration type distribution
|
||||
Map<String, Long> typeDistribution = allDefaults.getData().getList().stream()
|
||||
.collect(Collectors.groupingBy(
|
||||
DefaultConfigResponse::getConfigType,
|
||||
Collectors.counting()
|
||||
));
|
||||
|
||||
log.info("Configuration type distribution: {}", typeDistribution);
|
||||
|
||||
// Find most and least used configurations
|
||||
DefaultConfigResponse mostUsed = allDefaults.getData().getList().stream()
|
||||
.max(Comparator.comparing(DefaultConfigResponse::getUsageCount))
|
||||
.orElse(null);
|
||||
|
||||
DefaultConfigResponse leastUsed = allDefaults.getData().getList().stream()
|
||||
.min(Comparator.comparing(DefaultConfigResponse::getUsageCount))
|
||||
.orElse(null);
|
||||
|
||||
if (mostUsed != null) {
|
||||
log.info("Most used default config: {} (used {} times)",
|
||||
mostUsed.getConfigKey(), mostUsed.getUsageCount());
|
||||
}
|
||||
if (leastUsed != null) {
|
||||
log.info("Least used default config: {} (used {} times)",
|
||||
leastUsed.getConfigKey(), leastUsed.getUsageCount());
|
||||
}
|
||||
```
|
||||
|
||||
#### Advanced Batch Configuration with Error Handling
|
||||
```java
|
||||
// Complex batch operation with comprehensive error handling
|
||||
try {
|
||||
BatchDefaultConfigRequest complexBatch = defaultConfigService.createBatchConfigBuilder()
|
||||
.addVideoConfig("3840x2160", 60, "H265") // 4K configuration
|
||||
.addConfig("hdr_enabled", "true", "bool", "HDR video support")
|
||||
.addConfig("ai_analysis", "enabled", "string", "AI analysis features")
|
||||
.addConfig("edge_processing", "true", "bool", "Edge computing enabled")
|
||||
.addConfig("cloud_sync", "auto", "string", "Cloud synchronization mode")
|
||||
.build();
|
||||
|
||||
BatchDefaultConfigResponse result = defaultConfigService.batchUpdateDefaultConfigs(complexBatch);
|
||||
|
||||
// Detailed result analysis
|
||||
if (result.getSuccess() == complexBatch.getConfigs().size()) {
|
||||
log.info("✅ All {} configurations processed successfully", result.getSuccess());
|
||||
} else if (result.getSuccess() > 0) {
|
||||
log.warn("⚠️ Partial success: {} succeeded, {} failed", result.getSuccess(), result.getFailed());
|
||||
|
||||
// Analyze what succeeded vs failed
|
||||
if (result.getProcessedItems() != null) {
|
||||
Map<String, Long> statusCounts = result.getProcessedItems().stream()
|
||||
.collect(Collectors.groupingBy(
|
||||
ProcessedConfigItem::getStatus,
|
||||
Collectors.counting()
|
||||
));
|
||||
log.info("Processing breakdown: {}", statusCounts);
|
||||
}
|
||||
} else {
|
||||
log.error("❌ All configurations failed to process");
|
||||
}
|
||||
|
||||
// Handle different types of conflicts
|
||||
if (result.getConflicts() != null) {
|
||||
Map<String, List<DefaultConfigConflict>> conflictsByType = result.getConflicts().stream()
|
||||
.collect(Collectors.groupingBy(DefaultConfigConflict::getConflictType));
|
||||
|
||||
conflictsByType.forEach((conflictType, conflicts) -> {
|
||||
log.warn("Conflict type '{}' affects {} configurations:", conflictType, conflicts.size());
|
||||
conflicts.forEach(conflict ->
|
||||
log.warn(" {} affects {} devices", conflict.getConfigKey(), conflict.getDeviceCount())
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Batch default configuration operation failed", e);
|
||||
// Implement retry logic or fallback behavior as needed
|
||||
}
|
||||
```
|
||||
|
||||
#### Fallback Cache Management for Default Configurations
|
||||
```java
|
||||
@Autowired
|
||||
private IntegrationFallbackService fallbackService;
|
||||
|
||||
// Check fallback cache status for default configurations
|
||||
boolean hasDefaultListCache = fallbackService.hasFallbackCache("zt-device", "defaults:list:1:10");
|
||||
boolean hasSpecificConfigCache = fallbackService.hasFallbackCache("zt-device", "defaults:config:resolution");
|
||||
|
||||
// Get cache statistics
|
||||
IntegrationFallbackService.FallbackCacheStats stats = fallbackService.getFallbackCacheStats("zt-device");
|
||||
log.info("Default config fallback cache: {} items, TTL: {} days",
|
||||
stats.getTotalCacheCount(), stats.getFallbackTtlDays());
|
||||
|
||||
// Clear specific default configuration cache
|
||||
fallbackService.clearFallbackCache("zt-device", "defaults:config:resolution");
|
||||
|
||||
// Clear all default configuration caches
|
||||
fallbackService.clearAllFallbackCache("zt-device");
|
||||
```
|
||||
|
||||
### Default Configuration Types and Common Keys
|
||||
|
||||
#### Video Configuration Defaults
|
||||
- `resolution`: Video resolution ("1920x1080", "3840x2160", "1280x720")
|
||||
- `frameRate`: Video frame rate (integer: 15, 25, 30, 60)
|
||||
- `codec`: Video codec ("H264", "H265", "MJPEG")
|
||||
- `bitrate`: Video bitrate in bps (integer)
|
||||
- `quality`: Video quality level ("low", "medium", "high", "ultra")
|
||||
|
||||
#### Network Configuration Defaults
|
||||
- `protocol`: Network protocol ("RTSP", "HTTP", "ONVIF")
|
||||
- `port`: Network port (integer: 554, 80, 8080)
|
||||
- `timeout`: Connection timeout in seconds (integer)
|
||||
- `retry_count`: Maximum retry attempts (integer)
|
||||
|
||||
#### Authentication Defaults
|
||||
- `username`: Default username (string)
|
||||
- `password`: Default password (string)
|
||||
- `auth_method`: Authentication method ("basic", "digest", "none")
|
||||
|
||||
#### Storage and Recording Defaults
|
||||
- `storage_path`: Default storage path (string)
|
||||
- `recording_enabled`: Enable recording (boolean)
|
||||
- `retention_days`: Storage retention period (integer)
|
||||
- `max_file_size`: Maximum file size in MB (integer)
|
||||
|
||||
#### Feature Control Defaults
|
||||
- `motion_detection`: Enable motion detection (boolean)
|
||||
- `night_vision`: Enable night vision (boolean)
|
||||
- `audio_enabled`: Enable audio recording (boolean)
|
||||
- `ptz_enabled`: Enable PTZ control (boolean)
|
||||
- `ai_analysis`: AI analysis features ("enabled", "disabled", "auto")
|
||||
|
||||
### Error Handling and Responses
|
||||
|
||||
#### HTTP Status Codes
|
||||
- **200 OK**: Configuration retrieved/updated successfully
|
||||
- **201 Created**: New configuration created successfully
|
||||
- **202 Accepted**: Operation completed with conflicts (check response for details)
|
||||
- **400 Bad Request**: Invalid request parameters or validation errors
|
||||
- **404 Not Found**: Configuration key not found
|
||||
- **409 Conflict**: Configuration conflicts prevent operation (includes conflict details)
|
||||
|
||||
#### Conflict Resolution
|
||||
When updating default configurations that are actively used by devices, the API performs conflict detection:
|
||||
|
||||
1. **Type Conflicts**: Changing configuration type when devices use different types
|
||||
2. **Value Validation**: Ensuring new values are compatible with existing device configurations
|
||||
3. **Dependency Conflicts**: Checking for configuration dependencies and relationships
|
||||
|
||||
The response includes detailed conflict information to help resolve issues:
|
||||
```java
|
||||
// Example conflict handling
|
||||
DefaultConfigConflict conflict = updateResult;
|
||||
if (conflict != null) {
|
||||
switch (conflict.getConflictType()) {
|
||||
case "TYPE_MISMATCH":
|
||||
log.warn("Type conflict: {} devices using '{}' type, proposed '{}'type",
|
||||
conflict.getDeviceCount(), conflict.getCurrentType(), conflict.getProposedType());
|
||||
// Handle type conversion or device updates
|
||||
break;
|
||||
|
||||
case "VALUE_RANGE":
|
||||
log.warn("Value range conflict for key '{}' affects {} devices",
|
||||
conflict.getConfigKey(), conflict.getDeviceCount());
|
||||
// Handle value range adjustments
|
||||
break;
|
||||
|
||||
case "DEPENDENCY":
|
||||
log.warn("Dependency conflict detected for '{}'", conflict.getConfigKey());
|
||||
// Handle dependency resolution
|
||||
break;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Enhanced Batch Configuration API
|
||||
|
||||
Device Integration now supports an enhanced batch configuration API that provides detailed processing results and supports default configuration rules.
|
||||
@@ -432,6 +783,23 @@ Each processed item includes:
|
||||
- **IPC**: IP Camera devices for video monitoring
|
||||
- **CUSTOM**: Custom device types for sensors, controllers, etc.
|
||||
|
||||
### Testing Default Configuration Integration
|
||||
|
||||
```bash
|
||||
# Run default configuration integration tests
|
||||
mvn test -Dtest=DefaultConfigIntegrationServiceTest
|
||||
|
||||
# Run all device integration tests (including default configs)
|
||||
mvn test -Dtest="com.ycwl.basic.integration.device.*Test"
|
||||
|
||||
# Enable example runner in application-dev.yml
|
||||
integration:
|
||||
device:
|
||||
example:
|
||||
default-config:
|
||||
enabled: true
|
||||
```
|
||||
|
||||
### Common Configuration Keys
|
||||
- `ip_address`: Device IP address
|
||||
- `resolution`: Video resolution (e.g., "1920x1080", "3840x2160")
|
||||
|
Reference in New Issue
Block a user