]> git.basschouten.com Git - openhab-addons.git/blob
d87192c7fb5efdc8f805d73fa61785cf99a55d0a
[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 OwPageBuffer pages = new OwPageBuffer();
48
49     private ThingTypeUID thingTypeUID = new ThingTypeUID(BINDING_ID, "");
50
51     private final Map<SensorId, OwSensorType> associatedSensors = new HashMap<>();
52
53     public OwDiscoveryItem(OwserverBridgeHandler bridgeHandler, SensorId sensorId) throws OwException {
54         this.sensorId = sensorId;
55         sensorType = bridgeHandler.getType(sensorId);
56         switch (sensorType) {
57             case DS2438:
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();
64                 break;
65             case EDS:
66                 vendor = "Embedded Data Systems";
67                 pages = bridgeHandler.readPages(sensorId);
68
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;
73                 }
74                 break;
75             default:
76         }
77     }
78
79     /**
80      * get sensor type
81      *
82      * @return sensor type
83      */
84     public OwSensorType getSensorType() {
85         return sensorType;
86     }
87
88     /**
89      * get this sensor id
90      *
91      * @return sensor id
92      */
93     public SensorId getSensorId() {
94         return sensorId;
95     }
96
97     /**
98      * normalized sensor id (for naming the discovery result)
99      *
100      * @return sensor id in format familyId_xxxxxxxxxx
101      */
102     public String getNormalizedSensorId() {
103         return sensorId.getId().replace(".", "_");
104     }
105
106     /**
107      * get vendor name (if available)
108      *
109      * @return vendor name
110      */
111     public String getVendor() {
112         return vendor;
113     }
114
115     /**
116      * get this sensors ThingTypeUID
117      *
118      * @return ThingTypeUID if mapping successful
119      */
120     public ThingTypeUID getThingTypeUID() throws OwException {
121         ThingTypeUID thingTypeUID = THING_TYPE_MAP.get(sensorType);
122         if (thingTypeUID != null) {
123             return thingTypeUID;
124         } else {
125             throw new OwException(sensorType + " cannot be mapped to thing type");
126         }
127     }
128
129     /**
130      * get a list of all sensors associated to this sensor
131      *
132      * @return list of strings
133      */
134     public List<SensorId> getAssociatedSensorIds() {
135         return new ArrayList<>(associatedSensors.keySet());
136     }
137
138     /**
139      * determine this sensors type
140      */
141     public void checkSensorType() {
142         logger.debug("checkSensorType: {} with {}", this, associatedSensors);
143
144         switch (sensorType) {
145             case MS_TH:
146             case MS_TH_S:
147                 sensorType = DS2438Configuration.getMultisensorType(sensorType,
148                         new ArrayList<>(associatedSensors.values()));
149                 break;
150             default:
151         }
152     }
153
154     /**
155      * get Label "<thingtype> (<id>)"
156      *
157      * @return the thing label
158      */
159     public String getLabel() {
160         return THING_LABEL_MAP.get(sensorType) + " (" + this.sensorId.getId() + ")";
161     }
162
163     @Override
164     public String toString() {
165         return String.format("%s/%s (associated: %d)", sensorId, sensorType, associatedSensors.size());
166     }
167 }