2 * Copyright (c) 2010-2023 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.nio.charset.StandardCharsets;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.openhab.binding.onewire.internal.DS2438Configuration;
25 import org.openhab.binding.onewire.internal.OwException;
26 import org.openhab.binding.onewire.internal.OwPageBuffer;
27 import org.openhab.binding.onewire.internal.SensorId;
28 import org.openhab.binding.onewire.internal.device.OwSensorType;
29 import org.openhab.binding.onewire.internal.handler.OwserverBridgeHandler;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The {@link OwDiscoveryItem} class defines a discovery item for OneWire devices
37 * @author Jan N. Klug - Initial contribution
40 public class OwDiscoveryItem {
41 private final Logger logger = LoggerFactory.getLogger(OwDiscoveryItem.class);
43 private final SensorId sensorId;
44 private OwSensorType sensorType = OwSensorType.UNKNOWN;
45 private String vendor = "Dallas/Maxim";
47 private final Map<SensorId, OwSensorType> associatedSensors = new HashMap<>();
49 public OwDiscoveryItem(OwserverBridgeHandler bridgeHandler, SensorId sensorId) throws OwException {
50 this.sensorId = sensorId;
51 sensorType = bridgeHandler.getType(sensorId);
54 bridgeHandler.readPages(sensorId);
55 DS2438Configuration config = new DS2438Configuration(bridgeHandler, sensorId);
56 associatedSensors.putAll(config.getAssociatedSensors());
57 logger.trace("found associated sensors: {}", associatedSensors);
58 vendor = config.getVendor();
59 sensorType = config.getSensorSubType();
62 vendor = "Embedded Data Systems";
63 OwPageBuffer pages = bridgeHandler.readPages(sensorId);
64 try { // determine subsensorType
65 sensorType = OwSensorType.valueOf(new String(pages.getPage(0), 0, 7, StandardCharsets.US_ASCII));
66 } catch (IllegalArgumentException e) {
67 sensorType = OwSensorType.UNKNOWN;
80 public OwSensorType getSensorType() {
89 public SensorId getSensorId() {
94 * normalized sensor id (for naming the discovery result)
96 * @return sensor id in format familyId_xxxxxxxxxx
98 public String getNormalizedSensorId() {
99 return sensorId.getId().replace(".", "_");
103 * get vendor name (if available)
105 * @return vendor name
107 public String getVendor() {
112 * get this sensors ThingTypeUID
114 * @return ThingTypeUID if mapping successful
116 public ThingTypeUID getThingTypeUID() throws OwException {
117 ThingTypeUID thingTypeUID = THING_TYPE_MAP.get(sensorType);
118 if (thingTypeUID != null) {
121 throw new OwException(sensorType + " cannot be mapped to thing type");
126 * get a list of all sensors associated to this sensor
128 * @return list of strings
130 public List<SensorId> getAssociatedSensorIds() {
131 return new ArrayList<>(associatedSensors.keySet());
135 * determine this sensors type
137 public void checkSensorType() {
138 logger.debug("checkSensorType: {} with {}", this, associatedSensors);
140 switch (sensorType) {
141 case MS_TH, MS_TH_S -> sensorType = DS2438Configuration.getMultisensorType(sensorType,
142 new ArrayList<>(associatedSensors.values()));
149 * get Label {@code "<thingtype> (<id>)"}
151 * @return the thing label
153 public String getLabel() {
154 return THING_LABEL_MAP.get(sensorType) + " (" + this.sensorId.getId() + ")";
158 public String toString() {
159 return String.format("%s/%s (associated: %d)", sensorId, sensorType, associatedSensors.size());