2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.onewire.internal;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
22 * The {@link SensorId} provides a sensorID for the Onewire bus.
24 * @author Jan N. Klug - Initial contribution
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})$");
31 private final String sensorId;
32 private final String path;
33 private final String fullPath;
36 * construct a new SensorId object
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
46 public SensorId(@Nullable String fullPath) {
47 if (fullPath == null) {
48 throw new IllegalArgumentException();
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;
56 throw new IllegalArgumentException();
61 * get the full path to the sensor
63 * @return full path (including hub parts, separated by "/" characters)
65 public String getFullPath() {
72 * @return sensor id without leading "/" character
74 public String getId() {
79 * get the path of this sensorId
81 * @return path without sensor id (including hub parts, separated by "/" characters)
83 public String getPath() {
88 * get family id (first to characters of sensor id)
90 * @return the family id
92 public String getFamilyId() {
93 return sensorId.substring(0, 2);
97 public String toString() {
102 public int hashCode() {
103 return this.fullPath.hashCode();
107 public boolean equals(@Nullable Object o) {
112 if (!(o instanceof SensorId)) {
116 return ((SensorId) o).fullPath.equals(fullPath);