2 * Copyright (c) 2010-2024 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.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;
40 * The {@link JablotronDiscoveryService} is responsible for the thing discovery
43 * @author Ondrej Pecta - Initial contribution
46 public class JablotronDiscoveryService extends AbstractDiscoveryService
47 implements DiscoveryService, ThingHandlerService {
48 private final Logger logger = LoggerFactory.getLogger(JablotronDiscoveryService.class);
50 private @Nullable JablotronBridgeHandler bridgeHandler;
52 private @Nullable ScheduledFuture<?> discoveryJob = null;
54 public JablotronDiscoveryService() {
55 super(DISCOVERY_TIMEOUT_SEC);
56 logger.debug("Creating discovery service");
59 private void startDiscovery() {
60 JablotronBridgeHandler localBridgeHandler = bridgeHandler;
61 if (localBridgeHandler != null && ThingStatus.ONLINE == localBridgeHandler.getThing().getStatus()) {
67 public void setThingHandler(@Nullable ThingHandler thingHandler) {
68 if (thingHandler instanceof JablotronBridgeHandler bridgeHandler) {
69 this.bridgeHandler = bridgeHandler;
74 public @Nullable ThingHandler getThingHandler() {
79 protected void stopBackgroundDiscovery() {
80 super.stopBackgroundDiscovery();
81 ScheduledFuture<?> localDiscoveryJob = discoveryJob;
82 if (localDiscoveryJob != null) {
83 localDiscoveryJob.cancel(true);
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);
97 public void activate() {
102 public void deactivate() {
107 public Set<ThingTypeUID> getSupportedThingTypes() {
108 return SUPPORTED_THING_TYPES_UIDS;
112 protected void startScan() {
113 logger.debug("Starting scanning for items...");
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);
122 Map<String, Object> properties = new HashMap<>();
123 properties.put(PROPERTY_SERVICE_ID, serviceId);
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());
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);
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());
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);
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());
160 private synchronized void discoverServices() {
161 JablotronBridgeHandler localBridgeHandler = bridgeHandler;
162 if (localBridgeHandler != null) {
163 List<JablotronDiscoveredService> services = localBridgeHandler.discoverServices();
165 if (services == null || services.isEmpty()) {
166 logger.debug("Cannot find any Jablotron device");
170 for (JablotronDiscoveredService service : services) {
171 String serviceId = String.valueOf(service.getId());
172 logger.debug("Found Jablotron service: {} id: {}", service.getName(), serviceId);
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);
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");