]> git.basschouten.com Git - openhab-addons.git/blob
c35e0f0498fce6d6bbb695ed80ca7b6b1ab7db9c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.smartmeter.internal;
14
15 import java.util.Formatter;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.smartmeter.SmartMeterBindingConstants;
22
23 /**
24  * Represents an OBIS code.
25  *
26  * @see <a href="https://de.wikipedia.org/wiki/OBIS-Kennzahlen">https://de.wikipedia.org/wiki/OBIS-Kennzahlen</a> for
27  *      more information
28  * @author Matthias Steigenberger - Initial contribution
29  *
30  */
31 @NonNullByDefault
32 public class ObisCode {
33
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}))?";
35
36     private static Pattern obisPattern = Pattern.compile(OBIS_PATTERN);
37     @Nullable
38     private Byte a, b, f;
39     private Byte c, d, e;
40
41     private ObisCode(@Nullable Byte a, @Nullable Byte b, Byte c, Byte d, Byte e, @Nullable Byte f) {
42         this.a = a;
43         this.b = b;
44         this.c = c;
45         this.d = d;
46         this.e = e;
47         this.f = f;
48     }
49
50     /**
51      * Gets an {@link ObisCode} from a String. It must follow the pattern {@value #OBIS_PATTERN}
52      *
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.
56      */
57     public static ObisCode from(String obis) throws IllegalArgumentException {
58         try {
59             Matcher matcher = obisPattern.matcher(obis);
60             if (matcher.find()) {
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);
72             }
73             throw new IllegalArgumentException(obis + " is not correctly formated.");
74         } catch (Exception e) {
75             throw new IllegalArgumentException(obis + " is not correctly formated.", e);
76         }
77     }
78
79     /**
80      * Gets the OBIS as a String.
81      * 
82      * @return the obis as string.
83      */
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();
89         }
90     }
91
92     public @Nullable Byte getAGroup() {
93         return a;
94     }
95
96     public @Nullable Byte getBGroup() {
97         return b;
98     }
99
100     public @Nullable Byte getCGroup() {
101         return c;
102     }
103
104     public @Nullable Byte getDGroup() {
105         return d;
106     }
107
108     public @Nullable Byte getEGroup() {
109         return e;
110     }
111
112     public @Nullable Byte getFGroup() {
113         return f;
114     }
115
116     @Override
117     public String toString() {
118         return asDecimalString();
119     }
120
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));
125     }
126
127     public boolean matches(Byte c, Byte d, Byte e) {
128         return matches(null, null, c, d, e, null);
129     }
130 }