|
| 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 | +} |
0 commit comments