From d83af87f9f9bb7795eb030aef45ca502ce59d039 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 12 Apr 2025 21:05:49 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BB=A3=E7=A0=81=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E3=80=91IoT=EF=BC=9A=E7=BD=91=E7=BB=9C=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E7=9B=B8=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/IotProductScriptController.java | 2 ++ .../plugin/IotPluginConfigServiceImpl.java | 1 + .../IotNetComponentCommonAutoConfiguration.java | 1 + .../core/constants/IotDeviceTopicEnum.java | 8 +++++++- .../component/core/message/IotAlinkMessage.java | 1 + .../component/core/pojo/IotStandardResponse.java | 2 +- .../IotNetComponentEmqxAutoConfiguration.java | 7 ++++--- .../config/IotNetComponentEmqxProperties.java | 2 ++ .../emqx/upstream/IotDeviceUpstreamServer.java | 1 + .../router/IotDeviceMqttMessageHandler.java | 4 +++- .../IotNetComponentHttpAutoConfiguration.java | 4 +++- .../upstream/auth/IotDeviceAuthProvider.java | 3 ++- .../router/IotDeviceUpstreamVertxHandler.java | 16 +++++++--------- .../IotNetComponentServerConfiguration.java | 2 ++ .../server/controller/HealthController.java | 3 ++- .../heartbeat/IotComponentHeartbeatJob.java | 4 +--- yudao-module-iot/yudao-module-iot-script/pom.xml | 1 + .../yudao/module/iot/script/ScriptExample.java | 1 + .../module/iot/script/engine/JsScriptEngine.java | 11 ++++++++--- .../iot/script/engine/ScriptEngineFactory.java | 1 - .../iot/script/example/GraalJsExample.java | 1 + .../module/iot/script/sandbox/JsSandbox.java | 1 + .../iot/script/service/ScriptServiceImpl.java | 1 + .../module/iot/script/util/ScriptUtils.java | 1 + 24 files changed, 54 insertions(+), 25 deletions(-) diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/IotProductScriptController.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/IotProductScriptController.java index ca8666d730..92e52a39f0 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/IotProductScriptController.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/IotProductScriptController.java @@ -107,6 +107,7 @@ public class IotProductScriptController { @PreAuthorize("@ss.hasPermission('iot:product-script:query')") public CommonResult getSampleScript(@RequestParam("type") Integer type) { String sample; + // TODO @haohao:要不枚举下? switch (type) { case 1: sample = scriptSamples.getPropertyParserSample(); @@ -118,6 +119,7 @@ public class IotProductScriptController { sample = scriptSamples.getCommandEncoderSample(); break; default: + // TODO @haohao:不支持,返回 error 会不会好点哈?例如说,参数不正确; sample = "// 不支持的脚本类型"; } return success(sample); diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/plugin/IotPluginConfigServiceImpl.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/plugin/IotPluginConfigServiceImpl.java index 3b5e9e2e01..f7cb0972ae 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/plugin/IotPluginConfigServiceImpl.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/plugin/IotPluginConfigServiceImpl.java @@ -17,6 +17,7 @@ import java.util.List; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*; + /** * IoT 插件配置 Service 实现类 * diff --git a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-core/src/main/java/cn/iocoder/yudao/module/iot/net/component/core/config/IotNetComponentCommonAutoConfiguration.java b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-core/src/main/java/cn/iocoder/yudao/module/iot/net/component/core/config/IotNetComponentCommonAutoConfiguration.java index d880df5cfb..5208c1e66f 100644 --- a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-core/src/main/java/cn/iocoder/yudao/module/iot/net/component/core/config/IotNetComponentCommonAutoConfiguration.java +++ b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-core/src/main/java/cn/iocoder/yudao/module/iot/net/component/core/config/IotNetComponentCommonAutoConfiguration.java @@ -13,6 +13,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean; import org.springframework.scheduling.annotation.EnableScheduling; +// TODO @haohao:应该不用写 spring.factories 拉,因为被 imports 替代啦 /** * IoT 网络组件的通用自动配置类 * diff --git a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-core/src/main/java/cn/iocoder/yudao/module/iot/net/component/core/constants/IotDeviceTopicEnum.java b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-core/src/main/java/cn/iocoder/yudao/module/iot/net/component/core/constants/IotDeviceTopicEnum.java index 00e1142458..9429133a5f 100644 --- a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-core/src/main/java/cn/iocoder/yudao/module/iot/net/component/core/constants/IotDeviceTopicEnum.java +++ b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-core/src/main/java/cn/iocoder/yudao/module/iot/net/component/core/constants/IotDeviceTopicEnum.java @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.iot.net.component.core.constants; import lombok.Getter; +// TODO @haohao:要不放到 enums 包下; /** * IoT 设备主题枚举 *

@@ -12,6 +13,7 @@ import lombok.Getter; @Getter public enum IotDeviceTopicEnum { + // TODO @haohao:SYS_TOPIC_PREFIX、SERVICE_TOPIC_PREFIX、REPLY_SUFFIX 类似这种,要不搞成这个里面的静态变量?不是枚举值 /** * 系统主题前缀 */ @@ -22,6 +24,7 @@ public enum IotDeviceTopicEnum { */ SERVICE_TOPIC_PREFIX("/thing/service/", "服务调用主题前缀"), + // TODO @haohao:注释时,中英文之间,有个空格; /** * 设备属性设置主题 * 请求Topic:/sys/${productKey}/${deviceName}/thing/service/property/set @@ -75,6 +78,7 @@ public enum IotDeviceTopicEnum { private final String topic; private final String description; + // TODO @haohao:使用 lombok 去除 IotDeviceTopicEnum(String topic, String description) { this.topic = topic; this.description = description; @@ -89,6 +93,7 @@ public enum IotDeviceTopicEnum { * @return 完整的主题路径 */ public static String buildServiceTopic(String productKey, String deviceName, String serviceIdentifier) { + // TODO @haohao:貌似 SYS_TOPIC_PREFIX.getTopic() + productKey + "/" + deviceName 是统一的; return SYS_TOPIC_PREFIX.getTopic() + productKey + "/" + deviceName + SERVICE_TOPIC_PREFIX.getTopic() + serviceIdentifier; } @@ -127,7 +132,7 @@ public enum IotDeviceTopicEnum { } /** - * 构建设备OTA升级主题 + * 构建设备 OTA 升级主题 * * @param productKey 产品Key * @param deviceName 设备名称 @@ -170,4 +175,5 @@ public enum IotDeviceTopicEnum { public static String getReplyTopic(String requestTopic) { return requestTopic + REPLY_SUFFIX.getTopic(); } + } \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-core/src/main/java/cn/iocoder/yudao/module/iot/net/component/core/message/IotAlinkMessage.java b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-core/src/main/java/cn/iocoder/yudao/module/iot/net/component/core/message/IotAlinkMessage.java index f997f91f58..3aa07d4b24 100644 --- a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-core/src/main/java/cn/iocoder/yudao/module/iot/net/component/core/message/IotAlinkMessage.java +++ b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-core/src/main/java/cn/iocoder/yudao/module/iot/net/component/core/message/IotAlinkMessage.java @@ -11,6 +11,7 @@ import java.util.Map; * IoT Alink 消息模型 *

* 基于阿里云 Alink 协议规范实现的标准消息格式 + * @see 阿里云物联网 —— Alink 协议 * * @author haohao */ diff --git a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-core/src/main/java/cn/iocoder/yudao/module/iot/net/component/core/pojo/IotStandardResponse.java b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-core/src/main/java/cn/iocoder/yudao/module/iot/net/component/core/pojo/IotStandardResponse.java index 1e14c37ca0..5959072a4e 100644 --- a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-core/src/main/java/cn/iocoder/yudao/module/iot/net/component/core/pojo/IotStandardResponse.java +++ b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-core/src/main/java/cn/iocoder/yudao/module/iot/net/component/core/pojo/IotStandardResponse.java @@ -12,7 +12,7 @@ import lombok.experimental.Accessors; * @author haohao */ @Data -@Accessors(chain = true) +@Accessors(chain = true) // TODO @haohao:貌似不用写 @Accessors(chain = true),我全局加啦,可见 lombok.config public class IotStandardResponse { /** diff --git a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-emqx/src/main/java/cn/iocoder/yudao/module/iot/net/component/emqx/config/IotNetComponentEmqxAutoConfiguration.java b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-emqx/src/main/java/cn/iocoder/yudao/module/iot/net/component/emqx/config/IotNetComponentEmqxAutoConfiguration.java index bd6f88df3d..a20daf2518 100644 --- a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-emqx/src/main/java/cn/iocoder/yudao/module/iot/net/component/emqx/config/IotNetComponentEmqxAutoConfiguration.java +++ b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-emqx/src/main/java/cn/iocoder/yudao/module/iot/net/component/emqx/config/IotNetComponentEmqxAutoConfiguration.java @@ -28,13 +28,13 @@ import org.springframework.context.event.EventListener; * * @author haohao */ -@Slf4j @AutoConfiguration @EnableConfigurationProperties(IotNetComponentEmqxProperties.class) -@ConditionalOnProperty(prefix = "yudao.iot.component.emqx", name = "enabled", havingValue = "true", matchIfMissing = false) +@ConditionalOnProperty(prefix = "yudao.iot.component.emqx", name = "enabled", havingValue = "true") @ComponentScan(basePackages = { "cn.iocoder.yudao.module.iot.net.component.emqx" // 只扫描 EMQX 组件包 -}) +}) // TODO @haohao:自动配置后,不需要这个哈。 +@Slf4j public class IotNetComponentEmqxAutoConfiguration { /** @@ -42,6 +42,7 @@ public class IotNetComponentEmqxAutoConfiguration { */ private static final String PLUGIN_KEY = "emqx"; + // TODO @haohao:这个是不是要去掉哈。 public IotNetComponentEmqxAutoConfiguration() { // 构造函数中不输出日志,移到 initialize 方法中 } diff --git a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-emqx/src/main/java/cn/iocoder/yudao/module/iot/net/component/emqx/config/IotNetComponentEmqxProperties.java b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-emqx/src/main/java/cn/iocoder/yudao/module/iot/net/component/emqx/config/IotNetComponentEmqxProperties.java index d300bb70d3..7b230f5e5a 100644 --- a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-emqx/src/main/java/cn/iocoder/yudao/module/iot/net/component/emqx/config/IotNetComponentEmqxProperties.java +++ b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-emqx/src/main/java/cn/iocoder/yudao/module/iot/net/component/emqx/config/IotNetComponentEmqxProperties.java @@ -64,6 +64,7 @@ public class IotNetComponentEmqxProperties { @NotNull(message = "认证端口不能为空") private Integer authPort; + // TODO @haohao:可以使用 Duration 类型,可读性更好 /** * 重连延迟时间(毫秒) *

@@ -77,4 +78,5 @@ public class IotNetComponentEmqxProperties { * 默认值:10000 毫秒 */ private Integer connectionTimeoutMs = 10000; + } \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-emqx/src/main/java/cn/iocoder/yudao/module/iot/net/component/emqx/upstream/IotDeviceUpstreamServer.java b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-emqx/src/main/java/cn/iocoder/yudao/module/iot/net/component/emqx/upstream/IotDeviceUpstreamServer.java index 76d8f9e7eb..71aee5847b 100644 --- a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-emqx/src/main/java/cn/iocoder/yudao/module/iot/net/component/emqx/upstream/IotDeviceUpstreamServer.java +++ b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-emqx/src/main/java/cn/iocoder/yudao/module/iot/net/component/emqx/upstream/IotDeviceUpstreamServer.java @@ -82,6 +82,7 @@ public class IotDeviceUpstreamServer { log.info("[start][开始启动服务]"); // 检查 authPort 是否为 null + // TODO @haohao:authPort 里面搞默认值?包括下面,这个类不搞任何默认值,都交给 emqxProperties Integer authPort = emqxProperties.getAuthPort(); if (authPort == null) { log.warn("[start][authPort 为 null,使用默认端口 8080]"); diff --git a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-emqx/src/main/java/cn/iocoder/yudao/module/iot/net/component/emqx/upstream/router/IotDeviceMqttMessageHandler.java b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-emqx/src/main/java/cn/iocoder/yudao/module/iot/net/component/emqx/upstream/router/IotDeviceMqttMessageHandler.java index 66c38dfe15..d61e41b567 100644 --- a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-emqx/src/main/java/cn/iocoder/yudao/module/iot/net/component/emqx/upstream/router/IotDeviceMqttMessageHandler.java +++ b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-emqx/src/main/java/cn/iocoder/yudao/module/iot/net/component/emqx/upstream/router/IotDeviceMqttMessageHandler.java @@ -30,6 +30,7 @@ import java.util.Map; @Slf4j public class IotDeviceMqttMessageHandler { + // TODO @haohao:下面的,有办法也抽到 IotDeviceTopicEnum 么?想的是,尽量把这些 method、topic、url 统一化; private static final String PROPERTY_METHOD = "thing.event.property.post"; private static final String EVENT_METHOD_PREFIX = "thing.event."; private static final String EVENT_METHOD_SUFFIX = ".post"; @@ -223,6 +224,7 @@ public class IotDeviceMqttMessageHandler { * @return 设备属性上报请求对象 */ private IotDevicePropertyReportReqDTO buildPropertyReportDTO(JSONObject jsonObject, String[] topicParts) { + // TODO @haohao:IotDevicePropertyReportReqDTO 可以考虑链式哈。其它也是,尽量让同类参数在一行;这样,阅读起来更聚焦; IotDevicePropertyReportReqDTO reportReqDTO = new IotDevicePropertyReportReqDTO(); reportReqDTO.setRequestId(jsonObject.getStr("id")); reportReqDTO.setProcessId(IotNetComponentCommonUtils.getProcessId()); @@ -230,7 +232,7 @@ public class IotDeviceMqttMessageHandler { reportReqDTO.setProductKey(topicParts[2]); reportReqDTO.setDeviceName(topicParts[3]); - // 只使用标准JSON格式处理属性数据 + // 只使用标准 JSON格式处理属性数据 JSONObject params = jsonObject.getJSONObject("params"); if (params == null) { log.warn("[buildPropertyReportDTO][消息格式不正确,缺少params字段][jsonObject: {}]", jsonObject); diff --git a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-http/src/main/java/cn/iocoder/yudao/module/iot/net/component/http/config/IotNetComponentHttpAutoConfiguration.java b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-http/src/main/java/cn/iocoder/yudao/module/iot/net/component/http/config/IotNetComponentHttpAutoConfiguration.java index 2b3150c8be..1aa5903d47 100644 --- a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-http/src/main/java/cn/iocoder/yudao/module/iot/net/component/http/config/IotNetComponentHttpAutoConfiguration.java +++ b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-http/src/main/java/cn/iocoder/yudao/module/iot/net/component/http/config/IotNetComponentHttpAutoConfiguration.java @@ -61,13 +61,14 @@ public class IotNetComponentHttpAutoConfiguration { // 设置当前组件的核心标识 // 注意:这里只为当前 HTTP 组件设置 pluginKey,不影响其他组件 + // TODO @haohao:多个会存在冲突的问题哇? commonProperties.setPluginKey(PLUGIN_KEY); // 将 HTTP 组件注册到组件注册表 componentRegistry.registerComponent( PLUGIN_KEY, SystemUtil.getHostInfo().getAddress(), - 0, // 内嵌模式固定为 0 + 0, // 内嵌模式固定为 0:自动生成对应的 port 端口号 IotNetComponentCommonUtils.getProcessId()); log.info("[initialize][IoT HTTP 组件初始化完成]"); @@ -115,4 +116,5 @@ public class IotNetComponentHttpAutoConfiguration { public IotDeviceDownstreamHandler deviceDownstreamHandler() { return new IotDeviceDownstreamHandlerImpl(); } + } diff --git a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-http/src/main/java/cn/iocoder/yudao/module/iot/net/component/http/upstream/auth/IotDeviceAuthProvider.java b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-http/src/main/java/cn/iocoder/yudao/module/iot/net/component/http/upstream/auth/IotDeviceAuthProvider.java index 13977da7d1..10b00cd6b1 100644 --- a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-http/src/main/java/cn/iocoder/yudao/module/iot/net/component/http/upstream/auth/IotDeviceAuthProvider.java +++ b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-http/src/main/java/cn/iocoder/yudao/module/iot/net/component/http/upstream/auth/IotDeviceAuthProvider.java @@ -5,12 +5,13 @@ import io.vertx.ext.web.RoutingContext; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationContext; +// TODO @haohao:待实现,或者不需要? /** * IoT 设备认证提供者 *

* 用于 HTTP 设备接入时的身份认证 * - * @author 芋道源码 + * @author haohao */ @Slf4j public class IotDeviceAuthProvider { diff --git a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-http/src/main/java/cn/iocoder/yudao/module/iot/net/component/http/upstream/router/IotDeviceUpstreamVertxHandler.java b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-http/src/main/java/cn/iocoder/yudao/module/iot/net/component/http/upstream/router/IotDeviceUpstreamVertxHandler.java index 86c2e9dc13..85bfdc0be4 100644 --- a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-http/src/main/java/cn/iocoder/yudao/module/iot/net/component/http/upstream/router/IotDeviceUpstreamVertxHandler.java +++ b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-http/src/main/java/cn/iocoder/yudao/module/iot/net/component/http/upstream/router/IotDeviceUpstreamVertxHandler.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.iot.net.component.http.upstream.router; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.ObjUtil; @@ -35,6 +36,8 @@ import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeC @Slf4j public class IotDeviceUpstreamVertxHandler implements Handler { + // TODO @haohao:你说,咱要不要把 "/sys/:productKey/:deviceName" + // + IotDeviceTopicEnum.PROPERTY_POST_TOPIC.getTopic(),也抽到 IotDeviceTopicEnum 的 build 这种?尽量都收敛掉? /** * 属性上报路径 */ @@ -254,8 +257,8 @@ public class IotDeviceUpstreamVertxHandler implements Handler { return PROPERTY_METHOD; } - return EVENT_METHOD_PREFIX + - (routingContext.pathParams().containsKey("identifier") + return EVENT_METHOD_PREFIX + + (routingContext.pathParams().containsKey("identifier") ? routingContext.pathParam("identifier") : "unknown") + @@ -275,7 +278,6 @@ public class IotDeviceUpstreamVertxHandler implements Handler { .setReportTime(LocalDateTime.now()) .setProductKey(productKey) .setDeviceName(deviceName)).setState(IotDeviceStateEnum.ONLINE.getState()); - deviceUpstreamApi.updateDeviceState(reqDTO); } @@ -311,8 +313,7 @@ public class IotDeviceUpstreamVertxHandler implements Handler { private Map parsePropertiesFromBody(JsonObject body) { Map properties = MapUtil.newHashMap(); JsonObject params = body.getJsonObject("params"); - - if (params == null) { + if (CollUtil.isEmpty(params)) { return properties; } @@ -327,7 +328,6 @@ public class IotDeviceUpstreamVertxHandler implements Handler { properties.put(key, valueObj); } } - return properties; } @@ -364,15 +364,13 @@ public class IotDeviceUpstreamVertxHandler implements Handler { private Map parseParamsFromBody(JsonObject body) { Map params = MapUtil.newHashMap(); JsonObject paramsJson = body.getJsonObject("params"); - - if (paramsJson == null) { + if (CollUtil.isEmpty(paramsJson)) { return params; } for (String key : paramsJson.fieldNames()) { params.put(key, paramsJson.getValue(key)); } - return params; } } \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-server/src/main/java/cn/iocoder/yudao/module/iot/net/component/server/config/IotNetComponentServerConfiguration.java b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-server/src/main/java/cn/iocoder/yudao/module/iot/net/component/server/config/IotNetComponentServerConfiguration.java index 513e8693ef..abec49908d 100644 --- a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-server/src/main/java/cn/iocoder/yudao/module/iot/net/component/server/config/IotNetComponentServerConfiguration.java +++ b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-server/src/main/java/cn/iocoder/yudao/module/iot/net/component/server/config/IotNetComponentServerConfiguration.java @@ -32,6 +32,7 @@ public class IotNetComponentServerConfiguration { * @return RestTemplate */ @Bean + // TODO @haohao:貌似要独立一个 restTemplate 的名字?不然容易冲突; public RestTemplate restTemplate(IotNetComponentServerProperties properties) { return new RestTemplateBuilder() .connectTimeout(properties.getUpstreamConnectTimeout()) @@ -104,6 +105,7 @@ public class IotNetComponentServerConfiguration { return new Object(); } + // TODO @haohao:这个是不是木有用呀? /** * 配置默认的组件实例注册客户端 * diff --git a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-server/src/main/java/cn/iocoder/yudao/module/iot/net/component/server/controller/HealthController.java b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-server/src/main/java/cn/iocoder/yudao/module/iot/net/component/server/controller/HealthController.java index e30da459ea..4f652dae96 100644 --- a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-server/src/main/java/cn/iocoder/yudao/module/iot/net/component/server/controller/HealthController.java +++ b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-server/src/main/java/cn/iocoder/yudao/module/iot/net/component/server/controller/HealthController.java @@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; +// TODO @haohao:这个是必须的哇?可以考虑基于 spring boot actuator; /** * 健康检查接口 * @@ -29,4 +30,4 @@ public class HealthController { result.put("timestamp", System.currentTimeMillis()); return result; } -} \ No newline at end of file +} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-server/src/main/java/cn/iocoder/yudao/module/iot/net/component/server/heartbeat/IotComponentHeartbeatJob.java b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-server/src/main/java/cn/iocoder/yudao/module/iot/net/component/server/heartbeat/IotComponentHeartbeatJob.java index a76d72b43c..624d8f1ba8 100644 --- a/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-server/src/main/java/cn/iocoder/yudao/module/iot/net/component/server/heartbeat/IotComponentHeartbeatJob.java +++ b/yudao-module-iot/yudao-module-iot-net-components/yudao-module-iot-net-component-server/src/main/java/cn/iocoder/yudao/module/iot/net/component/server/heartbeat/IotComponentHeartbeatJob.java @@ -7,14 +7,12 @@ import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotPluginInst import cn.iocoder.yudao.module.iot.net.component.server.config.IotNetComponentServerProperties; import cn.iocoder.yudao.module.iot.net.component.server.downstream.IotComponentDownstreamServer; import lombok.extern.slf4j.Slf4j; -import org.springframework.scheduling.annotation.Scheduled; -import java.time.LocalDateTime; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import java.lang.ProcessHandle; +// TODO @haohao:有办法服用 yudao-module-iot-net-component-core 的么?就是 server,只是一个启动器,没什么特殊的功能; /** * IoT 组件心跳任务 *

diff --git a/yudao-module-iot/yudao-module-iot-script/pom.xml b/yudao-module-iot/yudao-module-iot-script/pom.xml index 8b46914a2d..92b51be680 100644 --- a/yudao-module-iot/yudao-module-iot-script/pom.xml +++ b/yudao-module-iot/yudao-module-iot-script/pom.xml @@ -44,6 +44,7 @@ + org.graalvm.sdk graal-sdk diff --git a/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/ScriptExample.java b/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/ScriptExample.java index 7a90251836..85e04cf527 100644 --- a/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/ScriptExample.java +++ b/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/ScriptExample.java @@ -10,6 +10,7 @@ import org.springframework.stereotype.Component; import java.util.Map; +// TODO @haohao:挪到 test 目录下 /** * 脚本使用示例类 */ diff --git a/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/engine/JsScriptEngine.java b/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/engine/JsScriptEngine.java index 222c56eb5a..0453ccf8a4 100644 --- a/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/engine/JsScriptEngine.java +++ b/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/engine/JsScriptEngine.java @@ -63,6 +63,7 @@ public class JsScriptEngine extends AbstractScriptEngine implements AutoCloseabl .build(); // 创建隔离的临时目录路径 + // TODO @haohao:貌似没用到? Path tempDirectory = Path.of(System.getProperty("java.io.tmpdir"), "graaljs-" + IdUtil.fastSimpleUUID()); // 初始化 GraalJS 上下文 @@ -94,6 +95,7 @@ public class JsScriptEngine extends AbstractScriptEngine implements AutoCloseabl Source source = getOrCreateSource(script); // 执行脚本并捕获结果,添加超时控制 + // TODO @haohao:通过线程池 + future 会好点? Value result; Thread executionThread = Thread.currentThread(); Thread watchdogThread = new Thread(() -> { @@ -236,11 +238,14 @@ public class JsScriptEngine extends AbstractScriptEngine implements AutoCloseabl if (result.isNumber()) { if (result.fitsInInt()) { return result.asInt(); - } else if (result.fitsInLong()) { + } + if (result.fitsInLong()) { return result.asLong(); - } else if (result.fitsInFloat()) { + } + if (result.fitsInFloat()) { return result.asFloat(); - } else if (result.fitsInDouble()) { + } + if (result.fitsInDouble()) { return result.asDouble(); } } diff --git a/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/engine/ScriptEngineFactory.java b/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/engine/ScriptEngineFactory.java index 25cdc85c7c..e6364f8467 100644 --- a/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/engine/ScriptEngineFactory.java +++ b/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/engine/ScriptEngineFactory.java @@ -79,7 +79,6 @@ public class ScriptEngineFactory implements DisposableBean { @Override public void destroy() { - // 应用关闭时,释放所有引擎资源 log.info("应用关闭,释放所有脚本引擎资源..."); releaseAllEngines(); } diff --git a/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/example/GraalJsExample.java b/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/example/GraalJsExample.java index 445b4410be..636f96b72d 100644 --- a/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/example/GraalJsExample.java +++ b/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/example/GraalJsExample.java @@ -14,6 +14,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +// TODO @haohao:搞到 test 里面哈; /** * GraalJS 脚本引擎示例 *

diff --git a/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/sandbox/JsSandbox.java b/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/sandbox/JsSandbox.java index 299f152c7f..0483b35053 100644 --- a/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/sandbox/JsSandbox.java +++ b/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/sandbox/JsSandbox.java @@ -56,6 +56,7 @@ public class JsSandbox implements ScriptSandbox { */ public JsSandbox() { // 初始化 Java 相关的不安全关键字 + // TODO @haohao:可以使用 addAll 哈。 Arrays.asList( "java.lang.System", "java.io", diff --git a/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/service/ScriptServiceImpl.java b/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/service/ScriptServiceImpl.java index b21136affb..044e55e3db 100644 --- a/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/service/ScriptServiceImpl.java +++ b/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/service/ScriptServiceImpl.java @@ -40,6 +40,7 @@ public class ScriptServiceImpl implements ScriptService { try { return engine.execute(script, context); } catch (Exception e) { + // TODO @haohao:最好打印一些参数;下面类似的也是 log.error("执行脚本失败: {}", e.getMessage(), e); throw new RuntimeException("执行脚本失败: " + e.getMessage(), e); } diff --git a/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/util/ScriptUtils.java b/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/util/ScriptUtils.java index acf51115f1..bb6af5d34b 100644 --- a/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/util/ScriptUtils.java +++ b/yudao-module-iot/yudao-module-iot-script/src/main/java/cn/iocoder/yudao/module/iot/script/util/ScriptUtils.java @@ -28,6 +28,7 @@ public class ScriptUtils { return INSTANCE; } + // TODO @haohao:使用 lombok 简化掉 private ScriptUtils() { // 私有构造函数 }