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
67
68
69
70
71
72
73
74
package cn.iocoder.yudao.module.iot.gateway.protocol.tcp.handler.downstream;
 
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageMethodEnum;
import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
import cn.iocoder.yudao.module.iot.gateway.protocol.tcp.codec.IotTcpFrameCodec;
import cn.iocoder.yudao.module.iot.gateway.protocol.tcp.manager.IotTcpConnectionManager;
import cn.iocoder.yudao.module.iot.gateway.serialize.IotMessageSerializer;
import io.vertx.core.buffer.Buffer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
 
/**
 * IoT 网关 TCP 下行消息处理器
 *
 * @author 芋道源码
 */
@Slf4j
@RequiredArgsConstructor
public class IotTcpDownstreamHandler {
 
    private final IotTcpConnectionManager connectionManager;
 
    /**
     * TCP 帧编解码器(处理粘包/拆包)
     */
    private final IotTcpFrameCodec codec;
    /**
     * 消息序列化器(处理业务消息序列化/反序列化)
     */
    private final IotMessageSerializer serializer;
 
    /**
     * 处理下行消息
     */
    public void handle(IotDeviceMessage message) {
        try {
            // 1.1 检查是否是属性设置消息
            if (ObjUtil.equals(IotDeviceMessageMethodEnum.PROPERTY_POST.getMethod(), message.getMethod())) {
                return;
            }
            if (ObjUtil.notEqual(IotDeviceMessageMethodEnum.PROPERTY_SET.getMethod(), message.getMethod())) {
                log.warn("[handle][忽略非属性设置消息: {}]", message.getMethod());
                return;
            }
            log.info("[handle][处理下行消息,设备 ID: {},方法: {},消息 ID: {}]",
                    message.getDeviceId(), message.getMethod(), message.getId());
            // 1.2 检查设备连接
            IotTcpConnectionManager.ConnectionInfo connectionInfo = connectionManager.getConnectionInfoByDeviceId(
                    message.getDeviceId());
            if (connectionInfo == null) {
                log.warn("[handle][连接信息不存在,设备 ID: {},方法: {},消息 ID: {}]",
                        message.getDeviceId(), message.getMethod(), message.getId());
                return;
            }
 
            // 2. 序列化 + 帧编码
            byte[] payload = serializer.serialize(message);
            Buffer frameData = codec.encode(payload);
 
            // 3. 发送到设备
            boolean success = connectionManager.sendToDevice(message.getDeviceId(), frameData.getBytes());
            if (!success) {
                throw new RuntimeException("下行消息发送失败");
            }
            log.info("[handle][下行消息发送成功,设备 ID: {},方法: {},消息 ID: {},数据长度: {} 字节]",
                    message.getDeviceId(), message.getMethod(), message.getId(), frameData.length());
        } catch (Exception e) {
            log.error("[handle][处理下行消息失败,设备 ID: {},方法: {},消息内容: {}]",
                    message.getDeviceId(), message.getMethod(), message, e);
        }
    }
 
}