2 * Copyright (c) 2010-2020 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(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;
53 throw new IllegalArgumentException();
58 * get the full path to the sensor
60 * @return full path (including hub parts, separated by "/" characters)
62 public String getFullPath() {
69 * @return sensor id without leading "/" character
71 public String getId() {
76 * get the path of this sensorId
78 * @return path without sensor id (including hub parts, separated by "/" characters)
80 public String getPath() {
85 * get family id (first to characters of sensor id)
87 * @return the family id
89 public String getFamilyId() {
90 return sensorId.substring(0, 2);
94 public String toString() {
99 public int hashCode() {
100 return this.fullPath.hashCode();
104 public boolean equals(@Nullable Object o) {
109 if (!(o instanceof SensorId)) {
113 return ((SensorId) o).fullPath.equals(fullPath);