]> git.basschouten.com Git - openhab-addons.git/blob
88fabcd1b1cec053f0d3d86c8af3c1318a03bc22
[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.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;
26
27 /***
28  * The{@link ShellyThingTable} implements a simple table to allow dispatching incoming events to the proper thing
29  * handler
30  *
31  * @author Markus Michels - Initial contribution
32  */
33 @NonNullByDefault
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;
38
39     public void addThing(String key, ShellyThingInterface thing) {
40         if (thingTable.containsKey(key)) {
41             thingTable.remove(key);
42         }
43         thingTable.put(key, thing);
44     }
45
46     public @Nullable ShellyThingInterface findThing(String key) {
47         ShellyThingInterface t = thingTable.get(key);
48         if (t != null) {
49             return t;
50         }
51         for (Map.Entry<String, ShellyThingInterface> entry : thingTable.entrySet()) {
52             t = entry.getValue();
53             if (t.checkRepresentation(key)) {
54                 return t;
55             }
56         }
57         return null;
58     }
59
60     public ShellyThingInterface getThing(String key) {
61         ShellyThingInterface t = findThing(key);
62         if (t == null) {
63             throw new IllegalArgumentException();
64         }
65         return t;
66     }
67
68     public void removeThing(String key) {
69         if (thingTable.containsKey(key)) {
70             thingTable.remove(key);
71         }
72     }
73
74     public Map<String, ShellyThingInterface> getTable() {
75         return thingTable;
76     }
77
78     public int size() {
79         return thingTable.size();
80     }
81
82     public void startDiscoveryService(BundleContext bundleContext) {
83         if (bluDiscoveryService == null) {
84             bluDiscoveryService = new ShellyBluDiscoveryService(bundleContext, this);
85             bluDiscoveryService.registerDeviceDiscoveryService();
86         }
87     }
88
89     public void startScan() {
90         for (Map.Entry<String, ShellyThingInterface> thing : thingTable.entrySet()) {
91             (thing.getValue()).startScan();
92         }
93     }
94
95     public void stopDiscoveryService() {
96         if (bluDiscoveryService != null) {
97             bluDiscoveryService.unregisterDeviceDiscoveryService();
98             bluDiscoveryService = null;
99         }
100     }
101
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);
106         }
107     }
108
109     @Deactivate
110     public void deactivate() {
111         stopDiscoveryService();
112     }
113 }