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.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 For more information see https://de.wikipedia.org/wiki/OBIS-Kennzahlen
27 * @author Matthias Steigenberger - Initial contribution
31 public class ObisCode {
33 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}))?";
35 private static Pattern obisPattern = Pattern.compile(OBIS_PATTERN);
40 private ObisCode(@Nullable Byte a, @Nullable Byte b, Byte c, Byte d, Byte e, @Nullable Byte f) {
50 * Gets an {@link ObisCode} from a String. It must follow the pattern {@value #OBIS_PATTERN}
52 * @param obis The obis as String.
53 * @return The new Obis code. Can not be null.
54 * @throws IllegalArgumentException If the <code>obis</code> has not the right format.
56 public static ObisCode from(String obis) throws IllegalArgumentException {
58 Matcher matcher = obisPattern.matcher(obis);
60 String a = matcher.group("A");
61 String b = matcher.group("B");
62 String c = matcher.group("C");
63 String d = matcher.group("D");
64 String e = matcher.group("E");
65 String f = matcher.group("F");
66 return new ObisCode(a != null && !a.isEmpty() ? (byte) (0xFF & Integer.valueOf(a)) : null,
67 b != null && !b.isEmpty() ? (byte) (0xFF & Integer.valueOf(b)) : null,
68 (byte) (0xFF & Integer.valueOf(c)), (byte) (0xFF & Integer.valueOf(d)),
69 (byte) (0xFF & Integer.valueOf(e)),
70 f != null && !f.isEmpty() ? (byte) (0xFF & Integer.valueOf(f)) : null);
72 throw new IllegalArgumentException(obis + " is not correctly formated.");
73 } catch (Exception e) {
74 throw new IllegalArgumentException(obis + " is not correctly formated.", e);
79 * Gets the OBIS as a String.
81 * @return the obis as string.
83 public String asDecimalString() {
84 try (Formatter format = new Formatter()) {
85 format.format(SmartMeterBindingConstants.OBIS_FORMAT, a != null ? a & 0xFF : 0, b != null ? b & 0xFF : 0,
86 c & 0xFF, d & 0xFF, e & 0xFF, f != null ? f & 0xFF : 0);
87 return format.toString();
91 public @Nullable Byte getAGroup() {
95 public @Nullable Byte getBGroup() {
99 public @Nullable Byte getCGroup() {
103 public @Nullable Byte getDGroup() {
107 public @Nullable Byte getEGroup() {
111 public @Nullable Byte getFGroup() {
116 public String toString() {
117 return asDecimalString();
120 public boolean matches(@Nullable Byte a, @Nullable Byte b, Byte c, Byte d, Byte e, @Nullable Byte f) {
121 return (this.a == null || a == null || this.a.equals(a)) && (this.b == null || b == null || this.b.equals(b))
122 && this.c.equals(c) && this.d.equals(d) && this.e.equals(e)
123 && (this.f == null || f == null || this.f.equals(f));
126 public boolean matches(Byte c, Byte d, Byte e) {
127 return matches(null, null, c, d, e, null);