Commit 252366781d38d5e053b000f10c99b88681f0b53e

Authored by YunaiV
1 parent a74459e9

【代码优化】IoT:优化插件 common 和 http 的配置类

Showing 16 changed files with 142 additions and 117 deletions
yudao-dependencies/pom.xml
@@ -600,39 +600,42 @@ @@ -600,39 +600,42 @@
600 </exclusions> 600 </exclusions>
601 </dependency> 601 </dependency>
602 602
603 - <!-- MQTT -->  
604 - <dependency>  
605 - <groupId>org.eclipse.paho</groupId>  
606 - <artifactId>org.eclipse.paho.client.mqttv3</artifactId>  
607 - <version>${mqtt.version}</version>  
608 - </dependency>  
609 -  
610 <!-- PF4J --> 603 <!-- PF4J -->
611 <dependency> 604 <dependency>
612 <groupId>org.pf4j</groupId> 605 <groupId>org.pf4j</groupId>
613 <artifactId>pf4j-spring</artifactId> 606 <artifactId>pf4j-spring</artifactId>
614 <version>${pf4j-spring.version}</version> 607 <version>${pf4j-spring.version}</version>
  608 + <exclusions>
  609 + <exclusion>
  610 + <groupId>org.slf4j</groupId>
  611 + <artifactId>slf4j-log4j12</artifactId>
  612 + </exclusion>
  613 + </exclusions>
615 </dependency> 614 </dependency>
616 615
617 - <!-- Vert.x 核心依赖 --> 616 + <!-- Vert.x -->
618 <dependency> 617 <dependency>
619 <groupId>io.vertx</groupId> 618 <groupId>io.vertx</groupId>
620 <artifactId>vertx-core</artifactId> 619 <artifactId>vertx-core</artifactId>
621 <version>${vertx.version}</version> 620 <version>${vertx.version}</version>
622 </dependency> 621 </dependency>
623 - <!-- Vert.x Web 模块 -->  
624 <dependency> 622 <dependency>
625 <groupId>io.vertx</groupId> 623 <groupId>io.vertx</groupId>
626 <artifactId>vertx-web</artifactId> 624 <artifactId>vertx-web</artifactId>
627 <version>${vertx.version}</version> 625 <version>${vertx.version}</version>
628 </dependency> 626 </dependency>
629 - <!-- Vert.x MQTT 模块 -->  
630 <dependency> 627 <dependency>
631 <groupId>io.vertx</groupId> 628 <groupId>io.vertx</groupId>
632 <artifactId>vertx-mqtt</artifactId> 629 <artifactId>vertx-mqtt</artifactId>
633 <version>${vertx.version}</version> 630 <version>${vertx.version}</version>
634 </dependency> 631 </dependency>
635 632
  633 + <!-- MQTT -->
  634 + <dependency>
  635 + <groupId>org.eclipse.paho</groupId>
  636 + <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
  637 + <version>${mqtt.version}</version>
  638 + </dependency>
636 </dependencies> 639 </dependencies>
637 </dependencyManagement> 640 </dependencyManagement>
638 641
yudao-framework/yudao-spring-boot-starter-mybatis/pom.xml
@@ -63,10 +63,6 @@ @@ -63,10 +63,6 @@
63 <artifactId>opengauss-jdbc</artifactId> 63 <artifactId>opengauss-jdbc</artifactId>
64 <optional>true</optional> 64 <optional>true</optional>
65 </dependency> 65 </dependency>
66 - <dependency>  
67 - <groupId>com.taosdata.jdbc</groupId>  
68 - <artifactId>taos-jdbcdriver</artifactId>  
69 - </dependency>  
70 66
71 <dependency> 67 <dependency>
72 <groupId>com.alibaba</groupId> 68 <groupId>com.alibaba</groupId>
yudao-module-iot/yudao-module-iot-api/pom.xml
@@ -40,13 +40,6 @@ @@ -40,13 +40,6 @@
40 <dependency> 40 <dependency>
41 <groupId>org.pf4j</groupId> <!-- PF4J:内置插件机制 --> 41 <groupId>org.pf4j</groupId> <!-- PF4J:内置插件机制 -->
42 <artifactId>pf4j-spring</artifactId> 42 <artifactId>pf4j-spring</artifactId>
43 - <!-- TODO @芋艿:可以放到 bom 里配置 -->  
44 - <exclusions>  
45 - <exclusion>  
46 - <groupId>org.slf4j</groupId>  
47 - <artifactId>slf4j-log4j12</artifactId>  
48 - </exclusion>  
49 - </exclusions>  
50 </dependency> 43 </dependency>
51 44
52 <!-- 参数校验 --> 45 <!-- 参数校验 -->
yudao-module-iot/yudao-module-iot-biz/pom.xml
@@ -52,6 +52,11 @@ @@ -52,6 +52,11 @@
52 <artifactId>yudao-spring-boot-starter-redis</artifactId> 52 <artifactId>yudao-spring-boot-starter-redis</artifactId>
53 </dependency> 53 </dependency>
54 54
  55 + <dependency>
  56 + <groupId>com.taosdata.jdbc</groupId>
  57 + <artifactId>taos-jdbcdriver</artifactId>
  58 + </dependency>
  59 +
