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 OwPageBuffer pages = new OwPageBuffer();
49 private ThingTypeUID thingTypeUID = new ThingTypeUID(BINDING_ID, "");
51 private final Map<SensorId, OwSensorType> associatedSensors = new HashMap<>();
53 public OwDiscoveryItem(OwserverBridgeHandler bridgeHandler, SensorId sensorId) throws OwException {
54 this.sensorId = sensorId;
55 sensorType = bridgeHandler.getType(sensorId);
58 pages = bridgeHandler.readPages(sensorId);
59 DS2438Configuration config = new DS2438Configuration(bridgeHandler, sensorId);
60 associatedSensors.putAll(config.getAssociatedSensors());
61 logger.trace("found associated sensors: {}", associatedSensors);
62 vendor = config.getVendor();
63 sensorType = config.getSensorSubType();
66 vendor = "Embedded Data Systems";
67 pages = bridgeHandler.readPages(sensorId);
69 try { // determine subsensorType
70 sensorType = OwSensorType.valueOf(new String(pages.getPage(0), 0, 7, StandardCharsets.US_ASCII));
71 } catch (IllegalArgumentException e) {
72 sensorType = OwSensorType.UNKNOWN;
84 public OwSensorType getSensorType() {
93 public SensorId getSensorId() {
98 * normalized sensor id (for naming the discovery result)
100 * @return sensor id in format familyId_xxxxxxxxxx
102 public String getNormalizedSensorId() {
103 return sensorId.getId().replace(".", "_");
107 * get vendor name (if available)
109 * @return vendor name
111 public String getVendor() {
116 * get this sensors ThingTypeUID
118 * @return ThingTypeUID if mapping successful
120 public ThingTypeUID getThingTypeUID() throws OwException {
121 ThingTypeUID thingTypeUID = THING_TYPE_MAP.get(sensorType);
122 if (thingTypeUID != null) {
125 throw new OwException(sensorType + " cannot be mapped to thing type");
130 * get a list of all sensors associated to this sensor
132 * @return list of strings
134 public List<SensorId> getAssociatedSensorIds() {
135 return new ArrayList<>(associatedSensors.keySet());
139 * determine this sensors type
141 public void checkSensorType() {
142 logger.debug("checkSensorType: {} with {}", this, associatedSensors);
144 switch (sensorType) {
147 sensorType = DS2438Configuration.getMultisensorType(sensorType,
148 new ArrayList<>(associatedSensors.values()));
155 * get Label "<thingtype> (<id>)"
157 * @return the thing label
159 public String getLabel() {
160 return THING_LABEL_MAP.get(sensorType) + " (" + this.sensorId.getId() + ")";
164 public String toString() {
165 return String.format("%s/%s (associated: %d)", sensorId, sensorType, associatedSensors.size());