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.conformity.negate;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.smartmeter.SmartMeterBindingConstants;
22 * Parses the NegateBit property.
24 * @author Matthias Steigenberger - Initial contribution
28 public class NegateBitParser {
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"
34 * @param negateProperty
35 * @return The parsed model
37 public static NegateBitModel parseNegateProperty(String negateProperty) throws IllegalArgumentException {
38 Pattern obisPattern = Pattern.compile(SmartMeterBindingConstants.OBIS_PATTERN_CHANNELID);
40 Matcher matcher = obisPattern.matcher(negateProperty);
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);
50 } catch (Exception e) {
51 throw new IllegalArgumentException("Negate property cannot be parsed: " + negateProperty, e);
53 throw new IllegalArgumentException("Negate property cannot be parsed: " + negateProperty);