Commit 30ae986c1af58d77ce94be1f25884eff61115cdd

Authored by YunaiV
1 parent 4a251b19

【代码优化】IoT:整理 plugins 的依赖,以及对应 server 的启动逻辑

Showing 15 changed files with 66 additions and 80 deletions
plugins/yudao-module-iot-plugin-http-1.0.0.jar
No preview for this file type
yudao-module-iot/yudao-module-iot-api/pom.xml
... ... @@ -30,11 +30,17 @@
30 30 <scope>provided</scope> <!-- 设置为 provided,只有工具类需要使用到 -->
31 31 </dependency>
32 32  
33   - <!-- PF4J -->
34   - <!-- TODO 芋艿:这个依赖,要不要放在 api 包 -->
  33 + <!-- 工具类相关 -->
  34 + <dependency>
  35 + <groupId>com.fasterxml.jackson.core</groupId>
  36 + <artifactId>jackson-databind</artifactId>
  37 + <scope>provided</scope> <!-- 设置为 provided,只有工具类需要使用到 -->
  38 + </dependency>
  39 +
35 40 <dependency>
36   - <groupId>org.pf4j</groupId>
  41 + <groupId>org.pf4j</groupId> <!-- PF4J:内置插件机制 -->
37 42 <artifactId>pf4j-spring</artifactId>
  43 + <!-- TODO @芋艿:可以放到 bom 里配置 -->
38 44 <exclusions>
39 45 <exclusion>
40 46 <groupId>org.slf4j</groupId>
... ... @@ -43,13 +49,6 @@
43 49 </exclusions>
44 50 </dependency>
45 51  
46   - <!-- 工具类相关 -->
47   - <dependency>
48   - <groupId>com.fasterxml.jackson.core</groupId>
49   - <artifactId>jackson-databind</artifactId>
50   - <scope>provided</scope> <!-- 设置为 provided,只有工具类需要使用到 -->
51   - </dependency>
52   -
53 52 <!-- 参数校验 -->
54 53 <dependency>
55 54 <groupId>org.springframework.boot</groupId>
... ...
yudao-module-iot/yudao-module-iot-biz/pom.xml
... ... @@ -64,25 +64,19 @@
64 64 <artifactId>yudao-spring-boot-starter-excel</artifactId>
65 65 </dependency>
66 66  
67   - <!-- Vert.x 核心依赖 -->
68   - <dependency>
69   - <groupId>io.vertx</groupId>
70   - <artifactId>vertx-core</artifactId>
71   - </dependency>
72   - <!-- Vert.x Web 模块 -->
  67 + <!-- 工具类相关 -->
73 68 <dependency>
74 69 <groupId>io.vertx</groupId>
75 70 <artifactId>vertx-web</artifactId>
76 71 </dependency>
77   - <!-- MQTT -->
  72 +
78 73 <dependency>
79   - <groupId>org.eclipse.paho</groupId>
  74 + <groupId>org.eclipse.paho</groupId> <!-- MQTT -->
80 75 <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
81 76 </dependency>
82 77  
83   - <!-- PF4J -->
84 78 <dependency>
85   - <groupId>org.pf4j</groupId>
  79 + <groupId>org.pf4j</groupId> <!-- PF4J:内置插件机制 -->
86 80 <artifactId>pf4j-spring</artifactId>
87 81 <!-- TODO @芋艿:可以放到 bom 里配置 -->
88 82 <exclusions>
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/UnifiedConfiguration.java renamed to yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/config/UnifiedConfiguration.java
1   -package cn.iocoder.yudao.module.iot.framework.plugin;
  1 +package cn.iocoder.yudao.module.iot.framework.plugin.config;
2 2  
3   -import cn.iocoder.yudao.module.iot.framework.plugin.listener.CustomPluginStateListener;
  3 +import cn.iocoder.yudao.module.iot.framework.plugin.core.CustomPluginStateListener;
4 4 import lombok.extern.slf4j.Slf4j;
5 5 import org.pf4j.spring.SpringPluginManager;
6 6 import org.springframework.beans.factory.annotation.Value;
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/listener/CustomPluginStateListener.java renamed to yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/core/CustomPluginStateListener.java
1   -package cn.iocoder.yudao.module.iot.framework.plugin.listener;
  1 +package cn.iocoder.yudao.module.iot.framework.plugin.core;
2 2  
3 3 import lombok.extern.slf4j.Slf4j;
4 4 import org.pf4j.PluginStateEvent;
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/PluginStart.java renamed to yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/core/PluginStart.java
1   -package cn.iocoder.yudao.module.iot.framework.plugin;
  1 +package cn.iocoder.yudao.module.iot.framework.plugin.core;