55 <!-- Test 测试相关 --> 60 <!-- Test 测试相关 -->
56 <dependency> 61 <dependency>
57 <groupId>cn.iocoder.boot</groupId> 62 <groupId>cn.iocoder.boot</groupId>
@@ -64,12 +69,13 @@ @@ -64,12 +69,13 @@
64 <artifactId>yudao-spring-boot-starter-excel</artifactId> 69 <artifactId>yudao-spring-boot-starter-excel</artifactId>
65 </dependency> 70 </dependency>
66 71
67 - <!-- 工具类相关 --> 72 + <!-- TODO @haohao:貌似不需要这个? -->
68 <dependency> 73 <dependency>
69 <groupId>io.vertx</groupId> 74 <groupId>io.vertx</groupId>
70 <artifactId>vertx-web</artifactId> 75 <artifactId>vertx-web</artifactId>
71 </dependency> 76 </dependency>
72 77
  78 + <!-- TODO @haohao:貌似 biz 模块,不需要 MQTT? -->
73 <dependency> 79 <dependency>
74 <groupId>org.eclipse.paho</groupId> <!-- MQTT --> 80 <groupId>org.eclipse.paho</groupId> <!-- MQTT -->
75 <artifactId>org.eclipse.paho.client.mqttv3</artifactId> 81 <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
@@ -78,13 +84,6 @@ @@ -78,13 +84,6 @@
78 <dependency> 84 <dependency>
79 <groupId>org.pf4j</groupId> <!-- PF4J:内置插件机制 --> 85 <groupId>org.pf4j</groupId> <!-- PF4J:内置插件机制 -->
80 <artifactId>pf4j-spring</artifactId> 86 <artifactId>pf4j-spring</artifactId>
81 - <!-- TODO @芋艿:可以放到 bom 里配置 -->  
82 - <exclusions>  
83 - <exclusion>  
84 - <groupId>org.slf4j</groupId>  
85 - <artifactId>slf4j-log4j12</artifactId>  
86 - </exclusion>  
87 - </exclusions>  
88 </dependency> 87 </dependency>
89 88
90 </dependencies> 89 </dependencies>
yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/plugininfo/PluginInfoMapper.xml deleted
1 -<?xml version="1.0" encoding="UTF-8"?>  
2 -<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
3 -<mapper namespace="cn.iocoder.yudao.module.iot.dal.mysql.plugininfo.PluginInfoMapper">  
4 -  
5 - <!--  
6 - 一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。  
7 - 无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。  
8 - 代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。  
9 - 文档可见:https://www.iocoder.cn/MyBatis/x-plugins/  
10 - -->  
11 -  
12 -</mapper>  
13 \ No newline at end of file 0 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/plugininstance/PluginInstanceMapper.xml deleted
1 -<?xml version="1.0" encoding="UTF-8"?>  
2 -<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
3 -<mapper namespace="cn.iocoder.yudao.module.iot.dal.mysql.plugin.IotPluginInstanceMapper">  
4 -  
5 - <!--  
6 - 一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。  
7 - 无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。  
8 - 代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。  
9 - 文档可见:https://www.iocoder.cn/MyBatis/x-plugins/  
10 - -->  
11 -  
12 -</mapper>  
13 \ No newline at end of file 0 \ No newline at end of file
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/pom.xml
@@ -40,6 +40,13 @@ @@ -40,6 +40,13 @@
40 <groupId>io.vertx</groupId> 40 <groupId>io.vertx</groupId>
41 <artifactId>vertx-web</artifactId> 41 <artifactId>vertx-web</artifactId>
42 </dependency> 42 </dependency>
  43 +
  44 + <!-- 参数校验 -->
  45 + <dependency>
  46 + <groupId>org.springframework.boot</groupId>
  47 + <artifactId>spring-boot-starter-validation</artifactId>
  48 + <optional>true</optional>
  49 + </dependency>
