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.ihc.internal.converters;
15 import java.math.BigDecimal;
16 import java.math.RoundingMode;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.openhab.binding.ihc.internal.ws.exeptions.ConversionException;
20 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSFloatingPointValue;
21 import org.openhab.core.library.types.DecimalType;
24 * DecimalType {@literal <->} WSFloatingPointValue converter.
26 * @author Pauli Anttila - Initial contribution
28 public class DecimalTypeWSFloatingPointValueConverter implements Converter<WSFloatingPointValue, DecimalType> {
31 public DecimalType convertFromResourceValue(@NonNull WSFloatingPointValue from,
32 @NonNull ConverterAdditionalInfo convertData) throws ConversionException {
33 BigDecimal bd = new BigDecimal(from.value).setScale(2, RoundingMode.HALF_EVEN);
34 return new DecimalType(bd);
38 public WSFloatingPointValue convertFromOHType(@NonNull DecimalType from, @NonNull WSFloatingPointValue value,
39 @NonNull ConverterAdditionalInfo convertData) throws ConversionException {
40 if (from.doubleValue() >= value.minimumValue && from.doubleValue() <= value.maximumValue) {
41 return new WSFloatingPointValue(value.resourceID, from.doubleValue(), value.minimumValue,
44 throw new ConversionException("Value is not between acceptable limits (min=" + value.minimumValue + ", max="
45 + value.maximumValue + ")");