Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental support for embedded MQTT broker.
Relates a bit to openhab/openhab-addons#7442. This addon does only initial provisioning of broker without any configuration possibilities. Signed-off-by: Łukasz Dywicki <luke@code-house.org>
- Loading branch information
Showing
13 changed files
with
485 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
- Copyright (C) 2024-2024 ConnectorIO Sp. z o.o. | ||
- | ||
- Licensed under the Apache License, Version 2.0 (the "License"); | ||
- you may not use this file except in compliance with the License. | ||
- You may obtain a copy of the License at | ||
- | ||
- http://www.apache.org/licenses/LICENSE-2.0 | ||
- | ||
- Unless required by applicable law or agreed to in writing, software | ||
- distributed under the License is distributed on an "AS IS" BASIS, | ||
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
- See the License for the specific language governing permissions and | ||
- limitations under the License. | ||
- | ||
- SPDX-License-Identifier: Apache-2.0 | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.connectorio.addons</groupId> | ||
<artifactId>bundles</artifactId> | ||
<version>3.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>org.connectorio.addons.mqtt</artifactId> | ||
<packaging>bundle</packaging> | ||
|
||
<name>ConnectorIO - Addons - MQTT</name> | ||
<description>MQTT broker.</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.openhab.addons.bundles</groupId> | ||
<artifactId>org.openhab.binding.mqtt</artifactId> | ||
<version>${openhab.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.openhab.addons.bundles</groupId> | ||
<artifactId>org.openhab.binding.mqtt.generic</artifactId> | ||
<version>${openhab.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.openhab.core.bundles</groupId> | ||
<artifactId>org.openhab.core.io.transport.mqtt</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.openhab.core.bundles</groupId> | ||
<artifactId>org.openhab.core.thing</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.moquette</groupId> | ||
<artifactId>moquette-broker</artifactId> | ||
<version>0.15</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-log4j12</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.osgi</groupId> | ||
<artifactId>org.osgi.service.component.annotations</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.osgi</groupId> | ||
<artifactId>osgi.core</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
112 changes: 112 additions & 0 deletions
112
...les/org.connectorio.addons.mqtt/src/main/java/org/connectorio/addons/mqtt/MqttBroker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright (C) 2024-2024 ConnectorIO Sp. z o.o. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.connectorio.addons.mqtt; | ||
|
||
import io.moquette.broker.Server; | ||
import io.moquette.broker.config.MemoryConfig; | ||
import io.moquette.broker.security.IAuthenticator; | ||
import io.moquette.broker.security.IAuthorizatorPolicy; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import org.openhab.binding.mqtt.MqttBindingConstants; | ||
import org.openhab.core.common.registry.ProviderChangeListener; | ||
import org.openhab.core.config.core.Configuration; | ||
import org.openhab.core.thing.Thing; | ||
import org.openhab.core.thing.ThingProvider; | ||
import org.openhab.core.thing.binding.builder.BridgeBuilder; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Deactivate; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@Component(service = ThingProvider.class) | ||
public class MqttBroker implements ThingProvider { | ||
|
||
private final List<ProviderChangeListener<Thing>> listeners = new CopyOnWriteArrayList<>(); | ||
|
||
private final Logger logger = LoggerFactory.getLogger(MqttBroker.class); | ||
private Server server; | ||
private Thing brokerThing; | ||
|
||
@Activate | ||
public MqttBroker() { | ||
server = new Server(); | ||
} | ||
|
||
@Activate | ||
public void start() throws IOException { | ||
IAuthenticator authenticator = new IAuthenticator() { | ||
@Override | ||
public boolean checkValid(String clientId, String username, byte[] password) { | ||
logger.debug("Attempt to authenticate client {} with username {}", clientId, username); | ||
return username != null && username.equals(clientId) && Arrays.equals(username.getBytes(), password); | ||
} | ||
}; | ||
IAuthorizatorPolicy authorizer = null; | ||
server.startServer(new MemoryConfig(new Properties()), Collections.emptyList(), null, authenticator, authorizer); | ||
|
||
Map<String, Object> cfg = new HashMap<>(); | ||
cfg.put("name", "system"); | ||
cfg.put("host", "127.0.0.1"); | ||
cfg.put("port", 1883); | ||
cfg.put("secure", false); | ||
cfg.put("clientID", "system"); | ||
cfg.put("username", "system"); | ||
cfg.put("password", "system"); | ||
brokerThing = BridgeBuilder.create(MqttBindingConstants.BRIDGE_TYPE_BROKER, "system") | ||
.withLabel("System broker") | ||
.withConfiguration(new Configuration(cfg)) | ||
.build(); | ||
listeners.forEach(listener -> listener.added(this, brokerThing)); | ||
} | ||
|
||
@Deactivate | ||
public void stop() { | ||
listeners.forEach(listener -> listener.removed(this, brokerThing)); | ||
brokerThing = null; | ||
|
||
if (server != null) { | ||
server.stopServer(); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public Collection<Thing> getAll() { | ||
return brokerThing != null ? Collections.singleton(brokerThing) : Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
public void addProviderChangeListener(ProviderChangeListener<Thing> listener) { | ||
this.listeners.add(listener); | ||
} | ||
|
||
@Override | ||
public void removeProviderChangeListener(ProviderChangeListener<Thing> listener) { | ||
this.listeners.remove(listener); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
- Copyright (C) 2024-2024 ConnectorIO Sp. z o.o. | ||
- | ||
- Licensed under the Apache License, Version 2.0 (the "License"); | ||
- you may not use this file except in compliance with the License. | ||
- You may obtain a copy of the License at | ||
- | ||
- http://www.apache.org/licenses/LICENSE-2.0 | ||
- | ||
- Unless required by applicable law or agreed to in writing, software | ||
- distributed under the License is distributed on an "AS IS" BASIS, | ||
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
- See the License for the specific language governing permissions and | ||
- limitations under the License. | ||
- | ||
- SPDX-License-Identifier: Apache-2.0 | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.connectorio.addons</groupId> | ||
<artifactId>features</artifactId> | ||
<version>3.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>org.connectorio.addons.feature.mqtt</artifactId> | ||
<packaging>pom</packaging> | ||
|
||
<name>ConnectorIO - Addons - Features - MQTT</name> | ||
<description>M-Bus deployment archive.</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.connectorio.addons</groupId> | ||
<artifactId>org.connectorio.addons.mqtt</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.openhab.core.features.karaf</groupId> | ||
<artifactId>org.openhab.core.features.karaf.openhab-core</artifactId> | ||
<classifier>features</classifier> | ||
<type>xml</type> | ||
<scope>runtime</scope> | ||
</dependency> | ||
<!-- remove after KARAF-7316 is fixed --> | ||
<dependency> | ||
<groupId>org.apache.karaf.features</groupId> | ||
<artifactId>framework</artifactId> | ||
<type>kar</type> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<resources> | ||
<resource> | ||
<directory>${basedir}/src/main/feature</directory> | ||
<filtering>true</filtering> | ||
<targetPath>${project.build.directory}/feature</targetPath> | ||
</resource> | ||
</resources> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-resources-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>resources</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>build-helper-maven-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.karaf.tooling</groupId> | ||
<artifactId>karaf-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>karaf-verification</id> | ||
<configuration> | ||
<descriptors> | ||
<descriptor>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${openhab.version}/xml/features</descriptor> | ||
<descriptor>mvn:org.openhab.distro/openhab-addons/${openhab.version}/xml/features</descriptor> | ||
</descriptors> | ||
<features> | ||
<feature>co7io-mqtt*</feature> | ||
</features> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
33 changes: 33 additions & 0 deletions
33
features/org.connectorio.addons.feature.mqtt/src/main/feature/feature.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
- Copyright (C) 2024-2024 ConnectorIO Sp. z o.o. | ||
- | ||
- Licensed under the Apache License, Version 2.0 (the "License"); | ||
- you may not use this file except in compliance with the License. | ||
- You may obtain a copy of the License at | ||
- | ||
- http://www.apache.org/licenses/LICENSE-2.0 | ||
- | ||
- Unless required by applicable law or agreed to in writing, software | ||
- distributed under the License is distributed on an "AS IS" BASIS, | ||
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
- See the License for the specific language governing permissions and | ||
- limitations under the License. | ||
- | ||
- SPDX-License-Identifier: Apache-2.0 | ||
--> | ||
<features name="co7io-mqtt-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.6.0"> | ||
|
||
<feature name="openhab-misc-mqtt-broker" description="MQTT broker for openHAB" version="${project.version}"> | ||
<feature>co7io-mqtt-broker</feature> | ||
</feature> | ||
|
||
<feature name="co7io-mqtt-broker" description="MQTT broker" version="${project.version}"> | ||
<feature prerequisite="true">wrap</feature> | ||
<feature>openhab-binding-mqtt</feature> | ||
<bundle>wrap:mvn:io.moquette/moquette-broker/0.15</bundle> | ||
|
||
<bundle start-level="80">mvn:org.connectorio.addons/org.connectorio.addons.mqtt/${project.version}</bundle> | ||
</feature> | ||
|
||
</features> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.