From 30ae986c1af58d77ce94be1f25884eff61115cdd Mon Sep 17 00:00:00 2001 From: YunaiV Date: Thu, 30 Jan 2025 20:00:54 +0800 Subject: [PATCH] 【代码优化】IoT:整理 plugins 的依赖,以及对应 server 的启动逻辑 --- plugins/yudao-module-iot-plugin-http-1.0.0.jar | Bin 12522 -> 0 bytes yudao-module-iot/yudao-module-iot-api/pom.xml | 19 +++++++++---------- yudao-module-iot/yudao-module-iot-biz/pom.xml | 14 ++++---------- yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/aspect/TaosAspect.java | 39 --------------------------------------- yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/PluginStart.java | 49 ------------------------------------------------- yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/UnifiedConfiguration.java | 37 ------------------------------------- yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/config/UnifiedConfiguration.java | 37 +++++++++++++++++++++++++++++++++++++ yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/core/CustomPluginStateListener.java | 25 +++++++++++++++++++++++++ yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/core/PluginStart.java | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/listener/CustomPluginStateListener.java | 25 ------------------------- yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/tdengine/core/TaosAspect.java | 39 +++++++++++++++++++++++++++++++++++++++ yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/pom.xml | 21 +++++---------------- yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/pom.xml | 17 +++++------------ yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/HttpPluginSpringbootApplication.java | 13 +++++-------- 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 | 17 +++++++++-------- 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 | 8 +++++--- 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 | 8 ++++++++ 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 | 2 +- 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 | 17 ++++++++++------- 19 files changed, 211 insertions(+), 225 deletions(-) delete mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/aspect/TaosAspect.java delete mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/PluginStart.java delete mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/UnifiedConfiguration.java create mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/config/UnifiedConfiguration.java create mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/core/CustomPluginStateListener.java create mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/core/PluginStart.java delete mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/listener/CustomPluginStateListener.java create mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/tdengine/core/TaosAspect.java diff --git a/plugins/yudao-module-iot-plugin-http-1.0.0.jar b/plugins/yudao-module-iot-plugin-http-1.0.0.jar index c504342..2d1f7be 100644 Binary files a/plugins/yudao-module-iot-plugin-http-1.0.0.jar and b/plugins/yudao-module-iot-plugin-http-1.0.0.jar differ diff --git a/yudao-module-iot/yudao-module-iot-api/pom.xml b/yudao-module-iot/yudao-module-iot-api/pom.xml index 9b47273..ab49272 100644 --- a/yudao-module-iot/yudao-module-iot-api/pom.xml +++ b/yudao-module-iot/yudao-module-iot-api/pom.xml @@ -30,11 +30,17 @@ provided - - + + + com.fasterxml.jackson.core + jackson-databind + provided + + - org.pf4j + org.pf4j pf4j-spring + org.slf4j @@ -43,13 +49,6 @@ - - - com.fasterxml.jackson.core - jackson-databind - provided - - org.springframework.boot diff --git a/yudao-module-iot/yudao-module-iot-biz/pom.xml b/yudao-module-iot/yudao-module-iot-biz/pom.xml index 66710ae..1ac3f91 100644 --- a/yudao-module-iot/yudao-module-iot-biz/pom.xml +++ b/yudao-module-iot/yudao-module-iot-biz/pom.xml @@ -64,25 +64,19 @@ yudao-spring-boot-starter-excel - - - io.vertx - vertx-core - - + io.vertx vertx-web - + - org.eclipse.paho + org.eclipse.paho org.eclipse.paho.client.mqttv3 - - org.pf4j + org.pf4j pf4j-spring diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/aspect/TaosAspect.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/aspect/TaosAspect.java deleted file mode 100644 index 61b03dc..0000000 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/aspect/TaosAspect.java +++ /dev/null @@ -1,39 +0,0 @@ -package cn.iocoder.yudao.module.iot.framework.aspect; - -import lombok.extern.slf4j.Slf4j; -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.annotation.Around; -import org.aspectj.lang.annotation.Aspect; -import org.springframework.stereotype.Component; - -import java.sql.Timestamp; -import java.util.Map; - -// TODO @haohao:这个还需要的么? -/** - * TaosAspect 是一个处理 Taos 数据库返回值的切面。 - */ -@Aspect -@Component -@Slf4j -public class TaosAspect { - - @Around("execution(java.util.Map cn.iocoder.yudao.module.iot.dal.tdengine.*.*(..))") - public Object handleType(ProceedingJoinPoint joinPoint) { - Map result = null; - try { - result = (Map) joinPoint.proceed(); - result.replaceAll((key, value) -> { - if (value instanceof byte[]) { - return new String((byte[]) value); - } else if (value instanceof Timestamp) { - return ((Timestamp) value).getTime(); - } - return value; - }); - } catch (Throwable e) { - log.error("TaosAspect handleType error", e); - } - return result; - } -} diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/PluginStart.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/PluginStart.java deleted file mode 100644 index 8298789..0000000 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/PluginStart.java +++ /dev/null @@ -1,49 +0,0 @@ -package cn.iocoder.yudao.module.iot.framework.plugin; - -import cn.hutool.core.collection.CollUtil; -import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; -import cn.iocoder.yudao.module.iot.dal.dataobject.plugin.PluginInfoDO; -import cn.iocoder.yudao.module.iot.enums.plugin.IotPluginStatusEnum; -import cn.iocoder.yudao.module.iot.service.plugin.PluginInfoService; -import lombok.extern.slf4j.Slf4j; -import org.pf4j.spring.SpringPluginManager; -import org.springframework.boot.ApplicationArguments; -import org.springframework.boot.ApplicationRunner; -import org.springframework.stereotype.Component; - -import javax.annotation.Resource; -import java.util.List; - -// TODO @芋艿:需要 review 下 -@Component -@Slf4j -public class PluginStart implements ApplicationRunner { - - @Resource - private PluginInfoService pluginInfoService; - - @Resource - private SpringPluginManager pluginManager; - - @Override - public void run(ApplicationArguments args) { - TenantUtils.executeIgnore(() -> { // 1. 忽略租户上下文执行 - List pluginInfoList = pluginInfoService - .getPluginInfoListByStatus(IotPluginStatusEnum.RUNNING.getStatus()); // 2. 获取运行中的插件列表 - if (CollUtil.isEmpty(pluginInfoList)) { // 3. 检查插件列表是否为空 - log.info("[run] 没有需要启动的插件"); // 4. 日志记录没有插件需要启动 - return; - } - pluginInfoList.forEach(pluginInfo -> { // 5. 使用lambda表达式遍历插件列表 - try { - log.info("[run][启动插件] pluginKey = {}", pluginInfo.getPluginKey()); // 6. 日志记录插件启动信息 - pluginManager.startPlugin(pluginInfo.getPluginKey()); // 7. 启动插件 - } catch (Exception e) { - log.error("[run][启动插件失败] pluginKey = {}", pluginInfo.getPluginKey(), e); // 8. 记录启动失败的日志 - } - }); - }); - - } - -} diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/UnifiedConfiguration.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/UnifiedConfiguration.java deleted file mode 100644 index 0c12525..0000000 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/UnifiedConfiguration.java +++ /dev/null @@ -1,37 +0,0 @@ -package cn.iocoder.yudao.module.iot.framework.plugin; - -import cn.iocoder.yudao.module.iot.framework.plugin.listener.CustomPluginStateListener; -import lombok.extern.slf4j.Slf4j; -import org.pf4j.spring.SpringPluginManager; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import java.nio.file.Paths; - -// TODO @芋艿:需要 review 下 -@Slf4j -@Configuration -public class UnifiedConfiguration { - - @Value("${pf4j.pluginsDir:pluginsDir}") - private String pluginsDir; - - @Bean - public SpringPluginManager pluginManager() { - log.info("[init][实例化 SpringPluginManager]"); - SpringPluginManager springPluginManager = new SpringPluginManager(Paths.get(pluginsDir)) { -// SpringPluginManager springPluginManager = new SpringPluginManager() { - - @Override - public void startPlugins() { - // 禁用插件启动,避免插件启动时,启动所有插件 - log.info("[init][禁用默认启动所有插件]"); - } - - }; - springPluginManager.addPluginStateListener(new CustomPluginStateListener()); - return springPluginManager; - } - -} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/config/UnifiedConfiguration.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/config/UnifiedConfiguration.java new file mode 100644 index 0000000..849b6b1 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/config/UnifiedConfiguration.java @@ -0,0 +1,37 @@ +package cn.iocoder.yudao.module.iot.framework.plugin.config; + +import cn.iocoder.yudao.module.iot.framework.plugin.core.CustomPluginStateListener; +import lombok.extern.slf4j.Slf4j; +import org.pf4j.spring.SpringPluginManager; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.nio.file.Paths; + +// TODO @芋艿:需要 review 下 +@Slf4j +@Configuration +public class UnifiedConfiguration { + + @Value("${pf4j.pluginsDir:pluginsDir}") + private String pluginsDir; + + @Bean + public SpringPluginManager pluginManager() { + log.info("[init][实例化 SpringPluginManager]"); + SpringPluginManager springPluginManager = new SpringPluginManager(Paths.get(pluginsDir)) { +// SpringPluginManager springPluginManager = new SpringPluginManager() { + + @Override + public void startPlugins() { + // 禁用插件启动,避免插件启动时,启动所有插件 + log.info("[init][禁用默认启动所有插件]"); + } + + }; + springPluginManager.addPluginStateListener(new CustomPluginStateListener()); + return springPluginManager; + } + +} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/core/CustomPluginStateListener.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/core/CustomPluginStateListener.java new file mode 100644 index 0000000..9833a81 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/core/CustomPluginStateListener.java @@ -0,0 +1,25 @@ +package cn.iocoder.yudao.module.iot.framework.plugin.core; + +import lombok.extern.slf4j.Slf4j; +import org.pf4j.PluginStateEvent; +import org.pf4j.PluginStateListener; +import org.springframework.stereotype.Component; + +// TODO @芋艿:需要 review 下 +@Component +@Slf4j +public class CustomPluginStateListener implements PluginStateListener { + + @Override + public void pluginStateChanged(PluginStateEvent event) { + // 1. 获取插件ID + String pluginId = event.getPlugin().getPluginId(); + // 2. 获取插件旧状态 + String oldState = event.getOldState().toString(); + // 3. 获取插件新状态 + String newState = event.getPluginState().toString(); + // 4. 打印日志信息 + log.info("插件的状态 '{}' 已更改为 '{}' 至 '{}'", pluginId, oldState, newState); + } + +} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/core/PluginStart.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/core/PluginStart.java new file mode 100644 index 0000000..680d7c3 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/core/PluginStart.java @@ -0,0 +1,49 @@ +package cn.iocoder.yudao.module.iot.framework.plugin.core; + +import cn.hutool.core.collection.CollUtil; +import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; +import cn.iocoder.yudao.module.iot.dal.dataobject.plugin.PluginInfoDO; +import cn.iocoder.yudao.module.iot.enums.plugin.IotPluginStatusEnum; +import cn.iocoder.yudao.module.iot.service.plugin.PluginInfoService; +import lombok.extern.slf4j.Slf4j; +import org.pf4j.spring.SpringPluginManager; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.List; + +// TODO @芋艿:需要 review 下 +@Component +@Slf4j +public class PluginStart implements ApplicationRunner { + + @Resource + private PluginInfoService pluginInfoService; + + @Resource + private SpringPluginManager pluginManager; + + @Override + public void run(ApplicationArguments args) { + TenantUtils.executeIgnore(() -> { // 1. 忽略租户上下文执行 + List pluginInfoList = pluginInfoService + .getPluginInfoListByStatus(IotPluginStatusEnum.RUNNING.getStatus()); // 2. 获取运行中的插件列表 + if (CollUtil.isEmpty(pluginInfoList)) { // 3. 检查插件列表是否为空 + log.info("[run] 没有需要启动的插件"); // 4. 日志记录没有插件需要启动 + return; + } + pluginInfoList.forEach(pluginInfo -> { // 5. 使用lambda表达式遍历插件列表 + try { + log.info("[run][启动插件] pluginKey = {}", pluginInfo.getPluginKey()); // 6. 日志记录插件启动信息 + pluginManager.startPlugin(pluginInfo.getPluginKey()); // 7. 启动插件 + } catch (Exception e) { + log.error("[run][启动插件失败] pluginKey = {}", pluginInfo.getPluginKey(), e); // 8. 记录启动失败的日志 + } + }); + }); + + } + +} diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/listener/CustomPluginStateListener.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/listener/CustomPluginStateListener.java deleted file mode 100644 index 4542868..0000000 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/listener/CustomPluginStateListener.java +++ /dev/null @@ -1,25 +0,0 @@ -package cn.iocoder.yudao.module.iot.framework.plugin.listener; - -import lombok.extern.slf4j.Slf4j; -import org.pf4j.PluginStateEvent; -import org.pf4j.PluginStateListener; -import org.springframework.stereotype.Component; - -// TODO @芋艿:需要 review 下 -@Component -@Slf4j -public class CustomPluginStateListener implements PluginStateListener { - - @Override - public void pluginStateChanged(PluginStateEvent event) { - // 1. 获取插件ID - String pluginId = event.getPlugin().getPluginId(); - // 2. 获取插件旧状态 - String oldState = event.getOldState().toString(); - // 3. 获取插件新状态 - String newState = event.getPluginState().toString(); - // 4. 打印日志信息 - log.info("插件的状态 '{}' 已更改为 '{}' 至 '{}'", pluginId, oldState, newState); - } - -} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/tdengine/core/TaosAspect.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/tdengine/core/TaosAspect.java new file mode 100644 index 0000000..d83f34d --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/tdengine/core/TaosAspect.java @@ -0,0 +1,39 @@ +package cn.iocoder.yudao.module.iot.framework.tdengine.core; + +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springframework.stereotype.Component; + +import java.sql.Timestamp; +import java.util.Map; + +// TODO @haohao:这个还需要的么? +/** + * TaosAspect 是一个处理 Taos 数据库返回值的切面。 + */ +@Aspect +@Component +@Slf4j +public class TaosAspect { + + @Around("execution(java.util.Map cn.iocoder.yudao.module.iot.dal.tdengine.*.*(..))") + public Object handleType(ProceedingJoinPoint joinPoint) { + Map result = null; + try { + result = (Map) joinPoint.proceed(); + result.replaceAll((key, value) -> { + if (value instanceof byte[]) { + return new String((byte[]) value); + } else if (value instanceof Timestamp) { + return ((Timestamp) value).getTime(); + } + return value; + }); + } catch (Throwable e) { + log.error("TaosAspect handleType error", e); + } + return result; + } +} diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/pom.xml b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/pom.xml index c02d266..ccf010f 100644 --- a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/pom.xml +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/pom.xml @@ -18,35 +18,24 @@ - - cn.iocoder.boot - yudao-common + org.springframework.boot + spring-boot-starter - + cn.iocoder.boot yudao-module-iot-api ${revision} - - org.projectlombok - lombok - - + org.springframework spring-web - - - org.springframework.boot - spring-boot-starter - - - + io.vertx vertx-web diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/pom.xml b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/pom.xml index b939428..8da327c 100644 --- a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/pom.xml +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/pom.xml @@ -16,6 +16,7 @@ ${project.artifactId} + 物联网 插件模块 - http 插件 @@ -31,7 +32,7 @@ - + org.apache.maven.plugins maven-antrun-plugin @@ -75,7 +76,7 @@ - + org.apache.maven.plugins maven-jar-plugin @@ -94,7 +95,7 @@ - + org.springframework.boot spring-boot-maven-plugin @@ -121,21 +122,13 @@ - - cn.iocoder.boot yudao-module-iot-plugin-common ${revision} - - - org.springframework.boot - spring-boot-starter-web - - - + io.vertx vertx-web diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/HttpPluginSpringbootApplication.java b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/HttpPluginSpringbootApplication.java index f2dbbde..8f0c5c7 100644 --- a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/HttpPluginSpringbootApplication.java +++ b/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,9 +1,7 @@ package cn.iocoder.yudao.module.iot.plugin.http; -import cn.iocoder.yudao.module.iot.plugin.http.upstream.IotDeviceUpstreamServer; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; -import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @@ -15,16 +13,15 @@ import org.springframework.context.ConfigurableApplicationContext; public class HttpPluginSpringbootApplication { public static void main(String[] args) { - SpringApplication application = new SpringApplication(HttpPluginSpringbootApplication.class); - application.setWebApplicationType(WebApplicationType.NONE); - ConfigurableApplicationContext context = application.run(args); + ConfigurableApplicationContext context = SpringApplication.run(HttpPluginSpringbootApplication.class, args); // 手动获取 VertxService 并启动 // TODO @haohao:可以放在 bean 的 init 里么?回复:会和插件模式冲突 @芋艿,测试下 - IotDeviceUpstreamServer vertxService = context.getBean(IotDeviceUpstreamServer.class); - vertxService.start(); + // TODO @haohao:貌似去掉,没有问题额。。。 +// IotDeviceUpstreamServer vertxService = context.getBean(IotDeviceUpstreamServer.class); +// vertxService.start(); - log.info("[HttpPluginSpringbootApplication] 独立模式启动完成"); + log.info("[main][独立模式启动完成]"); } } \ No newline at end of file diff --git a/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 b/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 index 0da7241..fe789af 100644 --- a/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 +++ b/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; import cn.hutool.core.lang.Assert; import cn.hutool.extra.spring.SpringUtil; import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi; -import cn.iocoder.yudao.module.iot.plugin.http.upstream.IotDeviceUpstreamServer; import lombok.extern.slf4j.Slf4j; import org.pf4j.PluginWrapper; import org.pf4j.spring.SpringPlugin; @@ -11,6 +10,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +// TODO @芋艿:完善注释 /** * 负责插件的启动和停止,与 Vert.x 的生命周期管理 */ @@ -30,8 +30,9 @@ public class IotHttpVertxPlugin extends SpringPlugin { Assert.notNull(pluginContext, "pluginContext 不能为空"); // 2. 启动 Vert.x - IotDeviceUpstreamServer vertxService = pluginContext.getBean(IotDeviceUpstreamServer.class); - vertxService.start(); + // TODO @haohao:貌似去掉,没有问题额。。。 +// IotDeviceUpstreamServer vertxService = pluginContext.getBean(IotDeviceUpstreamServer.class); +// vertxService.start(); log.info("[HttpVertxPlugin][HttpVertxPlugin 插件启动成功...]"); } catch (Exception e) { @@ -44,11 +45,11 @@ public class IotHttpVertxPlugin extends SpringPlugin { log.info("[HttpVertxPlugin][HttpVertxPlugin 插件停止开始...]"); try { // 停止服务器 - ApplicationContext pluginContext = getApplicationContext(); - if (pluginContext != null) { - IotDeviceUpstreamServer vertxService = pluginContext.getBean(IotDeviceUpstreamServer.class); - vertxService.stopServer(); - } +// ApplicationContext pluginContext = getApplicationContext(); +// if (pluginContext != null) { +// IotDeviceUpstreamServer vertxService = pluginContext.getBean(IotDeviceUpstreamServer.class); +// vertxService.stop(); +// } log.info("[HttpVertxPlugin][HttpVertxPlugin 插件停止成功...]"); } catch (Exception e) { diff --git a/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 b/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 index 3ecc2dc..388ad8a 100644 --- a/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 +++ b/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; import org.springframework.context.annotation.Configuration; /** - * 插件与独立运行都可复用的配置类 + * IoT 插件 HTTP 的专用自动配置类 + * + * @author haohao */ @Configuration public class IotPluginHttpAutoConfiguration { @@ -19,8 +21,8 @@ public class IotPluginHttpAutoConfiguration { @Value("${plugin.http.server.port:8092}") private Integer port; - @Bean - public IotDeviceUpstreamServer vertxService(IotDeviceUpstreamApi deviceUpstreamApi) { + @Bean(initMethod = "start", destroyMethod = "stop") + public IotDeviceUpstreamServer deviceUpstreamServer(IotDeviceUpstreamApi deviceUpstreamApi) { return new IotDeviceUpstreamServer(port, deviceUpstreamApi); } diff --git a/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 b/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 index 91604c6..816a172 100644 --- a/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 +++ b/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 import cn.iocoder.yudao.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler; import org.springframework.stereotype.Component; +/** + * HTTP 插件的 {@link IotDeviceDownstreamHandler} 实现类 + * + * 但是:由于设备通过 HTTP 短链接接入,导致其实无法下行指导给 device 设备,所以基本都是直接返回失败!!! + * 类似 MQTT、WebSocket、TCP 插件,是可以实现下行指令的。 + * + * @author 芋道源码 + */ @Component // TODO @芋艿:后续统一处理 public class IotDeviceDownstreamHandlerImpl implements IotDeviceDownstreamHandler { diff --git a/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 b/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 index 113c54e..e6f4dbb 100644 --- a/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 +++ b/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 { /** * 停止所有 */ - public void stopServer() { + public void stop() { log.info("[stop][开始关闭]"); try { // 关闭 HTTP 服务器 diff --git a/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 b/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 index 91700f4..e35d13b 100644 --- a/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 +++ b/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; import io.vertx.core.Handler; import io.vertx.ext.web.RequestBody; import io.vertx.ext.web.RoutingContext; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import java.time.LocalDateTime; import java.util.Map; +/** + * IoT 设备属性上报的 Vert.x Handler + */ +@RequiredArgsConstructor @Slf4j public class IotDevicePropertyReportVertxHandler implements Handler { public static final String PATH = "/sys/:productKey/:deviceName/thing/event/property/post"; - private final IotDeviceUpstreamApi deviceDataApi; - - public IotDevicePropertyReportVertxHandler(IotDeviceUpstreamApi deviceDataApi) { - this.deviceDataApi = deviceDataApi; - } + private final IotDeviceUpstreamApi deviceUpstreamApi; @Override public void handle(RoutingContext ctx) { @@ -51,7 +52,7 @@ public class IotDevicePropertyReportVertxHandler implements Handler