Commit 815f38436c4088361fa69fb6329c9ac6101e7338

Authored by YunaiV
1 parent def9ff11

review:【IoT 物联网】http、emqx 的实现

yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/config/IotGatewayProperties.java
... ... @@ -98,6 +98,21 @@ public class IotGatewayProperties {
98 98 */
99 99 private Integer serverPort;
100 100  
  101 + /**
  102 + * 是否开启 SSL
  103 + */
  104 + @NotNull(message = "是否开启 SSL 不能为空")
  105 + private Boolean sslEnabled = false;
  106 +
  107 + /**
  108 + * SSL 证书路径
  109 + */
  110 + private String sslKeyPath;
  111 + /**
  112 + * SSL 证书路径
  113 + */
  114 + private String sslCertPath;
  115 +
101 116 }
102 117  
103 118 @Data
... ...
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/emqx/IotEmqxAuthEventProtocol.java
... ... @@ -70,6 +70,7 @@ public class IotEmqxAuthEventProtocol {
70 70 IotEmqxAuthEventHandler handler = new IotEmqxAuthEventHandler(serverId);
71 71 router.post(IotMqttTopicUtils.MQTT_AUTH_PATH).handler(handler::handleAuth);
72 72 router.post(IotMqttTopicUtils.MQTT_EVENT_PATH).handler(handler::handleEvent);
  73 + // TODO @haohao:/mqtt/acl 需要处理么?
73 74  
74 75 // 3. 启动 HTTP 服务器
75 76 try {
... ...
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/emqx/IotEmqxUpstreamProtocol.java
... ... @@ -127,6 +127,7 @@ public class IotEmqxUpstreamProtocol {
127 127 // 1. 连接 MQTT Broker
128 128 CountDownLatch latch = new CountDownLatch(1);
129 129 AtomicBoolean success = new AtomicBoolean(false);
  130 + // TODO @haohao:要不要加 MqttClientOptions 参数?1)setCleanSession true;2)setMaxInflightQueue 10000;3)setKeepAliveInterval 60;4)setSsl/setTrustAll
130 131 mqttClient.connect(port, host, connectResult -> {
131 132 if (connectResult.succeeded()) {
132 133 log.info("[connectMqttSync][MQTT 客户端连接成功, host: {}, port: {}]", host, port);
... ...
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/http/IotHttpUpstreamProtocol.java
... ... @@ -7,6 +7,8 @@ import cn.iocoder.yudao.module.iot.gateway.protocol.http.router.IotHttpUpstreamH
7 7 import io.vertx.core.AbstractVerticle;
8 8 import io.vertx.core.Vertx;
9 9 import io.vertx.core.http.HttpServer;
  10 +import io.vertx.core.http.HttpServerOptions;
  11 +import io.vertx.core.net.PemKeyCertOptions;
10 12 import io.vertx.ext.web.Router;
11 13 import io.vertx.ext.web.handler.BodyHandler;
12 14 import jakarta.annotation.PostConstruct;
... ... @@ -49,10 +51,17 @@ public class IotHttpUpstreamProtocol extends AbstractVerticle {
49 51 router.post(IotHttpUpstreamHandler.PATH).handler(upstreamHandler);
50 52  
51 53 // 启动 HTTP 服务器
  54 + HttpServerOptions options = new HttpServerOptions()
  55 + .setPort(httpProperties.getServerPort());
  56 + if (Boolean.TRUE.equals(httpProperties.getSslEnabled())) {
  57 + PemKeyCertOptions pemKeyCertOptions = new PemKeyCertOptions().setKeyPath(httpProperties.getSslKeyPath())
  58 + .setCertPath(httpProperties.getSslCertPath());
  59 + options = options.setSsl(true).setKeyCertOptions(pemKeyCertOptions);
  60 + }
52 61 try {
53   - httpServer = vertx.createHttpServer()
  62 + httpServer = vertx.createHttpServer(options)
54 63 .requestHandler(router)
55   - .listen(httpProperties.getServerPort())
  64 + .listen()
56 65 .result();
57 66 log.info("[start][IoT 网关 HTTP 协议启动成功,端口:{}]", httpProperties.getServerPort());
58 67 } catch (Exception e) {
... ...