2 2  
3 3 import cn.hutool.core.collection.CollUtil;
4 4 import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/aspect/TaosAspect.java renamed to yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/tdengine/core/TaosAspect.java
1   -package cn.iocoder.yudao.module.iot.framework.aspect;
  1 +package cn.iocoder.yudao.module.iot.framework.tdengine.core;
2 2  
3 3 import lombok.extern.slf4j.Slf4j;
4 4 import org.aspectj.lang.ProceedingJoinPoint;
... ...
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/pom.xml
... ... @@ -18,35 +18,24 @@
18 18 </description>
19 19  
20 20 <dependencies>
21   - <!-- TODO 芋艿:依赖整理 -->
22 21 <dependency>
23   - <groupId>cn.iocoder.boot</groupId>
24   - <artifactId>yudao-common</artifactId>
  22 + <groupId>org.springframework.boot</groupId>
  23 + <artifactId>spring-boot-starter</artifactId>
25 24 </dependency>
26   - <!-- 项目依赖 -->
  25 +
27 26 <dependency>
28 27 <groupId>cn.iocoder.boot</groupId>
29 28 <artifactId>yudao-module-iot-api</artifactId>
30 29 <version>${revision}</version>
31 30 </dependency>
32 31  
33   - <dependency>
34   - <groupId>org.projectlombok</groupId>
35   - <artifactId>lombok</artifactId>
36   - </dependency>
37   -
  32 + <!-- Web 相关 -->
38 33 <dependency>
39 34 <groupId>org.springframework</groupId>
40 35 <artifactId>spring-web</artifactId>
41 36 </dependency>
42 37  
43   - <!-- 其他依赖项 -->
44   - <dependency>
45   - <groupId>org.springframework.boot</groupId>
46   - <artifactId>spring-boot-starter</artifactId>
47   - </dependency>
48   -
49   - <!-- Vert.x Web -->
  38 + <!-- 工具类相关 -->
50 39 <dependency>
51 40 <groupId>io.vertx</groupId>
52 41 <artifactId>vertx-web</artifactId>
... ...
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/pom.xml
... ... @@ -16,6 +16,7 @@
16 16  
17 17 <name>${project.artifactId}</name>
18 18 <description>
  19 + <!-- TODO @芋艿:注释 -->
19 20 物联网 插件模块 - http 插件
20 21 </description>
21 22  
... ... @@ -31,7 +32,7 @@
31 32  
32 33 <build>
33 34 <plugins>
34   - <!-- 插件模式 zip -->
  35 + <!-- 插件模式 zip -->
35 36 <plugin>
36 37 <groupId>org.apache.maven.plugins</groupId>
37 38 <artifactId>maven-antrun-plugin</artifactId>
... ... @@ -75,7 +76,7 @@
75 76 </executions>
76 77 </plugin>
77 78  
78   - <!-- 插件模式 jar -->
  79 + <!-- 插件模式 jar -->
79 80 <plugin>
80 81 <groupId>org.apache.maven.plugins</groupId>
81 82 <artifactId>maven-jar-plugin</artifactId>
... ... @@ -94,7 +95,7 @@
94 95 </configuration>
95 96 </plugin>
96 97  
97   - <!-- 独立模式 -->
  98 + <!-- 独立模式 -->
98 99 <plugin>
99 100 <groupId>org.springframework.boot</groupId>
100 101 <artifactId>spring-boot-maven-plugin</artifactId>
... ... @@ -121,21 +122,13 @@
121 122 </build>
122 123  
123 124 <dependencies>
124   - <!-- TODO 芋艿:优化下 -->
125   - <!-- 项目依赖 -->
126 125 <dependency>
127 126 <groupId>cn.iocoder.boot</groupId>
128 127 <artifactId>yudao-module-iot-plugin-common</artifactId>
129 128 <version>${revision}</version>
130 129 </dependency>
131 130  
132   - <!-- 其他依赖项 -->
133   - <dependency>
134   - <groupId>org.springframework.boot</groupId>
135   - <artifactId>spring-boot-starter-web</artifactId>
136   - </dependency>
137   -
138   - <!-- Vert.x Web -->
  131 + <!-- 工具类相关 -->
