]> git.basschouten.com Git - openhab-addons.git/blob
922c71fbd6d0d31424b02077bd23360ae568c50d
[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.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 /**
22  * The {@link SensorId} provides a sensorID for the Onewire bus.
23  *
24  * @author Jan N. Klug - Initial contribution
25  */
26 @NonNullByDefault
27 public class SensorId {
28     public static final Pattern SENSOR_ID_PATTERN = Pattern
29             .compile("^\\/?((?:(?:1F\\.[0-9A-Fa-f]{12})\\/(?:main|aux)\\/)+)?([0-9A-Fa-f]{2}\\.[0-9A-Fa-f]{12})$");
30
31     private final String sensorId;
32     private final String path;
33     private final String fullPath;
34
35     /**
36      * construct a new SensorId object
37      *
38      * allowed formats:
39      * - "28.0123456789ab"
40      * - "1F.1234566890ab/main/28.0123456789ab"
41      * - "1F.1234566890ab/aux/28.0123456789ab"
42      * - leading "/" characters are allowed but not required
43      * - characters are case-insensitive
44      * - hubs ("1F.xxxxxxxxxxxx/aux/") may be repeated
45      */
46     public SensorId(@Nullable String fullPath) {
47         if (fullPath == null) {
48             throw new IllegalArgumentException();
49         }
50         Matcher matcher = SENSOR_ID_PATTERN.matcher(fullPath);
51         if (matcher.matches() && matcher.groupCount() == 2) {
52             path = matcher.group(1) == null ? "" : matcher.group(1);
53             sensorId = matcher.group(2);
54             this.fullPath = "/" + path + sensorId;
55         } else {
56             throw new IllegalArgumentException();
57         }
58     }
59
60     /**
61      * get the full path to the sensor
62      *
63      * @return full path (including hub parts, separated by "/" characters)
64      */
65     public String getFullPath() {
66         return fullPath;
67     }
68
69     /**
70      * get the sensor id
71      *
72      * @return sensor id without leading "/" character
73      */
74     public String getId() {
75         return sensorId;
76     }
77
78     /**
79      * get the path of this sensorId
80      *
81      * @return path without sensor id (including hub parts, separated by "/" characters)
82      */
83     public String getPath() {
84         return path;
85     }
86
87     /**
88      * get family id (first to characters of sensor id)
89      *
90      * @return the family id
91      */
92     public String getFamilyId() {
93         return sensorId.substring(0, 2);
94     }
95
96     @Override
97     public String toString() {
98         return fullPath;
99     }
100
101     @Override
102     public int hashCode() {
103         return this.fullPath.hashCode();
104     }
105
106     @Override
107     public boolean equals(@Nullable Object o) {
108         if (o == this) {
109             return true;
110         }
111
112         if (!(o instanceof SensorId)) {
113             return false;
114         }
115
116         return ((SensorId) o).fullPath.equals(fullPath);
117     }
118 }