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.dsmr.internal.device.cosem;
15 import java.text.ParseException;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.library.types.DecimalType;
21 * CosemInteger represents a decimal value
23 * @author M. Volaart - Initial contribution
24 * @author Hilbrand Bouwkamp - Combined Integer and Double because {@link DecimalType} handles both
27 class CosemDecimal extends CosemValueDescriptor<DecimalType> {
29 public static final CosemDecimal INSTANCE = new CosemDecimal(false);
30 public static final CosemDecimal INSTANCE_WITH_UNITS = new CosemDecimal(true);
33 * If true it can be the input contains a unit. In that case the unit will be stripped before parsing.
35 private final boolean expectUnit;
37 private CosemDecimal(boolean expectUnit) {
38 this.expectUnit = expectUnit;
41 public CosemDecimal(String ohChannelId) {
43 this.expectUnit = false;
47 * Parses a String value (that represents a decimal) to a {@link DecimalType} object.
49 * @param cosemValue the value to parse
50 * @return {@link DecimalType} representing the value of the cosem value
51 * @throws ParseException if parsing failed
54 protected DecimalType getStateValue(String cosemValue) throws ParseException {
59 final int sep = cosemValue.indexOf('*');
60 value = sep > 0 ? cosemValue.substring(0, sep) : cosemValue;
64 return new DecimalType(value);
65 } catch (NumberFormatException nfe) {
66 throw new ParseException("Failed to parse value '" + cosemValue + "' as integer", 0);