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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package cn.iocoder.yudao.module.iot.gateway.protocol;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.iocoder.yudao.module.iot.core.enums.IotProtocolTypeEnum;
import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties;
import cn.iocoder.yudao.module.iot.gateway.protocol.coap.IotCoapProtocol;
import cn.iocoder.yudao.module.iot.gateway.protocol.emqx.IotEmqxProtocol;
import cn.iocoder.yudao.module.iot.gateway.protocol.http.IotHttpProtocol;
import cn.iocoder.yudao.module.iot.gateway.protocol.modbus.tcpclient.IotModbusTcpClientProtocol;
import cn.iocoder.yudao.module.iot.gateway.protocol.modbus.tcpserver.IotModbusTcpServerProtocol;
import cn.iocoder.yudao.module.iot.gateway.protocol.mqtt.IotMqttProtocol;
import cn.iocoder.yudao.module.iot.gateway.protocol.tcp.IotTcpProtocol;
import cn.iocoder.yudao.module.iot.gateway.protocol.udp.IotUdpProtocol;
import cn.iocoder.yudao.module.iot.gateway.protocol.websocket.IotWebSocketProtocol;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.SmartLifecycle;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * IoT 协议管理器:负责根据配置创建和管理协议实例
 *
 * @author 芋道源码
 */
@Slf4j
public class IotProtocolManager implements SmartLifecycle {
 
    private final IotGatewayProperties gatewayProperties;
 
    /**
     * 协议实例列表
     */
    private final List<IotProtocol> protocols = new ArrayList<>();
 
    @Getter
    private volatile boolean running = false;
 
    public IotProtocolManager(IotGatewayProperties gatewayProperties) {
        this.gatewayProperties = gatewayProperties;
    }
 
    @Override
    public void start() {
        if (running) {
            return;
        }
        List<IotGatewayProperties.ProtocolProperties> protocolConfigs = gatewayProperties.getProtocols();
        if (CollUtil.isEmpty(protocolConfigs)) {
            log.info("[start][没有配置协议实例,跳过启动]");
            return;
        }
 
        for (IotGatewayProperties.ProtocolProperties config : protocolConfigs) {
            if (BooleanUtil.isFalse(config.getEnabled())) {
                log.info("[start][协议实例 {} 未启用,跳过]", config.getId());
                continue;
            }
            IotProtocol protocol = createProtocol(config);
            if (protocol == null) {
                continue;
            }
            protocol.start();
            protocols.add(protocol);
        }
        running = true;
        log.info("[start][协议管理器启动完成,共启动 {} 个协议实例]", protocols.size());
    }
 
    @Override
    public void stop() {
        if (!running) {
            return;
        }
        for (IotProtocol protocol : protocols) {
            try {
                protocol.stop();
            } catch (Exception e) {
                log.error("[stop][协议实例 {} 停止失败]", protocol.getId(), e);
            }
        }
        protocols.clear();
        running = false;
        log.info("[stop][协议管理器已停止]");
    }
 
    /**
     * 创建协议实例
     *
     * @param config 协议实例配置
     * @return 协议实例
     */
    @SuppressWarnings({"EnhancedSwitchMigration"})
    private IotProtocol createProtocol(IotGatewayProperties.ProtocolProperties config) {
        IotProtocolTypeEnum protocolType = IotProtocolTypeEnum.of(config.getProtocol());
        if (protocolType == null) {
            log.error("[createProtocol][协议实例 {} 的协议类型 {} 不存在]", config.getId(), config.getProtocol());
            return null;
        }
        switch (protocolType) {
            case HTTP:
                return createHttpProtocol(config);
            case TCP:
                return createTcpProtocol(config);
            case UDP:
                return createUdpProtocol(config);
            case COAP:
                return createCoapProtocol(config);
            case WEBSOCKET:
                return createWebSocketProtocol(config);
            case MQTT:
                return createMqttProtocol(config);
            case EMQX:
                return createEmqxProtocol(config);
            case MODBUS_TCP_CLIENT:
                return createModbusTcpClientProtocol(config);
            case MODBUS_TCP_SERVER:
                return createModbusTcpServerProtocol(config);
            default:
                throw new IllegalArgumentException(String.format(
                        "[createProtocol][协议实例 %s 的协议类型 %s 暂不支持]", config.getId(), protocolType));
        }
    }
 
    /**
     * 创建 HTTP 协议实例
     *
     * @param config 协议实例配置
     * @return HTTP 协议实例
     */
    private IotHttpProtocol createHttpProtocol(IotGatewayProperties.ProtocolProperties config) {
        return new IotHttpProtocol(config);
    }
 
    /**
     * 创建 TCP 协议实例
     *
     * @param config 协议实例配置
     * @return TCP 协议实例
     */
    private IotTcpProtocol createTcpProtocol(IotGatewayProperties.ProtocolProperties config) {
        return new IotTcpProtocol(config);
    }
 
    /**
     * 创建 UDP 协议实例
     *
     * @param config 协议实例配置
     * @return UDP 协议实例
     */
    private IotUdpProtocol createUdpProtocol(IotGatewayProperties.ProtocolProperties config) {
        return new IotUdpProtocol(config);
    }
 
    /**
     * 创建 CoAP 协议实例
     *
     * @param config 协议实例配置
     * @return CoAP 协议实例
     */
    private IotCoapProtocol createCoapProtocol(IotGatewayProperties.ProtocolProperties config) {
        return new IotCoapProtocol(config);
    }
 
    /**
     * 创建 WebSocket 协议实例
     *
     * @param config 协议实例配置
     * @return WebSocket 协议实例
     */
    private IotWebSocketProtocol createWebSocketProtocol(IotGatewayProperties.ProtocolProperties config) {
        return new IotWebSocketProtocol(config);
    }
 
    /**
     * 创建 MQTT 协议实例
     *
     * @param config 协议实例配置
     * @return MQTT 协议实例
     */
    private IotMqttProtocol createMqttProtocol(IotGatewayProperties.ProtocolProperties config) {
        return new IotMqttProtocol(config);
    }
 
    /**
     * 创建 EMQX 协议实例
     *
     * @param config 协议实例配置
     * @return EMQX 协议实例
     */
    private IotEmqxProtocol createEmqxProtocol(IotGatewayProperties.ProtocolProperties config) {
        return new IotEmqxProtocol(config);
    }
 
    /**
     * 创建 Modbus TCP Client 协议实例
     *
     * @param config 协议实例配置
     * @return Modbus TCP Client 协议实例
     */
    private IotModbusTcpClientProtocol createModbusTcpClientProtocol(IotGatewayProperties.ProtocolProperties config) {
        return new IotModbusTcpClientProtocol(config);
    }
 
    /**
     * 创建 Modbus TCP Server 协议实例
     *
     * @param config 协议实例配置
     * @return Modbus TCP Server 协议实例
     */
    private IotModbusTcpServerProtocol createModbusTcpServerProtocol(IotGatewayProperties.ProtocolProperties config) {
        return new IotModbusTcpServerProtocol(config);
    }
 
}