]> git.basschouten.com Git - openhab-addons.git/blob
bff9c770366f9862777b09b426fbbc4f82c7297f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.dsmr.internal.device.cosem;
14
15 import java.text.ParseException;
16 import java.util.Objects;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22
23 /**
24  * Class representing an OBISIdentifier
25  *
26  * @author M. Volaart - Initial contribution
27  * @author Hilbrand Bouwkamp - Fix bug in regex pattern.
28  */
29 @NonNullByDefault
30 public class OBISIdentifier {
31     /**
32      * String representing a-b:c.d.e.f OBIS ID
33      */
34     private static final String OBISID_REGEX = "((\\d+)\\-)?((\\d+):)?((\\d+)\\.)(\\d+)(\\.(\\d+))?(.(\\d+))?";
35
36     /**
37      * OBIS ID pattern
38      */
39     private static final Pattern OBIS_ID_PATTERN = Pattern.compile(OBISID_REGEX);
40
41     /* the six individual group values of the OBIS ID */
42     private int groupA;
43     private @Nullable Integer groupB;
44     private int groupC;
45     private int groupD;
46     private @Nullable Integer groupE;
47     private @Nullable Integer groupF;
48
49     private boolean conflict;
50
51     /**
52      * Constructs a new OBIS Identifier (A-B:C.D.E.F)
53      *
54      * @param groupA A value
55      * @param groupB B value
56      * @param groupC C value
57      * @param groupD D value
58      * @param groupE E value
59      * @param groupF F value
60      */
61     public OBISIdentifier(int groupA, @Nullable Integer groupB, int groupC, int groupD, @Nullable Integer groupE,
62             @Nullable Integer groupF) {
63         this(groupA, groupB, groupC, groupD, groupE, groupF, false);
64     }
65
66     /**
67      * Constructs a new OBIS Identifier (A-B:C.D.E.F)
68      *
69      * @param groupA A value
70      * @param groupB B value
71      * @param groupC C value
72      * @param groupD D value
73      * @param groupE E value
74      * @param groupF F value
75      * @param conflict if true indicates this OBIS Identifier is used for different types of data.
76      */
77     public OBISIdentifier(int groupA, @Nullable Integer groupB, int groupC, int groupD, @Nullable Integer groupE,
78             @Nullable Integer groupF, boolean conflict) {
79         this.groupA = groupA;
80         this.groupB = groupB;
81         this.groupC = groupC;
82         this.groupD = groupD;
83         this.groupE = groupE;
84         this.groupF = groupF;
85         this.conflict = conflict;
86     }
87
88     /**
89      * Creates a new {@link OBISIdentifier} of the specified String
90      *
91      * @param obisIDString the OBIS String ID
92      * @throws ParseException if obisIDString is not a valid OBIS Identifier
93      */
94     public OBISIdentifier(String obisIDString) throws ParseException {
95         Matcher m = OBIS_ID_PATTERN.matcher(obisIDString);
96
97         if (m.matches()) {
98             // Optional value A
99             if (m.group(2) != null) {
100                 this.groupA = Integer.parseInt(m.group(2));
101             }
102
103             // Optional value B
104             if (m.group(4) != null) {
105                 this.groupB = Integer.valueOf(m.group(4));
106             }
107
108             // Required value C & D
109             this.groupC = Integer.parseInt(m.group(6));
110             this.groupD = Integer.parseInt(m.group(7));
111
112             // Optional value E
113             if (m.group(9) != null) {
114                 this.groupE = Integer.valueOf(m.group(9));
115             }
116
117             // Optional value F
118             if (m.group(11) != null) {
119                 this.groupF = Integer.valueOf(m.group(11));
120             }
121         } else {
122             throw new ParseException("Invalid OBIS identifier:" + obisIDString, 0);
123         }
124     }
125
126     public boolean isConflict() {
127         return conflict;
128     }
129
130     /**
131      * @return the groupA
132      */
133     public int getGroupA() {
134         return groupA;
135     }
136
137     /**
138      * @return the groupB
139      */
140     public @Nullable Integer getGroupB() {
141         return groupB;
142     }
143
144     /**
145      * @return the groupC
146      */
147     public int getGroupC() {
148         return groupC;
149     }
150
151     /**
152      * @return the groupD
153      */
154     public int getGroupD() {
155         return groupD;
156     }
157
158     /**
159      * @return the groupE
160      */
161     public @Nullable Integer getGroupE() {
162         return groupE;
163     }
164
165     /**
166      * @return the groupF
167      */
168     public @Nullable Integer getGroupF() {
169         return groupF;
170     }
171
172     @Override
173     public String toString() {
174         return groupA + "-" + (groupB == null ? "" : (groupB + ":")) + groupC + "." + groupD
175                 + (groupE == null ? "" : ("." + groupE)) + (groupF == null ? "" : ("*" + groupF));
176     }
177
178     /**
179      * Returns whether or not both {@link OBISIdentifier} are exact equal (all identifiers match).
180      *
181      * If wild card matching is needed (since some fields are null in case of a wildcard) use
182      * {@link #equalsWildCard(OBISIdentifier)} instead
183      *
184      * @return true if both OBISIdentifiers match, false otherwise
185      */
186     @Override
187     public boolean equals(@Nullable Object other) {
188         OBISIdentifier o;
189         if (other != null && other instanceof OBISIdentifier) {
190             o = (OBISIdentifier) other;
191         } else {
192             return false;
193         }
194         boolean result = true;
195
196         result &= groupA == o.groupA;
197         if (groupB != null && o.groupB != null) {
198             result &= (groupB.equals(o.groupB));
199         } else if (!(groupB == null && o.groupB == null)) {
200             result = false;
201         }
202         result &= groupC == o.groupC;
203         result &= groupD == o.groupD;
204         if (groupE != null && o.groupE != null) {
205             result &= groupE.equals(o.groupE);
206         } else if (!(groupE == null && o.groupE == null)) {
207             result = false;
208         }
209         if (groupF != null && o.groupF != null) {
210             result &= (groupF.equals(o.groupF));
211         } else if (!(groupF == null && o.groupF == null)) {
212             result = false;
213         }
214
215         return result;
216     }
217
218     /**
219      * Checks whether this OBIS Identifier and the other identifier equals taking the wildcards into account
220      *
221      * @param o OBISIdentifier to compare to
222      *
223      * @return true if identifiers match fully or against a wildcard, false otherwise
224      */
225     public boolean equalsWildCard(OBISIdentifier o) {
226         boolean result = true;
227
228         result &= groupA == o.groupA;
229         if (groupB != null && o.groupB != null) {
230             result &= (groupB.equals(o.groupB));
231         }
232         result &= groupC == o.groupC;
233         result &= groupD == o.groupD;
234         if (groupE != null && o.groupE != null) {
235             result &= (groupE.equals(o.groupE));
236         }
237         if (groupF != null && o.groupF != null) {
238             result &= (groupF.equals(o.groupF));
239         }
240
241         return result;
242     }
243
244     @Override
245     public int hashCode() {
246         return Objects.hash(groupA, (groupB != null ? groupB : 0), groupC, groupD, (groupE != null ? groupE : 0),
247                 (groupF != null ? groupF : 0));
248     }
249
250     /**
251      * Returns an reduced OBIS Identifier. This means group F is set to null
252      * (.i.e. not applicable)
253      *
254      * @return reduced OBIS Identifier
255      */
256     public OBISIdentifier getReducedOBISIdentifier() {
257         return new OBISIdentifier(groupA, groupB, groupC, groupD, groupE, null);
258     }
259
260     /**
261      * Returns an reduced OBIS Identifier with both group E and F is set to null
262      * (.i.e. not applicable)
263      *
264      * @return reduced OBIS Identifier
265      */
266     public OBISIdentifier getReducedOBISIdentifierGroupE() {
267         return new OBISIdentifier(groupA, groupB, groupC, groupD, null, null);
268     }
269
270     /**
271      * Returns whether or not the reduced OBIS Identifier is a wildcard identifier (meaning groupA groupB or groupC is
272      * null)
273      * Note that the DSMR specification does not use groupF so this is implemented always as a wildcard.
274      * To distinguish wildcard from non wildcard OBISIdentifiers, groupF is ignored.
275      *
276      * @return true if the reducedOBISIdentifier is a wildcard identifier, false otherwise.
277      */
278     public boolean reducedOBISIdentifierIsWildCard() {
279         return groupB == null;
280     }
281 }