]> git.basschouten.com Git - openhab-addons.git/blob
1be5407c200f0c3dab3298efbd7ced9fd7a2e739
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.shelly.internal.handler;
14
15 import java.util.Map;
16 import java.util.concurrent.ConcurrentHashMap;
17
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;
27
28 /***
29  * The{@link ShellyThingTable} implements a simple table to allow dispatching incoming events to the proper thing
30  * handler
31  *
32  * @author Markus Michels - Initial contribution
33  */
34 @NonNullByDefault
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;
39
40     public void addThing(String key, ShellyThingInterface thing) {
41         if (thingTable.containsKey(key)) {
42             thingTable.remove(key);
43         }
44         thingTable.put(key, thing);
45     }
46
47     public @Nullable ShellyThingInterface findThing(String key) {
48         ShellyThingInterface t = thingTable.get(key);
49         if (t != null) {
50             return t;
51         }
52         for (Map.Entry<String, ShellyThingInterface> entry : thingTable.entrySet()) {
53             t = entry.getValue();
54             if (t.checkRepresentation(key)) {
55                 return t;
56             }
57         }
58         return null;
59     }
60
61     public ShellyThingInterface getThing(String key) {
62         ShellyThingInterface t = findThing(key);
63         if (t == null) {
64             throw new IllegalArgumentException();
65         }
66         return t;
67     }
68
69     public void removeThing(String key) {
70         if (thingTable.containsKey(key)) {
71             thingTable.remove(key);
72         }
73     }
74
75     public Map<String, ShellyThingInterface> getTable() {
76         return thingTable;
77     }
78
79     public int size() {
80         return thingTable.size();
81     }
82
83     public void startDiscoveryService(BundleContext bundleContext) {
84         if (discoveryService == null) {
85             discoveryService = new ShellyBasicDiscoveryService(bundleContext, this);
86             discoveryService.registerDeviceDiscoveryService();
87         }
88     }
89
90     public void startScan() {
91         for (Map.Entry<String, ShellyThingInterface> thing : thingTable.entrySet()) {
92             (thing.getValue()).startScan();
93         }
94     }
95
96     public void stopDiscoveryService() {
97         if (discoveryService != null) {
98             discoveryService.unregisterDeviceDiscoveryService();
99             discoveryService = null;
100         }
101     }
102
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);
107         }
108     }
109
110     public void discoveredResult(DiscoveryResult result) {
111         if (discoveryService != null) {
112             discoveryService.discoveredResult(result);
113         }
114     }
115
116     @Deactivate
117     public void deactivate() {
118         stopDiscoveryService();
119     }
120 }