Skip to content

Commit

Permalink
MQTT API Controller: add optional config parameter to specify a topic…
Browse files Browse the repository at this point in the history
… prefix (OpenEMS#2553)

Co-authored-by: Stefan Feilmeier <[email protected]>
  • Loading branch information
2 people authored and fanass-dev committed May 6, 2024
1 parent 9dc338d commit 39e6787
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
@AttributeDefinition(name = "Edge-ID", description = "Client-ID for authentication at MQTT broker")
String clientId() default "edge0";

@AttributeDefinition(name = "Topic prefix", description = "Optional topic prefix (<topic_prefix>/edge/<edge_id>/...)")
String topicPrefix() default "";

@AttributeDefinition(name = "Username", description = "Username for authentication at MQTT broker")
String username();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

public interface ControllerApiMqtt extends Controller, OpenemsComponent, EventHandler {

public static final String TOPIC_PREFIX = "edge/%s/";
public static final String TOPIC_CHANNEL_PREFIX = "channel/";
public static final String TOPIC_CHANNEL_LAST_UPDATE = "lastUpdate";
public static final String TOPIC_EDGE_CONFIG = "edgeConfig/";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private void activate(ComponentContext context, Config config) throws Exception
this.config = config;

// Publish MQTT messages under the topic "edge/edge0/..."
this.topicPrefix = String.format(ControllerApiMqtt.TOPIC_PREFIX, config.clientId());
this.topicPrefix = createTopicPrefix(config);

super.activate(context, config.id(), config.alias(), config.enabled());
this.mqttConnector.connect(config.uri(), config.clientId(), config.username(), config.password(),
Expand All @@ -84,6 +84,31 @@ private void activate(ComponentContext context, Config config) throws Exception
});
}

/**
* Creates the topic prefix in either format.
*
* <ul>
* <li>topic_prefix/edge/edge_id/
* <li>edge/edge_id/
* </ul>
*
* @param config the {@link Config}
* @return the prefix
*/
protected static String createTopicPrefix(Config config) {
final var b = new StringBuilder();
if (config.topicPrefix() != null && !config.topicPrefix().isBlank()) {
b //
.append(config.topicPrefix()) //
.append("/");
}
b //
.append("edge/") //
.append(config.clientId()) //
.append("/");
return b.toString();
}

@Override
@Deactivate
protected void deactivate() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.openems.edge.controller.api.mqtt;

import static io.openems.edge.controller.api.mqtt.ControllerApiMqttImpl.createTopicPrefix;
import static org.junit.Assert.assertEquals;

import java.time.Instant;
import java.time.ZoneOffset;

Expand All @@ -25,6 +28,7 @@ public void test() throws Exception {
.activate(MyConfig.create() //
.setId(CTRL_ID) //
.setClientId("edge0") //
.setTopicPrefix("") //
.setUsername("guest") //
.setPassword("guest") //
.setUri("ws://localhost:1883") //
Expand All @@ -36,4 +40,19 @@ public void test() throws Exception {
.build());
}

@Test
public void testCreateTopicPrefix() throws Exception {
assertEquals("foo/bar/edge/edge0/", createTopicPrefix(MyConfig.create() //
.setClientId("edge0") //
.setTopicPrefix("foo/bar") //
.build()));
assertEquals("edge/edge0/", createTopicPrefix(MyConfig.create() //
.setClientId("edge0") //
.setTopicPrefix("") //
.build()));
assertEquals("edge/edge0/", createTopicPrefix(MyConfig.create() //
.setClientId("edge0") //
.setTopicPrefix(null) //
.build()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ protected static class Builder {
private PersistencePriority persistencePriority;
private boolean debugMode;
private String clientId;
private String topicPrefix;
private String username;
private String password;
private String certPem;
Expand All @@ -36,6 +37,11 @@ public Builder setClientId(String clientId) {
return this;
}

public Builder setTopicPrefix(String topicPrefix) {
this.topicPrefix = topicPrefix;
return this;
}

public Builder setUsername(String username) {
this.username = username;
return this;
Expand Down Expand Up @@ -112,6 +118,11 @@ public String clientId() {
return this.builder.clientId;
}

@Override
public String topicPrefix() {
return this.builder.topicPrefix;
}

@Override
public String username() {
return this.builder.username;
Expand Down

0 comments on commit 39e6787

Please sign in to comment.