|
@@ -31,41 +31,45 @@ import java.lang.management.ManagementFactory;
|
|
|
@AutoConfiguration
|
|
@AutoConfiguration
|
|
|
@EnableConfigurationProperties(IotComponentEmqxProperties.class)
|
|
@EnableConfigurationProperties(IotComponentEmqxProperties.class)
|
|
|
@ConditionalOnProperty(prefix = "yudao.iot.component.emqx", name = "enabled", havingValue = "true", matchIfMissing = false)
|
|
@ConditionalOnProperty(prefix = "yudao.iot.component.emqx", name = "enabled", havingValue = "true", matchIfMissing = false)
|
|
|
|
|
+// TODO @haohao:是不是不用扫 cn.iocoder.yudao.module.iot.component.core 拉,它尽量靠自动配置
|
|
|
@ComponentScan(basePackages = {
|
|
@ComponentScan(basePackages = {
|
|
|
"cn.iocoder.yudao.module.iot.component.core", // 核心包
|
|
"cn.iocoder.yudao.module.iot.component.core", // 核心包
|
|
|
- "cn.iocoder.yudao.module.iot.component.emqx" // EMQX组件包
|
|
|
|
|
|
|
+ "cn.iocoder.yudao.module.iot.component.emqx" // EMQX 组件包
|
|
|
})
|
|
})
|
|
|
public class IotComponentEmqxAutoConfiguration {
|
|
public class IotComponentEmqxAutoConfiguration {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 组件key
|
|
|
|
|
|
|
+ * 组件 key
|
|
|
*/
|
|
*/
|
|
|
private static final String PLUGIN_KEY = "emqx";
|
|
private static final String PLUGIN_KEY = "emqx";
|
|
|
|
|
|
|
|
public IotComponentEmqxAutoConfiguration() {
|
|
public IotComponentEmqxAutoConfiguration() {
|
|
|
|
|
+ // TODO @haohao:这个日志,融合到 initialize ?
|
|
|
log.info("[IotComponentEmqxAutoConfiguration][已启动]");
|
|
log.info("[IotComponentEmqxAutoConfiguration][已启动]");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@EventListener(ApplicationStartedEvent.class)
|
|
@EventListener(ApplicationStartedEvent.class)
|
|
|
public void initialize(ApplicationStartedEvent event) {
|
|
public void initialize(ApplicationStartedEvent event) {
|
|
|
- // 从应用上下文中获取需要的Bean
|
|
|
|
|
|
|
+ // 从应用上下文中获取需要的 Bean
|
|
|
IotComponentRegistry componentRegistry = event.getApplicationContext().getBean(IotComponentRegistry.class);
|
|
IotComponentRegistry componentRegistry = event.getApplicationContext().getBean(IotComponentRegistry.class);
|
|
|
IotComponentCommonProperties commonProperties = event.getApplicationContext().getBean(IotComponentCommonProperties.class);
|
|
IotComponentCommonProperties commonProperties = event.getApplicationContext().getBean(IotComponentCommonProperties.class);
|
|
|
|
|
|
|
|
// 设置当前组件的核心标识
|
|
// 设置当前组件的核心标识
|
|
|
|
|
+ // TODO @haohao:如果多个组件,都去设置,会不会冲突哈?
|
|
|
commonProperties.setPluginKey(PLUGIN_KEY);
|
|
commonProperties.setPluginKey(PLUGIN_KEY);
|
|
|
|
|
|
|
|
- // 将EMQX组件注册到组件注册表
|
|
|
|
|
|
|
+ // 将 EMQX 组件注册到组件注册表
|
|
|
componentRegistry.registerComponent(
|
|
componentRegistry.registerComponent(
|
|
|
PLUGIN_KEY,
|
|
PLUGIN_KEY,
|
|
|
SystemUtil.getHostInfo().getAddress(),
|
|
SystemUtil.getHostInfo().getAddress(),
|
|
|
- 0, // 内嵌模式固定为0
|
|
|
|
|
|
|
+ 0, // 内嵌模式固定为 0
|
|
|
getProcessId()
|
|
getProcessId()
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
log.info("[initialize][IoT EMQX 组件初始化完成]");
|
|
log.info("[initialize][IoT EMQX 组件初始化完成]");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // TODO @haohao:这个可能要注意,可能会有多个?冲突?
|
|
|
@Bean
|
|
@Bean
|
|
|
public Vertx vertx() {
|
|
public Vertx vertx() {
|
|
|
return Vertx.vertx();
|
|
return Vertx.vertx();
|
|
@@ -73,6 +77,7 @@ public class IotComponentEmqxAutoConfiguration {
|
|
|
|
|
|
|
|
@Bean
|
|
@Bean
|
|
|
public MqttClient mqttClient(Vertx vertx, IotComponentEmqxProperties emqxProperties) {
|
|
public MqttClient mqttClient(Vertx vertx, IotComponentEmqxProperties emqxProperties) {
|
|
|
|
|
+ // TODO @haohao:这个日志,要不要去掉,避免过多哈
|
|
|
log.info("MQTT配置: host={}, port={}, username={}, ssl={}",
|
|
log.info("MQTT配置: host={}, port={}, username={}, ssl={}",
|
|
|
emqxProperties.getMqttHost(), emqxProperties.getMqttPort(),
|
|
emqxProperties.getMqttHost(), emqxProperties.getMqttPort(),
|
|
|
emqxProperties.getMqttUsername(), emqxProperties.getMqttSsl());
|
|
emqxProperties.getMqttUsername(), emqxProperties.getMqttSsl());
|
|
@@ -81,14 +86,12 @@ public class IotComponentEmqxAutoConfiguration {
|
|
|
.setClientId("yudao-iot-downstream-" + IdUtil.fastSimpleUUID())
|
|
.setClientId("yudao-iot-downstream-" + IdUtil.fastSimpleUUID())
|
|
|
.setUsername(emqxProperties.getMqttUsername())
|
|
.setUsername(emqxProperties.getMqttUsername())
|
|
|
.setPassword(emqxProperties.getMqttPassword());
|
|
.setPassword(emqxProperties.getMqttPassword());
|
|
|
-
|
|
|
|
|
|
|
+ // TODO @haohao:可以用 ObjUtil.default
|
|
|
if (emqxProperties.getMqttSsl() != null) {
|
|
if (emqxProperties.getMqttSsl() != null) {
|
|
|
options.setSsl(emqxProperties.getMqttSsl());
|
|
options.setSsl(emqxProperties.getMqttSsl());
|
|
|
} else {
|
|
} else {
|
|
|
options.setSsl(false);
|
|
options.setSsl(false);
|
|
|
- log.warn("MQTT SSL配置为null,默认设置为false");
|
|
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
return MqttClient.create(vertx, options);
|
|
return MqttClient.create(vertx, options);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -106,6 +109,7 @@ public class IotComponentEmqxAutoConfiguration {
|
|
|
return new IotDeviceDownstreamHandlerImpl(mqttClient);
|
|
return new IotDeviceDownstreamHandlerImpl(mqttClient);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // TODO @haohao:这个通用下一下哈。
|
|
|
/**
|
|
/**
|
|
|
* 获取当前进程ID
|
|
* 获取当前进程ID
|
|
|
*
|
|
*
|
|
@@ -118,4 +122,5 @@ public class IotComponentEmqxAutoConfiguration {
|
|
|
String pid = name.split("@")[0];
|
|
String pid = name.split("@")[0];
|
|
|
return pid;
|
|
return pid;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
}
|
|
}
|