2 * Copyright (c) 2010-2024 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.math.BigDecimal;
16 import java.util.function.Consumer;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.http.internal.config.HttpChannelConfig;
21 import org.openhab.binding.http.internal.transform.ValueTransformation;
22 import org.openhab.core.library.types.PercentType;
23 import org.openhab.core.library.types.StopMoveType;
24 import org.openhab.core.library.types.UpDownType;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
30 * The {@link RollershutterItemConverter} implements {@link org.openhab.core.library.items.RollershutterItem}
33 * @author Jan N. Klug - Initial contribution
37 public class RollershutterItemConverter extends AbstractTransformingItemConverter {
38 private final HttpChannelConfig channelConfig;
40 public RollershutterItemConverter(Consumer<State> updateState, Consumer<Command> postCommand,
41 @Nullable Consumer<String> sendHttpValue, ValueTransformation stateTransformations,
42 ValueTransformation commandTransformations, HttpChannelConfig channelConfig) {
43 super(updateState, postCommand, sendHttpValue, stateTransformations, commandTransformations, channelConfig);
44 this.channelConfig = channelConfig;
48 public String toString(Command command) {
49 String string = channelConfig.commandToFixedValue(command);
54 if (command instanceof PercentType brightnessState) {
55 final String downValue = channelConfig.downValue;
56 final String upValue = channelConfig.upValue;
57 if (command.equals(PercentType.HUNDRED) && downValue != null) {
59 } else if (command.equals(PercentType.ZERO) && upValue != null) {
62 return brightnessState.toString();
66 throw new IllegalArgumentException("Command type '" + command.toString() + "' not supported");
70 protected @Nullable Command toCommand(String string) {
71 if (string.equals(channelConfig.upValue)) {
73 } else if (string.equals(channelConfig.downValue)) {
74 return UpDownType.DOWN;
75 } else if (string.equals(channelConfig.moveValue)) {
76 return StopMoveType.MOVE;
77 } else if (string.equals(channelConfig.stopValue)) {
78 return StopMoveType.STOP;
85 public State toState(String string) {
87 BigDecimal value = new BigDecimal(string);
88 if (value.compareTo(PercentType.HUNDRED.toBigDecimal()) > 0) {
89 return PercentType.HUNDRED;
91 if (value.compareTo(PercentType.ZERO.toBigDecimal()) < 0) {
92 return PercentType.ZERO;
94 return new PercentType(value);
95 } catch (NumberFormatException e) {
99 return UnDefType.UNDEF;