diff --git a/io.openems.edge.controller.api.mqtt/src/io/openems/edge/controller/api/mqtt/Config.java b/io.openems.edge.controller.api.mqtt/src/io/openems/edge/controller/api/mqtt/Config.java index 7d101ef1f60..8e3655d6555 100644 --- a/io.openems.edge.controller.api.mqtt/src/io/openems/edge/controller/api/mqtt/Config.java +++ b/io.openems.edge.controller.api.mqtt/src/io/openems/edge/controller/api/mqtt/Config.java @@ -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 (/edge//...)") + String topicPrefix() default ""; + @AttributeDefinition(name = "Username", description = "Username for authentication at MQTT broker") String username(); diff --git a/io.openems.edge.controller.api.mqtt/src/io/openems/edge/controller/api/mqtt/ControllerApiMqtt.java b/io.openems.edge.controller.api.mqtt/src/io/openems/edge/controller/api/mqtt/ControllerApiMqtt.java index 8ba29e82c37..e470466555e 100644 --- a/io.openems.edge.controller.api.mqtt/src/io/openems/edge/controller/api/mqtt/ControllerApiMqtt.java +++ b/io.openems.edge.controller.api.mqtt/src/io/openems/edge/controller/api/mqtt/ControllerApiMqtt.java @@ -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/"; diff --git a/io.openems.edge.controller.api.mqtt/src/io/openems/edge/controller/api/mqtt/ControllerApiMqttImpl.java b/io.openems.edge.controller.api.mqtt/src/io/openems/edge/controller/api/mqtt/ControllerApiMqttImpl.java index 14b355d8f99..7707612270a 100644 --- a/io.openems.edge.controller.api.mqtt/src/io/openems/edge/controller/api/mqtt/ControllerApiMqttImpl.java +++ b/io.openems.edge.controller.api.mqtt/src/io/openems/edge/controller/api/mqtt/ControllerApiMqttImpl.java @@ -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(), @@ -84,6 +84,31 @@ private void activate(ComponentContext context, Config config) throws Exception }); } + /** + * Creates the topic prefix in either format. + * + * + * + * @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() { diff --git a/io.openems.edge.controller.api.mqtt/test/io/openems/edge/controller/api/mqtt/ControllerApiMqttImplTest.java b/io.openems.edge.controller.api.mqtt/test/io/openems/edge/controller/api/mqtt/ControllerApiMqttImplTest.java index 515d91ef207..10981834b04 100644 --- a/io.openems.edge.controller.api.mqtt/test/io/openems/edge/controller/api/mqtt/ControllerApiMqttImplTest.java +++ b/io.openems.edge.controller.api.mqtt/test/io/openems/edge/controller/api/mqtt/ControllerApiMqttImplTest.java @@ -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; @@ -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") // @@ -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())); + } } \ No newline at end of file diff --git a/io.openems.edge.controller.api.mqtt/test/io/openems/edge/controller/api/mqtt/MyConfig.java b/io.openems.edge.controller.api.mqtt/test/io/openems/edge/controller/api/mqtt/MyConfig.java index aa2c382fba8..51e98901e37 100644 --- a/io.openems.edge.controller.api.mqtt/test/io/openems/edge/controller/api/mqtt/MyConfig.java +++ b/io.openems.edge.controller.api.mqtt/test/io/openems/edge/controller/api/mqtt/MyConfig.java @@ -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; @@ -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; @@ -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;