]> git.basschouten.com Git - openhab-addons.git/blob
f879eb00013f2cb2f4fbca27f798e8c2909879e6
[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;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.regex.Matcher;
20 import java.util.stream.Collectors;
21
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;
25
26 /**
27  * The {@link DS2438Configuration} is a helper class for the multisensor thing configuration
28  *
29  * @author Jan N. Klug - Initial contribution
30  */
31 @NonNullByDefault
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";
37
38     private final Map<SensorId, OwSensorType> associatedSensors = new HashMap<>();
39
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!");
44         }
45         OwPageBuffer pageBuffer = bridgeHandler.readPages(sensorId);
46
47         String sensorTypeId = pageBuffer.getPageString(3).substring(0, 2);
48         switch (sensorTypeId) {
49             case "19":
50                 vendor = "iButtonLink";
51                 sensorSubType = OwSensorType.MS_TH;
52                 break;
53             case "1A":
54                 vendor = "iButtonLink";
55                 sensorSubType = OwSensorType.MS_TV;
56                 break;
57             case "1B":
58                 vendor = "iButtonLink";
59                 sensorSubType = OwSensorType.MS_TL;
60                 break;
61             case "1C":
62                 vendor = "iButtonLink";
63                 sensorSubType = OwSensorType.MS_TC;
64                 break;
65             case "F1":
66             case "F3":
67                 vendor = "Elaborated Networks";
68                 sensorSubType = OwSensorType.MS_TH;
69                 break;
70             case "F2":
71                 vendor = "Elaborated Networks";
72                 sensorSubType = OwSensorType.MS_TH_S;
73                 break;
74             case "F4":
75                 vendor = "Elaborated Networks";
76                 sensorSubType = OwSensorType.MS_TV;
77                 break;
78             default:
79         }
80
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));
88
89                     switch (matcher.group(2).substring(0, 2)) {
90                         case "26" -> {
91                             DS2438Configuration associatedDs2438Config = new DS2438Configuration(bridgeHandler,
92                                     associatedSensorId);
93                             associatedSensors.put(associatedSensorId, associatedDs2438Config.getSensorSubType());
94                             associatedSensors.putAll(associatedDs2438Config.getAssociatedSensors());
95                         }
96                         case "28" -> associatedSensors.put(associatedSensorId, OwSensorType.DS18B20);
97                         case "3A" -> associatedSensors.put(associatedSensorId, OwSensorType.DS2413);
98                         default -> {
99                         }
100                     }
101                 }
102             }
103             prodDate = String.format("%d/%d", pageBuffer.getByte(5, 0),
104                     256 * pageBuffer.getByte(5, 1) + pageBuffer.getByte(5, 2));
105             hwRevision = String.valueOf(pageBuffer.getByte(5, 3));
106         }
107     }
108
109     public Map<SensorId, OwSensorType> getAssociatedSensors() {
110         return associatedSensors;
111     }
112
113     /**
114      * get a list of sensor ids associated with this sensor
115      *
116      * @return a list of the sensor ids (if found), empty list otherwise
117      */
118     public List<SensorId> getAssociatedSensorIds() {
119         return new ArrayList<>(associatedSensors.keySet());
120     }
121
122     /**
123      * get all secondary sensor ids of a given type
124      *
125      * @param sensorType filter for sensors
126      * @return a list of OwDiscoveryItems
127      */
128     public List<SensorId> getAssociatedSensorIds(OwSensorType sensorType) {
129         return associatedSensors.entrySet().stream().filter(s -> s.getValue() == sensorType).map(Map.Entry::getKey)
130                 .collect(Collectors.toList());
131     }
132
133     /**
134      * get a list of sensor types associated with this sensor
135      *
136      * @return a list of the sensor typess (if found), empty list otherwise
137      */
138     public List<OwSensorType> getAssociatedSensorTypes() {
139         return new ArrayList<>(associatedSensors.values());
140     }
141
142     /**
143      * get the number of associated sensors
144      *
145      * @return the number
146      */
147     public int getAssociatedSensorCount() {
148         return associatedSensors.size();
149     }
150
151     /**
152      * get hardware revision (available on some multisensors)
153      *
154      * @return hardware revision
155      */
156     public String getHardwareRevision() {
157         return hwRevision;
158     }
159
160     /**
161      * get production date (available on some multisensors)
162      *
163      * @return production date in ww/yy
164      */
165     public String getProductionDate() {
166         return prodDate;
167     }
168
169     /**
170      * get sensor type (without associated sensors)
171      *
172      * @return basic sensor type
173      */
174     public OwSensorType getSensorSubType() {
175         return sensorSubType;
176     }
177
178     /**
179      * get vendor name (if available)
180      *
181      * @return the vendor name
182      */
183     public String getVendor() {
184         return vendor;
185     }
186
187     /**
188      * determine multisensor type
189      *
190      * @param mainsensorType the type of the main sensor
191      * @param associatedSensorTypes a list of OwSensorTypes of all associated sensors
192      * @return the multisensor type (if known)
193      */
194     public static OwSensorType getMultisensorType(OwSensorType mainsensorType,
195             List<OwSensorType> associatedSensorTypes) {
196         OwSensorType multisensorType = OwSensorType.UNKNOWN;
197         switch (associatedSensorTypes.size()) {
198             case 0 -> multisensorType = mainsensorType;
199             case 1 -> {
200                 if (mainsensorType == OwSensorType.MS_TH_S && associatedSensorTypes.contains(OwSensorType.DS18B20)) {
201                     multisensorType = OwSensorType.BMS_S;
202                 } else if (mainsensorType == OwSensorType.MS_TH
203                         && associatedSensorTypes.contains(OwSensorType.DS18B20)) {
204                     multisensorType = OwSensorType.BMS;
205                 }
206             }
207             case 3 -> {
208                 if (mainsensorType == OwSensorType.MS_TH_S && associatedSensorTypes.contains(OwSensorType.MS_TV)
209                         && associatedSensorTypes.contains(OwSensorType.DS18B20)
210                         && associatedSensorTypes.contains(OwSensorType.DS2413)) {
211                     // two DS2438 (first THS, second TV), DS18B20, DS2413
212                     multisensorType = OwSensorType.AMS_S;
213                 } else if (mainsensorType == OwSensorType.MS_TH && associatedSensorTypes.contains(OwSensorType.MS_TV)
214                         && associatedSensorTypes.contains(OwSensorType.DS18B20)
215                         && associatedSensorTypes.contains(OwSensorType.DS2413)) {
216                     // two DS2438 (first TH, second TV), DS18B20, DS2413
217                     multisensorType = OwSensorType.AMS;
218                 }
219             }
220             default -> {
221             }
222         }
223
224         return multisensorType;
225     }
226 }