Skip to content

Commit

Permalink
Experimental support for embedded MQTT broker.
Browse files Browse the repository at this point in the history
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
splatch committed May 7, 2024
1 parent 37163bc commit 64786a4
Show file tree
Hide file tree
Showing 13 changed files with 485 additions and 1 deletion.
81 changes: 81 additions & 0 deletions bundles/org.connectorio.addons.mqtt/pom.xml
@@ -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>
@@ -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);
}

}
1 change: 1 addition & 0 deletions bundles/pom.xml
Expand Up @@ -66,6 +66,7 @@
<module>org.connectorio.addons.managed.link</module>
<module>org.connectorio.addons.managed.thing</module>
<module>org.connectorio.addons.managed.xstream</module>
<module>org.connectorio.addons.mqtt</module>
<module>org.connectorio.addons.network</module>
<module>org.connectorio.addons.network.core</module>
<module>org.connectorio.addons.network.ip</module>
Expand Down
102 changes: 102 additions & 0 deletions features/org.connectorio.addons.feature.mqtt/pom.xml
@@ -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>
@@ -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>
1 change: 1 addition & 0 deletions features/pom.xml
Expand Up @@ -45,6 +45,7 @@
<module>org.connectorio.addons.feature.io.transport.serial</module>
<module>org.connectorio.addons.feature.managed</module>
<module>org.connectorio.addons.feature.mbus</module>
<module>org.connectorio.addons.feature.mqtt</module>
<module>org.connectorio.addons.feature.network</module>
<module>org.connectorio.addons.feature.norule</module>
<module>org.connectorio.addons.feature.opcua</module>
Expand Down

0 comments on commit 64786a4

Please sign in to comment.