]> git.basschouten.com Git - openhab-addons.git/blob
40f13b91accaa6a679e426c4caba74647ec907dd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.types.Command;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.UnDefType;
26
27 /**
28  * The {@link DimmerItemConverter} implements {@link org.openhab.core.library.items.DimmerItem} conversions
29  *
30  * @author Jan N. Klug - Initial contribution
31  */
32
33 @NonNullByDefault
34 public class DimmerItemConverter extends AbstractTransformingItemConverter {
35     private static final BigDecimal HUNDRED = BigDecimal.valueOf(100);
36
37     private State state = UnDefType.UNDEF;
38
39     public DimmerItemConverter(Consumer<State> updateState, Consumer<Command> postCommand,
40             @Nullable Consumer<String> sendHttpValue, ValueTransformation stateTransformations,
41             ValueTransformation commandTransformations, HttpChannelConfig channelConfig) {
42         super(updateState, postCommand, sendHttpValue, stateTransformations, commandTransformations, channelConfig);
43         this.channelConfig = channelConfig;
44     }
45
46     @Override
47     protected @Nullable Command toCommand(String value) {
48         return null;
49     }
50
51     @Override
52     public String toString(Command command) {
53         String string = channelConfig.commandToFixedValue(command);
54         if (string != null) {
55             return string;
56         }
57
58         if (command instanceof PercentType percentCommand) {
59             return percentCommand.toString();
60         }
61
62         throw new IllegalArgumentException("Command type '" + command.toString() + "' not supported");
63     }
64
65     @Override
66     public State toState(String string) {
67         State newState = UnDefType.UNDEF;
68
69         if (string.equals(channelConfig.onValue)) {
70             newState = PercentType.HUNDRED;
71         } else if (string.equals(channelConfig.offValue)) {
72             newState = PercentType.ZERO;
73         } else if (string.equals(channelConfig.increaseValue) && state instanceof PercentType brightnessState) {
74             BigDecimal newBrightness = brightnessState.toBigDecimal().add(channelConfig.step);
75             if (HUNDRED.compareTo(newBrightness) < 0) {
76                 newBrightness = HUNDRED;
77             }
78             newState = new PercentType(newBrightness);
79         } else if (string.equals(channelConfig.decreaseValue) && state instanceof PercentType brightnessState) {
80             BigDecimal newBrightness = brightnessState.toBigDecimal().subtract(channelConfig.step);
81             if (BigDecimal.ZERO.compareTo(newBrightness) > 0) {
82                 newBrightness = BigDecimal.ZERO;
83             }
84             newState = new PercentType(newBrightness);
85         } else {
86             try {
87                 BigDecimal value = new BigDecimal(string);
88                 if (value.compareTo(PercentType.HUNDRED.toBigDecimal()) > 0) {
89                     value = PercentType.HUNDRED.toBigDecimal();
90                 }
91                 if (value.compareTo(PercentType.ZERO.toBigDecimal()) < 0) {
92                     value = PercentType.ZERO.toBigDecimal();
93                 }
94                 newState = new PercentType(value);
95             } catch (IllegalArgumentException e) {
96                 // ignore
97             }
98         }
99
100         state = newState;
101         return newState;
102     }
103 }