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.ShellyBasicDiscoveryService;
21 import org.openhab.core.config.discovery.DiscoveryResult;
22 import org.openhab.core.thing.ThingTypeUID;
23 import org.osgi.framework.BundleContext;
24 import org.osgi.service.component.annotations.Component;
25 import org.osgi.service.component.annotations.ConfigurationPolicy;
26 import org.osgi.service.component.annotations.Deactivate;
29 * The{@link ShellyThingTable} implements a simple table to allow dispatching incoming events to the proper thing
32 * @author Markus Michels - Initial contribution
35 @Component(service = ShellyThingTable.class, configurationPolicy = ConfigurationPolicy.OPTIONAL)
36 public class ShellyThingTable {
37 private Map<String, ShellyThingInterface> thingTable = new ConcurrentHashMap<>();
38 private @Nullable ShellyBasicDiscoveryService discoveryService;
40 public void addThing(String key, ShellyThingInterface thing) {
41 if (thingTable.containsKey(key)) {
42 thingTable.remove(key);
44 thingTable.put(key, thing);
47 public @Nullable ShellyThingInterface findThing(String key) {
48 ShellyThingInterface t = thingTable.get(key);
52 for (Map.Entry<String, ShellyThingInterface> entry : thingTable.entrySet()) {
54 if (t.checkRepresentation(key)) {
61 public ShellyThingInterface getThing(String key) {
62 ShellyThingInterface t = findThing(key);
64 throw new IllegalArgumentException();
69 public void removeThing(String key) {
70 if (thingTable.containsKey(key)) {
71 thingTable.remove(key);
75 public Map<String, ShellyThingInterface> getTable() {
80 return thingTable.size();
83 public void startDiscoveryService(BundleContext bundleContext) {
84 if (discoveryService == null) {
85 discoveryService = new ShellyBasicDiscoveryService(bundleContext, this);
86 discoveryService.registerDeviceDiscoveryService();
90 public void startScan() {
91 for (Map.Entry<String, ShellyThingInterface> thing : thingTable.entrySet()) {
92 (thing.getValue()).startScan();
96 public void stopDiscoveryService() {
97 if (discoveryService != null) {
98 discoveryService.unregisterDeviceDiscoveryService();
99 discoveryService = null;
103 public void discoveredResult(ThingTypeUID uid, String model, String serviceName, String address,
104 Map<String, Object> properties) {
105 if (discoveryService != null) {
106 discoveryService.discoveredResult(uid, model, serviceName, address, properties);
110 public void discoveredResult(DiscoveryResult result) {
111 if (discoveryService != null) {
112 discoveryService.discoveredResult(result);
117 public void deactivate() {
118 stopDiscoveryService();