Skip to content

Commit 64786a4

Browse files
committed
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 <[email protected]>
1 parent 37163bc commit 64786a4

File tree

13 files changed

+485
-1
lines changed

13 files changed

+485
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
- Copyright (C) 2024-2024 ConnectorIO Sp. z o.o.
4+
-
5+
- Licensed under the Apache License, Version 2.0 (the "License");
6+
- you may not use this file except in compliance with the License.
7+
- You may obtain a copy of the License at
8+
-
9+
- http://www.apache.org/licenses/LICENSE-2.0
10+
-
11+
- Unless required by applicable law or agreed to in writing, software
12+
- distributed under the License is distributed on an "AS IS" BASIS,
13+
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
- See the License for the specific language governing permissions and
15+
- limitations under the License.
16+
-
17+
- SPDX-License-Identifier: Apache-2.0
18+
-->
19+
<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">
20+
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<parent>
24+
<groupId>org.connectorio.addons</groupId>
25+
<artifactId>bundles</artifactId>
26+
<version>3.0.0-SNAPSHOT</version>
27+
</parent>
28+
29+
<artifactId>org.connectorio.addons.mqtt</artifactId>
30+
<packaging>bundle</packaging>
31+
32+
<name>ConnectorIO - Addons - MQTT</name>
33+
<description>MQTT broker.</description>
34+
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.openhab.addons.bundles</groupId>
38+
<artifactId>org.openhab.binding.mqtt</artifactId>
39+
<version>${openhab.version}</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.openhab.addons.bundles</groupId>
43+
<artifactId>org.openhab.binding.mqtt.generic</artifactId>
44+
<version>${openhab.version}</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.openhab.core.bundles</groupId>
48+
<artifactId>org.openhab.core.io.transport.mqtt</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.openhab.core.bundles</groupId>
52+
<artifactId>org.openhab.core.thing</artifactId>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>io.moquette</groupId>
57+
<artifactId>moquette-broker</artifactId>
58+
<version>0.15</version>
59+
<exclusions>
60+
<exclusion>
61+
<groupId>org.slf4j</groupId>
62+
<artifactId>slf4j-log4j12</artifactId>
63+
</exclusion>
64+
</exclusions>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>org.slf4j</groupId>
69+
<artifactId>slf4j-api</artifactId>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.osgi</groupId>
73+
<artifactId>org.osgi.service.component.annotations</artifactId>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.osgi</groupId>
77+
<artifactId>osgi.core</artifactId>
78+
</dependency>
79+
</dependencies>
80+
81+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright (C) 2024-2024 ConnectorIO Sp. z o.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
package org.connectorio.addons.mqtt;
19+
20+
import io.moquette.broker.Server;
21+
import io.moquette.broker.config.MemoryConfig;
22+
import io.moquette.broker.security.IAuthenticator;
23+
import io.moquette.broker.security.IAuthorizatorPolicy;
24+
import java.io.IOException;
25+
import java.util.Arrays;
26+
import java.util.Collection;
27+
import java.util.Collections;
28+
import java.util.HashMap;
29+
import java.util.List;
30+
import java.util.Map;
31+
import java.util.Properties;
32+
import java.util.concurrent.CopyOnWriteArrayList;
33+
import org.openhab.binding.mqtt.MqttBindingConstants;
34+
import org.openhab.core.common.registry.ProviderChangeListener;
35+
import org.openhab.core.config.core.Configuration;
36+
import org.openhab.core.thing.Thing;
37+
import org.openhab.core.thing.ThingProvider;
38+
import org.openhab.core.thing.binding.builder.BridgeBuilder;
39+
import org.osgi.service.component.annotations.Activate;
40+
import org.osgi.service.component.annotations.Component;
41+
import org.osgi.service.component.annotations.Deactivate;
42+
import org.slf4j.Logger;
43+
import org.slf4j.LoggerFactory;
44+
45+
@Component(service = ThingProvider.class)
46+
public class MqttBroker implements ThingProvider {
47+
48+
private final List<ProviderChangeListener<Thing>> listeners = new CopyOnWriteArrayList<>();
49+
50+
private final Logger logger = LoggerFactory.getLogger(MqttBroker.class);
51+
private Server server;
52+
private Thing brokerThing;
53+
54+
@Activate
55+
public MqttBroker() {
56+
server = new Server();
57+
}
58+
59+
@Activate
60+
public void start() throws IOException {
61+
IAuthenticator authenticator = new IAuthenticator() {
62+
@Override
63+
public boolean checkValid(String clientId, String username, byte[] password) {
64+
logger.debug("Attempt to authenticate client {} with username {}", clientId, username);
65+
return username != null && username.equals(clientId) && Arrays.equals(username.getBytes(), password);
66+
}
67+
};
68+
IAuthorizatorPolicy authorizer = null;
69+
server.startServer(new MemoryConfig(new Properties()), Collections.emptyList(), null, authenticator, authorizer);
70+
71+
Map<String, Object> cfg = new HashMap<>();
72+
cfg.put("name", "system");
73+
cfg.put("host", "127.0.0.1");
74+
cfg.put("port", 1883);
75+
cfg.put("secure", false);
76+
cfg.put("clientID", "system");
77+
cfg.put("username", "system");
78+
cfg.put("password", "system");
79+
brokerThing = BridgeBuilder.create(MqttBindingConstants.BRIDGE_TYPE_BROKER, "system")
80+
.withLabel("System broker")
81+
.withConfiguration(new Configuration(cfg))
82+
.build();
83+
listeners.forEach(listener -> listener.added(this, brokerThing));
84+
}
85+
86+
@Deactivate
87+
public void stop() {
88+
listeners.forEach(listener -> listener.removed(this, brokerThing));
89+
brokerThing = null;
90+
91+
if (server != null) {
92+
server.stopServer();
93+
}
94+
95+
}
96+
97+
@Override
98+
public Collection<Thing> getAll() {
99+
return brokerThing != null ? Collections.singleton(brokerThing) : Collections.emptyList();
100+
}
101+
102+
@Override
103+
public void addProviderChangeListener(ProviderChangeListener<Thing> listener) {
104+
this.listeners.add(listener);
105+
}
106+
107+
@Override
108+
public void removeProviderChangeListener(ProviderChangeListener<Thing> listener) {
109+
this.listeners.remove(listener);
110+
}
111+
112+
}

