2 * Copyright (c) 2010-2020 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.jablotron.internal.discovery;
15 import static org.openhab.binding.jablotron.JablotronBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.List;
21 import java.util.concurrent.ScheduledFuture;
22 import java.util.concurrent.TimeUnit;
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;
38 * The {@link JablotronDiscoveryService} is responsible for the thing discovery
41 * @author Ondrej Pecta - Initial contribution
44 public class JablotronDiscoveryService extends AbstractDiscoveryService
45 implements DiscoveryService, ThingHandlerService {
46 private final Logger logger = LoggerFactory.getLogger(JablotronDiscoveryService.class);
48 private @Nullable JablotronBridgeHandler bridgeHandler;
50 private @Nullable ScheduledFuture<?> discoveryJob = null;
52 public JablotronDiscoveryService() {
53 super(DISCOVERY_TIMEOUT_SEC);
54 logger.debug("Creating discovery service");
57 private void startDiscovery() {
58 JablotronBridgeHandler localBridgeHandler = bridgeHandler;
59 if (localBridgeHandler != null && ThingStatus.ONLINE == localBridgeHandler.getThing().getStatus()) {
65 public void setThingHandler(@Nullable ThingHandler thingHandler) {
66 if (thingHandler instanceof JablotronBridgeHandler) {
67 bridgeHandler = (JablotronBridgeHandler) thingHandler;
72 public @Nullable ThingHandler getThingHandler() {
77 protected void stopBackgroundDiscovery() {
78 super.stopBackgroundDiscovery();
79 ScheduledFuture<?> localDiscoveryJob = discoveryJob;
80 if (localDiscoveryJob != null) {
81 localDiscoveryJob.cancel(true);
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);
95 public void activate() {
100 public void deactivate() {
105 public Set<ThingTypeUID> getSupportedThingTypes() {
106 return SUPPORTED_THING_TYPES_UIDS;
110 protected void startScan() {
111 logger.debug("Starting scanning for items...");
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);
120 Map<String, Object> properties = new HashMap<>();
121 properties.put(PROPERTY_SERVICE_ID, serviceId);
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());
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);
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());
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);
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());
158 private synchronized void discoverServices() {
159 JablotronBridgeHandler localBridgeHandler = bridgeHandler;
160 if (localBridgeHandler != null) {
161 List<JablotronDiscoveredService> services = localBridgeHandler.discoverServices();
163 if (services == null || services.isEmpty()) {
164 logger.debug("Cannot find any Jablotron device");
168 for (JablotronDiscoveredService service : services) {
169 String serviceId = String.valueOf(service.getId());
170 logger.debug("Found Jablotron service: {} id: {}", service.getName(), serviceId);
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);
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");