]> git.basschouten.com Git - openhab-addons.git/blob
b6369fc6e571d4020e8b82db20be94f7b1a9158e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.jablotron.internal.discovery;
14
15 import static org.openhab.binding.jablotron.JablotronBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.concurrent.ScheduledFuture;
22 import java.util.concurrent.TimeUnit;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.jablotron.internal.handler.JablotronBridgeHandler;
27 import org.openhab.binding.jablotron.internal.model.JablotronDiscoveredService;
28 import org.openhab.core.config.discovery.AbstractDiscoveryService;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.config.discovery.DiscoveryService;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerService;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * The {@link JablotronDiscoveryService} is responsible for the thing discovery
41  * process.
42  *
43  * @author Ondrej Pecta - Initial contribution
44  */
45 @NonNullByDefault
46 public class JablotronDiscoveryService extends AbstractDiscoveryService
47         implements DiscoveryService, ThingHandlerService {
48     private final Logger logger = LoggerFactory.getLogger(JablotronDiscoveryService.class);
49
50     private @Nullable JablotronBridgeHandler bridgeHandler;
51
52     private @Nullable ScheduledFuture<?> discoveryJob = null;
53
54     public JablotronDiscoveryService() {
55         super(DISCOVERY_TIMEOUT_SEC);
56         logger.debug("Creating discovery service");
57     }
58
59     private void startDiscovery() {
60         JablotronBridgeHandler localBridgeHandler = bridgeHandler;
61         if (localBridgeHandler != null && ThingStatus.ONLINE == localBridgeHandler.getThing().getStatus()) {
62             discoverServices();
63         }
64     }
65
66     @Override
67     public void setThingHandler(@Nullable ThingHandler thingHandler) {
68         if (thingHandler instanceof JablotronBridgeHandler) {
69             bridgeHandler = (JablotronBridgeHandler) thingHandler;
70         }
71     }
72
73     @Override
74     public @Nullable ThingHandler getThingHandler() {
75         return bridgeHandler;
76     }
77
78     @Override
79     protected void stopBackgroundDiscovery() {
80         super.stopBackgroundDiscovery();
81         ScheduledFuture<?> localDiscoveryJob = discoveryJob;
82         if (localDiscoveryJob != null) {
83             localDiscoveryJob.cancel(true);
84         }
85     }
86
87     @Override
88     protected void startBackgroundDiscovery() {
89         logger.debug("Starting Jablotron background discovery");
90         ScheduledFuture<?> localDiscoveryJob = discoveryJob;
91         if (localDiscoveryJob == null || localDiscoveryJob.isCancelled()) {
92             discoveryJob = scheduler.scheduleWithFixedDelay(this::startDiscovery, 10, 3600, TimeUnit.SECONDS);
93         }
94     }
95
96     @Override
97     public void activate() {
98         super.activate(null);
99     }
100
101     @Override
102     public void deactivate() {
103         super.deactivate();
104     }
105
106     @Override
107     public Set<ThingTypeUID> getSupportedThingTypes() {
108         return SUPPORTED_THING_TYPES_UIDS;
109     }
110
111     @Override
112     protected void startScan() {
113         logger.debug("Starting scanning for items...");
114         startDiscovery();
115     }
116
117     public void oasisDiscovered(String label, String serviceId) {
118         JablotronBridgeHandler localBridgeHandler = bridgeHandler;
119         if (localBridgeHandler != null) {
120             ThingUID thingUID = new ThingUID(THING_TYPE_OASIS, localBridgeHandler.getThing().getUID(), serviceId);
121
122             Map<String, Object> properties = new HashMap<>();
123             properties.put(PROPERTY_SERVICE_ID, serviceId);
124
125             logger.debug("Detected an OASIS alarm with service id: {}", serviceId);
126             thingDiscovered(DiscoveryResultBuilder.create(thingUID).withThingType(THING_TYPE_OASIS).withLabel(label)
127                     .withProperties(properties).withRepresentationProperty(PROPERTY_SERVICE_ID)
128                     .withBridge(localBridgeHandler.getThing().getUID()).build());
129         }
130     }
131
132     public void ja100Discovered(String label, String serviceId) {
133         JablotronBridgeHandler localBridgeHandler = bridgeHandler;
134         if (localBridgeHandler != null) {
135             ThingUID thingUID = new ThingUID(THING_TYPE_JA100, localBridgeHandler.getThing().getUID(), serviceId);
136             Map<String, Object> properties = new HashMap<>();
137             properties.put(PROPERTY_SERVICE_ID, serviceId);
138
139             logger.debug("Detected a JA100 alarm with service id: {}", serviceId);
140             thingDiscovered(DiscoveryResultBuilder.create(thingUID).withThingType(THING_TYPE_JA100).withLabel(label)
141                     .withProperties(properties).withRepresentationProperty(PROPERTY_SERVICE_ID)
142                     .withBridge(localBridgeHandler.getThing().getUID()).build());
143         }
144     }
145
146     public void ja100fDiscovered(String label, String serviceId) {
147         JablotronBridgeHandler localBridgeHandler = bridgeHandler;
148         if (localBridgeHandler != null) {
149             ThingUID thingUID = new ThingUID(THING_TYPE_JA100F, localBridgeHandler.getThing().getUID(), serviceId);
150             Map<String, Object> properties = new HashMap<>();
151             properties.put(PROPERTY_SERVICE_ID, serviceId);
152
153             logger.debug("Detected a JA100+ alarm with service id: {}", serviceId);
154             thingDiscovered(DiscoveryResultBuilder.create(thingUID).withThingType(THING_TYPE_JA100F).withLabel(label)
155                     .withProperties(properties).withRepresentationProperty(PROPERTY_SERVICE_ID)
156                     .withBridge(localBridgeHandler.getThing().getUID()).build());
157         }
158     }
159
160     private synchronized void discoverServices() {
161         JablotronBridgeHandler localBridgeHandler = bridgeHandler;
162         if (localBridgeHandler != null) {
163             List<JablotronDiscoveredService> services = localBridgeHandler.discoverServices();
164
165             if (services == null || services.isEmpty()) {
166                 logger.debug("Cannot find any Jablotron device");
167                 return;
168             }
169
170             for (JablotronDiscoveredService service : services) {
171                 String serviceId = String.valueOf(service.getId());
172                 logger.debug("Found Jablotron service: {} id: {}", service.getName(), serviceId);
173
174                 String serviceType = service.getServiceType().toLowerCase();
175                 if (serviceType.equals(THING_TYPE_OASIS.getId())) {
176                     oasisDiscovered("Jablotron OASIS Alarm : " + service.getName(), serviceId);
177                 } else if (serviceType.equals(THING_TYPE_JA100.getId())) {
178                     ja100Discovered("Jablotron JA100 Alarm : " + service.getName(), serviceId);
179                 } else if (serviceType.equals(THING_TYPE_JA100F.getId())) {
180                     ja100fDiscovered("Jablotron JA100+ Alarm : " + service.getName(), serviceId);
181                 } else {
182                     logger.info("Unsupported device type discovered: {} with serviceId: {} and type: {}",
183                             service.getName(), serviceId, service.getServiceType());
184                     logger.info("Please create a new issue and attach the above information");
185                 }
186             }
187         }
188     }
189 }