43 </dependencies> 50 </dependencies>
44 51
45 </project> 52 </project>
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/java/cn/iocoder/yudao/module/iot/plugin/common/config/IotPluginCommonAutoConfiguration.java
@@ -3,59 +3,43 @@ package cn.iocoder.yudao.module.iot.plugin.common.config; @@ -3,59 +3,43 @@ package cn.iocoder.yudao.module.iot.plugin.common.config;
3 import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi; 3 import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi;
4 import cn.iocoder.yudao.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler; 4 import cn.iocoder.yudao.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler;
5 import cn.iocoder.yudao.module.iot.plugin.common.downstream.IotDeviceDownstreamServer; 5 import cn.iocoder.yudao.module.iot.plugin.common.downstream.IotDeviceDownstreamServer;
6 -import cn.iocoder.yudao.module.iot.plugin.common.heartbeta.IotPluginInstanceHeartbeatJob; 6 +import cn.iocoder.yudao.module.iot.plugin.common.heartbeat.IotPluginInstanceHeartbeatJob;
7 import cn.iocoder.yudao.module.iot.plugin.common.upstream.IotDeviceUpstreamClient; 7 import cn.iocoder.yudao.module.iot.plugin.common.upstream.IotDeviceUpstreamClient;
8 -import org.springframework.beans.factory.annotation.Value;  
9 import org.springframework.boot.autoconfigure.AutoConfiguration; 8 import org.springframework.boot.autoconfigure.AutoConfiguration;
  9 +import org.springframework.boot.context.properties.EnableConfigurationProperties;
10 import org.springframework.boot.web.client.RestTemplateBuilder; 10 import org.springframework.boot.web.client.RestTemplateBuilder;
11 import org.springframework.context.annotation.Bean; 11 import org.springframework.context.annotation.Bean;
12 import org.springframework.scheduling.annotation.EnableScheduling; 12 import org.springframework.scheduling.annotation.EnableScheduling;
13 import org.springframework.web.client.RestTemplate; 13 import org.springframework.web.client.RestTemplate;
14 14
15 -import java.time.Duration;  
16 -  
17 /** 15 /**
18 * IoT 插件的通用自动配置类 16 * IoT 插件的通用自动配置类
19 * 17 *
20 * @author haohao 18 * @author haohao
21 */ 19 */
22 @AutoConfiguration 20 @AutoConfiguration
  21 +@EnableConfigurationProperties(IotPluginCommonProperties.class)
