Skip to content

Commit

Permalink
Merge pull request #74 from rraboy/basic-mqtts-support
Browse files Browse the repository at this point in the history
Basic MQTTS support
  • Loading branch information
TheMrBooyah authored Apr 10, 2023
2 parents 8f17499 + f644cc7 commit bc89124
Showing 1 changed file with 13 additions and 4 deletions.
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

0 comments on commit bc89124

Please sign in to comment.