]> git.basschouten.com Git - openhab-addons.git/blob
bb2c8196e8956f7a0b8fad28b528ef350b528e2f
[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.discovery;
14
15 import static org.openhab.binding.onewire.internal.OwBindingConstants.*;
16
17 import java.nio.charset.StandardCharsets;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
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;
33
34 /**
35  * The {@link OwDiscoveryItem} class defines a discovery item for OneWire devices
36  *
37  * @author Jan N. Klug - Initial contribution
38  */
39 @NonNullByDefault
40 public class OwDiscoveryItem {
41     private final Logger logger = LoggerFactory.getLogger(OwDiscoveryItem.class);
42
43     private final SensorId sensorId;
44     private OwSensorType sensorType = OwSensorType.UNKNOWN;
45     private String vendor = "Dallas/Maxim";
46
47     private final Map<SensorId, OwSensorType> associatedSensors = new HashMap<>();
48
49     public OwDiscoveryItem(OwserverBridgeHandler bridgeHandler, SensorId sensorId) throws OwException {
50         this.sensorId = sensorId;
51         sensorType = bridgeHandler.getType(sensorId);
52         switch (sensorType) {
53             case DS2438 -> {
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();
60             }
61             case EDS -> {
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;
68                 }
69             }
70             default -> {
71             }
72         }
73     }
74
75     /**
76      * get sensor type
77      *
78      * @return sensor type
79      */
80     public OwSensorType getSensorType() {
81         return sensorType;
82     }
83
84     /**
85      * get this sensor id
86      *
87      * @return sensor id
88      */
89     public SensorId getSensorId() {
90         return sensorId;
91     }
92
93     /**
94      * normalized sensor id (for naming the discovery result)
95      *
96      * @return sensor id in format familyId_xxxxxxxxxx
97      */
98     public String getNormalizedSensorId() {
99         return sensorId.getId().replace(".", "_");
100     }
101
102     /**
103      * get vendor name (if available)
104      *
105      * @return vendor name
106      */
107     public String getVendor() {
108         return vendor;
109     }
110
111     /**
112      * get this sensors ThingTypeUID
113      *
114      * @return ThingTypeUID if mapping successful
115      */
116     public ThingTypeUID getThingTypeUID() throws OwException {
117         ThingTypeUID thingTypeUID = THING_TYPE_MAP.get(sensorType);
118         if (thingTypeUID != null) {
119             return thingTypeUID;
120         } else {
121             throw new OwException(sensorType + " cannot be mapped to thing type");
122         }
123     }
124
125     /**
126      * get a list of all sensors associated to this sensor
127      *
128      * @return list of strings
129      */
130     public List<SensorId> getAssociatedSensorIds() {
131         return new ArrayList<>(associatedSensors.keySet());
132     }
133
134     /**
135      * determine this sensors type
136      */
137     public void checkSensorType() {
138         logger.debug("checkSensorType: {} with {}", this, associatedSensors);
139
140         switch (sensorType) {
141             case MS_TH, MS_TH_S -> sensorType = DS2438Configuration.getMultisensorType(sensorType,
142                     new ArrayList<>(associatedSensors.values()));
143             default -> {
144             }
145         }
146     }
147
148     /**
149      * get Label "<thingtype> (<id>)"
150      *
151      * @return the thing label
152      */
153     public String getLabel() {
154         return THING_LABEL_MAP.get(sensorType) + " (" + this.sensorId.getId() + ")";
155     }
156
157     @Override
158     public String toString() {
159         return String.format("%s/%s (associated: %d)", sensorId, sensorType, associatedSensors.size());
160     }
161 }