]> git.basschouten.com Git - openhab-addons.git/blob
db176a9fad906b80e3f9a50caab9c9a48bc7e166
[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.http.internal.converter;
14
15 import java.util.function.Consumer;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.http.internal.config.HttpChannelConfig;
20 import org.openhab.binding.http.internal.transform.ValueTransformation;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.QuantityType;
23 import org.openhab.core.types.Command;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.UnDefType;
26
27 /**
28  * The {@link NumberItemConverter} implements {@link org.openhab.core.library.items.NumberItem} conversions
29  *
30  * @author Jan N. Klug - Initial contribution
31  */
32 @NonNullByDefault
33 public class NumberItemConverter extends AbstractTransformingItemConverter {
34
35     public NumberItemConverter(Consumer<State> updateState, Consumer<Command> postCommand,
36             @Nullable Consumer<String> sendHttpValue, ValueTransformation stateTransformations,
37             ValueTransformation commandTransformations, HttpChannelConfig channelConfig) {
38         super(updateState, postCommand, sendHttpValue, stateTransformations, commandTransformations, channelConfig);
39     }
40
41     @Override
42     protected @Nullable Command toCommand(String value) {
43         return null;
44     }
45
46     @Override
47     protected State toState(String value) {
48         String trimmedValue = value.trim();
49         if (!trimmedValue.isEmpty()) {
50             try {
51                 if (channelConfig.unit != null) {
52                     // we have a given unit - use that
53                     return new QuantityType<>(trimmedValue + " " + channelConfig.unit);
54                 } else {
55                     try {
56                         // try if we have a simple number
57                         return new DecimalType(trimmedValue);
58                     } catch (IllegalArgumentException e1) {
59                         // not a plain number, maybe with unit?
60                         return new QuantityType<>(trimmedValue);
61                     }
62                 }
63             } catch (IllegalArgumentException e) {
64                 // finally failed
65             }
66         }
67         return UnDefType.UNDEF;
68     }
69
70     @Override
71     protected String toString(Command command) {
72         return command.toString();
73     }
74 }