Commit 4a251b19c48c5f8f2568acc222b06601a6e26c9c
1 parent
2d18e218
【代码新增】IoT:优化 plugins 相关的代码,包拆成分 upstream、downstream、config 三个,职责更明确
Showing
17 changed files
with
146 additions
and
162 deletions
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/java/cn/iocoder/yudao/module/iot/plugin/common/config/YudaoDeviceDataApiAutoConfiguration.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/config/IotPluginCommonAutoConfiguration.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.plugin.common.config; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi; |
| 4 | -import cn.iocoder.yudao.module.iot.plugin.common.core.downstream.IotDeviceDownstreamHandler; | |
| 5 | -import cn.iocoder.yudao.module.iot.plugin.common.core.downstream.IotDeviceDownstreamServer; | |
| 6 | -import cn.iocoder.yudao.module.iot.plugin.common.core.upstream.IotDeviceUpstreamClient; | |
| 4 | +import cn.iocoder.yudao.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler; | |
| 5 | +import cn.iocoder.yudao.module.iot.plugin.common.downstream.IotDeviceDownstreamServer; | |
| 6 | +import cn.iocoder.yudao.module.iot.plugin.common.upstream.IotDeviceUpstreamClient; | |
| 7 | 7 | import org.springframework.beans.factory.annotation.Value; |
| 8 | 8 | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 9 | 9 | import org.springframework.boot.web.client.RestTemplateBuilder; |
| ... | ... | @@ -12,14 +12,13 @@ import org.springframework.web.client.RestTemplate; |
| 12 | 12 | |
| 13 | 13 | import java.time.Duration; |
| 14 | 14 | |
| 15 | -// TODO @芋艿:配置类的名字 | |
| 16 | 15 | /** |
| 17 | - * 设备数据 API 初始化器 | |
| 16 | + * IoT 插件的通用自动配置类 | |
| 18 | 17 | * |
| 19 | 18 | * @author haohao |
| 20 | 19 | */ |
| 21 | 20 | @AutoConfiguration |
| 22 | -public class YudaoDeviceDataApiAutoConfiguration { | |
| 21 | +public class IotPluginCommonAutoConfiguration { | |
| 23 | 22 | |
| 24 | 23 | // TODO @haohao:这个要不搞个配置类哈 |
| 25 | 24 | @Value("${iot.device-data.url}") | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/java/cn/iocoder/yudao/module/iot/plugin/common/core/downstream/IotDeviceDownstreamHandler.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/downstream/IotDeviceDownstreamHandler.java
| 1 | -package cn.iocoder.yudao.module.iot.plugin.common.core.downstream; | |
| 1 | +package cn.iocoder.yudao.module.iot.plugin.common.downstream; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | 4 | import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDevicePropertyGetReqDTO; | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/java/cn/iocoder/yudao/module/iot/plugin/common/core/downstream/IotDeviceDownstreamServer.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/downstream/IotDeviceDownstreamServer.java
| 1 | -package cn.iocoder.yudao.module.iot.plugin.common.core.downstream; | |
| 1 | +package cn.iocoder.yudao.module.iot.plugin.common.downstream; | |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.module.iot.plugin.common.core.downstream.router.IotDeviceServiceInvokeVertxHandler; | |
| 3 | +import cn.iocoder.yudao.module.iot.plugin.common.downstream.router.IotDeviceServiceInvokeVertxHandler; | |
| 4 | 4 | import io.vertx.core.Vertx; |
| 5 | 5 | import io.vertx.core.http.HttpServer; |
| 6 | 6 | import io.vertx.ext.web.Router; |
| 7 | 7 | import io.vertx.ext.web.handler.BodyHandler; |
| 8 | 8 | import lombok.extern.slf4j.Slf4j; |
| 9 | 9 | |
| 10 | +/** | |
| 11 | + * IoT 设备下行服务端,接收来自 server 服务器的请求,转发给 device 设备 | |
| 12 | + * | |
| 13 | + * @author 芋道源码 | |
| 14 | + */ | |
| 10 | 15 | @Slf4j |
| 11 | 16 | public class IotDeviceDownstreamServer { |
| 12 | 17 | |
| ... | ... | @@ -25,6 +30,9 @@ public class IotDeviceDownstreamServer { |
| 25 | 30 | this.server = vertx.createHttpServer().requestHandler(router); |
| 26 | 31 | } |
| 27 | 32 | |
| 33 | + /** | |
| 34 | + * 启动 HTTP 服务器 | |
| 35 | + */ | |
| 28 | 36 | public void start() { |
| 29 | 37 | log.info("[start][开始启动]"); |
| 30 | 38 | server.listen(0) // 通过 0 自动选择端口 |
| ... | ... | @@ -34,6 +42,9 @@ public class IotDeviceDownstreamServer { |
| 34 | 42 | log.info("[start][启动完成,端口({})]", this.server.actualPort()); |
| 35 | 43 | } |
| 36 | 44 | |
| 45 | + /** | |
| 46 | + * 停止所有 | |
| 47 | + */ | |
| 37 | 48 | public void stop() { |
| 38 | 49 | log.info("[stop][开始关闭]"); |
| 39 | 50 | try { | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/java/cn/iocoder/yudao/module/iot/plugin/common/core/downstream/router/IotDeviceServiceInvokeVertxHandler.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/downstream/router/IotDeviceServiceInvokeVertxHandler.java
| 1 | -package cn.iocoder.yudao.module.iot.plugin.common.core.downstream.router; | |
| 1 | +package cn.iocoder.yudao.module.iot.plugin.common.downstream.router; | |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.module.iot.plugin.common.core.downstream.IotDeviceDownstreamHandler; | |
| 3 | +import cn.iocoder.yudao.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler; | |
| 4 | 4 | import io.vertx.core.Handler; |
| 5 | 5 | import io.vertx.ext.web.RoutingContext; |
| 6 | 6 | import lombok.RequiredArgsConstructor; | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/java/cn/iocoder/yudao/module/iot/plugin/common/core/upstream/IotDeviceUpstreamClient.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/upstream/IotDeviceUpstreamClient.java
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/plugin.properties
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/pom.xml
| ... | ... | @@ -22,7 +22,7 @@ |
| 22 | 22 | <properties> |
| 23 | 23 | <!-- 插件相关 --> |
| 24 | 24 | <plugin.id>${project.artifactId}</plugin.id> |
| 25 | - <plugin.class>cn.iocoder.yudao.module.iot.plugin.http.config.HttpVertxPlugin</plugin.class> | |
| 25 | + <plugin.class>cn.iocoder.yudao.module.iot.plugin.http.config.IotHttpVertxPlugin</plugin.class> | |
| 26 | 26 | <plugin.version>${project.version}</plugin.version> |
| 27 | 27 | <plugin.provider>yudao</plugin.provider> |
| 28 | 28 | <plugin.description>${project.artifactId}-${project.version}</plugin.description> | ... | ... |
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.config.VertxService; | |
| 3 | +import cn.iocoder.yudao.module.iot.plugin.http.upstream.IotDeviceUpstreamServer; | |
| 4 | 4 | import lombok.extern.slf4j.Slf4j; |
| 5 | 5 | import org.springframework.boot.SpringApplication; |
| 6 | 6 | import org.springframework.boot.WebApplicationType; |
| ... | ... | @@ -21,8 +21,8 @@ public class HttpPluginSpringbootApplication { |
| 21 | 21 | |
| 22 | 22 | // 手动获取 VertxService 并启动 |
| 23 | 23 | // TODO @haohao:可以放在 bean 的 init 里么?回复:会和插件模式冲突 @芋艿,测试下 |
| 24 | - VertxService vertxService = context.getBean(VertxService.class); | |
| 25 | - vertxService.startServer(); | |
| 24 | + IotDeviceUpstreamServer vertxService = context.getBean(IotDeviceUpstreamServer.class); | |
| 25 | + vertxService.start(); | |
| 26 | 26 | |
| 27 | 27 | log.info("[HttpPluginSpringbootApplication] 独立模式启动完成"); |
| 28 | 28 | } | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/config/HttpVertxPluginConfiguration.java deleted
| 1 | -package cn.iocoder.yudao.module.iot.plugin.http.config; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi; | |
| 4 | -import cn.iocoder.yudao.module.iot.plugin.http.framework.upstream.HttpVertxHandler; | |
| 5 | -import io.vertx.core.Vertx; | |
| 6 | -import io.vertx.ext.web.Router; | |
| 7 | -import io.vertx.ext.web.handler.BodyHandler; | |
| 8 | -import lombok.extern.slf4j.Slf4j; | |
| 9 | -import org.springframework.beans.factory.annotation.Value; | |
| 10 | -import org.springframework.context.annotation.Bean; | |
| 11 | -import org.springframework.context.annotation.Configuration; | |
| 12 | - | |
| 13 | -/** | |
| 14 | - * 插件与独立运行都可复用的配置类 | |
| 15 | - */ | |
| 16 | -@Slf4j | |
| 17 | -@Configuration | |
| 18 | -public class HttpVertxPluginConfiguration { | |
| 19 | - | |
| 20 | - // TODO @haohao:这个要不要搞个配置类,更容易维护; | |
| 21 | - /** | |
| 22 | - * 可在 application.yml 中配置,默认端口 8092 | |
| 23 | - */ | |
| 24 | - @Value("${plugin.http.server.port:8092}") | |
| 25 | - private Integer port; | |
| 26 | - | |
| 27 | - /** | |
| 28 | - * 创建 Vert.x 实例 | |
| 29 | - */ | |
| 30 | - @Bean | |
| 31 | - public Vertx vertx() { | |
| 32 | - return Vertx.vertx(); | |
| 33 | - } | |
| 34 | - | |
| 35 | - /** | |
| 36 | - * 创建路由 | |
| 37 | - * | |
| 38 | - * @param vertx Vertx 实例 | |
| 39 | - * @param httpVertxHandler HttpVertxHandler 实例 | |
| 40 | - * @return Router 实例 | |
| 41 | - */ | |
| 42 | - @Bean | |
| 43 | - public Router router(Vertx vertx, HttpVertxHandler httpVertxHandler) { | |
| 44 | - Router router = Router.router(vertx); | |
| 45 | - | |
| 46 | - // 处理 Body | |
| 47 | - router.route().handler(BodyHandler.create()); | |
| 48 | - | |
| 49 | - // 设置路由 | |
| 50 | - // TODO @haohao:这个后续,我们是多个 Handler ,还是一个哈? | |
| 51 | - router.post("/sys/:productKey/:deviceName/thing/event/property/post") | |
| 52 | - .handler(httpVertxHandler); | |
| 53 | - | |
| 54 | - return router; | |
| 55 | - } | |
| 56 | - | |
| 57 | - /** | |
| 58 | - * 创建 HttpVertxHandler 实例 | |
| 59 | - * | |
| 60 | - * @param deviceDataApi DeviceDataApi 实例 | |
| 61 | - * @return HttpVertxHandler 实例 | |
| 62 | - */ | |
| 63 | - @Bean | |
| 64 | - public HttpVertxHandler httpVertxHandler(IotDeviceUpstreamApi deviceDataApi) { | |
| 65 | - return new HttpVertxHandler(deviceDataApi); | |
| 66 | - } | |
| 67 | - | |
| 68 | - /** | |
| 69 | - * 定义一个 VertxService 来管理服务器启动逻辑 | |
| 70 | - * 无论是独立运行还是插件方式,都可以共用此类 | |
| 71 | - * | |
| 72 | - * @param vertx Vertx 实例 | |
| 73 | - * @param router Router 实例 | |
| 74 | - * @return VertxService 实例 | |
| 75 | - */ | |
| 76 | - @Bean | |
| 77 | - public VertxService vertxService(Vertx vertx, Router router) { | |
| 78 | - return new VertxService(port, vertx, router); | |
| 79 | - } | |
| 80 | - | |
| 81 | -} |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/config/HttpVertxPlugin.java renamed to 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,6 +3,7 @@ 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; | |
| 6 | 7 | import lombok.extern.slf4j.Slf4j; |
| 7 | 8 | import org.pf4j.PluginWrapper; |
| 8 | 9 | import org.pf4j.spring.SpringPlugin; |
| ... | ... | @@ -14,9 +15,9 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext |
| 14 | 15 | * 负责插件的启动和停止,与 Vert.x 的生命周期管理 |
| 15 | 16 | */ |
| 16 | 17 | @Slf4j |
| 17 | -public class HttpVertxPlugin extends SpringPlugin { | |
| 18 | +public class IotHttpVertxPlugin extends SpringPlugin { | |
| 18 | 19 | |
| 19 | - public HttpVertxPlugin(PluginWrapper wrapper) { | |
| 20 | + public IotHttpVertxPlugin(PluginWrapper wrapper) { | |
| 20 | 21 | super(wrapper); |
| 21 | 22 | } |
| 22 | 23 | |
| ... | ... | @@ -29,8 +30,8 @@ public class HttpVertxPlugin extends SpringPlugin { |
| 29 | 30 | Assert.notNull(pluginContext, "pluginContext 不能为空"); |
| 30 | 31 | |
| 31 | 32 | // 2. 启动 Vert.x |
| 32 | - VertxService vertxService = pluginContext.getBean(VertxService.class); | |
| 33 | - vertxService.startServer(); | |
| 33 | + IotDeviceUpstreamServer vertxService = pluginContext.getBean(IotDeviceUpstreamServer.class); | |
| 34 | + vertxService.start(); | |
| 34 | 35 | |
| 35 | 36 | log.info("[HttpVertxPlugin][HttpVertxPlugin 插件启动成功...]"); |
| 36 | 37 | } catch (Exception e) { |
| ... | ... | @@ -45,7 +46,7 @@ public class HttpVertxPlugin extends SpringPlugin { |
| 45 | 46 | // 停止服务器 |
| 46 | 47 | ApplicationContext pluginContext = getApplicationContext(); |
| 47 | 48 | if (pluginContext != null) { |
| 48 | - VertxService vertxService = pluginContext.getBean(VertxService.class); | |
| 49 | + IotDeviceUpstreamServer vertxService = pluginContext.getBean(IotDeviceUpstreamServer.class); | |
| 49 | 50 | vertxService.stopServer(); |
| 50 | 51 | } |
| 51 | 52 | ... | ... |
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
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot.plugin.http.config; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi; | |
| 4 | +import cn.iocoder.yudao.module.iot.plugin.http.upstream.IotDeviceUpstreamServer; | |
| 5 | +import org.springframework.beans.factory.annotation.Value; | |
| 6 | +import org.springframework.context.annotation.Bean; | |
| 7 | +import org.springframework.context.annotation.Configuration; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * 插件与独立运行都可复用的配置类 | |
| 11 | + */ | |
| 12 | +@Configuration | |
| 13 | +public class IotPluginHttpAutoConfiguration { | |
| 14 | + | |
| 15 | + // TODO @haohao:这个要不要搞个配置类,更容易维护; | |
| 16 | + /** | |
| 17 | + * 可在 application.yml 中配置,默认端口 8092 | |
| 18 | + */ | |
| 19 | + @Value("${plugin.http.server.port:8092}") | |
| 20 | + private Integer port; | |
| 21 | + | |
| 22 | + @Bean | |
| 23 | + public IotDeviceUpstreamServer vertxService(IotDeviceUpstreamApi deviceUpstreamApi) { | |
| 24 | + return new IotDeviceUpstreamServer(port, deviceUpstreamApi); | |
| 25 | + } | |
| 26 | + | |
| 27 | +} | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/config/VertxService.java deleted
| 1 | -package cn.iocoder.yudao.module.iot.plugin.http.config; | |
| 2 | - | |
| 3 | -import io.vertx.core.Vertx; | |
| 4 | -import io.vertx.ext.web.Router; | |
| 5 | -import lombok.extern.slf4j.Slf4j; | |
| 6 | - | |
| 7 | -/** | |
| 8 | - * 封装 Vert.x HTTP 服务的启动/关闭逻辑 | |
| 9 | - */ | |
| 10 | -@Slf4j | |
| 11 | -public class VertxService { | |
| 12 | - | |
| 13 | - private final Integer port; | |
| 14 | - private final Vertx vertx; | |
| 15 | - private final Router router; | |
| 16 | - | |
| 17 | - public VertxService(Integer port, Vertx vertx, Router router) { | |
| 18 | - this.port = port; | |
| 19 | - this.vertx = vertx; | |
| 20 | - this.router = router; | |
| 21 | - } | |
| 22 | - | |
| 23 | - /** | |
| 24 | - * 启动 HTTP 服务器 | |
| 25 | - */ | |
| 26 | - public void startServer() { | |
| 27 | - vertx.createHttpServer() | |
| 28 | - .requestHandler(router) | |
| 29 | - .listen(port, http -> { | |
| 30 | - if (http.succeeded()) { | |
| 31 | - log.info("[VertxService] HTTP 服务器启动成功, 端口: {}", port); | |
| 32 | - } else { | |
| 33 | - log.error("[VertxService] HTTP 服务器启动失败", http.cause()); | |
| 34 | - } | |
| 35 | - }); | |
| 36 | - } | |
| 37 | - | |
| 38 | - /** | |
| 39 | - * 关闭 HTTP 服务器 | |
| 40 | - */ | |
| 41 | - public void stopServer() { | |
| 42 | - if (vertx != null) { | |
| 43 | - vertx.close(ar -> { | |
| 44 | - if (ar.succeeded()) { | |
| 45 | - log.info("[VertxService] Vert.x 关闭成功"); | |
| 46 | - } else { | |
| 47 | - log.error("[VertxService] Vert.x 关闭失败", ar.cause()); | |
| 48 | - } | |
| 49 | - }); | |
| 50 | - } | |
| 51 | - } | |
| 52 | -} |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/framework/downstream/IotDeviceDownstreamHandlerImpl.java renamed to 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
| 1 | -package cn.iocoder.yudao.module.iot.plugin.http.framework.downstream; | |
| 1 | +package cn.iocoder.yudao.module.iot.plugin.http.downstream; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | 4 | import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDevicePropertyGetReqDTO; |
| 5 | 5 | import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDevicePropertySetReqDTO; |
| 6 | 6 | import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDeviceServiceInvokeReqDTO; |
| 7 | -import cn.iocoder.yudao.module.iot.plugin.common.core.downstream.IotDeviceDownstreamHandler; | |
| 7 | +import cn.iocoder.yudao.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler; | |
| 8 | 8 | import org.springframework.stereotype.Component; |
| 9 | 9 | |
| 10 | 10 | @Component // TODO @芋艿:后续统一处理 | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/framework/package-info.java deleted
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
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot.plugin.http.upstream; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi; | |
| 4 | +import cn.iocoder.yudao.module.iot.plugin.http.upstream.router.IotDevicePropertyReportVertxHandler; | |
| 5 | +import io.vertx.core.Vertx; | |
| 6 | +import io.vertx.core.http.HttpServer; | |
| 7 | +import io.vertx.ext.web.Router; | |
| 8 | +import io.vertx.ext.web.handler.BodyHandler; | |
| 9 | +import lombok.extern.slf4j.Slf4j; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * IoT 设备下行服务端,接收来自 device 设备的请求,转发给 server 服务器 | |
| 13 | + * | |
| 14 | + * 协议:HTTP | |
| 15 | + * | |
| 16 | + * @author haohao | |
| 17 | + */ | |
| 18 | +@Slf4j | |
| 19 | +public class IotDeviceUpstreamServer { | |
| 20 | + | |
| 21 | + private final Vertx vertx; | |
| 22 | + private final HttpServer server; | |
| 23 | + private final Integer port; | |
| 24 | + | |
| 25 | + public IotDeviceUpstreamServer(Integer port, | |
| 26 | + IotDeviceUpstreamApi deviceUpstreamApi) { | |
| 27 | + this.port = port; | |
| 28 | + // 创建 Vertx 实例 | |
| 29 | + this.vertx = Vertx.vertx(); | |
| 30 | + // 创建 Router 实例 | |
| 31 | + Router router = Router.router(vertx); | |
| 32 | + router.route().handler(BodyHandler.create()); // 处理 Body | |
| 33 | + router.post(IotDevicePropertyReportVertxHandler.PATH) | |
| 34 | + .handler(new IotDevicePropertyReportVertxHandler(deviceUpstreamApi)); // 处理设备属性上报 | |
| 35 | + // 创建 HttpServer 实例 | |
| 36 | + this.server = vertx.createHttpServer().requestHandler(router); | |
| 37 | + } | |
| 38 | + | |
| 39 | + /** | |
| 40 | + * 启动 HTTP 服务器 | |
| 41 | + */ | |
| 42 | + public void start() { | |
| 43 | + log.info("[start][开始启动]"); | |
| 44 | + server.listen(port) | |
| 45 | + .toCompletionStage() | |
| 46 | + .toCompletableFuture() | |
| 47 | + .join(); | |
| 48 | + log.info("[start][启动完成,端口({})]", this.server.actualPort()); | |
| 49 | + } | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * 停止所有 | |
| 53 | + */ | |
| 54 | + public void stopServer() { | |
| 55 | + log.info("[stop][开始关闭]"); | |
| 56 | + try { | |
| 57 | + // 关闭 HTTP 服务器 | |
| 58 | + if (server != null) { | |
| 59 | + server.close() | |
| 60 | + .toCompletionStage() | |
| 61 | + .toCompletableFuture() | |
| 62 | + .join(); | |
| 63 | + } | |
| 64 | + | |
| 65 | + // 关闭 Vertx 实例 | |
| 66 | + if (vertx != null) { | |
| 67 | + vertx.close() | |
| 68 | + .toCompletionStage() | |
| 69 | + .toCompletableFuture() | |
| 70 | + .join(); | |
| 71 | + } | |
| 72 | + log.info("[stop][关闭完成]"); | |
| 73 | + } catch (Exception e) { | |
| 74 | + log.error("[stop][关闭异常]", e); | |
| 75 | + throw new RuntimeException(e); | |
| 76 | + } | |
| 77 | + } | |
| 78 | +} | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/framework/upstream/HttpVertxHandler.java renamed to 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
| 1 | -package cn.iocoder.yudao.module.iot.plugin.http.framework.upstream; | |
| 1 | +package cn.iocoder.yudao.module.iot.plugin.http.upstream.router; | |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.util.IdUtil; |
| 4 | 4 | import cn.hutool.json.JSONObject; |
| ... | ... | @@ -16,11 +16,13 @@ import java.time.LocalDateTime; |
| 16 | 16 | import java.util.Map; |
| 17 | 17 | |
| 18 | 18 | @Slf4j |
| 19 | -public class HttpVertxHandler implements Handler<RoutingContext> { | |
| 19 | +public class IotDevicePropertyReportVertxHandler implements Handler<RoutingContext> { | |
| 20 | + | |
| 21 | + public static final String PATH = "/sys/:productKey/:deviceName/thing/event/property/post"; | |
| 20 | 22 | |
| 21 | 23 | private final IotDeviceUpstreamApi deviceDataApi; |
| 22 | 24 | |
| 23 | - public HttpVertxHandler(IotDeviceUpstreamApi deviceDataApi) { | |
| 25 | + public IotDevicePropertyReportVertxHandler(IotDeviceUpstreamApi deviceDataApi) { | |
| 24 | 26 | this.deviceDataApi = deviceDataApi; |
| 25 | 27 | } |
| 26 | 28 | ... | ... |