2026-06-26 20b96473f2520590a0dca6b775b81e3ea06a77a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cn.iocoder.yudao.module.iot.gateway.protocol.coap.handler.upstream;
 
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 String serverId;
    private final IotCoapUpstreamHandler upstreamHandler;
 
    /**
     * 创建根资源(/topic)
     */
    public IotCoapUpstreamTopicResource(String serverId,
                                         IotCoapUpstreamHandler upstreamHandler) {
        this(PATH, serverId, upstreamHandler);
        log.info("[IotCoapUpstreamTopicResource][创建 CoAP 上行 Topic 资源: /{}]", PATH);
    }
 
    /**
     * 创建子资源(动态路径)
     */
    private IotCoapUpstreamTopicResource(String name,
                                          String serverId,
                                          IotCoapUpstreamHandler upstreamHandler) {
        super(name);
        this.serverId = serverId;
        this.upstreamHandler = upstreamHandler;
    }
 
    @Override
    public Resource getChild(String name) {
        // 递归创建动态子资源,支持任意深度路径
        return new IotCoapUpstreamTopicResource(name, serverId, upstreamHandler);
    }
 
    @Override
    public void handleGET(CoapExchange exchange) {
        upstreamHandler.handle(exchange);
    }
 
    @Override
    public void handlePOST(CoapExchange exchange) {
        upstreamHandler.handle(exchange);
    }
 
    @Override
    public void handlePUT(CoapExchange exchange) {
        upstreamHandler.handle(exchange);
    }
 
}