【代码评审】IoT:数据桥梁的 review

This commit is contained in:
YunaiV 2025-03-25 21:01:12 +08:00
parent 37fdd6247d
commit e5a74193ba
3 changed files with 14 additions and 29 deletions

View File

@ -72,22 +72,20 @@ public class IotThingModelController {
@PreAuthorize("@ss.hasPermission('iot:thing-model:query')") @PreAuthorize("@ss.hasPermission('iot:thing-model:query')")
public CommonResult<IotThingModelTSLRespVO> getThingModelTsl(@RequestParam("productId") Long productId) { public CommonResult<IotThingModelTSLRespVO> getThingModelTsl(@RequestParam("productId") Long productId) {
IotThingModelTSLRespVO tslRespVO = new IotThingModelTSLRespVO(); IotThingModelTSLRespVO tslRespVO = new IotThingModelTSLRespVO();
// TODO @puhui999是不是要先查询产品哈原因是万一没配置物模型但是产品已经有了
// 1. 获得产品所有物模型定义 // 1. 获得产品所有物模型定义
List<IotThingModelDO> thingModels = thingModelService.getThingModelListByProductId(productId); List<IotThingModelDO> thingModels = thingModelService.getThingModelListByProductId(productId);
if (CollUtil.isEmpty(thingModels)) { if (CollUtil.isEmpty(thingModels)) {
return success(tslRespVO); return success(tslRespVO);
} }
// 2.1 设置公共部分参数 // 2. 设置公共部分参数
IotThingModelDO thingModel = thingModels.get(0); IotThingModelDO thingModel = thingModels.get(0);
tslRespVO.setProductId(thingModel.getProductId()).setProductKey(thingModel.getProductKey()); tslRespVO.setProductId(thingModel.getProductId()).setProductKey(thingModel.getProductKey());
// 2.2 处理属性列表
tslRespVO.setProperties(convertList(filterList(thingModels, item -> tslRespVO.setProperties(convertList(filterList(thingModels, item ->
ObjUtil.equal(IotThingModelTypeEnum.PROPERTY.getType(), item.getType())), IotThingModelDO::getProperty)); ObjUtil.equal(IotThingModelTypeEnum.PROPERTY.getType(), item.getType())), IotThingModelDO::getProperty));
// 2.3 处理服务列表
tslRespVO.setServices(convertList(filterList(thingModels, item -> tslRespVO.setServices(convertList(filterList(thingModels, item ->
ObjUtil.equal(IotThingModelTypeEnum.SERVICE.getType(), item.getType())), IotThingModelDO::getService)); ObjUtil.equal(IotThingModelTypeEnum.SERVICE.getType(), item.getType())), IotThingModelDO::getService));
// 2.4 处理事件列表
tslRespVO.setEvents(convertList(filterList(thingModels, item -> tslRespVO.setEvents(convertList(filterList(thingModels, item ->
ObjUtil.equal(IotThingModelTypeEnum.EVENT.getType(), item.getType())), IotThingModelDO::getEvent)); ObjUtil.equal(IotThingModelTypeEnum.EVENT.getType(), item.getType())), IotThingModelDO::getEvent));
return success(tslRespVO); return success(tslRespVO);

View File

@ -56,7 +56,6 @@ public class IotRedisStreamDataBridgeExecute extends
serverConfig.setPassword(config.getPassword()); serverConfig.setPassword(config.getPassword());
} }
// TODO @芋艿看看怎么优化
// 创建 RedisTemplate 并配置 // 创建 RedisTemplate 并配置
RedissonClient redisson = Redisson.create(redissonConfig); RedissonClient redisson = Redisson.create(redissonConfig);
RedisTemplate<String, Object> template = new RedisTemplate<>(); RedisTemplate<String, Object> template = new RedisTemplate<>();

View File

@ -41,16 +41,9 @@ public class IotDataBridgeExecuteTest extends BaseMockitoUnitTest {
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
// 创建共享的测试消息 // 创建共享的测试消息
message = IotDeviceMessage.builder() message = IotDeviceMessage.builder().requestId("TEST-001").reportTime(LocalDateTime.now()).tenantId(1L)
.requestId("TEST-001") .productKey("testProduct").deviceName("testDevice").deviceKey("testDeviceKey")
.reportTime(LocalDateTime.now()) .type("property").identifier("temperature").data("{\"value\": 60}")
.tenantId(1L)
.productKey("testProduct")
.deviceName("testDevice")
.deviceKey("testDeviceKey")
.type("property")
.identifier("temperature")
.data("{\"value\": 60}")
.build(); .build();
} }
@ -132,14 +125,12 @@ public class IotDataBridgeExecuteTest extends BaseMockitoUnitTest {
// 2. 创建配置 // 2. 创建配置
IotDataBridgeHttpConfig config = new IotDataBridgeHttpConfig() IotDataBridgeHttpConfig config = new IotDataBridgeHttpConfig()
.setUrl("https://doc.iocoder.cn/") .setUrl("https://doc.iocoder.cn/").setMethod(HttpMethod.GET.name());
.setMethod(HttpMethod.GET.name());
// 3. 执行测试 // 3. 执行测试
log.info("[testHttpDataBridge][执行HTTP数据桥接测试]"); log.info("[testHttpDataBridge][执行HTTP数据桥接测试]");
httpDataBridgeExecute.execute(message, new IotDataBridgeDO() httpDataBridgeExecute.execute(message, new IotDataBridgeDO()
.setType(httpDataBridgeExecute.getType()) .setType(httpDataBridgeExecute.getType()).setConfig(config));
.setConfig(config));
} }
/** /**
@ -147,19 +138,16 @@ public class IotDataBridgeExecuteTest extends BaseMockitoUnitTest {
* *
* @param action 执行器实例 * @param action 执行器实例
* @param config 配置对象 * @param config 配置对象
* @param mqType MQ类型 * @param type MQ 类型
* @throws Exception 如果执行过程中发生异常 * @throws Exception 如果执行过程中发生异常
*/ */
private void executeAndVerifyCache(IotDataBridgeExecute<?> action, IotDataBridgeAbstractConfig config, String mqType) throws Exception { private void executeAndVerifyCache(IotDataBridgeExecute<?> action, IotDataBridgeAbstractConfig config, String type)
log.info("[test{}DataBridge][第一次执行,应该会创建新的 producer]", mqType); throws Exception {
action.execute(message, new IotDataBridgeDO() log.info("[test{}DataBridge][第一次执行,应该会创建新的 producer]", type);
.setType(action.getType()) action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
.setConfig(config));
log.info("[test{}DataBridge][第二次执行,应该会复用缓存的 producer]", mqType); log.info("[test{}DataBridge][第二次执行,应该会复用缓存的 producer]", type);
action.execute(message, new IotDataBridgeDO() action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
.setType(action.getType())
.setConfig(config));
} }
} }