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.http.internal.converter;
15 import java.util.function.Consumer;
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;
28 * The {@link NumberItemConverter} implements {@link org.openhab.core.library.items.NumberItem} conversions
30 * @author Jan N. Klug - Initial contribution
33 public class NumberItemConverter extends AbstractTransformingItemConverter {
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);
42 protected @Nullable Command toCommand(String value) {
47 protected State toState(String value) {
48 String trimmedValue = value.trim();
49 if (!trimmedValue.isEmpty()) {
51 if (channelConfig.unit != null) {
52 // we have a given unit - use that
53 return new QuantityType<>(trimmedValue + " " + channelConfig.unit);
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);
63 } catch (IllegalArgumentException e) {
67 return UnDefType.UNDEF;
71 protected String toString(Command command) {
72 return command.toString();