2 * Copyright (c) 2010-2024 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.smartmeter.internal;
15 import java.util.Formatter;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.smartmeter.SmartMeterBindingConstants;
24 * Represents an OBIS code.
26 * @see <a href="https://de.wikipedia.org/wiki/OBIS-Kennzahlen">https://de.wikipedia.org/wiki/OBIS-Kennzahlen</a> for
28 * @author Matthias Steigenberger - Initial contribution
32 public class ObisCode {
34 public static final String OBIS_PATTERN = "((?<A>[0-9]{1,3})-(?<B>[0-9]{1,3}):)?(?<C>[0-9]{1,3}).(?<D>[0-9]{1,3}).(?<E>[0-9]{1,3})(\\*(?<F>[0-9][0-9]{1,3}))?";
36 private static Pattern obisPattern = Pattern.compile(OBIS_PATTERN);
41 private ObisCode(@Nullable Byte a, @Nullable Byte b, Byte c, Byte d, Byte e, @Nullable Byte f) {
51 * Gets an {@link ObisCode} from a String. It must follow the pattern {@value #OBIS_PATTERN}
53 * @param obis The obis as String.
54 * @return The new Obis code. Can not be null.
55 * @throws IllegalArgumentException If the <code>obis</code> has not the right format.
57 public static ObisCode from(String obis) throws IllegalArgumentException {
59 Matcher matcher = obisPattern.matcher(obis);
61 String a = matcher.group("A");
62 String b = matcher.group("B");
63 String c = matcher.group("C");
64 String d = matcher.group("D");
65 String e = matcher.group("E");
66 String f = matcher.group("F");
67 return new ObisCode(a != null && !a.isEmpty() ? (byte) (0xFF & Integer.valueOf(a)) : null,
68 b != null && !b.isEmpty() ? (byte) (0xFF & Integer.valueOf(b)) : null,
69 (byte) (0xFF & Integer.valueOf(c)), (byte) (0xFF & Integer.valueOf(d)),
70 (byte) (0xFF & Integer.valueOf(e)),
71 f != null && !f.isEmpty() ? (byte) (0xFF & Integer.valueOf(f)) : null);
73 throw new IllegalArgumentException(obis + " is not correctly formated.");
74 } catch (Exception e) {
75 throw new IllegalArgumentException(obis + " is not correctly formated.", e);
80 * Gets the OBIS as a String.
82 * @return the obis as string.
84 public String asDecimalString() {
85 try (Formatter format = new Formatter()) {
86 format.format(SmartMeterBindingConstants.OBIS_FORMAT, a != null ? a & 0xFF : 0, b != null ? b & 0xFF : 0,
87 c & 0xFF, d & 0xFF, e & 0xFF, f != null ? f & 0xFF : 0);
88 return format.toString();
92 public @Nullable Byte getAGroup() {
96 public @Nullable Byte getBGroup() {
100 public @Nullable Byte getCGroup() {
104 public @Nullable Byte getDGroup() {
108 public @Nullable Byte getEGroup() {
112 public @Nullable Byte getFGroup() {
117 public String toString() {
118 return asDecimalString();
121 public boolean matches(@Nullable Byte a, @Nullable Byte b, Byte c, Byte d, Byte e, @Nullable Byte f) {
122 return (this.a == null || a == null || this.a.equals(a)) && (this.b == null || b == null || this.b.equals(b))
123 && this.c.equals(c) && this.d.equals(d) && this.e.equals(e)
124 && (this.f == null || f == null || this.f.equals(f));
127 public boolean matches(Byte c, Byte d, Byte e) {
128 return matches(null, null, c, d, e, null);