2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.shelly.internal.handler;
16 import java.util.concurrent.ConcurrentHashMap;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.shelly.internal.discovery.ShellyBluDiscoveryService;
21 import org.openhab.core.thing.ThingTypeUID;
22 import org.osgi.framework.BundleContext;
23 import org.osgi.service.component.annotations.Component;
24 import org.osgi.service.component.annotations.ConfigurationPolicy;
25 import org.osgi.service.component.annotations.Deactivate;
28 * The{@link ShellyThingTable} implements a simple table to allow dispatching incoming events to the proper thing
31 * @author Markus Michels - Initial contribution
34 @Component(service = ShellyThingTable.class, configurationPolicy = ConfigurationPolicy.OPTIONAL)
35 public class ShellyThingTable {
36 private Map<String, ShellyThingInterface> thingTable = new ConcurrentHashMap<>();
37 private @Nullable ShellyBluDiscoveryService bluDiscoveryService;
39 public void addThing(String key, ShellyThingInterface thing) {
40 if (thingTable.containsKey(key)) {
41 thingTable.remove(key);
43 thingTable.put(key, thing);
46 public @Nullable ShellyThingInterface findThing(String key) {
47 ShellyThingInterface t = thingTable.get(key);
51 for (Map.Entry<String, ShellyThingInterface> entry : thingTable.entrySet()) {
53 if (t.checkRepresentation(key)) {
60 public ShellyThingInterface getThing(String key) {
61 ShellyThingInterface t = findThing(key);
63 throw new IllegalArgumentException();
68 public void removeThing(String key) {
69 if (thingTable.containsKey(key)) {
70 thingTable.remove(key);
74 public Map<String, ShellyThingInterface> getTable() {
79 return thingTable.size();
82 public void startDiscoveryService(BundleContext bundleContext) {
83 if (bluDiscoveryService == null) {
84 bluDiscoveryService = new ShellyBluDiscoveryService(bundleContext, this);
85 bluDiscoveryService.registerDeviceDiscoveryService();
89 public void startScan() {
90 for (Map.Entry<String, ShellyThingInterface> thing : thingTable.entrySet()) {
91 (thing.getValue()).startScan();
95 public void stopDiscoveryService() {
96 if (bluDiscoveryService != null) {
97 bluDiscoveryService.unregisterDeviceDiscoveryService();
98 bluDiscoveryService = null;
102 public void discoveredResult(ThingTypeUID uid, String model, String serviceName, String address,
103 Map<String, Object> properties) {
104 if (bluDiscoveryService != null) {
105 bluDiscoveryService.discoveredResult(uid, model, serviceName, address, properties);
110 public void deactivate() {
111 stopDiscoveryService();