Commit 3191d1bd1a474a07c02ccf2593a80d7b2d837d8a

Authored by 芋道源码
Committed by Gitee
2 parents 0bdd0002 acd32f7b

!1284 【代码优化】IoT: 数据桥梁代码优化

Merge pull request !1284 from puhui999/iot
Showing 14 changed files with 187 additions and 227 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/rule/IotDataBridgeController.java
1 package cn.iocoder.yudao.module.iot.controller.admin.rule; 1 package cn.iocoder.yudao.module.iot.controller.admin.rule;
2 2
3 -import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;  
4 import cn.iocoder.yudao.framework.common.pojo.CommonResult; 3 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
5 -import cn.iocoder.yudao.framework.common.pojo.PageParam;  
6 import cn.iocoder.yudao.framework.common.pojo.PageResult; 4 import cn.iocoder.yudao.framework.common.pojo.PageResult;
7 import cn.iocoder.yudao.framework.common.util.object.BeanUtils; 5 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
8 -import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;  
9 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.IotDataBridgePageReqVO; 6 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.IotDataBridgePageReqVO;
10 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.IotDataBridgeRespVO; 7 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.IotDataBridgeRespVO;
11 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.IotDataBridgeSaveReqVO; 8 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.IotDataBridgeSaveReqVO;
@@ -15,16 +12,11 @@ import io.swagger.v3.oas.annotations.Operation; @@ -15,16 +12,11 @@ import io.swagger.v3.oas.annotations.Operation;
15 import io.swagger.v3.oas.annotations.Parameter; 12 import io.swagger.v3.oas.annotations.Parameter;
16 import io.swagger.v3.oas.annotations.tags.Tag; 13 import io.swagger.v3.oas.annotations.tags.Tag;
17 import jakarta.annotation.Resource; 14 import jakarta.annotation.Resource;
18 -import jakarta.servlet.http.HttpServletResponse;  
19 import jakarta.validation.Valid; 15 import jakarta.validation.Valid;
20 import org.springframework.security.access.prepost.PreAuthorize; 16 import org.springframework.security.access.prepost.PreAuthorize;
21 import org.springframework.validation.annotation.Validated; 17 import org.springframework.validation.annotation.Validated;
22 import org.springframework.web.bind.annotation.*; 18 import org.springframework.web.bind.annotation.*;
23 19
24 -import java.io.IOException;  
25 -import java.util.List;  
26 -  
27 -import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;  
28 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; 20 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
29 21
30 @Tag(name = "管理后台 - IoT 数据桥梁") 22 @Tag(name = "管理后台 - IoT 数据桥梁")
@@ -77,18 +69,4 @@ public class IotDataBridgeController { @@ -77,18 +69,4 @@ public class IotDataBridgeController {
77 return success(BeanUtils.toBean(pageResult, IotDataBridgeRespVO.class)); 69 return success(BeanUtils.toBean(pageResult, IotDataBridgeRespVO.class));
78 } 70 }
79 71
80 - // TODO @puhui999:不用导出哈。相关的 IotDataBridgeRespVO 里的导出也注释掉哈  
81 - @GetMapping("/export-excel")  
82 - @Operation(summary = "导出数据桥梁 Excel")  
83 - @PreAuthorize("@ss.hasPermission('iot:data-bridge:export')")  
84 - @ApiAccessLog(operateType = EXPORT)  
85 - public void exportDataBridgeExcel(@Valid IotDataBridgePageReqVO pageReqVO,  
86 - HttpServletResponse response) throws IOException {  
87 - pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);  
88 - List<IotDataBridgeDO> list = dataBridgeService.getDataBridgePage(pageReqVO).getList();  
89 - // 导出 Excel  
90 - ExcelUtils.write(response, "IoT 数据桥梁.xls", "数据", IotDataBridgeRespVO.class,  
91 - BeanUtils.toBean(list, IotDataBridgeRespVO.class));  
92 - }  
93 -  
94 } 72 }
95 \ No newline at end of file 73 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/rule/vo/databridge/IotDataBridgePageReqVO.java
@@ -20,22 +20,11 @@ public class IotDataBridgePageReqVO extends PageParam { @@ -20,22 +20,11 @@ public class IotDataBridgePageReqVO extends PageParam {
20 @Schema(description = "桥梁名称", example = "赵六") 20 @Schema(description = "桥梁名称", example = "赵六")
21 private String name; 21 private String name;
22 22
23 - // TODO @puhui999:description、direction、type 不过滤哈  
24 - @Schema(description = "桥梁描述", example = "随便")  
25 - private String description;  
26 -  
27 @Schema(description = "桥梁状态", example = "2") 23 @Schema(description = "桥梁状态", example = "2")
28 private Integer status; 24 private Integer status;
29 25
30 - @Schema(description = "桥梁方向")  
31 - private Integer direction;  
32 -  
33 - @Schema(description = "桥梁类型", example = "1")  
34 - private Integer type;  
35 -  
36 @Schema(description = "创建时间") 26 @Schema(description = "创建时间")
37 @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) 27 @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
38 private LocalDateTime[] createTime; 28 private LocalDateTime[] createTime;
39 29
40 -  
41 } 30 }
42 \ No newline at end of file 31 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/rule/vo/databridge/IotDataBridgeRespVO.java
1 package cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge; 1 package cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge;
2 2
3 -import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat;  
4 -import cn.iocoder.yudao.framework.excel.core.convert.DictConvert;  
5 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeAbstractConfig; 3 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeAbstractConfig;
6 import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; 4 import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
7 -import com.alibaba.excel.annotation.ExcelProperty;  
8 import io.swagger.v3.oas.annotations.media.Schema; 5 import io.swagger.v3.oas.annotations.media.Schema;
9 import lombok.Data; 6 import lombok.Data;
10 7
11 import java.time.LocalDateTime; 8 import java.time.LocalDateTime;
12 9
13 -import static cn.iocoder.yudao.module.iot.enums.DictTypeConstants.IOT_DATA_BRIDGE_DIRECTION_ENUM;  
14 -import static cn.iocoder.yudao.module.iot.enums.DictTypeConstants.IOT_DATA_BRIDGE_TYPE_ENUM;  
15 -import static cn.iocoder.yudao.module.system.enums.DictTypeConstants.COMMON_STATUS;  
16 -  
17 @Schema(description = "管理后台 - IoT 数据桥梁 Response VO") 10 @Schema(description = "管理后台 - IoT 数据桥梁 Response VO")
18 @Data 11 @Data
19 @ExcelIgnoreUnannotated 12 @ExcelIgnoreUnannotated
20 public class IotDataBridgeRespVO { 13 public class IotDataBridgeRespVO {
21 14
22 @Schema(description = "桥梁编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "18564") 15 @Schema(description = "桥梁编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "18564")
23 - @ExcelProperty("桥梁编号")  
24 private Long id; 16 private Long id;
25 17
26 @Schema(description = "桥梁名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "赵六") 18 @Schema(description = "桥梁名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "赵六")
27 - @ExcelProperty("桥梁名称")  
28 private String name; 19 private String name;
29 20
30 @Schema(description = "桥梁描述", example = "随便") 21 @Schema(description = "桥梁描述", example = "随便")
31 - @ExcelProperty("桥梁描述")  
32 private String description; 22 private String description;
33 23
34 @Schema(description = "桥梁状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "2") 24 @Schema(description = "桥梁状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
35 - @ExcelProperty(value = "桥梁状态", converter = DictConvert.class)  
36 - @DictFormat(COMMON_STATUS)  
37 private Integer status; 25 private Integer status;
38 26
39 @Schema(description = "桥梁方向", requiredMode = Schema.RequiredMode.REQUIRED) 27 @Schema(description = "桥梁方向", requiredMode = Schema.RequiredMode.REQUIRED)
40 - @ExcelProperty(value = "桥梁方向", converter = DictConvert.class)  
41 - @DictFormat(IOT_DATA_BRIDGE_DIRECTION_ENUM)  
42 private Integer direction; 28 private Integer direction;
43 29
44 @Schema(description = "桥梁类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") 30 @Schema(description = "桥梁类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
45 - @ExcelProperty(value = "桥梁类型", converter = DictConvert.class)  
46 - @DictFormat(IOT_DATA_BRIDGE_TYPE_ENUM)  
47 private Integer type; 31 private Integer type;
48 32
49 @Schema(description = "桥梁配置") 33 @Schema(description = "桥梁配置")
50 - @ExcelProperty("桥梁配置")  
51 private IotDataBridgeAbstractConfig config; 34 private IotDataBridgeAbstractConfig config;
52 35
53 @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED) 36 @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
54 - @ExcelProperty("创建时间")  
55 private LocalDateTime createTime; 37 private LocalDateTime createTime;
56 38
57 } 39 }
58 \ No newline at end of file 40 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/rule/vo/databridge/IotDataBridgeSaveReqVO.java
1 package cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge; 1 package cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge;
2 2
  3 +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
  4 +import cn.iocoder.yudao.framework.common.validation.InEnum;
3 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeAbstractConfig; 5 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeAbstractConfig;
  6 +import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeDirectionEnum;
  7 +import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeTypeEnum;
4 import io.swagger.v3.oas.annotations.media.Schema; 8 import io.swagger.v3.oas.annotations.media.Schema;
5 import jakarta.validation.constraints.NotEmpty; 9 import jakarta.validation.constraints.NotEmpty;
6 import jakarta.validation.constraints.NotNull; 10 import jakarta.validation.constraints.NotNull;
@@ -22,15 +26,17 @@ public class IotDataBridgeSaveReqVO { @@ -22,15 +26,17 @@ public class IotDataBridgeSaveReqVO {
22 26
23 @Schema(description = "桥梁状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "2") 27 @Schema(description = "桥梁状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
24 @NotNull(message = "桥梁状态不能为空") 28 @NotNull(message = "桥梁状态不能为空")
  29 + @InEnum(CommonStatusEnum.class)
25 private Integer status; 30 private Integer status;
26 31
27 - // TODO @puhui999:枚举的校验  
28 @Schema(description = "桥梁方向", requiredMode = Schema.RequiredMode.REQUIRED) 32 @Schema(description = "桥梁方向", requiredMode = Schema.RequiredMode.REQUIRED)
29 @NotNull(message = "桥梁方向不能为空") 33 @NotNull(message = "桥梁方向不能为空")
  34 + @InEnum(IotDataBridgeDirectionEnum.class)
30 private Integer direction; 35 private Integer direction;
31 36
32 @Schema(description = "桥梁类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") 37 @Schema(description = "桥梁类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
33 @NotNull(message = "桥梁类型不能为空") 38 @NotNull(message = "桥梁类型不能为空")
  39 + @InEnum(IotDataBridgeTypeEnum.class)
34 private Integer type; 40 private Integer type;
35 41
36 @Schema(description = "桥梁配置") 42 @Schema(description = "桥梁配置")
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/rule/vo/databridge/config/IotDataBridgeAbstractConfig.java
@@ -15,12 +15,12 @@ import lombok.Data; @@ -15,12 +15,12 @@ import lombok.Data;
15 @Data 15 @Data
16 @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", visible = true) 16 @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", visible = true)
17 @JsonSubTypes({ 17 @JsonSubTypes({
18 - @JsonSubTypes.Type(value = IotDataBridgeHttpConfig.class, name = "HTTP"),  
19 - @JsonSubTypes.Type(value = IotDataBridgeKafkaMQConfig.class, name = "KAFKA"),  
20 - @JsonSubTypes.Type(value = IotDataBridgeMqttConfig.class, name = "MQTT"),  
21 - @JsonSubTypes.Type(value = IotDataBridgeRabbitMQConfig.class, name = "RABBITMQ"),  
22 - @JsonSubTypes.Type(value = IotDataBridgeRedisStreamMQConfig.class, name = "REDIS_STREAM"),  
23 - @JsonSubTypes.Type(value = IotDataBridgeRocketMQConfig.class, name = "ROCKETMQ"), 18 + @JsonSubTypes.Type(value = IotDataBridgeHttpConfig.class, name = "1"),
  19 + @JsonSubTypes.Type(value = IotDataBridgeMqttConfig.class, name = "10"),
  20 + @JsonSubTypes.Type(value = IotDataBridgeRedisStreamMQConfig.class, name = "21"),
  21 + @JsonSubTypes.Type(value = IotDataBridgeRocketMQConfig.class, name = "30"),
  22 + @JsonSubTypes.Type(value = IotDataBridgeRabbitMQConfig.class, name = "31"),
  23 + @JsonSubTypes.Type(value = IotDataBridgeKafkaMQConfig.class, name = "32"),
24 }) 24 })
25 public abstract class IotDataBridgeAbstractConfig { 25 public abstract class IotDataBridgeAbstractConfig {
26 26
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/rule/IotDataBridgeDO.java
1 package cn.iocoder.yudao.module.iot.dal.dataobject.rule; 1 package cn.iocoder.yudao.module.iot.dal.dataobject.rule;
2 2
  3 +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
3 import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; 4 import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
4 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeAbstractConfig; 5 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeAbstractConfig;
5 import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeDirectionEnum; 6 import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeDirectionEnum;
@@ -42,7 +43,7 @@ public class IotDataBridgeDO extends BaseDO { @@ -42,7 +43,7 @@ public class IotDataBridgeDO extends BaseDO {
42 /** 43 /**
43 * 桥梁状态 44 * 桥梁状态
44 * 45 *
45 - * 枚举 {@link cn.iocoder.yudao.framework.common.enums.CommonStatusEnum} 46 + * 枚举 {@link CommonStatusEnum}
46 */ 47 */
47 private Integer status; 48 private Integer status;
48 /** 49 /**
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/rule/IotDataBridgeMapper.java
@@ -18,10 +18,7 @@ public interface IotDataBridgeMapper extends BaseMapperX&lt;IotDataBridgeDO&gt; { @@ -18,10 +18,7 @@ public interface IotDataBridgeMapper extends BaseMapperX&lt;IotDataBridgeDO&gt; {
18 default PageResult<IotDataBridgeDO> selectPage(IotDataBridgePageReqVO reqVO) { 18 default PageResult<IotDataBridgeDO> selectPage(IotDataBridgePageReqVO reqVO) {
19 return selectPage(reqVO, new LambdaQueryWrapperX<IotDataBridgeDO>() 19 return selectPage(reqVO, new LambdaQueryWrapperX<IotDataBridgeDO>()
20 .likeIfPresent(IotDataBridgeDO::getName, reqVO.getName()) 20 .likeIfPresent(IotDataBridgeDO::getName, reqVO.getName())
21 - .likeIfPresent(IotDataBridgeDO::getDescription, reqVO.getDescription())  
22 .eqIfPresent(IotDataBridgeDO::getStatus, reqVO.getStatus()) 21 .eqIfPresent(IotDataBridgeDO::getStatus, reqVO.getStatus())
23 - .eqIfPresent(IotDataBridgeDO::getDirection, reqVO.getDirection())  
24 - .eqIfPresent(IotDataBridgeDO::getType, reqVO.getType())  
25 .betweenIfPresent(IotDataBridgeDO::getCreateTime, reqVO.getCreateTime()) 22 .betweenIfPresent(IotDataBridgeDO::getCreateTime, reqVO.getCreateTime())
26 .orderByDesc(IotDataBridgeDO::getId)); 23 .orderByDesc(IotDataBridgeDO::getId));
27 } 24 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotDataBridgeExecute.java
1 package cn.iocoder.yudao.module.iot.service.rule.action.databridge; 1 package cn.iocoder.yudao.module.iot.service.rule.action.databridge;
2 2
3 -import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeAbstractConfig;  
4 import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO; 3 import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO;
5 import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage; 4 import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
6 5
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotKafkaMQDataBridgeExecute.java
1 package cn.iocoder.yudao.module.iot.service.rule.action.databridge; 1 package cn.iocoder.yudao.module.iot.service.rule.action.databridge;
2 2
3 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeKafkaMQConfig; 3 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeKafkaMQConfig;
4 -import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO;  
5 import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeTypeEnum; 4 import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeTypeEnum;
6 import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage; 5 import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
7 import lombok.extern.slf4j.Slf4j; 6 import lombok.extern.slf4j.Slf4j;
@@ -13,7 +12,6 @@ import org.springframework.kafka.core.KafkaTemplate; @@ -13,7 +12,6 @@ import org.springframework.kafka.core.KafkaTemplate;
13 import org.springframework.stereotype.Component; 12 import org.springframework.stereotype.Component;
14 13
15 import java.time.Duration; 14 import java.time.Duration;
16 -import java.time.LocalDateTime;  
17 import java.util.HashMap; 15 import java.util.HashMap;
18 import java.util.Map; 16 import java.util.Map;
19 import java.util.concurrent.TimeUnit; 17 import java.util.concurrent.TimeUnit;
@@ -77,38 +75,4 @@ public class IotKafkaMQDataBridgeExecute extends @@ -77,38 +75,4 @@ public class IotKafkaMQDataBridgeExecute extends
77 producer.destroy(); 75 producer.destroy();
78 } 76 }
79 77
80 - // TODO @芋艿:测试代码,后续清理  
81 - public static void main(String[] args) {  
82 - // 1. 创建一个共享的实例  
83 - IotKafkaMQDataBridgeExecute action = new IotKafkaMQDataBridgeExecute();  
84 -  
85 - // 2. 创建共享的配置  
86 - IotDataBridgeKafkaMQConfig config = new IotDataBridgeKafkaMQConfig();  
87 - config.setBootstrapServers("127.0.0.1:9092");  
88 - config.setTopic("test-topic");  
89 - config.setSsl(false);  
90 - config.setUsername(null);  
91 - config.setPassword(null);  
92 -  
93 - // 3. 创建共享的消息  
94 - IotDeviceMessage message = IotDeviceMessage.builder()  
95 - .requestId("TEST-001")  
96 - .productKey("testProduct")  
97 - .deviceName("testDevice")  
98 - .deviceKey("testDeviceKey")  
99 - .type("property")  
100 - .identifier("temperature")  
101 - .data("{\"value\": 60}")  
102 - .reportTime(LocalDateTime.now())  
103 - .tenantId(1L)  
104 - .build();  
105 -  
106 - // 4. 执行两次测试,验证缓存  
107 - log.info("[main][第一次执行,应该会创建新的 producer]");  
108 - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));  
109 -  
110 - log.info("[main][第二次执行,应该会复用缓存的 producer]");  
111 - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));  
112 - }  
113 -  
114 } 78 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotRabbitMQDataBridgeExecute.java
1 package cn.iocoder.yudao.module.iot.service.rule.action.databridge; 1 package cn.iocoder.yudao.module.iot.service.rule.action.databridge;
2 2
3 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeRabbitMQConfig; 3 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeRabbitMQConfig;
4 -import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO;  
5 import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeTypeEnum; 4 import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeTypeEnum;
6 import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage; 5 import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
7 import com.rabbitmq.client.Channel; 6 import com.rabbitmq.client.Channel;
@@ -12,7 +11,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -12,7 +11,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
12 import org.springframework.stereotype.Component; 11 import org.springframework.stereotype.Component;
13 12
14 import java.nio.charset.StandardCharsets; 13 import java.nio.charset.StandardCharsets;
15 -import java.time.LocalDateTime;  
16 14
17 /** 15 /**
18 * RabbitMQ 的 {@link IotDataBridgeExecute} 实现类 16 * RabbitMQ 的 {@link IotDataBridgeExecute} 实现类
@@ -76,42 +74,4 @@ public class IotRabbitMQDataBridgeExecute extends @@ -76,42 +74,4 @@ public class IotRabbitMQDataBridgeExecute extends
76 } 74 }
77 } 75 }
78 76
79 - // TODO @芋艿:测试代码,后续清理  
80 - public static void main(String[] args) {  
81 - // 1. 创建一个共享的实例  
82 - IotRabbitMQDataBridgeExecute action = new IotRabbitMQDataBridgeExecute();  
83 -  
84 - // 2. 创建共享的配置  
85 - IotDataBridgeRabbitMQConfig config = new IotDataBridgeRabbitMQConfig();  
86 - config.setHost("localhost");  
87 - config.setPort(5672);  
88 - config.setVirtualHost("/");  
89 - config.setUsername("admin");  
90 - config.setPassword("123456");  
91 - config.setExchange("test-exchange");  
92 - config.setRoutingKey("test-key");  
93 - config.setQueue("test-queue");  
94 -  
95 - // 3. 创建共享的消息  
96 - IotDeviceMessage message = IotDeviceMessage.builder()  
97 - .requestId("TEST-001")  
98 - .productKey("testProduct")  
99 - .deviceName("testDevice")  
100 - .deviceKey("testDeviceKey")  
101 - .type("property")  
102 - .identifier("temperature")  
103 - .data("{\"value\": 60}")  
104 - .reportTime(LocalDateTime.now())  
105 - .tenantId(1L)  
106 - .build();  
107 -  
108 - // 4. 执行两次测试,验证缓存  
109 - // 4. 执行两次测试,验证缓存  
110 - log.info("[main][第一次执行,应该会创建新的 producer]");  
111 - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));  
112 -  
113 - log.info("[main][第二次执行,应该会复用缓存的 producer]");  
114 - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));  
115 - }  
116 -  
117 } 77 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotRedisStreamMQDataBridgeExecute.java
@@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.iot.service.rule.action.databridge; @@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.iot.service.rule.action.databridge;
3 import cn.hutool.core.util.ReflectUtil; 3 import cn.hutool.core.util.ReflectUtil;
4 import cn.hutool.core.util.StrUtil; 4 import cn.hutool.core.util.StrUtil;
5 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeRedisStreamMQConfig; 5 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeRedisStreamMQConfig;
6 -import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO;  
7 import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeTypeEnum; 6 import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeTypeEnum;
8 import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage; 7 import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
9 import com.fasterxml.jackson.databind.ObjectMapper; 8 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -21,8 +20,6 @@ import org.springframework.data.redis.core.RedisTemplate; @@ -21,8 +20,6 @@ import org.springframework.data.redis.core.RedisTemplate;
21 import org.springframework.data.redis.serializer.RedisSerializer; 20 import org.springframework.data.redis.serializer.RedisSerializer;
22 import org.springframework.stereotype.Component; 21 import org.springframework.stereotype.Component;
23 22
24 -import java.time.LocalDateTime;  
25 -  
26 /** 23 /**
27 * Redis Stream MQ 的 {@link IotDataBridgeExecute} 实现类 24 * Redis Stream MQ 的 {@link IotDataBridgeExecute} 实现类
28 * 25 *
@@ -96,38 +93,4 @@ public class IotRedisStreamMQDataBridgeExecute extends @@ -96,38 +93,4 @@ public class IotRedisStreamMQDataBridgeExecute extends
96 return json; 93 return json;
97 } 94 }
98 95
99 - // TODO @芋艿:测试代码,后续清理  
100 - public static void main(String[] args) {  
101 - // 1. 创建一个共享的实例  
102 - IotRedisStreamMQDataBridgeExecute action = new IotRedisStreamMQDataBridgeExecute();  
103 -  
104 - // 2. 创建共享的配置  
105 - IotDataBridgeRedisStreamMQConfig config = new IotDataBridgeRedisStreamMQConfig();  
106 - config.setHost("127.0.0.1");  
107 - config.setPort(6379);  
108 - config.setDatabase(0);  
109 - config.setPassword("123456");  
110 - config.setTopic("test-stream");  
111 -  
112 - // 3. 创建共享的消息  
113 - IotDeviceMessage message = IotDeviceMessage.builder()  
114 - .requestId("TEST-001")  
115 - .productKey("testProduct")  
116 - .deviceName("testDevice")  
117 - .deviceKey("testDeviceKey")  
118 - .type("property")  
119 - .identifier("temperature")  
120 - .data("{\"value\": 60}")  
121 - .reportTime(LocalDateTime.now())  
122 - .tenantId(1L)  
123 - .build();  
124 -  
125 - // 4. 执行两次测试,验证缓存  
126 - log.info("[main][第一次执行,应该会创建新的 producer]");  
127 - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));  
128 -  
129 - log.info("[main][第二次执行,应该会复用缓存的 producer]");  
130 - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));  
131 - }  
132 -  
133 -}  
134 \ No newline at end of file 96 \ No newline at end of file
  97 +}
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotRocketMQDataBridgeExecute.java
1 package cn.iocoder.yudao.module.iot.service.rule.action.databridge; 1 package cn.iocoder.yudao.module.iot.service.rule.action.databridge;
2 2
3 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeRocketMQConfig; 3 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeRocketMQConfig;
4 -import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO;  
5 import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeTypeEnum; 4 import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeTypeEnum;
6 import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage; 5 import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
7 import lombok.extern.slf4j.Slf4j; 6 import lombok.extern.slf4j.Slf4j;
@@ -13,8 +12,6 @@ import org.apache.rocketmq.remoting.common.RemotingHelper; @@ -13,8 +12,6 @@ import org.apache.rocketmq.remoting.common.RemotingHelper;
13 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 12 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
14 import org.springframework.stereotype.Component; 13 import org.springframework.stereotype.Component;
15 14
16 -import java.time.LocalDateTime;  
17 -  
18 /** 15 /**
19 * RocketMQ 的 {@link IotDataBridgeExecute} 实现类 16 * RocketMQ 的 {@link IotDataBridgeExecute} 实现类
20 * 17 *
@@ -65,38 +62,4 @@ public class IotRocketMQDataBridgeExecute extends @@ -65,38 +62,4 @@ public class IotRocketMQDataBridgeExecute extends
65 producer.shutdown(); 62 producer.shutdown();
66 } 63 }
67 64
68 - // TODO @芋艿:测试代码,后续清理  
69 - // TODO @puhui999:搞到测试类里。  
70 - public static void main(String[] args) {  
71 - // 1. 创建一个共享的实例  
72 - IotRocketMQDataBridgeExecute action = new IotRocketMQDataBridgeExecute();  
73 -  
74 - // 2. 创建共享的配置  
75 - IotDataBridgeRocketMQConfig config = new IotDataBridgeRocketMQConfig();  
76 - config.setNameServer("127.0.0.1:9876");  
77 - config.setGroup("test-group");  
78 - config.setTopic("test-topic");  
79 - config.setTags("test-tag");  
80 -  
81 - // 3. 创建共享的消息  
82 - IotDeviceMessage message = IotDeviceMessage.builder()  
83 - .requestId("TEST-001")  
84 - .productKey("testProduct")  
85 - .deviceName("testDevice")  
86 - .deviceKey("testDeviceKey")  
87 - .type("property")  
88 - .identifier("temperature")  
89 - .data("{\"value\": 60}")  
90 - .reportTime(LocalDateTime.now())  
91 - .tenantId(1L)  
92 - .build();  
93 -  
94 - // 4. 执行两次测试,验证缓存  
95 - log.info("[main][第一次执行,应该会创建新的 producer]");  
96 - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));  
97 -  
98 - log.info("[main][第二次执行,应该会复用缓存的 producer]");  
99 - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));  
100 - }  
101 -  
102 } 65 }
yudao-module-iot/yudao-module-iot-biz/src/test/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotDataBridgeExecuteTest.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.service.rule.action.databridge;
  2 +
  3 +import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
  4 +import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.*;
  5 +import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO;
  6 +import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
  7 +import lombok.extern.slf4j.Slf4j;
  8 +import org.junit.jupiter.api.BeforeEach;
  9 +import org.junit.jupiter.api.Disabled;
  10 +import org.junit.jupiter.api.Test;
  11 +import org.mockito.InjectMocks;
  12 +import org.mockito.Mock;
  13 +import org.mockito.Mockito;
  14 +import org.springframework.http.HttpMethod;
  15 +import org.springframework.http.HttpStatus;
  16 +import org.springframework.http.ResponseEntity;
  17 +import org.springframework.web.client.RestTemplate;
  18 +
  19 +import java.time.LocalDateTime;
  20 +
  21 +import static org.mockito.ArgumentMatchers.any;
  22 +import static org.mockito.ArgumentMatchers.anyString;
  23 +
  24 +/**
  25 + * {@link IotDataBridgeExecute} 实现类的测试
  26 + *
  27 + * @author HUIHUI
  28 + */
  29 +@Disabled // 默认禁用,需要手动启用测试
  30 +@Slf4j
  31 +public class IotDataBridgeExecuteTest extends BaseMockitoUnitTest {
  32 +
  33 + private IotDeviceMessage message;
  34 +
  35 + @Mock
  36 + private RestTemplate restTemplate;
  37 +
  38 + @InjectMocks
  39 + private IotHttpDataBridgeExecute httpDataBridgeExecute;
  40 +
  41 + @BeforeEach
  42 + public void setUp() {
  43 + // 创建共享的测试消息
  44 + message = IotDeviceMessage.builder()
  45 + .requestId("TEST-001")
  46 + .productKey("testProduct")
  47 + .deviceName("testDevice")
  48 + .deviceKey("testDeviceKey")
  49 + .type("property")
  50 + .identifier("temperature")
  51 + .data("{\"value\": 60}")
  52 + .reportTime(LocalDateTime.now())
  53 + .tenantId(1L)
  54 + .build();
  55 +
  56 + // 配置 RestTemplate mock 返回成功响应
  57 + Mockito.when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(), any(Class.class)))
  58 + .thenReturn(new ResponseEntity<>("Success", HttpStatus.OK));
  59 + }
  60 +
  61 + @Test
  62 + public void testKafkaMQDataBridge() {
  63 + // 1. 创建执行器实例
  64 + IotKafkaMQDataBridgeExecute action = new IotKafkaMQDataBridgeExecute();
  65 +
  66 + // 2. 创建配置
  67 + IotDataBridgeKafkaMQConfig config = new IotDataBridgeKafkaMQConfig();
  68 + config.setBootstrapServers("127.0.0.1:9092");
  69 + config.setTopic("test-topic");
  70 + config.setSsl(false);
  71 + config.setUsername(null);
  72 + config.setPassword(null);
  73 +
  74 + // 3. 执行两次测试,验证缓存
  75 + log.info("[testKafkaMQDataBridge][第一次执行,应该会创建新的 producer]");
  76 + action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  77 +
  78 + log.info("[testKafkaMQDataBridge][第二次执行,应该会复用缓存的 producer]");
  79 + action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  80 + }
  81 +
  82 + @Test
  83 + public void testRabbitMQDataBridge() {
  84 + // 1. 创建执行器实例
  85 + IotRabbitMQDataBridgeExecute action = new IotRabbitMQDataBridgeExecute();
  86 +
  87 + // 2. 创建配置
  88 + IotDataBridgeRabbitMQConfig config = new IotDataBridgeRabbitMQConfig();
  89 + config.setHost("localhost");
  90 + config.setPort(5672);
  91 + config.setVirtualHost("/");
  92 + config.setUsername("admin");
  93 + config.setPassword("123456");
  94 + config.setExchange("test-exchange");
  95 + config.setRoutingKey("test-key");
  96 + config.setQueue("test-queue");
  97 +
  98 + // 3. 执行两次测试,验证缓存
  99 + log.info("[testRabbitMQDataBridge][第一次执行,应该会创建新的 producer]");
  100 + action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  101 +
  102 + log.info("[testRabbitMQDataBridge][第二次执行,应该会复用缓存的 producer]");
  103 + action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  104 + }
  105 +
  106 + @Test
  107 + public void testRedisStreamMQDataBridge() {
  108 + // 1. 创建执行器实例
  109 + IotRedisStreamMQDataBridgeExecute action = new IotRedisStreamMQDataBridgeExecute();
  110 +
  111 + // 2. 创建配置
  112 + IotDataBridgeRedisStreamMQConfig config = new IotDataBridgeRedisStreamMQConfig();
  113 + config.setHost("127.0.0.1");
  114 + config.setPort(6379);
  115 + config.setDatabase(0);
  116 + config.setPassword("123456");
  117 + config.setTopic("test-stream");
  118 +
  119 + // 3. 执行两次测试,验证缓存
  120 + log.info("[testRedisStreamMQDataBridge][第一次执行,应该会创建新的 producer]");
  121 + action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  122 +
  123 + log.info("[testRedisStreamMQDataBridge][第二次执行,应该会复用缓存的 producer]");
  124 + action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  125 + }
  126 +
  127 + @Test
  128 + public void testRocketMQDataBridge() {
  129 + // 1. 创建执行器实例
  130 + IotRocketMQDataBridgeExecute action = new IotRocketMQDataBridgeExecute();
  131 +
  132 + // 2. 创建配置
  133 + IotDataBridgeRocketMQConfig config = new IotDataBridgeRocketMQConfig();
  134 + config.setNameServer("127.0.0.1:9876");
  135 + config.setGroup("test-group");
  136 + config.setTopic("test-topic");
  137 + config.setTags("test-tag");
  138 +
  139 + // 3. 执行两次测试,验证缓存
  140 + log.info("[testRocketMQDataBridge][第一次执行,应该会创建新的 producer]");
  141 + action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  142 +
  143 + log.info("[testRocketMQDataBridge][第二次执行,应该会复用缓存的 producer]");
  144 + action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  145 + }
  146 +
  147 + @Test
  148 + public void testHttpDataBridge() throws Exception {
  149 + // 创建配置
  150 + IotDataBridgeHttpConfig config = new IotDataBridgeHttpConfig();
  151 + config.setUrl("https://doc.iocoder.cn/");
  152 + config.setMethod(HttpMethod.GET.name());
  153 +
  154 + // 执行测试
  155 + log.info("[testHttpDataBridge][执行HTTP数据桥接测试]");
  156 + httpDataBridgeExecute.execute(message, new IotDataBridgeDO().setType(httpDataBridgeExecute.getType()).setConfig(config));
  157 + }
  158 +
  159 +}
yudao-server/pom.xml
@@ -115,19 +115,18 @@ @@ -115,19 +115,18 @@
115 <version>${revision}</version> 115 <version>${revision}</version>
116 </dependency> 116 </dependency>
117 <!-- IoT 数据桥梁的执行器所需消息队列。如果您只需要使用 rocketmq 那么则注释掉其它消息队列即可 --> 117 <!-- IoT 数据桥梁的执行器所需消息队列。如果您只需要使用 rocketmq 那么则注释掉其它消息队列即可 -->
118 - <!-- TODO @puhui999:默认不使用哈。可以在 iot biz 那,作为 optional 进行引入 -->  
119 - <dependency>  
120 - <groupId>org.apache.rocketmq</groupId>  
121 - <artifactId>rocketmq-spring-boot-starter</artifactId>  
122 - </dependency>  
123 - <dependency>  
124 - <groupId>org.springframework.kafka</groupId>  
125 - <artifactId>spring-kafka</artifactId>  
126 - </dependency>  
127 - <dependency>  
128 - <groupId>org.springframework.boot</groupId>  
129 - <artifactId>spring-boot-starter-amqp</artifactId>  
130 - </dependency> 118 + <!-- <dependency>-->
  119 + <!-- <groupId>org.apache.rocketmq</groupId>-->
  120 + <!-- <artifactId>rocketmq-spring-boot-starter</artifactId>-->
  121 + <!-- </dependency>-->
  122 + <!-- <dependency>-->
  123 + <!-- <groupId>org.springframework.kafka</groupId>-->
  124 + <!-- <artifactId>spring-kafka</artifactId>-->
  125 + <!-- </dependency>-->
  126 + <!-- <dependency>-->
  127 + <!-- <groupId>org.springframework.boot</groupId>-->
  128 + <!-- <artifactId>spring-boot-starter-amqp</artifactId>-->
  129 + <!-- </dependency>-->
131 130
132 <!-- spring boot 配置所需依赖 --> 131 <!-- spring boot 配置所需依赖 -->
133 <dependency> 132 <dependency>