diff --git a/yudao-dependencies/pom.xml b/yudao-dependencies/pom.xml index ae72b994ad..82afd1f09b 100644 --- a/yudao-dependencies/pom.xml +++ b/yudao-dependencies/pom.xml @@ -68,6 +68,8 @@ 4.2.9.Final 1.2.5 4.5.22 + 4.12.0 + 3.12.0 2.40.15 1.16.7 @@ -76,7 +78,6 @@ 2.3.0 4.7.9-20251224.161447 4.40.607.ALL - 4.12.0 @@ -556,6 +557,50 @@ ${jsoup.version} + + + io.vertx + vertx-core + ${vertx.version} + + + io.vertx + vertx-web + ${vertx.version} + + + io.vertx + vertx-mqtt + ${vertx.version} + + + + + org.eclipse.paho + org.eclipse.paho.client.mqttv3 + ${mqtt.version} + + + + + com.squareup.okhttp3 + okhttp + ${okhttp.version} + + + com.squareup.okhttp3 + mockwebserver + ${okhttp.version} + test + + + + + org.eclipse.californium + californium-core + ${californium.version} + + software.amazon.awssdk @@ -630,43 +675,6 @@ - - - - io.vertx - vertx-core - ${vertx.version} - - - io.vertx - vertx-web - ${vertx.version} - - - io.vertx - vertx-mqtt - ${vertx.version} - - - - - org.eclipse.paho - org.eclipse.paho.client.mqttv3 - ${mqtt.version} - - - - - com.squareup.okhttp3 - okhttp - ${okhttp.version} - - - com.squareup.okhttp3 - mockwebserver - ${okhttp.version} - test - diff --git a/yudao-module-iot/yudao-module-iot-gateway/pom.xml b/yudao-module-iot/yudao-module-iot-gateway/pom.xml index 8fde9dc3ce..38ace822d8 100644 --- a/yudao-module-iot/yudao-module-iot-gateway/pom.xml +++ b/yudao-module-iot/yudao-module-iot-gateway/pom.xml @@ -48,6 +48,12 @@ vertx-mqtt + + + org.eclipse.californium + californium-core + + cn.iocoder.boot diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/config/IotGatewayConfiguration.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/config/IotGatewayConfiguration.java index 68266b9cca..47c7a79720 100644 --- a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/config/IotGatewayConfiguration.java +++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/config/IotGatewayConfiguration.java @@ -1,6 +1,8 @@ package cn.iocoder.yudao.module.iot.gateway.config; import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageBus; +import cn.iocoder.yudao.module.iot.gateway.protocol.coap.IotCoapDownstreamSubscriber; +import cn.iocoder.yudao.module.iot.gateway.protocol.coap.IotCoapUpstreamProtocol; import cn.iocoder.yudao.module.iot.gateway.protocol.emqx.IotEmqxAuthEventProtocol; import cn.iocoder.yudao.module.iot.gateway.protocol.emqx.IotEmqxDownstreamSubscriber; import cn.iocoder.yudao.module.iot.gateway.protocol.emqx.IotEmqxUpstreamProtocol; @@ -232,4 +234,25 @@ public class IotGatewayConfiguration { } + /** + * IoT 网关 CoAP 协议配置类 + */ + @Configuration + @ConditionalOnProperty(prefix = "yudao.iot.gateway.protocol.coap", name = "enabled", havingValue = "true") + @Slf4j + public static class CoapProtocolConfiguration { + + @Bean + public IotCoapUpstreamProtocol iotCoapUpstreamProtocol(IotGatewayProperties gatewayProperties) { + return new IotCoapUpstreamProtocol(gatewayProperties.getProtocol().getCoap()); + } + + @Bean + public IotCoapDownstreamSubscriber iotCoapDownstreamSubscriber(IotCoapUpstreamProtocol coapUpstreamProtocol, + IotMessageBus messageBus) { + return new IotCoapDownstreamSubscriber(coapUpstreamProtocol, messageBus); + } + + } + } diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/config/IotGatewayProperties.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/config/IotGatewayProperties.java index 0675f839cf..3bdf6fb3e4 100644 --- a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/config/IotGatewayProperties.java +++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/config/IotGatewayProperties.java @@ -98,6 +98,11 @@ public class IotGatewayProperties { */ private UdpProperties udp; + /** + * CoAP 组件配置 + */ + private CoapProperties coap; + } @Data @@ -546,4 +551,39 @@ public class IotGatewayProperties { } + @Data + public static class CoapProperties { + + /** + * 是否开启 + */ + @NotNull(message = "是否开启不能为空") + private Boolean enabled; + + /** + * 服务端口(CoAP 默认端口 5683) + */ + // TODO @AI:默认不为空 + private Integer port = 5683; + + /** + * 最大消息大小(字节) + */ + // TODO @AI:默认不为空 + private Integer maxMessageSize = 1024; + + /** + * ACK 超时时间(毫秒) + */ + // TODO @AI:默认不为空 + private Integer ackTimeout = 2000; + + /** + * 最大重传次数 + */ + // TODO @AI:默认不为空 + private Integer maxRetransmit = 4; + + } + } diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/IotCoapDownstreamSubscriber.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/IotCoapDownstreamSubscriber.java new file mode 100644 index 0000000000..d01cdc416c --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/IotCoapDownstreamSubscriber.java @@ -0,0 +1,46 @@ +package cn.iocoder.yudao.module.iot.gateway.protocol.coap; + +import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageBus; +import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageSubscriber; +import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; +import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils; +import jakarta.annotation.PostConstruct; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +/** + * IoT 网关 CoAP 订阅者:接收下行给设备的消息 + * + * @author 芋道源码 + */ +@RequiredArgsConstructor +@Slf4j +public class IotCoapDownstreamSubscriber implements IotMessageSubscriber { + + private final IotCoapUpstreamProtocol protocol; + + private final IotMessageBus messageBus; + + @PostConstruct + public void init() { + messageBus.register(this); + } + + @Override + public String getTopic() { + return IotDeviceMessageUtils.buildMessageBusGatewayDeviceMessageTopic(protocol.getServerId()); + } + + @Override + public String getGroup() { + // 保证点对点消费,需要保证独立的 Group,所以使用 Topic 作为 Group + return getTopic(); + } + + @Override + public void onMessage(IotDeviceMessage message) { + // 如需支持,可通过 CoAP Observe 模式实现(设备订阅资源,服务器推送变更) + log.warn("[onMessage][IoT 网关 CoAP 协议暂不支持下行消息,忽略消息:{}]", message); + } + +} diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/IotCoapUpstreamProtocol.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/IotCoapUpstreamProtocol.java new file mode 100644 index 0000000000..42f4c8edc2 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/IotCoapUpstreamProtocol.java @@ -0,0 +1,86 @@ +package cn.iocoder.yudao.module.iot.gateway.protocol.coap; + +import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils; +import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties; +import cn.iocoder.yudao.module.iot.gateway.protocol.coap.router.IotCoapAuthHandler; +import cn.iocoder.yudao.module.iot.gateway.protocol.coap.router.IotCoapAuthResource; +import cn.iocoder.yudao.module.iot.gateway.protocol.coap.router.IotCoapUpstreamTopicResource; +import cn.iocoder.yudao.module.iot.gateway.protocol.coap.router.IotCoapUpstreamHandler; +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.californium.core.CoapServer; +import org.eclipse.californium.core.config.CoapConfig; +import org.eclipse.californium.elements.config.Configuration; + +import java.util.concurrent.TimeUnit; + +/** + * IoT 网关 CoAP 协议:接收设备上行消息 + * + * 基于 Eclipse Californium 实现,支持: + * 1. 认证:POST /auth + * 2. 属性上报:POST /topic/sys/{productKey}/{deviceName}/thing/property/post + * 3. 事件上报:POST /topic/sys/{productKey}/{deviceName}/thing/event/post + * + * @author 芋道源码 + */ +@Slf4j +public class IotCoapUpstreamProtocol { + + private final IotGatewayProperties.CoapProperties coapProperties; + + private CoapServer coapServer; + + @Getter + private final String serverId; + + public IotCoapUpstreamProtocol(IotGatewayProperties.CoapProperties coapProperties) { + this.coapProperties = coapProperties; + this.serverId = IotDeviceMessageUtils.generateServerId(coapProperties.getPort()); + } + + @PostConstruct + public void start() { + try { + // 1.1 创建网络配置(Californium 3.x API) + Configuration config = Configuration.createStandardWithoutFile(); + config.set(CoapConfig.COAP_PORT, coapProperties.getPort()); + config.set(CoapConfig.MAX_MESSAGE_SIZE, coapProperties.getMaxMessageSize()); + config.set(CoapConfig.ACK_TIMEOUT, coapProperties.getAckTimeout(), TimeUnit.MILLISECONDS); + config.set(CoapConfig.MAX_RETRANSMIT, coapProperties.getMaxRetransmit()); + // 1.2 创建 CoAP 服务器 + coapServer = new CoapServer(config); + + // 2.1 添加 /auth 认证资源 + IotCoapAuthHandler authHandler = new IotCoapAuthHandler(); + IotCoapAuthResource authResource = new IotCoapAuthResource(this, authHandler); + coapServer.add(authResource); + // 2.2 添加 /topic 根资源(用于上行消息) + IotCoapUpstreamHandler upstreamHandler = new IotCoapUpstreamHandler(); + IotCoapUpstreamTopicResource topicResource = new IotCoapUpstreamTopicResource(this, upstreamHandler); + coapServer.add(topicResource); + + // 3. 启动服务器 + coapServer.start(); + log.info("[start][IoT 网关 CoAP 协议启动成功,端口:{},资源:/auth, /topic]", coapProperties.getPort()); + } catch (Exception e) { + log.error("[start][IoT 网关 CoAP 协议启动失败]", e); + throw e; + } + } + + @PreDestroy + public void stop() { + if (coapServer != null) { + try { + coapServer.stop(); + log.info("[stop][IoT 网关 CoAP 协议已停止]"); + } catch (Exception e) { + log.error("[stop][IoT 网关 CoAP 协议停止失败]", e); + } + } + } + +} diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/package-info.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/package-info.java new file mode 100644 index 0000000000..94536a6439 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/package-info.java @@ -0,0 +1,13 @@ +/** + * CoAP 协议实现包 + *

+ * 提供基于 Eclipse Californium 的 IoT 设备连接和消息处理功能 + *

+ * URI 路径: + * - 认证:POST /auth + * - 属性上报:POST /topic/sys/{productKey}/{deviceName}/thing/property/post + * - 事件上报:POST /topic/sys/{productKey}/{deviceName}/thing/event/post + *

+ * Token 通过 CoAP Option 2088 携带 + */ +package cn.iocoder.yudao.module.iot.gateway.protocol.coap; diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/router/IotCoapAuthHandler.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/router/IotCoapAuthHandler.java new file mode 100644 index 0000000000..2348cf990b --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/router/IotCoapAuthHandler.java @@ -0,0 +1,117 @@ +package cn.iocoder.yudao.module.iot.gateway.protocol.coap.router; + +import cn.hutool.core.lang.Assert; +import cn.hutool.core.map.MapUtil; +import cn.hutool.core.util.BooleanUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.extra.spring.SpringUtil; +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.framework.common.util.json.JsonUtils; +import cn.iocoder.yudao.module.iot.core.biz.IotDeviceCommonApi; +import cn.iocoder.yudao.module.iot.core.biz.dto.IotDeviceAuthReqDTO; +import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; +import cn.iocoder.yudao.module.iot.core.util.IotDeviceAuthUtils; +import cn.iocoder.yudao.module.iot.gateway.protocol.coap.IotCoapUpstreamProtocol; +import cn.iocoder.yudao.module.iot.gateway.protocol.coap.util.IotCoapUtils; +import cn.iocoder.yudao.module.iot.gateway.service.auth.IotDeviceTokenService; +import cn.iocoder.yudao.module.iot.gateway.service.device.message.IotDeviceMessageService; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.californium.core.coap.CoAP; +import org.eclipse.californium.core.server.resources.CoapExchange; + +import java.util.Map; + +/** + * IoT 网关 CoAP 协议的【认证】处理器 + * + * 参考 {@link cn.iocoder.yudao.module.iot.gateway.protocol.http.router.IotHttpAuthHandler} + * + * @author 芋道源码 + */ +@Slf4j +public class IotCoapAuthHandler { + + private final IotDeviceTokenService deviceTokenService; + private final IotDeviceCommonApi deviceApi; + private final IotDeviceMessageService deviceMessageService; + + public IotCoapAuthHandler() { + this.deviceTokenService = SpringUtil.getBean(IotDeviceTokenService.class); + this.deviceApi = SpringUtil.getBean(IotDeviceCommonApi.class); + this.deviceMessageService = SpringUtil.getBean(IotDeviceMessageService.class); + } + + /** + * 处理认证请求 + * + * @param exchange CoAP 交换对象 + * @param protocol 协议对象 + */ + @SuppressWarnings("unchecked") + public void handle(CoapExchange exchange, IotCoapUpstreamProtocol protocol) { + try { + // 1.1 解析请求体 + byte[] payload = exchange.getRequestPayload(); + if (payload == null || payload.length == 0) { + IotCoapUtils.respondError(exchange, CoAP.ResponseCode.BAD_REQUEST, "请求体不能为空"); + return; + } + Map body; + try { + body = JsonUtils.parseObject(new String(payload), Map.class); + } catch (Exception e) { + IotCoapUtils.respondError(exchange, CoAP.ResponseCode.BAD_REQUEST, "请求体 JSON 格式错误"); + return; + } + // 1.2 解析参数 + String clientId = MapUtil.getStr(body, "clientId"); + if (StrUtil.isEmpty(clientId)) { + IotCoapUtils.respondError(exchange, CoAP.ResponseCode.BAD_REQUEST, "clientId 不能为空"); + return; + } + String username = MapUtil.getStr(body, "username"); + if (StrUtil.isEmpty(username)) { + IotCoapUtils.respondError(exchange, CoAP.ResponseCode.BAD_REQUEST, "username 不能为空"); + return; + } + String password = MapUtil.getStr(body, "password"); + if (StrUtil.isEmpty(password)) { + IotCoapUtils.respondError(exchange, CoAP.ResponseCode.BAD_REQUEST, "password 不能为空"); + return; + } + + // 2.1 执行认证 + CommonResult result = deviceApi.authDevice(new IotDeviceAuthReqDTO() + .setClientId(clientId).setUsername(username).setPassword(password)); + if (result.isError()) { + log.warn("[handle][认证失败,clientId: {}, 错误: {}]", clientId, result.getMsg()); + IotCoapUtils.respondError(exchange, CoAP.ResponseCode.UNAUTHORIZED, "认证失败:" + result.getMsg()); + return; + } + if (!BooleanUtil.isTrue(result.getData())) { + log.warn("[handle][认证失败,clientId: {}]", clientId); + IotCoapUtils.respondError(exchange, CoAP.ResponseCode.UNAUTHORIZED, "认证失败"); + return; + } + // 2.2 生成 Token + IotDeviceAuthUtils.DeviceInfo deviceInfo = deviceTokenService.parseUsername(username); + Assert.notNull(deviceInfo, "设备信息不能为空"); + String token = deviceTokenService.createToken(deviceInfo.getProductKey(), deviceInfo.getDeviceName()); + Assert.notBlank(token, "生成 token 不能为空"); + + // 3. 执行上线 + IotDeviceMessage message = IotDeviceMessage.buildStateUpdateOnline(); + deviceMessageService.sendDeviceMessage(message, + deviceInfo.getProductKey(), deviceInfo.getDeviceName(), protocol.getServerId()); + + // 4. 返回成功响应 + log.info("[handle][认证成功,productKey: {}, deviceName: {}]", + deviceInfo.getProductKey(), deviceInfo.getDeviceName()); + IotCoapUtils.respondSuccess(exchange, MapUtil.of("token", token)); + } catch (Exception e) { + log.error("[handle][认证处理异常]", e); + IotCoapUtils.respondError(exchange, CoAP.ResponseCode.INTERNAL_SERVER_ERROR, "服务器内部错误"); + } + } + +} diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/router/IotCoapAuthResource.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/router/IotCoapAuthResource.java new file mode 100644 index 0000000000..9d0d90cb3e --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/router/IotCoapAuthResource.java @@ -0,0 +1,37 @@ +package cn.iocoder.yudao.module.iot.gateway.protocol.coap.router; + +import cn.iocoder.yudao.module.iot.gateway.protocol.coap.IotCoapUpstreamProtocol; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.californium.core.CoapResource; +import org.eclipse.californium.core.server.resources.CoapExchange; + +/** + * IoT 网关 CoAP 协议的认证资源(/auth) + * + * 设备通过此资源进行认证,获取 Token + * + * @author 芋道源码 + */ +@Slf4j +public class IotCoapAuthResource extends CoapResource { + + public static final String PATH = "auth"; + + private final IotCoapUpstreamProtocol protocol; + private final IotCoapAuthHandler authHandler; + + public IotCoapAuthResource(IotCoapUpstreamProtocol protocol, + IotCoapAuthHandler authHandler) { + super(PATH); + this.protocol = protocol; + this.authHandler = authHandler; + log.info("[IotCoapAuthResource][创建 CoAP 认证资源: /{}]", PATH); + } + + @Override + public void handlePOST(CoapExchange exchange) { + log.debug("[handlePOST][收到 /auth POST 请求]"); + authHandler.handle(exchange, protocol); + } + +} diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/router/IotCoapUpstreamHandler.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/router/IotCoapUpstreamHandler.java new file mode 100644 index 0000000000..d51215fd6c --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/router/IotCoapUpstreamHandler.java @@ -0,0 +1,108 @@ +package cn.iocoder.yudao.module.iot.gateway.protocol.coap.router; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.lang.Assert; +import cn.hutool.core.map.MapUtil; +import cn.hutool.core.text.StrPool; +import cn.hutool.core.util.ArrayUtil; +import cn.hutool.core.util.ObjUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.extra.spring.SpringUtil; +import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; +import cn.iocoder.yudao.module.iot.core.util.IotDeviceAuthUtils; +import cn.iocoder.yudao.module.iot.gateway.protocol.coap.IotCoapUpstreamProtocol; +import cn.iocoder.yudao.module.iot.gateway.protocol.coap.util.IotCoapUtils; +import cn.iocoder.yudao.module.iot.gateway.service.auth.IotDeviceTokenService; +import cn.iocoder.yudao.module.iot.gateway.service.device.message.IotDeviceMessageService; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.californium.core.coap.CoAP; +import org.eclipse.californium.core.server.resources.CoapExchange; + +import java.util.List; + +/** + * IoT 网关 CoAP 协议的【上行】处理器 + * + * 处理设备通过 CoAP 协议发送的上行消息,包括: + * 1. 属性上报:POST /topic/sys/{productKey}/{deviceName}/thing/property/post + * 2. 事件上报:POST /topic/sys/{productKey}/{deviceName}/thing/event/post + * + * Token 通过自定义 CoAP Option 2088 携带 + * + * @author 芋道源码 + */ +@Slf4j +public class IotCoapUpstreamHandler { + + private final IotDeviceTokenService deviceTokenService; + private final IotDeviceMessageService deviceMessageService; + + public IotCoapUpstreamHandler() { + this.deviceTokenService = SpringUtil.getBean(IotDeviceTokenService.class); + this.deviceMessageService = SpringUtil.getBean(IotDeviceMessageService.class); + } + + /** + * 处理 CoAP 请求 + * + * @param exchange CoAP 交换对象 + * @param protocol 协议对象 + */ + public void handle(CoapExchange exchange, IotCoapUpstreamProtocol protocol) { + try { + // 1. 解析通用参数 + List uriPath = exchange.getRequestOptions().getUriPath(); + String productKey = CollUtil.get(uriPath, 2); + String deviceName = CollUtil.get(uriPath, 3); + byte[] payload = exchange.getRequestPayload(); + if (StrUtil.isEmpty(productKey)) { + IotCoapUtils.respondError(exchange, CoAP.ResponseCode.BAD_REQUEST, "productKey 不能为空"); + return; + } + if (StrUtil.isEmpty(deviceName)) { + IotCoapUtils.respondError(exchange, CoAP.ResponseCode.BAD_REQUEST, "deviceName 不能为空"); + return; + } + if (ArrayUtil.isEmpty(payload)) { + IotCoapUtils.respondError(exchange, CoAP.ResponseCode.BAD_REQUEST, "请求体不能为空"); + return; + } + + // 2. 认证:从自定义 Option 获取 token + String token = IotCoapUtils.getTokenFromOption(exchange, IotCoapUtils.OPTION_TOKEN); + if (StrUtil.isEmpty(token)) { + IotCoapUtils.respondError(exchange, CoAP.ResponseCode.UNAUTHORIZED, "token 不能为空"); + return; + } + // 验证 token + IotDeviceAuthUtils.DeviceInfo deviceInfo = deviceTokenService.verifyToken(token); + if (deviceInfo == null) { + IotCoapUtils.respondError(exchange, CoAP.ResponseCode.UNAUTHORIZED, "token 无效或已过期"); + return; + } + // 验证设备信息匹配 + if (ObjUtil.notEqual(productKey, deviceInfo.getProductKey()) + || ObjUtil.notEqual(deviceName, deviceInfo.getDeviceName())) { + IotCoapUtils.respondError(exchange, CoAP.ResponseCode.FORBIDDEN, "设备信息与 token 不匹配"); + return; + } + + // 2.1 解析 method:deviceName 后面的路径,用 . 拼接 + // 路径格式:[topic, sys, productKey, deviceName, thing, property, post] + String method = String.join(StrPool.DOT, uriPath.subList(4, uriPath.size())); + + // 2.2 解码消息 + IotDeviceMessage message = deviceMessageService.decodeDeviceMessage(payload, productKey, deviceName); + Assert.equals(method, message.getMethod(), "method 不匹配"); + // 2.3 发送消息到消息总线 + deviceMessageService.sendDeviceMessage(message, productKey, deviceName, protocol.getServerId()); + + // 3. 返回成功响应 + IotCoapUtils.respondSuccess(exchange, MapUtil.of("messageId", message.getId())); + } catch (Exception e) { + log.error("[handle][CoAP 请求处理异常]", e); + IotCoapUtils.respondError(exchange, CoAP.ResponseCode.INTERNAL_SERVER_ERROR, "服务器内部错误"); + } + } + +} diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/router/IotCoapUpstreamTopicResource.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/router/IotCoapUpstreamTopicResource.java new file mode 100644 index 0000000000..1c694483fa --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/router/IotCoapUpstreamTopicResource.java @@ -0,0 +1,67 @@ +package cn.iocoder.yudao.module.iot.gateway.protocol.coap.router; + +import cn.iocoder.yudao.module.iot.gateway.protocol.coap.IotCoapUpstreamProtocol; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.californium.core.CoapResource; +import org.eclipse.californium.core.server.resources.CoapExchange; +import org.eclipse.californium.core.server.resources.Resource; + +/** + * IoT 网关 CoAP 协议的【上行】Topic 资源 + * + * 支持任意深度的路径匹配: + * - /topic/sys/{productKey}/{deviceName}/thing/property/post + * - /topic/sys/{productKey}/{deviceName}/thing/event/{eventId}/post + * + * @author 芋道源码 + */ +@Slf4j +public class IotCoapUpstreamTopicResource extends CoapResource { + + public static final String PATH = "topic"; + + private final IotCoapUpstreamProtocol protocol; + private final IotCoapUpstreamHandler upstreamHandler; + + /** + * 创建根资源(/topic) + */ + public IotCoapUpstreamTopicResource(IotCoapUpstreamProtocol protocol, + IotCoapUpstreamHandler upstreamHandler) { + this(PATH, protocol, upstreamHandler); + log.info("[IotCoapUpstreamTopicResource][创建 CoAP 上行 Topic 资源: /{}]", PATH); + } + + /** + * 创建子资源(动态路径) + */ + private IotCoapUpstreamTopicResource(String name, + IotCoapUpstreamProtocol protocol, + IotCoapUpstreamHandler upstreamHandler) { + super(name); + this.protocol = protocol; + this.upstreamHandler = upstreamHandler; + } + + @Override + public Resource getChild(String name) { + // 递归创建动态子资源,支持任意深度路径 + return new IotCoapUpstreamTopicResource(name, protocol, upstreamHandler); + } + + @Override + public void handleGET(CoapExchange exchange) { + upstreamHandler.handle(exchange, protocol); + } + + @Override + public void handlePOST(CoapExchange exchange) { + upstreamHandler.handle(exchange, protocol); + } + + @Override + public void handlePUT(CoapExchange exchange) { + upstreamHandler.handle(exchange, protocol); + } + +} diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/util/IotCoapUtils.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/util/IotCoapUtils.java new file mode 100644 index 0000000000..9d5cdf3ffb --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/util/IotCoapUtils.java @@ -0,0 +1,84 @@ +package cn.iocoder.yudao.module.iot.gateway.protocol.coap.util; + +import cn.hutool.core.collection.CollUtil; +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.framework.common.util.json.JsonUtils; +import org.eclipse.californium.core.coap.CoAP; +import org.eclipse.californium.core.coap.MediaTypeRegistry; +import org.eclipse.californium.core.coap.Option; +import org.eclipse.californium.core.server.resources.CoapExchange; + +import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.*; + +/** + * IoT CoAP 协议工具类 + * + * @author 芋道源码 + */ +public class IotCoapUtils { + + /** + * 自定义 CoAP Option 编号,用于携带 Token + *

+ * CoAP Option 范围 2048-65535 属于实验/自定义范围 + */ + public static final int OPTION_TOKEN = 2088; + + /** + * 返回成功响应 + * + * @param exchange CoAP 交换对象 + * @param data 响应数据 + */ + public static void respondSuccess(CoapExchange exchange, Object data) { + CommonResult result = CommonResult.success(data); + String json = JsonUtils.toJsonString(result); + exchange.respond(CoAP.ResponseCode.CONTENT, json, MediaTypeRegistry.APPLICATION_JSON); + } + + /** + * 返回错误响应 + * + * @param exchange CoAP 交换对象 + * @param code CoAP 响应码 + * @param message 错误消息 + */ + public static void respondError(CoapExchange exchange, CoAP.ResponseCode code, String message) { + int errorCode = mapCoapCodeToErrorCode(code); + CommonResult result = CommonResult.error(errorCode, message); + String json = JsonUtils.toJsonString(result); + exchange.respond(code, json, MediaTypeRegistry.APPLICATION_JSON); + } + + /** + * 从自定义 CoAP Option 中获取 Token + * + * @param exchange CoAP 交换对象 + * @param optionNumber Option 编号 + * @return Token 值,如果不存在则返回 null + */ + public static String getTokenFromOption(CoapExchange exchange, int optionNumber) { + Option option = CollUtil.findOne(exchange.getRequestOptions().getOthers(), + o -> o.getNumber() == optionNumber); + return option != null ? new String(option.getValue()) : null; + } + + /** + * 将 CoAP 响应码映射到业务错误码 + * + * @param code CoAP 响应码 + * @return 业务错误码 + */ + public static int mapCoapCodeToErrorCode(CoAP.ResponseCode code) { + if (code == CoAP.ResponseCode.BAD_REQUEST) { + return BAD_REQUEST.getCode(); + } else if (code == CoAP.ResponseCode.UNAUTHORIZED) { + return UNAUTHORIZED.getCode(); + } else if (code == CoAP.ResponseCode.FORBIDDEN) { + return FORBIDDEN.getCode(); + } else { + return INTERNAL_SERVER_ERROR.getCode(); + } + } + +} diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/emqx/package-info.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/emqx/package-info.java new file mode 100644 index 0000000000..b64dd122bb --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/emqx/package-info.java @@ -0,0 +1 @@ +package cn.iocoder.yudao.module.iot.gateway.protocol.emqx; \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/http/package-info.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/http/package-info.java new file mode 100644 index 0000000000..5a027da02b --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/http/package-info.java @@ -0,0 +1,2 @@ +// TODO @AI:参考 /Users/yunai/Java/ruoyi-vue-pro-jdk25/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/mqtt/package-info.java 完善注释; +package cn.iocoder.yudao.module.iot.gateway.protocol.http; \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/package-info.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/package-info.java new file mode 100644 index 0000000000..e67eb497f4 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/package-info.java @@ -0,0 +1,2 @@ +// TODO @AI:参考 /Users/yunai/Java/ruoyi-vue-pro-jdk25/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/mqtt/package-info.java +package cn.iocoder.yudao.module.iot.gateway.protocol.tcp; \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/resources/application.yaml b/yudao-module-iot/yudao-module-iot-gateway/src/main/resources/application.yaml index c81fef77df..49d590ab4b 100644 --- a/yudao-module-iot/yudao-module-iot-gateway/src/main/resources/application.yaml +++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/resources/application.yaml @@ -127,6 +127,15 @@ yudao: keep-alive-timeout-seconds: 300 # 保持连接超时时间(秒) ssl-enabled: false # 是否启用 SSL(wss://) sub-protocol: mqtt # WebSocket 子协议 + # ==================================== + # 针对引入的 CoAP 组件的配置 + # ==================================== + coap: + enabled: true # 是否启用 CoAP 协议 + port: 5683 # CoAP 服务端口(默认 5683) + max-message-size: 1024 # 最大消息大小(字节) + ack-timeout: 2000 # ACK 超时时间(毫秒) + max-retransmit: 4 # 最大重传次数 --- #################### 日志相关配置 #################### @@ -147,6 +156,7 @@ logging: cn.iocoder.yudao.module.iot.gateway.protocol.http: DEBUG cn.iocoder.yudao.module.iot.gateway.protocol.mqtt: DEBUG cn.iocoder.yudao.module.iot.gateway.protocol.mqttws: DEBUG + cn.iocoder.yudao.module.iot.gateway.protocol.coap: DEBUG # 根日志级别 root: INFO diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/test/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/IotCoapProtocolIntegrationTest.java b/yudao-module-iot/yudao-module-iot-gateway/src/test/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/IotCoapProtocolIntegrationTest.java new file mode 100644 index 0000000000..5856ced429 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-gateway/src/test/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/IotCoapProtocolIntegrationTest.java @@ -0,0 +1,169 @@ +package cn.iocoder.yudao.module.iot.gateway.protocol.coap; + +import cn.hutool.core.map.MapUtil; +import cn.hutool.core.util.IdUtil; +import cn.iocoder.yudao.framework.common.util.json.JsonUtils; +import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageMethodEnum; +import cn.iocoder.yudao.module.iot.gateway.protocol.coap.util.IotCoapUtils; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.californium.core.CoapClient; +import org.eclipse.californium.core.CoapResponse; +import org.eclipse.californium.core.coap.MediaTypeRegistry; +import org.eclipse.californium.core.coap.Option; +import org.eclipse.californium.core.coap.Request; +import org.eclipse.californium.core.config.CoapConfig; +import org.eclipse.californium.elements.config.Configuration; +import org.eclipse.californium.elements.config.UdpConfig; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +/** + * IoT 网关 CoAP 协议集成测试(手动测试) + * + *

使用步骤: + *

    + *
  1. 启动 yudao-module-iot-gateway 服务(CoAP 端口 5683)
  2. + *
  3. 运行 {@link #testAuth()} 获取 token,将返回的 token 粘贴到 {@link #TOKEN} 常量
  4. + *
  5. 运行 {@link #testPropertyPost()} 测试属性上报,或运行 {@link #testEventPost()} 测试事件上报
  6. + *
+ * + * @author 芋道源码 + */ +@Slf4j +public class IotCoapProtocolIntegrationTest { + + private static final String SERVER_HOST = "127.0.0.1"; + private static final int SERVER_PORT = 5683; + + // 设备信息(根据实际情况修改 PRODUCT_KEY、DEVICE_NAME、PASSWORD) + private static final String PRODUCT_KEY = "4aymZgOTOOCrDKRT"; + private static final String DEVICE_NAME = "small"; + private static final String PASSWORD = "509e2b08f7598eb139d276388c600435913ba4c94cd0d50aebc5c0d1855bcb75"; + + private static final String CLIENT_ID = PRODUCT_KEY + "." + DEVICE_NAME; + private static final String USERNAME = DEVICE_NAME + "&" + PRODUCT_KEY; + + /** + * 设备 Token:从 {@link #testAuth()} 方法获取后,粘贴到这里 + */ + private static final String TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwcm9kdWN0S2V5IjoiNGF5bVpnT1RPT0NyREtSVCIsImV4cCI6MTc2OTMwNTA1NSwiZGV2aWNlTmFtZSI6InNtYWxsIn0.mf3MEATCn5bp6cXgULunZjs8d00RGUxj96JEz0hMS7k"; + + @BeforeAll + public static void initCaliforniumConfig() { + // 注册 Californium 配置定义 + CoapConfig.register(); + UdpConfig.register(); + // 创建默认配置 + Configuration.setStandard(Configuration.createStandardWithoutFile()); + } + + /** + * 认证测试:获取设备 Token + */ + @Test + @SuppressWarnings("deprecation") + public void testAuth() throws Exception { + String uri = String.format("coap://%s:%d/auth", SERVER_HOST, SERVER_PORT); + String payload = JsonUtils.toJsonString(MapUtil.builder() + .put("clientId", CLIENT_ID) + .put("username", USERNAME) + .put("password", PASSWORD) + .build()); + + CoapClient client = new CoapClient(uri); + try { + log.info("[testAuth][请求 URI: {}]", uri); + log.info("[testAuth][请求体: {}]", payload); + + CoapResponse response = client.post(payload, MediaTypeRegistry.APPLICATION_JSON); + + log.info("[testAuth][响应码: {}]", response.getCode()); + log.info("[testAuth][响应体: {}]", response.getResponseText()); + log.info("[testAuth][请将返回的 token 复制到 TOKEN 常量中]"); + } finally { + client.shutdown(); + } + } + + /** + * 属性上报测试 + */ + @Test + @SuppressWarnings("deprecation") + public void testPropertyPost() throws Exception { + String uri = String.format("coap://%s:%d/topic/sys/%s/%s/thing/property/post", + SERVER_HOST, SERVER_PORT, PRODUCT_KEY, DEVICE_NAME); + String payload = JsonUtils.toJsonString(MapUtil.builder() + .put("id", IdUtil.fastSimpleUUID()) + .put("method", IotDeviceMessageMethodEnum.PROPERTY_POST.getMethod()) + .put("version", "1.0") + .put("params", MapUtil.builder() + .put("width", 1) + .put("height", "2") + .build()) + .build()); + + CoapClient client = new CoapClient(uri); + try { + Request request = Request.newPost(); + request.setURI(uri); + request.setPayload(payload); + request.getOptions().setContentFormat(MediaTypeRegistry.APPLICATION_JSON); + request.getOptions().addOption(new Option(IotCoapUtils.OPTION_TOKEN, TOKEN)); + + log.info("[testPropertyPost][请求 URI: {}]", uri); + log.info("[testPropertyPost][请求体: {}]", payload); + + CoapResponse response = client.advanced(request); + + log.info("[testPropertyPost][响应码: {}]", response.getCode()); + log.info("[testPropertyPost][响应体: {}]", response.getResponseText()); + } finally { + client.shutdown(); + } + } + + /** + * 事件上报测试 + */ + @Test + @SuppressWarnings("deprecation") + public void testEventPost() throws Exception { + String uri = String.format("coap://%s:%d/topic/sys/%s/%s/thing/event/post", + SERVER_HOST, SERVER_PORT, PRODUCT_KEY, DEVICE_NAME); + String payload = JsonUtils.toJsonString(MapUtil.builder() + .put("id", IdUtil.fastSimpleUUID()) + .put("method", IotDeviceMessageMethodEnum.EVENT_POST.getMethod()) + .put("version", "1.0") + .put("params", MapUtil.builder() + .put("identifier", "eat") + .put("value", MapUtil.builder() + .put("width", 1) + .put("height", "2") + .put("oneThree", "3") + .build()) + .put("time", System.currentTimeMillis()) + .build()) + .build()); + + CoapClient client = new CoapClient(uri); + try { + Request request = Request.newPost(); + request.setURI(uri); + request.setPayload(payload); + request.getOptions().setContentFormat(MediaTypeRegistry.APPLICATION_JSON); + request.getOptions().addOption(new Option(IotCoapUtils.OPTION_TOKEN, TOKEN)); + + log.info("[testEventPost][请求 URI: {}]", uri); + log.info("[testEventPost][请求体: {}]", payload); + + CoapResponse response = client.advanced(request); + + log.info("[testEventPost][响应码: {}]", response.getCode()); + log.info("[testEventPost][响应体: {}]", response.getResponseText()); + } finally { + client.shutdown(); + } + } + +}