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