]> git.basschouten.com Git - openhab-addons.git/blob
1c5ba2685488ad7b861d9b94b256ca5c1c11b964
[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.math.BigDecimal;
16 import java.util.function.Consumer;
17
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;
28
29 /**
30  * The {@link RollershutterItemConverter} implements {@link org.openhab.core.library.items.RollershutterItem}
31  * conversions
32  *
33  * @author Jan N. Klug - Initial contribution
34  */
35
36 @NonNullByDefault
37 public class RollershutterItemConverter extends AbstractTransformingItemConverter {
38     private final HttpChannelConfig channelConfig;
39
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;
45     }
46
47     @Override
48     public String toString(Command command) {
49         String string = channelConfig.commandToFixedValue(command);
50         if (string != null) {
51             return string;
52         }
53
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) {
58                 return downValue;
59             } else if (command.equals(PercentType.ZERO) && upValue != null) {
60                 return upValue;
61             } else {
62                 return brightnessState.toString();
63             }
64         }
65
66         throw new IllegalArgumentException("Command type '" + command.toString() + "' not supported");
67     }
68
69     @Override
70     protected @Nullable Command toCommand(String string) {
71         if (string.equals(channelConfig.upValue)) {
72             return UpDownType.UP;
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;
79         }
80
81         return null;
82     }
83
84     @Override
85     public State toState(String string) {
86         try {
87             BigDecimal value = new BigDecimal(string);
88             if (value.compareTo(PercentType.HUNDRED.toBigDecimal()) > 0) {
89                 return PercentType.HUNDRED;
90             }
91             if (value.compareTo(PercentType.ZERO.toBigDecimal()) < 0) {
92                 return PercentType.ZERO;
93             }
94             return new PercentType(value);
95         } catch (NumberFormatException e) {
96             // ignore
97         }
98
99         return UnDefType.UNDEF;
100     }
101 }