]> git.basschouten.com Git - openhab-addons.git/blob
008905e0687f8643fd9e0ef106dd283b8d74b748
[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                             break;
96                         case "28":
97                             associatedSensors.put(associatedSensorId, OwSensorType.DS18B20);
98                             break;
99                         case "3A":
100                             associatedSensors.put(associatedSensorId, OwSensorType.DS2413);
101                             break;
102                         default:
103                     }
104                 }
105             }
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));
109         }
110     }
111
112     public Map<SensorId, OwSensorType> getAssociatedSensors() {
113         return associatedSensors;
114     }
115
116     /**
117      * get a list of sensor ids associated with this sensor
118      *
119      * @return a list of the sensor ids (if found), empty list otherwise
120      */
121     public List<SensorId> getAssociatedSensorIds() {
122         return new ArrayList<>(associatedSensors.keySet());
123     }
124
125     /**
126      * get all secondary sensor ids of a given type
127      *
128      * @param sensorType filter for sensors
129      * @return a list of OwDiscoveryItems
130      */
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());
134     }
135
136     /**
137      * get a list of sensor types associated with this sensor
138      *
139      * @return a list of the sensor typess (if found), empty list otherwise
140      */
141     public List<OwSensorType> getAssociatedSensorTypes() {
142         return new ArrayList<>(associatedSensors.values());
143     }
144
145     /**
146      * get the number of associated sensors
147      *
148      * @return the number
149      */
150     public int getAssociatedSensorCount() {
151         return associatedSensors.size();
152     }
153
154     /**
155      * get hardware revision (available on some multisensors)
156      *
157      * @return hardware revision
158      */
159     public String getHardwareRevision() {
160         return hwRevision;
161     }
162
163     /**
164      * get production date (available on some multisensors)
165      *
166      * @return production date in ww/yy
167      */
168     public String getProductionDate() {
169         return prodDate;
170     }
171
172     /**
173      * get sensor type (without associated sensors)
174      *
175      * @return basic sensor type
176      */
177     public OwSensorType getSensorSubType() {
178         return sensorSubType;
179     }
180
181     /**
182      * get vendor name (if available)
183      *
184      * @return the vendor name
185      */
186     public String getVendor() {
187         return vendor;
188     }
189
190     /**
191      * determine multisensor type
192      *
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)
196      */
197     public static OwSensorType getMultisensorType(OwSensorType mainsensorType,
198             List<OwSensorType> associatedSensorTypes) {
199         OwSensorType multisensorType = OwSensorType.UNKNOWN;
200         switch (associatedSensorTypes.size()) {
201             case 0:
202                 multisensorType = mainsensorType;
203                 break;
204             case 1:
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;
210                 }
211                 break;
212             case 3:
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;
223                 }
224                 break;
225             default:
226         }
227
228         return multisensorType;
229     }
230 }