]> git.basschouten.com Git - openhab-addons.git/blob
ec2e39a03cd99d159f1ab93c580c8765178a9326
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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(String fullPath) {
47         Matcher matcher = SENSOR_ID_PATTERN.matcher(fullPath);
48         if (matcher.matches() && matcher.groupCount() == 2) {
49             path = matcher.group(1) == null ? "" : matcher.group(1);
50             sensorId = matcher.group(2);
51             this.fullPath = "/" + path + sensorId;
52         } else {
53             throw new IllegalArgumentException();
54         }
55     }
56
57     /**
58      * get the full path to the sensor
59      *
60      * @return full path (including hub parts, separated by "/" characters)
61      */
62     public String getFullPath() {
63         return fullPath;
64     }
65
66     /**
67      * get the sensor id
68      *
69      * @return sensor id without leading "/" character
70      */
71     public String getId() {
72         return sensorId;
73     }
74
75     /**
76      * get the path of this sensorId
77      *
78      * @return path without sensor id (including hub parts, separated by "/" characters)
79      */
80     public String getPath() {
81         return path;
82     }
83
84     /**
85      * get family id (first to characters of sensor id)
86      *
87      * @return the family id
88      */
89     public String getFamilyId() {
90         return sensorId.substring(0, 2);
91     }
92
93     @Override
94     public String toString() {
95         return fullPath;
96     }
97
98     @Override
99     public int hashCode() {
100         return this.fullPath.hashCode();
101     }
102
103     @Override
104     public boolean equals(@Nullable Object o) {
105         if (o == this) {
106             return true;
107         }
108
109         if (!(o instanceof SensorId)) {
110             return false;
111         }
112
113         return ((SensorId) o).fullPath.equals(fullPath);
114     }
115 }