Skip to content

Commit

Permalink
Added the possibility to specify the MQTT ClientId both for source an…
Browse files Browse the repository at this point in the history
…d destination brokers
  • Loading branch information
piconem committed May 27, 2021
1 parent c2cda23 commit 977a106
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
2 changes: 2 additions & 0 deletions conf/mqtt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ brokerClientUsername: null
brokerClientPassword: null
brokerSecureCommunicationRequired: false
brokerServerCertPath: null
brokerClientId: null #If null the clientId will be randomly generated by the library (Default Option)

#Destination Broker Parameters
destinationBrokerAddress: 127.0.0.1
Expand All @@ -20,6 +21,7 @@ destinationBrokerClientUsername: null
destinationBrokerClientPassword: null
destinationBrokerSecureCommunicationRequired: false
destinationBrokerServerCertPath: null
destinationBrokerClientId: null #If null the clientId will be randomly generated by the library (Default Option)

#e.g., digitaltwin (without the final /)
destinationBrokerBaseTopic: null
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>it.unimore.dipi.iot</groupId>
<artifactId>wldt-core</artifactId>
<version>0.1.3.5</version>
<version>0.1.3.6</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class Mqtt2MqttConfiguration implements WldtWorkerConfiguration {

private String destinationBrokerTlsContext = "TLSv1.2";

private String destinationBrokerClientId = null;

private String deviceId = null;

private String brokerAddress = "127.0.0.1";
Expand All @@ -48,6 +50,8 @@ public class Mqtt2MqttConfiguration implements WldtWorkerConfiguration {

private String brokerTlsContext = "TLSv1.2";

private String brokerClientId = null;

private List<MqttTopicDescriptor> topicList;

public Mqtt2MqttConfiguration() {
Expand Down Expand Up @@ -213,6 +217,22 @@ public void setBrokerTlsContext(String brokerTlsContext) {
this.brokerTlsContext = brokerTlsContext;
}

public String getBrokerClientId() {
return brokerClientId;
}

public void setBrokerClientId(String brokerClientId) {
this.brokerClientId = brokerClientId;
}

public String getDestinationBrokerClientId() {
return destinationBrokerClientId;
}

public void setDestinationBrokerClientId(String destinationBrokerClientId) {
this.destinationBrokerClientId = destinationBrokerClientId;
}

@Override
public String toString() {
final StringBuffer sb = new StringBuffer("Mqtt2MqttConfiguration{");
Expand All @@ -226,6 +246,7 @@ public String toString() {
sb.append(", destinationBrokerSecureCommunicationRequired=").append(destinationBrokerSecureCommunicationRequired);
sb.append(", destinationBrokerServerCertPath='").append(destinationBrokerServerCertPath).append('\'');
sb.append(", destinationBrokerTlsContext='").append(destinationBrokerTlsContext).append('\'');
sb.append(", destinationBrokerClientId='").append(destinationBrokerClientId).append('\'');
sb.append(", deviceId='").append(deviceId).append('\'');
sb.append(", brokerAddress='").append(brokerAddress).append('\'');
sb.append(", brokerPort=").append(brokerPort);
Expand All @@ -235,6 +256,7 @@ public String toString() {
sb.append(", brokerSecureCommunicationRequired=").append(brokerSecureCommunicationRequired);
sb.append(", brokerServerCertPath='").append(brokerServerCertPath).append('\'');
sb.append(", brokerTlsContext='").append(brokerTlsContext).append('\'');
sb.append(", brokerClientId='").append(brokerClientId).append('\'');
sb.append(", topicList=").append(topicList);
sb.append('}');
return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,21 @@ private void initPhysicalDeviceMqttBrokerClient() throws WldtMqttModuleException

try {

physicalDeviceMqttBrokerClient = new MqttClient(getPhysicalThingMqttBrokerUrl(),String.format("%s%s", this.wldtId, "physical"), new MemoryPersistence());
String targetUrl = getPhysicalThingMqttBrokerUrl();

//Default client id
String mqttClientId = String.format("%s%s", this.wldtId, "physical");

//Check if a clientId is specified in the configuration and use it
if(this.mqtt2MqttConfiguration != null &&
this.mqtt2MqttConfiguration.getBrokerClientId() != null &&
this.mqtt2MqttConfiguration.getBrokerClientId().length() > 0
)
mqttClientId = this.mqtt2MqttConfiguration.getBrokerClientId();

logger.info("{} Client ({}) - Connecting to {} ...", TAG, mqttClientId, targetUrl);

physicalDeviceMqttBrokerClient = new MqttClient(targetUrl, mqttClientId, new MemoryPersistence());

MqttConnectOptions options = new MqttConnectOptions();
options.setAutomaticReconnect(true);
Expand Down Expand Up @@ -283,9 +297,19 @@ private void initDigitalTwinMqttBrokerClient() throws WldtMqttModuleException {

String targetUrl = getDestinationMqttBrokerUrl();

logger.info("{} Connecting to {} ...", TAG, targetUrl);
//Default client id
String mqttClientId = String.format("%s%s", this.wldtId, "digital");

//Check if a clientId is specified in the configuration and use it
if(this.mqtt2MqttConfiguration != null &&
this.mqtt2MqttConfiguration.getDestinationBrokerClientId() != null &&
this.mqtt2MqttConfiguration.getDestinationBrokerClientId().length() > 0
)
mqttClientId = this.mqtt2MqttConfiguration.getDestinationBrokerClientId();

logger.info("{} Client ({}) - Connecting to {} ...", TAG, mqttClientId, targetUrl);

digitalTwinMqttBrokerClient = new MqttClient(targetUrl,String.format("%s%s", this.wldtId, "digital"), new MemoryPersistence());
digitalTwinMqttBrokerClient = new MqttClient(targetUrl, mqttClientId, new MemoryPersistence());

MqttConnectOptions options = new MqttConnectOptions();
options.setAutomaticReconnect(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ private static Mqtt2MqttConfiguration getMqttComplexProtocolConfiguration(){
mqtt2MqttConfiguration.setDestinationBrokerPort(DESTINATION_BROKER_PORT);
mqtt2MqttConfiguration.setDeviceId(DEVICE_ID);

//If Required Specify the ClientId
//mqtt2MqttConfiguration.setBrokerClientId("physicalBrokerTestClientId");
//mqtt2MqttConfiguration.setDestinationBrokerClientId("digitalBrokerTestClientId");

//Specify Topic List Configuration
mqtt2MqttConfiguration.setTopicList(
Arrays.asList(
Expand Down

0 comments on commit 977a106

Please sign in to comment.