Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic MQTTS support #74

Merged
merged 2 commits into from
Apr 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import javax.net.ssl.SSLSocketFactory;

@SpringBootApplication
@ComponentScan(basePackageClasses = {
Expand Down Expand Up @@ -41,6 +42,7 @@ public MqttClient getMqttClient(@Value("${dirigera.mqtt.hostname:localhost}") fi
@Value("${dirigera.mqtt.port:1883}") final Short port,
@Value("${dirigera.mqtt.username:}") final String username,
@Value("${dirigera.mqtt.password:}") final String password,
@Value("${dirigera.mqtt.use-ssl:false}") final Boolean useSsl,
@Value("${dirigera.mqtt.reconnect:true}") final Boolean reconnect,
@Value("${dirigera.mqtt.timeout:0}") final Integer timeout,
@Value("${dirigera.mqtt.keep-alive:2}") final Integer keepAliveInterval,
Expand All @@ -52,13 +54,20 @@ public MqttClient getMqttClient(@Value("${dirigera.mqtt.hostname:localhost}") fi
final String uri;

publisherId = api.status().map(s -> s.id).block();
uri = String.format("tcp://%s:%d", host, port);
options = new MqttConnectOptions();

if (useSsl) {
uri = String.format("ssl://%s:%d", host, port);
options.setSocketFactory(SSLSocketFactory.getDefault());
} else {
uri = String.format("tcp://%s:%d", host, port);
}

client = new MqttClient(uri, publisherId);

log.info("Connect to MQTT broker: host={}, port={}, publisherId={}, reconnect={}, timeout={}",
host, port, publisherId, reconnect, timeout);
log.info("Connect to MQTT broker: host={}, port={}, publisherId={}, reconnect={}, timeout={}, useSsl={}",
host, port, publisherId, reconnect, timeout, useSsl);

options = new MqttConnectOptions();
options.setKeepAliveInterval(keepAliveInterval);
options.setMaxReconnectDelay(reconnectDelay);
options.setAutomaticReconnect(reconnect);
Expand Down