]> git.basschouten.com Git - openhab-addons.git/blob
6a0f4e61a6c03e30db45af469739b63b5a7dc3f3
[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.ihc.internal.converters;
14
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.openhab.binding.ihc.internal.ws.exeptions.ConversionException;
17 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSIntegerValue;
18 import org.openhab.core.library.types.UpDownType;
19
20 /**
21  * UpDownType <-> WSIntegerValue converter.
22  *
23  * @author Pauli Anttila - Initial contribution
24  */
25 public class UpDownTypeWSIntegerValueConverter implements Converter<WSIntegerValue, UpDownType> {
26
27     @Override
28     public UpDownType convertFromResourceValue(@NonNull WSIntegerValue from,
29             @NonNull ConverterAdditionalInfo convertData) throws ConversionException {
30         return from.value > 0 ^ convertData.getInverted() ? UpDownType.UP : UpDownType.DOWN;
31     }
32
33     @Override
34     public WSIntegerValue convertFromOHType(@NonNull UpDownType from, @NonNull WSIntegerValue value,
35             @NonNull ConverterAdditionalInfo convertData) throws ConversionException {
36         int newVal = from == UpDownType.UP ? value.maximumValue : value.minimumValue;
37
38         if (convertData.getInverted()) {
39             newVal = newVal == value.maximumValue ? value.minimumValue : value.maximumValue;
40         }
41         if (newVal >= value.minimumValue && newVal <= value.maximumValue) {
42             return new WSIntegerValue(value.resourceID, newVal, value.minimumValue, value.maximumValue);
43         } else {
44             throw new ConversionException("Value is not between acceptable limits (min=" + value.minimumValue + ", max="
45                     + value.maximumValue + ")");
46         }
47     }
48 }