]> git.basschouten.com Git - openhab-addons.git/blob
f6d6a700fdcaca1d88469558a84e61634ad1ad7f
[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.conformity.negate;
14
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.smartmeter.SmartMeterBindingConstants;
20
21 /**
22  * Parses the NegateBit property.
23  *
24  * @author Matthias Steigenberger - Initial contribution
25  *
26  */
27 @NonNullByDefault
28 public class NegateBitParser {
29
30     /**
31      * Parsing of negate bit property. This is in the format: {@literal <OBIS>:<POSITION>:<BIT_SET>"}
32      * e.g. "1-0:1-8-0:5:1"
33      *
34      * @param negateProperty
35      * @return The parsed model
36      */
37     public static NegateBitModel parseNegateProperty(String negateProperty) throws IllegalArgumentException {
38         Pattern obisPattern = Pattern.compile(SmartMeterBindingConstants.OBIS_PATTERN_CHANNELID);
39         try {
40             Matcher matcher = obisPattern.matcher(negateProperty);
41             if (matcher.find()) {
42                 String obis = matcher.group();
43                 String substring = negateProperty.substring(matcher.end() + 1, negateProperty.length());
44                 String[] split = substring.split(":");
45                 int negatePosition = Integer.parseInt(split[0]);
46                 boolean negateBit = Integer.parseInt(split[1]) == 0 ? false : true;
47                 boolean status = split.length > 2 ? "status".equalsIgnoreCase(split[2]) : false;
48                 return new NegateBitModel((byte) negatePosition, negateBit, obis, status);
49             }
50         } catch (Exception e) {
51             throw new IllegalArgumentException("Negate property cannot be parsed: " + negateProperty, e);
52         }
53         throw new IllegalArgumentException("Negate property cannot be parsed: " + negateProperty);
54     }
55 }