23 @EnableScheduling // 开启定时任务,因为 IotPluginInstanceHeartbeatJob 是一个定时任务 22 @EnableScheduling // 开启定时任务,因为 IotPluginInstanceHeartbeatJob 是一个定时任务
24 public class IotPluginCommonAutoConfiguration { 23 public class IotPluginCommonAutoConfiguration {
25 24
26 - // TODO @haohao:这个要不搞个配置类哈  
27 - @Value("${iot.device-data.url}")  
28 - private String deviceDataUrl;  
29 -  
30 - /**  
31 - * 创建 RestTemplate 实例  
32 - *  
33 - * @return RestTemplate 实例  
34 - */  
35 @Bean 25 @Bean
36 - public RestTemplate restTemplate() {  
37 - // 如果你有更多的自定义需求,比如连接池、超时时间等,可以在这里设置 26 + public RestTemplate restTemplate(IotPluginCommonProperties properties) {
38 return new RestTemplateBuilder() 27 return new RestTemplateBuilder()
39 - .connectTimeout(Duration.ofMillis(5000)) // 设置连接超时时间  
40 - .readTimeout(Duration.ofMillis(5000)) // 设置读取超时时间 28 + .connectTimeout(properties.getUpstreamConnectTimeout())
  29 + .readTimeout(properties.getUpstreamReadTimeout())
41 .build(); 30 .build();
42 } 31 }
43 32
44 - /**  
45 - * 创建 DeviceDataApi 实例  
46 - *  
47 - * @param restTemplate RestTemplate 实例  
48 - * @return DeviceDataApi 实例  
49 - */  
50 @Bean 33 @Bean
51 - public IotDeviceUpstreamApi deviceDataApi(RestTemplate restTemplate) {  
52 - return new IotDeviceUpstreamClient(restTemplate, deviceDataUrl); 34 + public IotDeviceUpstreamApi deviceUpstreamApi(IotPluginCommonProperties properties,
  35 + RestTemplate restTemplate) {
  36 + return new IotDeviceUpstreamClient(properties, restTemplate);
53 } 37 }
54 38
55 @Bean(initMethod = "start", destroyMethod = "stop") 39 @Bean(initMethod = "start", destroyMethod = "stop")
56 - @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")  
57 - public IotDeviceDownstreamServer deviceDownstreamServer(IotDeviceDownstreamHandler deviceDownstreamHandler) {  
58 - return new IotDeviceDownstreamServer(deviceDownstreamHandler); 40 + public IotDeviceDownstreamServer deviceDownstreamServer(IotPluginCommonProperties properties,
  41 + IotDeviceDownstreamHandler deviceDownstreamHandler) {
  42 + return new IotDeviceDownstreamServer(properties, deviceDownstreamHandler);
59 } 43 }
60 44
61 @Bean(initMethod = "init", destroyMethod = "stop") 45 @Bean(initMethod = "init", destroyMethod = "stop")
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/java/cn/iocoder/yudao/module/iot/plugin/common/config/IotPluginCommonProperties.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.plugin.common.config;
  2 +
  3 +import jakarta.validation.constraints.NotEmpty;
  4 +import lombok.Data;
  5 +import org.springframework.boot.context.properties.ConfigurationProperties;
  6 +import org.springframework.validation.annotation.Validated;
  7 +
  8 +import java.time.Duration;
  9 +
  10 +@ConfigurationProperties(prefix = "yudao.iot.plugin.common")
  11 +@Validated
  12 +@Data
  13 +public class IotPluginCommonProperties {
  14 +
  15 + /**
  16 + * 上行连接超时的默认值
  17 + */
  18 + public static final Duration UPSTREAM_CONNECT_TIMEOUT_DEFAULT = Duration.ofSeconds(30);
  19 + /**
  20 + * 上行读取超时的默认值
  21 + */
  22 + public static final Duration UPSTREAM_READ_TIMEOUT_DEFAULT = Duration.ofSeconds(30);
  23 +
  24 + /**
  25 + * 下行端口 - 随机
  26 + */
  27 + public static final Integer DOWNSTREAM_PORT_RANDOM = 0;
  28 +
  29 + /**
  30 + * 上行 URL
  31 + */
  32 + @NotEmpty(message = "上行 URL 不能为空")
  33 + private String upstreamUrl;
  34 + /**
  35 + * 上行连接超时
  36 + */
  37 + private Duration upstreamConnectTimeout = UPSTREAM_CONNECT_TIMEOUT_DEFAULT;
  38 + /**
  39 + * 上行读取超时
  40 + */
  41 + private Duration upstreamReadTimeout = UPSTREAM_READ_TIMEOUT_DEFAULT;
  42 +
  43 + /**
  44 + * 下行端口
  45 + */
  46 + private Integer downstreamPort = DOWNSTREAM_PORT_RANDOM;
  47 +
  48 +}
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/java/cn/iocoder/yudao/module/iot/plugin/common/downstream/IotDeviceDownstreamServer.java
1 package cn.iocoder.yudao.module.iot.plugin.common.downstream; 1 package cn.iocoder.yudao.module.iot.plugin.common.downstream;
2 2
  3 +import cn.iocoder.yudao.module.iot.plugin.common.config.IotPluginCommonProperties;
3 import cn.iocoder.yudao.module.iot.plugin.common.downstream.router.IotDevicePropertyGetVertxHandler; 4 import cn.iocoder.yudao.module.iot.plugin.common.downstream.router.IotDevicePropertyGetVertxHandler;
4 import cn.iocoder.yudao.module.iot.plugin.common.downstream.router.IotDevicePropertySetVertxHandler; 5 import cn.iocoder.yudao.module.iot.plugin.common.downstream.router.IotDevicePropertySetVertxHandler;
5 import cn.iocoder.yudao.module.iot.plugin.common.downstream.router.IotDeviceServiceInvokeVertxHandler; 6 import cn.iocoder.yudao.module.iot.plugin.common.downstream.router.IotDeviceServiceInvokeVertxHandler;
@@ -19,8 +20,11 @@ public class IotDeviceDownstreamServer { @@ -19,8 +20,11 @@ public class IotDeviceDownstreamServer {
19 20
20 private final Vertx vertx; 21 private final Vertx vertx;
21 private final HttpServer server; 22 private final HttpServer server;
  23 + private final IotPluginCommonProperties properties;
22 24
23 - public IotDeviceDownstreamServer(IotDeviceDownstreamHandler deviceDownstreamHandler) { 25 + public IotDeviceDownstreamServer(IotPluginCommonProperties properties,
  26 + IotDeviceDownstreamHandler deviceDownstreamHandler) {
  27 + this.properties = properties;
24 // 创建 Vertx 实例 28 // 创建 Vertx 实例
25 this.vertx = Vertx.vertx(); 29 this.vertx = Vertx.vertx();
26 // 创建 Router 实例 30 // 创建 Router 实例
@@ -41,7 +45,7 @@ public class IotDeviceDownstreamServer { @@ -41,7 +45,7 @@ public class IotDeviceDownstreamServer {
41 */ 45 */
42 public void start() { 46 public void start() {
43 log.info("[start][开始启动]"); 47 log.info("[start][开始启动]");
44 - server.listen(0) // 通过 0 自动选择端口 48 + server.listen(properties.getDownstreamPort())
45 .toCompletionStage() 49 .toCompletionStage()
46 .toCompletableFuture() 50 .toCompletableFuture()
47 .join(); 51 .join();
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/java/cn/iocoder/yudao/module/iot/plugin/common/heartbeta/IotPluginInstanceHeartbeatJob.java renamed to yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/java/cn/iocoder/yudao/module/iot/plugin/common/heartbeat/IotPluginInstanceHeartbeatJob.java
1 -package cn.iocoder.yudao.module.iot.plugin.common.heartbeta; 1 +package cn.iocoder.yudao.module.iot.plugin.common.heartbeat;
2 2
3 import cn.hutool.system.SystemUtil; 3 import cn.hutool.system.SystemUtil;
4 import cn.iocoder.yudao.framework.common.pojo.CommonResult; 4 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
@@ -7,6 +7,7 @@ import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotPluginInst @@ -7,6 +7,7 @@ import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotPluginInst
7 import cn.iocoder.yudao.module.iot.plugin.common.downstream.IotDeviceDownstreamServer; 7 import cn.iocoder.yudao.module.iot.plugin.common.downstream.IotDeviceDownstreamServer;
8 import cn.iocoder.yudao.module.iot.plugin.common.util.IotPluginCommonUtils; 8 import cn.iocoder.yudao.module.iot.plugin.common.util.IotPluginCommonUtils;
9 import lombok.RequiredArgsConstructor; 9 import lombok.RequiredArgsConstructor;
  10 +import lombok.extern.slf4j.Slf4j;
10 import org.springframework.scheduling.annotation.Scheduled; 11 import org.springframework.scheduling.annotation.Scheduled;
11 12
12 import java.util.concurrent.TimeUnit; 13 import java.util.concurrent.TimeUnit;
@@ -17,6 +18,7 @@ import java.util.concurrent.TimeUnit; @@ -17,6 +18,7 @@ import java.util.concurrent.TimeUnit;
17 * 用于定时发送心跳给服务端 18 * 用于定时发送心跳给服务端
18 */ 19 */
19 @RequiredArgsConstructor 20 @RequiredArgsConstructor
  21 +@Slf4j
20 public class IotPluginInstanceHeartbeatJob { 22 public class IotPluginInstanceHeartbeatJob {
21 23
22 private final IotDeviceUpstreamApi deviceUpstreamApi; 24 private final IotDeviceUpstreamApi deviceUpstreamApi;
@@ -24,22 +26,22 @@ public class IotPluginInstanceHeartbeatJob { @@ -24,22 +26,22 @@ public class IotPluginInstanceHeartbeatJob {
24 26
25 public void init() { 27 public void init() {
26 CommonResult<Boolean> result = deviceUpstreamApi.heartbeatPluginInstance(buildPluginInstanceHeartbeatReqDTO(true)); 28 CommonResult<Boolean> result = deviceUpstreamApi.heartbeatPluginInstance(buildPluginInstanceHeartbeatReqDTO(true));
27 - // TODO @芋艿:结果的处理 29 + log.info("[init][上线结果:{})]", result);
28 } 30 }
29 31
30 public void stop() { 32 public void stop() {
31 CommonResult<Boolean> result = deviceUpstreamApi.heartbeatPluginInstance(buildPluginInstanceHeartbeatReqDTO(false)); 33 CommonResult<Boolean> result = deviceUpstreamApi.heartbeatPluginInstance(buildPluginInstanceHeartbeatReqDTO(false));
32 - // TODO @芋艿:结果的处理 34 + log.info("[stop][下线结果:{})]", result);
33 } 35 }
34 36
35 @Scheduled(initialDelay = 3, fixedRate = 3, timeUnit = TimeUnit.MINUTES) // 3 分钟执行一次 37 @Scheduled(initialDelay = 3, fixedRate = 3, timeUnit = TimeUnit.MINUTES) // 3 分钟执行一次
36 public void execute() { 38 public void execute() {
37 CommonResult<Boolean> result = deviceUpstreamApi.heartbeatPluginInstance(buildPluginInstanceHeartbeatReqDTO(true)); 39 CommonResult<Boolean> result = deviceUpstreamApi.heartbeatPluginInstance(buildPluginInstanceHeartbeatReqDTO(true));
38 - // TODO @芋艿:结果的处理 40 + log.info("[execute][心跳结果:{})]", result);
39 } 41 }
40 42
41 private IotPluginInstanceHeartbeatReqDTO buildPluginInstanceHeartbeatReqDTO(Boolean online) { 43 private IotPluginInstanceHeartbeatReqDTO buildPluginInstanceHeartbeatReqDTO(Boolean online) {
42 - // TODO @芋艿:pluginKey 的获取??? 44 + // TODO @haohao:pluginKey 的获取???
43 return new IotPluginInstanceHeartbeatReqDTO() 45 return new IotPluginInstanceHeartbeatReqDTO()
44 .setPluginKey("yudao-module-iot-plugin-http").setProcessId(IotPluginCommonUtils.getProcessId()) 46 .setPluginKey("yudao-module-iot-plugin-http").setProcessId(IotPluginCommonUtils.getProcessId())
45 .setHostIp(SystemUtil.getHostInfo().getAddress()).setDownstreamPort(deviceDownstreamServer.getPort()) 47 .setHostIp(SystemUtil.getHostInfo().getAddress()).setDownstreamPort(deviceDownstreamServer.getPort())
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/java/cn/iocoder/yudao/module/iot/plugin/common/upstream/IotDeviceUpstreamClient.java
@@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceEven @@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceEven
6 import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDevicePropertyReportReqDTO; 6 import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDevicePropertyReportReqDTO;
7 import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceStateUpdateReqDTO; 7 import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceStateUpdateReqDTO;
8 import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotPluginInstanceHeartbeatReqDTO; 8 import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotPluginInstanceHeartbeatReqDTO;
  9 +import cn.iocoder.yudao.module.iot.plugin.common.config.IotPluginCommonProperties;
9 import lombok.RequiredArgsConstructor; 10 import lombok.RequiredArgsConstructor;
10 import lombok.extern.slf4j.Slf4j; 11 import lombok.extern.slf4j.Slf4j;
11 import org.springframework.web.client.RestTemplate; 12 import org.springframework.web.client.RestTemplate;
@@ -25,32 +26,31 @@ public class IotDeviceUpstreamClient implements IotDeviceUpstreamApi { @@ -25,32 +26,31 @@ public class IotDeviceUpstreamClient implements IotDeviceUpstreamApi {
25 26
26 public static final String URL_PREFIX = "/rpc-api/iot/device/upstream"; 27 public static final String URL_PREFIX = "/rpc-api/iot/device/upstream";
27 28
28 - private final RestTemplate restTemplate; 29 + private final IotPluginCommonProperties properties;
29 30
30 - // TODO @芋艿:改个名字  
31 - private final String deviceDataUrl; 31 + private final RestTemplate restTemplate;
32 32
33 @Override 33 @Override
34 public CommonResult<Boolean> updateDeviceState(IotDeviceStateUpdateReqDTO updateReqDTO) { 34 public CommonResult<Boolean> updateDeviceState(IotDeviceStateUpdateReqDTO updateReqDTO) {
35 - String url = deviceDataUrl + URL_PREFIX + "/update-state"; 35 + String url = properties.getUpstreamUrl() + URL_PREFIX + "/update-state";
36 return doPost(url, updateReqDTO); 36 return doPost(url, updateReqDTO);
37 } 37 }
38 38
39 @Override 39 @Override
40 public CommonResult<Boolean> reportDeviceEvent(IotDeviceEventReportReqDTO reportReqDTO) { 40 public CommonResult<Boolean> reportDeviceEvent(IotDeviceEventReportReqDTO reportReqDTO) {
41 - String url = deviceDataUrl + URL_PREFIX + "/report-event"; 41 + String url = properties.getUpstreamUrl() + URL_PREFIX + "/report-event";
42 return doPost(url, reportReqDTO); 42 return doPost(url, reportReqDTO);
43 } 43 }
44 44
45 @Override 45 @Override
46 public CommonResult<Boolean> reportDeviceProperty(IotDevicePropertyReportReqDTO reportReqDTO) { 46 public CommonResult<Boolean> reportDeviceProperty(IotDevicePropertyReportReqDTO reportReqDTO) {
47 - String url = deviceDataUrl + URL_PREFIX + "/report-property"; 47 + String url = properties.getUpstreamUrl() + URL_PREFIX + "/report-property";
48 return doPost(url, reportReqDTO); 48 return doPost(url, reportReqDTO);
49 } 49 }
50 50
51 @Override 51 @Override
52 public CommonResult<Boolean> heartbeatPluginInstance(IotPluginInstanceHeartbeatReqDTO heartbeatReqDTO) { 52 public CommonResult<Boolean> heartbeatPluginInstance(IotPluginInstanceHeartbeatReqDTO heartbeatReqDTO) {
53 - String url = deviceDataUrl + URL_PREFIX + "/heartbeat-plugin-instance"; 53 + String url = properties.getUpstreamUrl() + URL_PREFIX + "/heartbeat-plugin-instance";
54 return doPost(url, heartbeatReqDTO); 54 return doPost(url, heartbeatReqDTO);
55 } 55 }
56 56
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/config/IotPluginHttpAutoConfiguration.java
@@ -4,7 +4,7 @@ import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi; @@ -4,7 +4,7 @@ import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi;
4 import cn.iocoder.yudao.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler; 4 import cn.iocoder.yudao.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler;
5 import cn.iocoder.yudao.module.iot.plugin.http.downstream.IotDeviceDownstreamHandlerImpl; 5 import cn.iocoder.yudao.module.iot.plugin.http.downstream.IotDeviceDownstreamHandlerImpl;
6 import cn.iocoder.yudao.module.iot.plugin.http.upstream.IotDeviceUpstreamServer; 6 import cn.iocoder.yudao.module.iot.plugin.http.upstream.IotDeviceUpstreamServer;
7 -import org.springframework.beans.factory.annotation.Value; 7 +import org.springframework.boot.context.properties.EnableConfigurationProperties;
8 import org.springframework.context.annotation.Bean; 8 import org.springframework.context.annotation.Bean;
9 import org.springframework.context.annotation.Configuration; 9 import org.springframework.context.annotation.Configuration;
10 10
@@ -14,18 +14,13 @@ import org.springframework.context.annotation.Configuration; @@ -14,18 +14,13 @@ import org.springframework.context.annotation.Configuration;
14 * @author haohao 14 * @author haohao
15 */ 15 */
16 @Configuration 16 @Configuration
  17 +@EnableConfigurationProperties(IotPluginHttpProperties.class)
17 public class IotPluginHttpAutoConfiguration { 18 public class IotPluginHttpAutoConfiguration {
18 19
19 - // TODO @haohao:这个要不要搞个配置类,更容易维护;  
20 - /**  
21 - * 可在 application.yml 中配置,默认端口 8092  
22 - */  
23 - @Value("${plugin.http.server.port:8092}")  
24 - private Integer port;  
25 -  
26 @Bean(initMethod = "start", destroyMethod = "stop") 20 @Bean(initMethod = "start", destroyMethod = "stop")
27 - public IotDeviceUpstreamServer deviceUpstreamServer(IotDeviceUpstreamApi deviceUpstreamApi) {  
28 - return new IotDeviceUpstreamServer(port, deviceUpstreamApi); 21 + public IotDeviceUpstreamServer deviceUpstreamServer(IotDeviceUpstreamApi deviceUpstreamApi,
  22 + IotPluginHttpProperties properties) {
  23 + return new IotDeviceUpstreamServer(properties, deviceUpstreamApi);
29 } 24 }
30 25
31 @Bean 26 @Bean
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/config/IotPluginHttpProperties.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.plugin.http.config;
  2 +
  3 +import lombok.Data;
  4 +import org.springframework.boot.context.properties.ConfigurationProperties;
  5 +import org.springframework.validation.annotation.Validated;
  6 +
  7 +@ConfigurationProperties(prefix = "yudao.iot.plugin.http")
  8 +@Validated
  9 +@Data
  10 +public class IotPluginHttpProperties {
  11 +
  12 + /**
  13 + * HTTP 服务端口
  14 + */
  15 + private Integer serverPort;
  16 +
  17 +}
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/upstream/IotDeviceUpstreamServer.java
1 package cn.iocoder.yudao.module.iot.plugin.http.upstream; 1 package cn.iocoder.yudao.module.iot.plugin.http.upstream;
2 2
3 import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi; 3 import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi;
  4 +import cn.iocoder.yudao.module.iot.plugin.http.config.IotPluginHttpProperties;
4 import cn.iocoder.yudao.module.iot.plugin.http.upstream.router.IotDeviceEventReportVertxHandler; 5 import cn.iocoder.yudao.module.iot.plugin.http.upstream.router.IotDeviceEventReportVertxHandler;
5 import cn.iocoder.yudao.module.iot.plugin.http.upstream.router.IotDevicePropertyReportVertxHandler; 6 import cn.iocoder.yudao.module.iot.plugin.http.upstream.router.IotDevicePropertyReportVertxHandler;
6 import io.vertx.core.Vertx; 7 import io.vertx.core.Vertx;
@@ -21,11 +22,11 @@ public class IotDeviceUpstreamServer { @@ -21,11 +22,11 @@ public class IotDeviceUpstreamServer {
21 22
22 private final Vertx vertx; 23 private final Vertx vertx;
23 private final HttpServer server; 24 private final HttpServer server;
24 - private final Integer port; 25 + private final IotPluginHttpProperties properties;
25 26
26 - public IotDeviceUpstreamServer(Integer port, 27 + public IotDeviceUpstreamServer(IotPluginHttpProperties properties,
27 IotDeviceUpstreamApi deviceUpstreamApi) { 28 IotDeviceUpstreamApi deviceUpstreamApi) {
28 - this.port = port; 29 + this.properties = properties;
29 // 创建 Vertx 实例 30 // 创建 Vertx 实例
30 this.vertx = Vertx.vertx(); 31 this.vertx = Vertx.vertx();
31 // 创建 Router 实例 32 // 创建 Router 实例
@@ -44,7 +45,7 @@ public class IotDeviceUpstreamServer { @@ -44,7 +45,7 @@ public class IotDeviceUpstreamServer {
44 */ 45 */
45 public void start() { 46 public void start() {
46 log.info("[start][开始启动]"); 47 log.info("[start][开始启动]");
47 - server.listen(port) 48 + server.listen(properties.getServerPort())
48 .toCompletionStage() 49 .toCompletionStage()
49 .toCompletableFuture() 50 .toCompletableFuture()
50 .join(); 51 .join();
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/resources/application.yml
@@ -2,11 +2,11 @@ spring: @@ -2,11 +2,11 @@ spring:
2 application: 2 application:
3 name: yudao-module-iot-plugin-http 3 name: yudao-module-iot-plugin-http
4 4
5 -iot:  
6 - device-data:  
7 - url: http://127.0.0.1:48080  
8 -  
9 -plugin:  
10 - http:  
11 - server:  
12 - port: 8092  
13 \ No newline at end of file 5 \ No newline at end of file
  6 +yudao:
  7 + iot:
  8 + plugin:
  9 + common:
  10 + upstream-url: http://127.0.0.1:48080
  11 + downstream-port: 8093
  12 + http:
  13 + server-port: 8092