]> git.basschouten.com Git - openhab-addons.git/blob
90b6a202689acba61813a298e842d12ec8364b5f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.onewire.internal.discovery;
14
15 import static org.openhab.binding.onewire.internal.OwBindingConstants.*;
16
17 import java.util.Date;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23
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.AbstractDiscoveryService;
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.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * The {@link OwDiscoveryService} implements the discovery service for the OneWire binding.
39  *
40  * @author Jan N. Klug - Initial contribution
41  */
42 @NonNullByDefault
43 public class OwDiscoveryService extends AbstractDiscoveryService {
44     private final Logger logger = LoggerFactory.getLogger(OwDiscoveryService.class);
45
46     private final OwserverBridgeHandler owBridgeHandler;
47     private final ThingUID bridgeUID;
48
49     Map<SensorId, OwDiscoveryItem> owDiscoveryItems = new HashMap<>();
50     Set<SensorId> associatedSensors = new HashSet<>();
51
52     public OwDiscoveryService(OwserverBridgeHandler owBridgeHandler) {
53         super(SUPPORTED_THING_TYPES, 60, false);
54         this.owBridgeHandler = owBridgeHandler;
55         this.bridgeUID = owBridgeHandler.getThing().getUID();
56         logger.debug("registering discovery service for {}", owBridgeHandler);
57     }
58
59     private void scanDirectory(String baseDirectory) {
60         List<SensorId> directoryList;
61
62         logger.trace("scanning {} on bridge {}", baseDirectory, bridgeUID);
63         try {
64             directoryList = owBridgeHandler.getDirectory(baseDirectory);
65         } catch (OwException e) {
66             logger.info("empty directory '{}' for {}", baseDirectory, bridgeUID);
67             return;
68         }
69
70         // find all valid sensors
71         for (SensorId directoryEntry : directoryList) {
72             try {
73                 OwDiscoveryItem owDiscoveryItem = new OwDiscoveryItem(owBridgeHandler, directoryEntry);
74                 if (owDiscoveryItem.getSensorType() == OwSensorType.DS2409) {
75                     // scan hub sub-directories
76                     logger.trace("found hub {}, scanning sub-directories", directoryEntry);
77
78                     scanDirectory(owDiscoveryItem.getSensorId().getFullPath() + "/main/");
79                     scanDirectory(owDiscoveryItem.getSensorId().getFullPath() + "/aux/");
80                 } else {
81                     // add found sensor to list
82                     logger.trace("found sensor {} (type: {})", directoryEntry, owDiscoveryItem.getSensorType());
83
84                     owDiscoveryItems.put(owDiscoveryItem.getSensorId(), owDiscoveryItem);
85                     associatedSensors.addAll(owDiscoveryItem.getAssociatedSensorIds());
86                 }
87             } catch (OwException e) {
88                 logger.debug("error while scanning for sensors in directory {} on bridge {}: {}", baseDirectory,
89                         bridgeUID, e.getMessage());
90             }
91         }
92     }
93
94     @Override
95     public void startScan() {
96         scanDirectory("/");
97
98         // remove duplicates
99         owDiscoveryItems.entrySet().removeIf(s -> associatedSensors.contains(s.getKey()));
100
101         // make discovery results
102         for (OwDiscoveryItem owDiscoveryItem : owDiscoveryItems.values()) {
103             owDiscoveryItem.checkSensorType();
104             try {
105                 ThingTypeUID thingTypeUID = owDiscoveryItem.getThingTypeUID();
106
107                 String normalizedId = owDiscoveryItem.getNormalizedSensorId();
108                 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, normalizedId);
109                 logger.debug("created thing UID {} for sensor {}, type {}", thingUID, owDiscoveryItem.getSensorId(),
110                         owDiscoveryItem.getSensorType());
111
112                 Map<String, Object> properties = new HashMap<>();
113                 properties.put(PROPERTY_MODELID, owDiscoveryItem.getSensorType().toString());
114                 properties.put(PROPERTY_VENDOR, owDiscoveryItem.getVendor());
115                 properties.put(CONFIG_ID, owDiscoveryItem.getSensorId().getFullPath());
116
117                 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID)
118                         .withProperties(properties).withBridge(bridgeUID).withLabel(owDiscoveryItem.getLabel()).build();
119
120                 thingDiscovered(discoveryResult);
121             } catch (OwException e) {
122                 logger.info("sensor-id {}: {}", owDiscoveryItem.getSensorId(), e.getMessage());
123             }
124         }
125     }
126
127     @Override
128     protected synchronized void stopScan() {
129         removeOlderResults(getTimestampOfLastScan());
130         super.stopScan();
131     }
132
133     @Override
134     public void deactivate() {
135         removeOlderResults(new Date().getTime());
136     }
137 }