139 132 <dependency>
140 133 <groupId>io.vertx</groupId>
141 134 <artifactId>vertx-web</artifactId>
... ...
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/HttpPluginSpringbootApplication.java
1 1 package cn.iocoder.yudao.module.iot.plugin.http;
2 2  
3   -import cn.iocoder.yudao.module.iot.plugin.http.upstream.IotDeviceUpstreamServer;
4 3 import lombok.extern.slf4j.Slf4j;
5 4 import org.springframework.boot.SpringApplication;
6   -import org.springframework.boot.WebApplicationType;
7 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
8 6 import org.springframework.context.ConfigurableApplicationContext;
9 7  
... ... @@ -15,16 +13,15 @@ import org.springframework.context.ConfigurableApplicationContext;
15 13 public class HttpPluginSpringbootApplication {
16 14  
17 15 public static void main(String[] args) {
18   - SpringApplication application = new SpringApplication(HttpPluginSpringbootApplication.class);
19   - application.setWebApplicationType(WebApplicationType.NONE);
20   - ConfigurableApplicationContext context = application.run(args);
  16 + ConfigurableApplicationContext context = SpringApplication.run(HttpPluginSpringbootApplication.class, args);
21 17  
22 18 // 手动获取 VertxService 并启动
23 19 // TODO @haohao:可以放在 bean 的 init 里么?回复:会和插件模式冲突 @芋艿,测试下
24   - IotDeviceUpstreamServer vertxService = context.getBean(IotDeviceUpstreamServer.class);
25   - vertxService.start();
  20 + // TODO @haohao:貌似去掉,没有问题额。。。
  21 +// IotDeviceUpstreamServer vertxService = context.getBean(IotDeviceUpstreamServer.class);
  22 +// vertxService.start();
26 23  
27   - log.info("[HttpPluginSpringbootApplication] 独立模式启动完成");
  24 + log.info("[main][独立模式启动完成]");
28 25 }
29 26  
30 27 }
31 28 \ No newline at end of file
... ...
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/config/IotHttpVertxPlugin.java
... ... @@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.iot.plugin.http.config;
3 3 import cn.hutool.core.lang.Assert;
4 4 import cn.hutool.extra.spring.SpringUtil;
5 5 import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi;
6   -import cn.iocoder.yudao.module.iot.plugin.http.upstream.IotDeviceUpstreamServer;
7 6 import lombok.extern.slf4j.Slf4j;
8 7 import org.pf4j.PluginWrapper;
9 8 import org.pf4j.spring.SpringPlugin;
... ... @@ -11,6 +10,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
11 10 import org.springframework.context.ApplicationContext;
12 11 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
13 12  
  13 +// TODO @芋艿:完善注释
