]> git.basschouten.com Git - openhab-addons.git/blob
1cf84d8b2034f01887825ebb93d18dbab5175da0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.ihc.internal.converters;
14
15 import java.math.BigDecimal;
16 import java.math.RoundingMode;
17
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;
22
23 /**
24  * DecimalType <-> WSFloatingPointValue converter.
25  *
26  * @author Pauli Anttila - Initial contribution
27  */
28 public class DecimalTypeWSFloatingPointValueConverter implements Converter<WSFloatingPointValue, DecimalType> {
29
30     @Override
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);
35     }
36
37     @Override
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,
42                     value.maximumValue);
43         } else {
44             throw new ConversionException("Value is not between acceptable limits (min=" + value.minimumValue + ", max="
45                     + value.maximumValue + ")");
46         }
47     }
48 }