2026-06-24 f4bd1f3c89d906131495a0aca5aaf82966378510
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
package cn.iocoder.yudao.module.iot.gateway.protocol.http.handler.upstream;
 
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.StrPool;
import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
import cn.iocoder.yudao.module.iot.gateway.protocol.http.IotHttpProtocol;
import cn.iocoder.yudao.module.iot.gateway.service.device.message.IotDeviceMessageService;
import io.vertx.ext.web.RoutingContext;
import lombok.extern.slf4j.Slf4j;
 
/**
 * IoT 网关 HTTP 协议的【上行】处理器
 *
 * @author 芋道源码
 */
@Slf4j
public class IotHttpUpstreamHandler extends IotHttpAbstractHandler {
 
    public static final String PATH = "/topic/sys/:productKey/:deviceName/*";
 
    private final String serverId;
 
    private final IotDeviceMessageService deviceMessageService;
 
    public IotHttpUpstreamHandler(IotHttpProtocol protocol) {
        this.serverId = protocol.getServerId();
        this.deviceMessageService = SpringUtil.getBean(IotDeviceMessageService.class);
    }
 
    @Override
    protected CommonResult<Object> handle0(RoutingContext context) {
        // 1.1 解析通用参数
        String productKey = context.pathParam("productKey");
        String deviceName = context.pathParam("deviceName");
        String method = context.pathParam("*").replaceAll(StrPool.SLASH, StrPool.DOT);
        // 1.2 根据 Content-Type 反序列化消息
        IotDeviceMessage message = deserializeRequest(context, IotDeviceMessage.class);
        Assert.notNull(message, "请求参数不能为空");
        Assert.equals(method, message.getMethod(), "method 不匹配");
 
        // 2. 发送消息
        deviceMessageService.sendDeviceMessage(message,
                productKey, deviceName, serverId);
 
        // 3. 返回结果
        return CommonResult.success(MapUtil.of("messageId", message.getId()));
    }
 
}