feat(device): 添加设备排序功能并优化示例代码

- 在 CreateDeviceRequest 和 UpdateDeviceRequest 中添加 sort 字段
- 在 DeviceV2DTO 中添加 sort 属性- 更新 DeviceIntegrationExample 中的示例代码,演示设备排序功能- 新增设备排序相关的服务方法,如 createIpcDeviceWithSort 和 updateDeviceSort
- 优化 runAllExamples 方法,移除部分冗余示例
- 新增 runBasicExamples 方法,用于运行基础示例
This commit is contained in:
2025-09-02 01:32:12 +08:00
parent ad7d1042f4
commit b475e38018
5 changed files with 149 additions and 304 deletions

View File

@@ -9,4 +9,5 @@ public class CreateDeviceRequest {
private String type;
private Integer isActive;
private Long scenicId;
private Integer sort;
}

View File

@@ -26,6 +26,9 @@ public class DeviceV2DTO {
@JsonProperty("scenicId")
private Long scenicId;
@JsonProperty("sort")
private Integer sort;
@JsonProperty("createTime")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;

View File

@@ -9,4 +9,5 @@ public class UpdateDeviceRequest {
private String type;
private Integer isActive;
private Long scenicId;
private Integer sort;
}

View File

@@ -8,7 +8,6 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -24,352 +23,142 @@ public class DeviceIntegrationExample {
private final DeviceConfigIntegrationService deviceConfigService;
/**
* 基本设备操作示例
* 基本设备操作
*/
public void basicDeviceOperations() {
log.info("=== 基本设备操作示例 ===");
log.info("=== 基本设备操作 ===");
// 1. 创建IPC摄像头设备
// 创建IPC摄像头设备(默认排序)
DeviceV2DTO ipcDevice = deviceService.createIpcDevice(
"前门摄像头", "CAM001", 1001L);
log.info("创建IPC设备: {}", ipcDevice);
log.info("创建IPC设备: {}, 排序值: {}", ipcDevice.getName(), ipcDevice.getSort());
// 2. 创建自定义设备
DeviceV2DTO customDevice = deviceService.createCustomDevice(
"温度传感器", "TEMP001", 1001L);
log.info("创建自定义设备: {}", customDevice);
// 3. 根据ID获取设备信息
// 根据ID获取设备信息
DeviceV2DTO device = deviceService.getDevice(ipcDevice.getId());
log.info("获取设备信息: {}", device);
log.info("获取设备信息: {}, 排序值: {}", device.getName(), device.getSort());
// 4. 根据设备编号获取设备信息
// 根据设备编号获取设备信息
DeviceV2DTO deviceByNo = deviceService.getDeviceByNo("CAM001");
log.info("根据编号获取设备: {}", deviceByNo);
log.info("根据编号获取设备: {}", deviceByNo.getName());
// 5. 获取设备详细信息(含配置)
// 获取设备详细信息(含配置)
DeviceV2WithConfigDTO deviceWithConfig = deviceService.getDeviceWithConfig(ipcDevice.getId());
log.info("获取设备配置: {}", deviceWithConfig);
log.info("获取设备配置: {}", deviceWithConfig.getName());
// 6. 分页查询景区设备列表
// 分页查询景区设备列表
DeviceV2ListResponse deviceList = deviceService.getScenicIpcDevices(1001L, 1, 10);
log.info("景区IPC设备列表: 总数={}, 当前页={}", deviceList.getTotal(), deviceList.getList().size());
log.info("景区设备列表: 总数={}", deviceList.getTotal());
// 7. 启用/禁用设备
// 用设备
deviceService.enableDevice(ipcDevice.getId());
log.info("设备已启用");
deviceService.disableDevice(customDevice.getId());
log.info("设备已禁用");
}
/**
* 设备配置管理示例
* 设备排序功能演示
*/
public void deviceConfigurationOperations() {
log.info("=== 设备配置管理示例 ===");
public void deviceSortingOperations() {
log.info("=== 设备排序功能演示 ===");
// 假设已有设备ID
Long deviceId = 1L;
Long scenicId = 1001L;
// 1. 配置摄像头基本参数
deviceConfigService.configureCameraBasicParams(
deviceId, "192.168.1.100", "1920x1080", 30, "RTSP");
log.info("摄像头基本参数已配置");
// 创建带排序的设备
DeviceV2DTO camera1 = deviceService.createIpcDeviceWithSort(
"大门摄像头", "CAM_GATE", scenicId, 10);
log.info("创建摄像头1: {}, 排序: {}", camera1.getName(), camera1.getSort());
// 2. 配置摄像头完整参数(包含认证)
deviceConfigService.configureCameraFullParams(
deviceId, "192.168.1.101", "3840x2160", 25, "RTSP", "admin", "password123");
log.info("摄像头完整参数已配置");
DeviceV2DTO camera2 = deviceService.createIpcDeviceWithSort(
"后门摄像头", "CAM_BACK", scenicId, 20);
log.info("创建摄像头2: {}, 排序: {}", camera2.getName(), camera2.getSort());
// 3. 单独设置特定配置
deviceConfigService.setDeviceIpAddress(deviceId, "192.168.1.102");
deviceConfigService.setDeviceResolution(deviceId, "2560x1440");
deviceConfigService.setDeviceFramerate(deviceId, 60);
log.info("单独配置项已更新");
DeviceV2DTO sensor1 = deviceService.createCustomDeviceWithSort(
"温度传感器", "TEMP_01", scenicId, 5);
log.info("创建传感器: {}, 排序: {}", sensor1.getName(), sensor1.getSort());
// 4. 获取设备所有配置
List<DeviceConfigV2DTO> configs = deviceConfigService.getDeviceConfigs(deviceId);
log.info("设备配置列表: {}", configs.size());
// 更新设备排序
deviceService.updateDeviceSort(camera1.getId(), 1);
log.info("更新摄像头1排序为1(置顶)");
// 5. 获取扁平化配置
Map<String, Object> flatConfig = deviceConfigService.getDeviceFlatConfig(deviceId);
log.info("扁平化配置: {}", flatConfig);
// 6. 获取特定配置值
String ipAddress = deviceConfigService.getDeviceIpAddress(deviceId);
String resolution = deviceConfigService.getDeviceResolution(deviceId);
log.info("IP地址: {}, 分辨率: {}", ipAddress, resolution);
// 7. 批量更新配置(扁平化方式)
Map<String, Object> batchConfigs = new HashMap<>();
batchConfigs.put("brightness", "50");
batchConfigs.put("contrast", "80");
batchConfigs.put("quality", "high");
deviceConfigService.batchFlatUpdateDeviceConfig(deviceId, batchConfigs);
log.info("批量配置已更新");
// 8. 使用新的批量配置API(返回详细结果)
BatchDeviceConfigRequest.BatchDeviceConfigItem item1 = new BatchDeviceConfigRequest.BatchDeviceConfigItem();
item1.setConfigKey("resolution");
item1.setConfigValue("4K");
BatchDeviceConfigRequest.BatchDeviceConfigItem item2 = new BatchDeviceConfigRequest.BatchDeviceConfigItem();
item2.setConfigKey("custom_setting");
item2.setConfigValue("custom_value");
item2.setConfigType("string");
item2.setDescription("自定义设置");
BatchDeviceConfigRequest batchRequest = new BatchDeviceConfigRequest();
batchRequest.setConfigs(List.of(item1, item2));
BatchUpdateResponse result = deviceConfigService.batchUpdateDeviceConfigWithResult(deviceId, batchRequest);
log.info("批量配置更新结果: 成功={}, 失败={}", result.getSuccess(), result.getFailed());
for (ProcessedConfigItem processedItem : result.getProcessedItems()) {
log.info("配置项 {} 处理状态: {}, 动作: {}, 有默认配置: {}",
processedItem.getConfigKey(),
processedItem.getStatus(),
processedItem.getAction(),
processedItem.getHasDefault());
// 获取排序后的设备列表
DeviceV2ListResponse sortedList = deviceService.listDevices(1, 10, null, null, null, 1, scenicId);
log.info("排序后的设备列表:");
for (DeviceV2DTO device : sortedList.getList()) {
log.info(" - {}: 排序={}, 类型={}", device.getName(), device.getSort(), device.getType());
}
// 9. 使用配置构建器简化批量配置
// 批量调整排序演示
log.info("--- 批量调整排序演示 ---");
deviceService.updateDeviceSort(sensor1.getId(), 15); // 传感器排到中间
deviceService.updateDeviceSort(camera2.getId(), 30); // 后门摄像头排到最后
log.info("批量排序调整完成");
}
/**
* 设备配置管理
*/
public void deviceConfigurationOperations() {
log.info("=== 设备配置管理 ===");
Long deviceId = 1L;
// 配置摄像头基本参数
deviceConfigService.configureCameraParams(
deviceId, "192.168.1.100", "1920x1080", 30, "RTSP", "admin", "password123");
log.info("摄像头基本参数已配置");
// 获取设备所有配置
List<DeviceConfigV2DTO> configs = deviceConfigService.getDeviceConfigs(deviceId);
log.info("设备配置数量: {}", configs.size());
// 获取扁平化配置
Map<String, Object> flatConfig = deviceConfigService.getDeviceFlatConfig(deviceId);
log.info("扁平化配置项数: {}", flatConfig.size());
// 使用批量配置API
BatchDeviceConfigRequest builderRequest = deviceConfigService.createBatchConfigBuilder()
.addVideoConfig("1920x1080", 30, "H264")
.addNetworkConfig("192.168.1.100", 554, "RTSP")
.addAuthConfig("admin", "password123")
.addConfig("custom_key", "custom_value", "string", "自定义配置")
.build();
BatchUpdateResponse builderResult = deviceConfigService.batchUpdateDeviceConfigWithResult(deviceId, builderRequest);
log.info("构建器批量配置更新结果: 成功={}, 失败={}", builderResult.getSuccess(), builderResult.getFailed());
BatchUpdateResponse result = deviceConfigService.batchUpdateDeviceConfig(deviceId, builderRequest);
log.info("批量配置更新结果: 成功={}, 失败={}", result.getSuccess(), result.getFailed());
}
/**
* 摄像头管理示例
* 排序最佳实践演示
*/
public void cameraManagementExample() {
log.info("=== 摄像头管理示例 ===");
public void sortingBestPractices() {
log.info("=== 排序最佳实践演示 ===");
Long scenicId = 1001L;
// 1. 批量创建摄像头
for (int i = 1; i <= 5; i++) {
DeviceV2DTO camera = deviceService.createIpcDevice(
"摄像头" + i, "CAM00" + i, scenicId);
// 配置每个摄像头的基本参数
deviceConfigService.configureCameraFullParams(
camera.getId(),
"192.168.1." + (100 + i),
"1920x1080",
30,
"RTSP",
"admin",
"camera" + i
);
log.info("创建并配置摄像头: {}", camera.getName());
}
// 推荐使用10的倍数作为排序值
DeviceV2DTO device1 = deviceService.createIpcDeviceWithSort(
"重要摄像头", "CAM_IMPORTANT", scenicId, 10);
// 2. 获取景区所有摄像头状态
DeviceV2WithConfigListResponse camerasWithConfig =
deviceService.listDevicesWithConfig(1, 100, null, null, "IPC", 1, scenicId);
DeviceV2DTO device2 = deviceService.createIpcDeviceWithSort(
"普通摄像头", "CAM_NORMAL", scenicId, 20);
log.info("景区摄像头总数: {}", camerasWithConfig.getTotal());
for (DeviceV2WithConfigDTO camera : camerasWithConfig.getList()) {
log.info("摄像头: {}, IP: {}",
camera.getName(),
camera.getConfig().get("ip_address"));
}
DeviceV2DTO device3 = deviceService.createIpcDeviceWithSort(
"备用摄像头", "CAM_BACKUP", scenicId, 30);
// 3. 批量更新摄像头分辨率
for (DeviceV2WithConfigDTO camera : camerasWithConfig.getList()) {
deviceConfigService.setDeviceResolution(camera.getId(), "2560x1440");
}
log.info("所有摄像头分辨率已更新为 2560x1440");
}
/**
* 设备监控和状态管理示例
*/
public void deviceMonitoringExample() {
log.info("=== 设备监控和状态管理示例 ===");
log.info("使用10的倍数创建设备排序: 10, 20, 30");
Long scenicId = 1001L;
// 在中间插入新设备
DeviceV2DTO insertDevice = deviceService.createIpcDeviceWithSort(
"中间摄像头", "CAM_MIDDLE", scenicId, 25);
log.info("在20和30之间插入设备,排序值: 25");
// 1. 获取景区所有激活设备
DeviceV2ListResponse activeDevices = deviceService.getScenicActiveDevices(scenicId, 1, 50);
log.info("景区激活设备数量: {}", activeDevices.getTotal());
// 置顶操作
deviceService.updateDeviceSort(device2.getId(), 1);
log.info("将普通摄像头置顶(排序值: 1)");
// 2. 按设备类型分类统计
Map<String, Integer> deviceTypeCount = new HashMap<>();
for (DeviceV2DTO device : activeDevices.getList()) {
deviceTypeCount.merge(device.getType(), 1, Integer::sum);
}
log.info("设备类型统计: {}", deviceTypeCount);
// 3. 检查设备配置完整性
for (DeviceV2DTO device : activeDevices.getList()) {
try {
Map<String, Object> config = deviceConfigService.getDeviceFlatConfig(device.getId());
boolean hasIpConfig = config.containsKey("ip_address");
log.info("设备 {} 配置状态: IP配置={}", device.getName(), hasIpConfig ? "已配置" : "未配置");
} catch (Exception e) {
log.warn("获取设备 {} 配置失败: {}", device.getName(), e.getMessage());
}
}
// 4. 按设备编号搜索
try {
DeviceV2WithConfigDTO deviceByNo = deviceService.getDeviceWithConfigByNo("CAM001");
log.info("根据编号找到设备: {} (配置项数量: {})",
deviceByNo.getName(),
deviceByNo.getConfig().size());
} catch (Exception e) {
log.warn("未找到设备编号 CAM001: {}", e.getMessage());
}
}
/**
* 新批量配置API使用示例
*/
public void batchConfigurationExample() {
log.info("=== 新批量配置API使用示例 ===");
Long deviceId = 1L;
// 1. 使用基础批量配置API
try {
// 创建批量配置请求
BatchDeviceConfigRequest.BatchDeviceConfigItem item1 = new BatchDeviceConfigRequest.BatchDeviceConfigItem();
item1.setConfigKey("resolution");
item1.setConfigValue("3840x2160");
BatchDeviceConfigRequest.BatchDeviceConfigItem item2 = new BatchDeviceConfigRequest.BatchDeviceConfigItem();
item2.setConfigKey("framerate");
item2.setConfigValue("60");
BatchDeviceConfigRequest.BatchDeviceConfigItem item3 = new BatchDeviceConfigRequest.BatchDeviceConfigItem();
item3.setConfigKey("custom_quality");
item3.setConfigValue("ultra");
item3.setConfigType("string");
item3.setDescription("自定义画质设置");
BatchDeviceConfigRequest request = new BatchDeviceConfigRequest();
request.setConfigs(List.of(item1, item2, item3));
// 调用批量配置API并获取详细结果
BatchUpdateResponse result = deviceConfigService.batchUpdateDeviceConfigWithResult(deviceId, request);
// 分析结果
log.info("批量配置更新完成 - 成功: {}, 失败: {}", result.getSuccess(), result.getFailed());
// 详细处理结果
for (ProcessedConfigItem item : result.getProcessedItems()) {
if ("success".equals(item.getStatus())) {
log.info("✅ 配置 {} 更新成功 - 动作: {}, 默认配置: {}, 最终类型: {}",
item.getConfigKey(),
item.getAction(),
item.getHasDefault() ? "" : "",
item.getFinalType());
} else {
log.warn("❌ 配置 {} 更新失败 - 原因: {}",
item.getConfigKey(),
item.getMessage());
}
}
// 错误信息
if (!result.getErrors().isEmpty()) {
log.warn("批量更新错误列表:");
result.getErrors().forEach(error -> log.warn(" - {}", error));
}
} catch (Exception e) {
log.error("批量配置更新异常", e);
}
// 2. 使用构建器模式简化批量配置
try {
log.info("--- 使用构建器模式 ---");
BatchDeviceConfigRequest builderRequest = deviceConfigService.createBatchConfigBuilder()
.addVideoConfig("1920x1080", 30, "H265") // 添加视频配置
.addNetworkConfig("192.168.1.200", 8554, "RTSP") // 添加网络配置
.addAuthConfig("operator", "newpassword") // 添加认证配置
.addConfig("recording_enabled", "true") // 使用默认配置的项
.addConfig("storage_path", "/data/recordings", "string", "录像存储路径") // 自定义配置项
.build();
BatchUpdateResponse builderResult = deviceConfigService.batchUpdateDeviceConfigWithResult(deviceId, builderRequest);
log.info("构建器模式批量配置结果 - 成功: {}, 失败: {}",
builderResult.getSuccess(), builderResult.getFailed());
} catch (Exception e) {
log.error("构建器模式批量配置异常", e);
}
// 3. 处理部分成功的情况
try {
log.info("--- 处理部分成功场景 ---");
BatchDeviceConfigRequest mixedRequest = deviceConfigService.createBatchConfigBuilder()
.addConfig("resolution", "1920x1080") // 正常配置
.addConfig("invalid_key_format", "test_value") // 可能失败的配置
.addConfig("", "empty_key_value") // 肯定失败的配置
.addConfig("max_connections", "100") // 正常配置
.build();
BatchUpdateResponse mixedResult = deviceConfigService.batchUpdateDeviceConfigWithResult(deviceId, mixedRequest);
if (mixedResult.getFailed() > 0) {
log.warn("存在部分失败的配置项:");
mixedResult.getProcessedItems().stream()
.filter(item -> "failed".equals(item.getStatus()))
.forEach(item -> log.warn(" - {}: {}", item.getConfigKey(), item.getMessage()));
log.info("成功配置的项:");
mixedResult.getProcessedItems().stream()
.filter(item -> "success".equals(item.getStatus()))
.forEach(item -> log.info(" - {}: {} ({})",
item.getConfigKey(),
item.getAction(),
item.getHasDefault() ? "使用默认规则" : "自定义配置"));
}
} catch (Exception e) {
log.error("混合配置场景异常", e);
}
}
/**
* 错误处理和故障恢复示例
*/
public void errorHandlingExample() {
log.info("=== 错误处理和故障恢复示例 ===");
// 1. 尝试获取不存在的设备
try {
deviceService.getDevice(99999L);
} catch (Exception e) {
log.warn("获取不存在设备的预期错误: {}", e.getMessage());
}
// 2. 尝试获取不存在的配置
try {
deviceConfigService.getDeviceConfigByKey(1L, "non_existent_key");
} catch (Exception e) {
log.warn("获取不存在配置的预期错误: {}", e.getMessage());
}
// 3. 创建设备时的参数验证
try {
CreateDeviceRequest invalidRequest = new CreateDeviceRequest();
// 缺少必要字段
deviceService.createDevice(invalidRequest);
} catch (Exception e) {
log.warn("创建设备参数错误: {}", e.getMessage());
// 查看最终排序结果
DeviceV2ListResponse finalList = deviceService.listDevices(1, 10, null, null, null, 1, scenicId);
log.info("最终排序结果:");
for (DeviceV2DTO device : finalList.getList()) {
log.info(" - {}: 排序={}", device.getName(), device.getSort());
}
}
@@ -379,14 +168,25 @@ public class DeviceIntegrationExample {
public void runAllExamples() {
try {
basicDeviceOperations();
deviceSortingOperations();
sortingBestPractices();
deviceConfigurationOperations();
batchConfigurationExample(); // 新增的批量配置示例
cameraManagementExample();
deviceMonitoringExample();
errorHandlingExample();
log.info("=== 所有示例执行完成 ===");
} catch (Exception e) {
log.error("示例执行过程中发生错误", e);
}
}
/**
* 运行基础示例(简化版)
*/
public void runBasicExamples() {
try {
basicDeviceOperations();
deviceConfigurationOperations();
log.info("=== 基础示例执行完成 ===");
} catch (Exception e) {
log.error("示例执行过程中发生错误", e);
}
}
}

View File

@@ -85,6 +85,21 @@ public class DeviceIntegrationService {
request.setType("IPC");
request.setIsActive(1);
request.setScenicId(scenicId);
request.setSort(0);
return createDevice(request);
}
/**
* 创建IPC摄像头设备(带排序)
*/
public DeviceV2DTO createIpcDeviceWithSort(String name, String deviceNo, Long scenicId, Integer sort) {
CreateDeviceRequest request = new CreateDeviceRequest();
request.setName(name);
request.setNo(deviceNo);
request.setType("IPC");
request.setIsActive(1);
request.setScenicId(scenicId);
request.setSort(sort != null ? sort : 0);
return createDevice(request);
}
@@ -98,6 +113,21 @@ public class DeviceIntegrationService {
request.setType("CUSTOM");
request.setIsActive(1);
request.setScenicId(scenicId);
request.setSort(0);
return createDevice(request);
}
/**
* 创建自定义设备(带排序)
*/
public DeviceV2DTO createCustomDeviceWithSort(String name, String deviceNo, Long scenicId, Integer sort) {
CreateDeviceRequest request = new CreateDeviceRequest();
request.setName(name);
request.setNo(deviceNo);
request.setType("CUSTOM");
request.setIsActive(1);
request.setScenicId(scenicId);
request.setSort(sort != null ? sort : 0);
return createDevice(request);
}
@@ -119,6 +149,16 @@ public class DeviceIntegrationService {
updateDevice(deviceId, request);
}
/**
* 更新设备排序
*/
public void updateDeviceSort(Long deviceId, Integer sort) {
log.info("更新设备排序, deviceId: {}, sort: {}", deviceId, sort);
UpdateDeviceRequest request = new UpdateDeviceRequest();
request.setSort(sort);
updateDevice(deviceId, request);
}
/**
* 获取景区的IPC设备列表
*/