bundles/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<module>org.connectorio.addons.managed.link</module>
6767
<module>org.connectorio.addons.managed.thing</module>
6868
<module>org.connectorio.addons.managed.xstream</module>
69+
<module>org.connectorio.addons.mqtt</module>
6970
<module>org.connectorio.addons.network</module>
7071
<module>org.connectorio.addons.network.core</module>
7172
<module>org.connectorio.addons.network.ip</module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
- Copyright (C) 2024-2024 ConnectorIO Sp. z o.o.
4+
-
5+
- Licensed under the Apache License, Version 2.0 (the "License");
6+
- you may not use this file except in compliance with the License.
7+
- You may obtain a copy of the License at
8+
-
9+
- http://www.apache.org/licenses/LICENSE-2.0
10+
-
11+
- Unless required by applicable law or agreed to in writing, software
12+
- distributed under the License is distributed on an "AS IS" BASIS,
13+
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
- See the License for the specific language governing permissions and
15+
- limitations under the License.
16+
-
17+
- SPDX-License-Identifier: Apache-2.0
18+
-->
19+
<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">
20+
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<parent>
24+
<groupId>org.connectorio.addons</groupId>
25+
<artifactId>features</artifactId>
26+
<version>3.0.0-SNAPSHOT</version>
27+
</parent>
28+
29+
<artifactId>org.connectorio.addons.feature.mqtt</artifactId>
30+
<packaging>pom</packaging>
31+
32+
<name>ConnectorIO - Addons - Features - MQTT</name>
33+
<description>M-Bus deployment archive.</description>
34+
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.connectorio.addons</groupId>
38+
<artifactId>org.connectorio.addons.mqtt</artifactId>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.openhab.core.features.karaf</groupId>
43+
<artifactId>org.openhab.core.features.karaf.openhab-core</artifactId>
44+
<classifier>features</classifier>
45+
<type>xml</type>
46+
<scope>runtime</scope>
47+
</dependency>
48+
<!-- remove after KARAF-7316 is fixed -->
49+
<dependency>
50+
<groupId>org.apache.karaf.features</groupId>
51+
<artifactId>framework</artifactId>
52+
<type>kar</type>
53+
<scope>provided</scope>
54+
</dependency>
55+
</dependencies>
56+
57+
<build>
58+
<resources>
59+
<resource>
60+
<directory>${basedir}/src/main/feature</directory>
61+
<filtering>true</filtering>
62+
<targetPath>${project.build.directory}/feature</targetPath>
63+
</resource>
64+
</resources>
65+
<plugins>
66+
<plugin>
67+
<groupId>org.apache.maven.plugins</groupId>
68+
<artifactId>maven-resources-plugin</artifactId>
69+
<executions>
70+
<execution>
71+
<goals>
72+
<goal>resources</goal>
73+
</goals>
74+
</execution>
75+
</executions>
76+
</plugin>
77+
<plugin>
78+
<groupId>org.codehaus.mojo</groupId>
79+
<artifactId>build-helper-maven-plugin</artifactId>
80+
</plugin>
81+
<plugin>
82+
<groupId>org.apache.karaf.tooling</groupId>
83+
<artifactId>karaf-maven-plugin</artifactId>
84+
<executions>
85+
<execution>
86+
<id>karaf-verification</id>
87+
<configuration>
88+
<descriptors>
89+
<descriptor>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${openhab.version}/xml/features</descriptor>
90+
<descriptor>mvn:org.openhab.distro/openhab-addons/${openhab.version}/xml/features</descriptor>
91+
</descriptors>
92+
<features>
93+
<feature>co7io-mqtt*</feature>
94+
</features>
95+
</configuration>
96+
</execution>
97+
</executions>
98+
</plugin>
99+
</plugins>
100+
</build>
101+
102+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
- Copyright (C) 2024-2024 ConnectorIO Sp. z o.o.
4+
-
5+
- Licensed under the Apache License, Version 2.0 (the "License");
6+
- you may not use this file except in compliance with the License.
7+
- You may obtain a copy of the License at
8+
-
9+
- http://www.apache.org/licenses/LICENSE-2.0
10+
-
11+
- Unless required by applicable law or agreed to in writing, software
12+
- distributed under the License is distributed on an "AS IS" BASIS,
13+
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
- See the License for the specific language governing permissions and
15+
- limitations under the License.
16+
-
17+
- SPDX-License-Identifier: Apache-2.0
18+
-->
19+
<features name="co7io-mqtt-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.6.0">
20+
21+
<feature name="openhab-misc-mqtt-broker" description="MQTT broker for openHAB" version="${project.version}">
22+
<feature>co7io-mqtt-broker</feature>
23+
</feature>
24+
25+
<feature name="co7io-mqtt-broker" description="MQTT broker" version="${project.version}">
26+
<feature prerequisite="true">wrap</feature>
27+
<feature>openhab-binding-mqtt</feature>
28+
<bundle>wrap:mvn:io.moquette/moquette-broker/0.15</bundle>
29+
30+
<bundle start-level="80">mvn:org.connectorio.addons/org.connectorio.addons.mqtt/${project.version}</bundle>
31+
</feature>
32+
33+
</features>

features/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<module>org.connectorio.addons.feature.io.transport.serial</module>
4646
<module>org.connectorio.addons.feature.managed</module>
4747
<module>org.connectorio.addons.feature.mbus</module>
48+
<module>org.connectorio.addons.feature.mqtt</module>
4849
<module>org.connectorio.addons.feature.network</module>
4950
<module>org.connectorio.addons.feature.norule</module>
5051
<module>org.connectorio.addons.feature.opcua</module>

0 commit comments

Comments
 (0)