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;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
19 import java.util.regex.Matcher;
20 import java.util.stream.Collectors;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.onewire.internal.device.OwSensorType;
24 import org.openhab.binding.onewire.internal.handler.OwserverBridgeHandler;
27 * The {@link DS2438Configuration} is a helper class for the multisensor thing configuration
29 * @author Jan N. Klug - Initial contribution
32 public class DS2438Configuration {
33 private OwSensorType sensorSubType = OwSensorType.DS2438;
34 private String vendor = "Dallas/Maxim";
35 private String hwRevision = "0";
36 private String prodDate = "unknown";
38 private final Map<SensorId, OwSensorType> associatedSensors = new HashMap<>();
40 public DS2438Configuration(OwserverBridgeHandler bridgeHandler, SensorId sensorId) throws OwException {
41 OwSensorType sensorType = bridgeHandler.getType(sensorId);
42 if (sensorType != OwSensorType.DS2438) {
43 throw new OwException("sensor " + sensorId.getId() + " is not a DS2438!");
45 OwPageBuffer pageBuffer = bridgeHandler.readPages(sensorId);
47 String sensorTypeId = pageBuffer.getPageString(3).substring(0, 2);
48 switch (sensorTypeId) {
50 vendor = "iButtonLink";
51 sensorSubType = OwSensorType.MS_TH;
54 vendor = "iButtonLink";
55 sensorSubType = OwSensorType.MS_TV;
58 vendor = "iButtonLink";
59 sensorSubType = OwSensorType.MS_TL;
62 vendor = "iButtonLink";
63 sensorSubType = OwSensorType.MS_TC;
67 vendor = "Elaborated Networks";
68 sensorSubType = OwSensorType.MS_TH;
71 vendor = "Elaborated Networks";
72 sensorSubType = OwSensorType.MS_TH_S;
75 vendor = "Elaborated Networks";
76 sensorSubType = OwSensorType.MS_TV;
81 if (sensorSubType == OwSensorType.MS_TH || sensorSubType == OwSensorType.MS_TH_S
82 || sensorSubType == OwSensorType.MS_TV) {
83 for (int i = 4; i < 7; i++) {
84 String str = new StringBuilder(pageBuffer.getPageString(i)).insert(2, ".").delete(15, 17).toString();
85 Matcher matcher = SensorId.SENSOR_ID_PATTERN.matcher(str);
86 if (matcher.matches()) {
87 SensorId associatedSensorId = new SensorId(sensorId.getPath() + matcher.group(2));
89 switch (matcher.group(2).substring(0, 2)) {
91 DS2438Configuration associatedDs2438Config = new DS2438Configuration(bridgeHandler,
93 associatedSensors.put(associatedSensorId, associatedDs2438Config.getSensorSubType());
94 associatedSensors.putAll(associatedDs2438Config.getAssociatedSensors());
97 associatedSensors.put(associatedSensorId, OwSensorType.DS18B20);
100 associatedSensors.put(associatedSensorId, OwSensorType.DS2413);
106 prodDate = String.format("%d/%d", pageBuffer.getByte(5, 0),
107 256 * pageBuffer.getByte(5, 1) + pageBuffer.getByte(5, 2));
108 hwRevision = String.valueOf(pageBuffer.getByte(5, 3));
112 public Map<SensorId, OwSensorType> getAssociatedSensors() {
113 return associatedSensors;
117 * get a list of sensor ids associated with this sensor
119 * @return a list of the sensor ids (if found), empty list otherwise
121 public List<SensorId> getAssociatedSensorIds() {
122 return new ArrayList<>(associatedSensors.keySet());
126 * get all secondary sensor ids of a given type
128 * @param sensorType filter for sensors
129 * @return a list of OwDiscoveryItems
131 public List<SensorId> getAssociatedSensorIds(OwSensorType sensorType) {
132 return associatedSensors.entrySet().stream().filter(s -> s.getValue() == sensorType).map(s -> s.getKey())
133 .collect(Collectors.toList());
137 * get a list of sensor types associated with this sensor
139 * @return a list of the sensor typess (if found), empty list otherwise
141 public List<OwSensorType> getAssociatedSensorTypes() {
142 return new ArrayList<>(associatedSensors.values());
146 * get the number of associated sensors
150 public int getAssociatedSensorCount() {
151 return associatedSensors.size();
155 * get hardware revision (available on some multisensors)
157 * @return hardware revision
159 public String getHardwareRevision() {
164 * get production date (available on some multisensors)
166 * @return production date in ww/yy
168 public String getProductionDate() {
173 * get sensor type (without associated sensors)
175 * @return basic sensor type
177 public OwSensorType getSensorSubType() {
178 return sensorSubType;
182 * get vendor name (if available)
184 * @return the vendor name
186 public String getVendor() {
191 * determine multisensor type
193 * @param mainsensorType the type of the main sensor
194 * @param associatedSensorTypes a list of OwSensorTypes of all associated sensors
195 * @return the multisensor type (if known)
197 public static OwSensorType getMultisensorType(OwSensorType mainsensorType,
198 List<OwSensorType> associatedSensorTypes) {
199 OwSensorType multisensorType = OwSensorType.UNKNOWN;
200 switch (associatedSensorTypes.size()) {
202 multisensorType = mainsensorType;
205 if (mainsensorType == OwSensorType.MS_TH_S && associatedSensorTypes.contains(OwSensorType.DS18B20)) {
206 multisensorType = OwSensorType.BMS_S;
207 } else if (mainsensorType == OwSensorType.MS_TH
208 && associatedSensorTypes.contains(OwSensorType.DS18B20)) {
209 multisensorType = OwSensorType.BMS;
213 if (mainsensorType == OwSensorType.MS_TH_S && associatedSensorTypes.contains(OwSensorType.MS_TV)
214 && associatedSensorTypes.contains(OwSensorType.DS18B20)
215 && associatedSensorTypes.contains(OwSensorType.DS2413)) {
216 // two DS2438 (first THS, second TV), DS18B20, DS2413
217 multisensorType = OwSensorType.AMS_S;
218 } else if (mainsensorType == OwSensorType.MS_TH && associatedSensorTypes.contains(OwSensorType.MS_TV)
219 && associatedSensorTypes.contains(OwSensorType.DS18B20)
220 && associatedSensorTypes.contains(OwSensorType.DS2413)) {
221 // two DS2438 (first TH, second TV), DS18B20, DS2413
222 multisensorType = OwSensorType.AMS;
228 return multisensorType;