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.onewire.internal.discovery;
15 import static org.openhab.binding.onewire.internal.OwBindingConstants.*;
17 import java.util.Date;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.openhab.binding.onewire.internal.OwException;
26 import org.openhab.binding.onewire.internal.SensorId;
27 import org.openhab.binding.onewire.internal.device.OwSensorType;
28 import org.openhab.binding.onewire.internal.handler.OwserverBridgeHandler;
29 import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService;
30 import org.openhab.core.config.discovery.DiscoveryResult;
31 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.ServiceScope;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link OwDiscoveryService} implements the discovery service for the OneWire binding.
42 * @author Jan N. Klug - Initial contribution
44 @Component(scope = ServiceScope.PROTOTYPE, service = OwDiscoveryService.class)
46 public class OwDiscoveryService extends AbstractThingHandlerDiscoveryService<OwserverBridgeHandler> {
47 private final Logger logger = LoggerFactory.getLogger(OwDiscoveryService.class);
49 Map<SensorId, OwDiscoveryItem> owDiscoveryItems = new HashMap<>();
50 Set<SensorId> associatedSensors = new HashSet<>();
52 public OwDiscoveryService() {
53 super(OwserverBridgeHandler.class, SUPPORTED_THING_TYPES, 60, false);
56 private void scanDirectory(OwserverBridgeHandler bridgeHandler, String baseDirectory) {
57 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
59 List<SensorId> directoryList;
61 logger.trace("scanning {} on bridge {}", baseDirectory, bridgeUID);
63 directoryList = bridgeHandler.getDirectory(baseDirectory);
64 } catch (OwException e) {
65 logger.info("empty directory '{}' for {}", baseDirectory, bridgeUID);
69 // find all valid sensors
70 for (SensorId directoryEntry : directoryList) {
72 OwDiscoveryItem owDiscoveryItem = new OwDiscoveryItem(bridgeHandler, directoryEntry);
73 if (owDiscoveryItem.getSensorType() == OwSensorType.DS2409) {
74 // scan hub sub-directories
75 logger.trace("found hub {}, scanning sub-directories", directoryEntry);
77 scanDirectory(bridgeHandler, owDiscoveryItem.getSensorId().getFullPath() + "/main/");
78 scanDirectory(bridgeHandler, owDiscoveryItem.getSensorId().getFullPath() + "/aux/");
80 // add found sensor to list
81 logger.trace("found sensor {} (type: {})", directoryEntry, owDiscoveryItem.getSensorType());
83 owDiscoveryItems.put(owDiscoveryItem.getSensorId(), owDiscoveryItem);
84 associatedSensors.addAll(owDiscoveryItem.getAssociatedSensorIds());
86 } catch (OwException e) {
87 logger.debug("error while scanning for sensors in directory {} on bridge {}: {}", baseDirectory,
88 bridgeUID, e.getMessage());
94 public void startScan() {
95 ThingUID bridgeUID = thingHandler.getThing().getUID();
97 scanDirectory(thingHandler, "/");
100 owDiscoveryItems.entrySet().removeIf(s -> associatedSensors.contains(s.getKey()));
102 // make discovery results
103 for (OwDiscoveryItem owDiscoveryItem : owDiscoveryItems.values()) {
104 owDiscoveryItem.checkSensorType();
106 ThingTypeUID thingTypeUID = owDiscoveryItem.getThingTypeUID();
108 String normalizedId = owDiscoveryItem.getNormalizedSensorId();
109 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, normalizedId);
110 logger.debug("created thing UID {} for sensor {}, type {}", thingUID, owDiscoveryItem.getSensorId(),
111 owDiscoveryItem.getSensorType());
113 Map<String, Object> properties = new HashMap<>();
114 properties.put(PROPERTY_MODELID, owDiscoveryItem.getSensorType().toString());
115 properties.put(PROPERTY_VENDOR, owDiscoveryItem.getVendor());
116 properties.put(CONFIG_ID, owDiscoveryItem.getSensorId().getFullPath());
118 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID)
119 .withProperties(properties).withBridge(bridgeUID).withLabel(owDiscoveryItem.getLabel()).build();
121 thingDiscovered(discoveryResult);
122 } catch (OwException e) {
123 logger.info("sensor-id {}: {}", owDiscoveryItem.getSensorId(), e.getMessage());
129 protected synchronized void stopScan() {
130 removeOlderResults(getTimestampOfLastScan());
135 public void dispose() {
137 removeOlderResults(new Date().getTime());