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