14 14 /**
15 15 * 负责插件的启动和停止,与 Vert.x 的生命周期管理
16 16 */
... ... @@ -30,8 +30,9 @@ public class IotHttpVertxPlugin extends SpringPlugin {
30 30 Assert.notNull(pluginContext, "pluginContext 不能为空");
31 31  
32 32 // 2. 启动 Vert.x
33   - IotDeviceUpstreamServer vertxService = pluginContext.getBean(IotDeviceUpstreamServer.class);
34   - vertxService.start();
  33 + // TODO @haohao:貌似去掉,没有问题额。。。
  34 +// IotDeviceUpstreamServer vertxService = pluginContext.getBean(IotDeviceUpstreamServer.class);
  35 +// vertxService.start();
35 36  
36 37 log.info("[HttpVertxPlugin][HttpVertxPlugin 插件启动成功...]");
37 38 } catch (Exception e) {
... ... @@ -44,11 +45,11 @@ public class IotHttpVertxPlugin extends SpringPlugin {
44 45 log.info("[HttpVertxPlugin][HttpVertxPlugin 插件停止开始...]");
45 46 try {
46 47 // 停止服务器
47   - ApplicationContext pluginContext = getApplicationContext();
48   - if (pluginContext != null) {
49   - IotDeviceUpstreamServer vertxService = pluginContext.getBean(IotDeviceUpstreamServer.class);
50   - vertxService.stopServer();
51   - }
  48 +// ApplicationContext pluginContext = getApplicationContext();
  49 +// if (pluginContext != null) {
  50 +// IotDeviceUpstreamServer vertxService = pluginContext.getBean(IotDeviceUpstreamServer.class);
  51 +// vertxService.stop();
  52 +// }
52 53  
53 54 log.info("[HttpVertxPlugin][HttpVertxPlugin 插件停止成功...]");
54 55 } catch (Exception e) {
... ...
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
... ... @@ -7,7 +7,9 @@ import org.springframework.context.annotation.Bean;
7 7 import org.springframework.context.annotation.Configuration;
8 8  
9 9 /**
10   - * 插件与独立运行都可复用的配置类
  10 + * IoT 插件 HTTP 的专用自动配置类
  11 + *
  12 + * @author haohao
11 13 */
12 14 @Configuration
13 15 public class IotPluginHttpAutoConfiguration {
... ... @@ -19,8 +21,8 @@ public class IotPluginHttpAutoConfiguration {
19 21 @Value("${plugin.http.server.port:8092}")
20 22 private Integer port;
21 23  
22   - @Bean
23   - public IotDeviceUpstreamServer vertxService(IotDeviceUpstreamApi deviceUpstreamApi) {
  24 + @Bean(initMethod = "start", destroyMethod = "stop")
  25 + public IotDeviceUpstreamServer deviceUpstreamServer(IotDeviceUpstreamApi deviceUpstreamApi) {
24 26 return new IotDeviceUpstreamServer(port, deviceUpstreamApi);
25 27 }
26 28  
... ...
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/downstream/IotDeviceDownstreamHandlerImpl.java
... ... @@ -7,6 +7,14 @@ import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDeviceSe
7 7 import cn.iocoder.yudao.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler;
8 8 import org.springframework.stereotype.Component;
9 9  
  10 +/**
  11 + * HTTP 插件的 {@link IotDeviceDownstreamHandler} 实现类
  12 + *
  13 + * 但是:由于设备通过 HTTP 短链接接入,导致其实无法下行指导给 device 设备,所以基本都是直接返回失败!!!
  14 + * 类似 MQTT、WebSocket、TCP 插件,是可以实现下行指令的。
  15 + *
  16 + * @author 芋道源码
  17 + */
10 18 @Component // TODO @芋艿:后续统一处理
11 19 public class IotDeviceDownstreamHandlerImpl implements IotDeviceDownstreamHandler {
12 20  
... ...
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
... ... @@ -51,7 +51,7 @@ public class IotDeviceUpstreamServer {
51 51 /**
52 52 * 停止所有
53 53 */
54   - public void stopServer() {
  54 + public void stop() {
55 55 log.info("[stop][开始关闭]");
56 56 try {
57 57 // 关闭 HTTP 服务器
... ...
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/upstream/router/IotDevicePropertyReportVertxHandler.java
... ... @@ -10,21 +10,22 @@ import cn.iocoder.yudao.module.iot.enums.device.IotDeviceStateEnum;
10 10 import io.vertx.core.Handler;
11 11 import io.vertx.ext.web.RequestBody;
12 12 import io.vertx.ext.web.RoutingContext;
  13 +import lombok.RequiredArgsConstructor;
13 14 import lombok.extern.slf4j.Slf4j;
14 15  
15 16 import java.time.LocalDateTime;
16 17 import java.util.Map;
17 18  
  19 +/**
  20 + * IoT 设备属性上报的 Vert.x Handler
  21 + */
  22 +@RequiredArgsConstructor
18 23 @Slf4j
19 24 public class IotDevicePropertyReportVertxHandler implements Handler<RoutingContext> {
20 25  
21 26 public static final String PATH = "/sys/:productKey/:deviceName/thing/event/property/post";
22 27  
23   - private final IotDeviceUpstreamApi deviceDataApi;
24   -
25   - public IotDevicePropertyReportVertxHandler(IotDeviceUpstreamApi deviceDataApi) {
26   - this.deviceDataApi = deviceDataApi;
27   - }
  28 + private final IotDeviceUpstreamApi deviceUpstreamApi;
28 29  
29 30 @Override
30 31 public void handle(RoutingContext ctx) {
... ... @@ -51,7 +52,7 @@ public class IotDevicePropertyReportVertxHandler implements Handler&lt;RoutingConte
51 52 try {
52 53 // TODO @haohao:pluginKey 需要设置
53 54 // 设备上线
54   - deviceDataApi.updateDeviceState(((IotDeviceStateUpdateReqDTO)
  55 + deviceUpstreamApi.updateDeviceState(((IotDeviceStateUpdateReqDTO)
55 56 new IotDeviceStateUpdateReqDTO().setRequestId(IdUtil.fastSimpleUUID())
56 57 .setPluginKey("http")
57 58 .setReportTime(LocalDateTime.now())
... ... @@ -59,7 +60,7 @@ public class IotDevicePropertyReportVertxHandler implements Handler&lt;RoutingConte
59 60 .setState(IotDeviceStateEnum.ONLINE.getState()));
60 61  
61 62 // 属性上报
62   - deviceDataApi.reportDeviceProperty(((IotDevicePropertyReportReqDTO)
  63 + deviceUpstreamApi.reportDeviceProperty(((IotDevicePropertyReportReqDTO)
63 64 new IotDevicePropertyReportReqDTO().setRequestId(IdUtil.fastSimpleUUID())
64 65 .setPluginKey("http").setReportTime(LocalDateTime.now())
65 66 .setProductKey(productKey).setDeviceName(deviceName))
... ... @@ -81,6 +82,7 @@ public class IotDevicePropertyReportVertxHandler implements Handler&lt;RoutingConte
81 82 }
82 83 }
83 84  
  85 + // TODO @芋艿:抽一个 IotPluginCommonResult 出来?等 mqtt、websocket 出来后,再考虑优化!
84 86 private JSONObject createResponseJson(int code, JSONObject data, String id,
85 87 String message, String method, String version) {
86 88 JSONObject res = new JSONObject();
... ... @@ -92,4 +94,5 @@ public class IotDevicePropertyReportVertxHandler implements Handler&lt;RoutingConte
92 94 res.set("version", version);
93 95 return res;
94 96 }
  97 +
95 98 }
... ...