]> git.basschouten.com Git - openhab-addons.git/blob
c320a2bed5e8760e5e4cb1775326072b3b5b09ab
[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 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.http.internal.config.HttpChannelConfig;
23 import org.openhab.binding.http.internal.transform.ValueTransformation;
24 import org.openhab.core.library.types.HSBType;
25 import org.openhab.core.library.types.PercentType;
26 import org.openhab.core.types.Command;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.UnDefType;
29
30 /**
31  * The {@link ColorItemConverter} implements {@link org.openhab.core.library.items.ColorItem} conversions
32  *
33  * @author Jan N. Klug - Initial contribution
34  */
35
36 @NonNullByDefault
37 public class ColorItemConverter extends AbstractTransformingItemConverter {
38     private static final BigDecimal BYTE_FACTOR = BigDecimal.valueOf(2.55);
39     private static final BigDecimal HUNDRED = BigDecimal.valueOf(100);
40     private static final Pattern TRIPLE_MATCHER = Pattern.compile("(\\d+),(\\d+),(\\d+)");
41
42     private State state = UnDefType.UNDEF;
43
44     public ColorItemConverter(Consumer<State> updateState, Consumer<Command> postCommand,
45             @Nullable Consumer<String> sendHttpValue, ValueTransformation stateTransformations,
46             ValueTransformation commandTransformations, HttpChannelConfig channelConfig) {
47         super(updateState, postCommand, sendHttpValue, stateTransformations, commandTransformations, channelConfig);
48         this.channelConfig = channelConfig;
49     }
50
51     @Override
52     protected @Nullable Command toCommand(String value) {
53         return null;
54     }
55
56     @Override
57     public String toString(Command command) {
58         String string = channelConfig.commandToFixedValue(command);
59         if (string != null) {
60             return string;
61         }
62
63         if (command instanceof HSBType newState) {
64             state = newState;
65             return hsbToString(newState);
66         } else if (command instanceof PercentType percentCommand && state instanceof HSBType hsb) {
67             HSBType newState = new HSBType(hsb.getHue(), hsb.getSaturation(), percentCommand);
68             state = newState;
69             return hsbToString(newState);
70         }
71
72         throw new IllegalArgumentException("Command type '" + command.toString() + "' not supported");
73     }
74
75     @Override
76     public State toState(String string) {
77         State newState = UnDefType.UNDEF;
78         if (string.equals(channelConfig.onValue)) {
79             if (state instanceof HSBType hsb) {
80                 newState = new HSBType(hsb.getHue(), hsb.getSaturation(), PercentType.HUNDRED);
81             } else {
82                 newState = HSBType.WHITE;
83             }
84         } else if (string.equals(channelConfig.offValue)) {
85             if (state instanceof HSBType hsb) {
86                 newState = new HSBType(hsb.getHue(), hsb.getSaturation(), PercentType.ZERO);
87             } else {
88                 newState = HSBType.BLACK;
89             }
90         } else if (string.equals(channelConfig.increaseValue) && state instanceof HSBType hsb) {
91             BigDecimal newBrightness = hsb.getBrightness().toBigDecimal().add(channelConfig.step);
92             if (HUNDRED.compareTo(newBrightness) < 0) {
93                 newBrightness = HUNDRED;
94             }
95             newState = new HSBType(hsb.getHue(), hsb.getSaturation(), new PercentType(newBrightness));
96         } else if (string.equals(channelConfig.decreaseValue) && state instanceof HSBType hsb) {
97             BigDecimal newBrightness = hsb.getBrightness().toBigDecimal().subtract(channelConfig.step);
98             if (BigDecimal.ZERO.compareTo(newBrightness) > 0) {
99                 newBrightness = BigDecimal.ZERO;
100             }
101             newState = new HSBType(hsb.getHue(), hsb.getSaturation(), new PercentType(newBrightness));
102         } else {
103             Matcher matcher = TRIPLE_MATCHER.matcher(string);
104             if (matcher.matches()) {
105                 switch (channelConfig.colorMode) {
106                     case RGB:
107                         int r = Integer.parseInt(matcher.group(1));
108                         int g = Integer.parseInt(matcher.group(2));
109                         int b = Integer.parseInt(matcher.group(3));
110                         newState = HSBType.fromRGB(r, g, b);
111                         break;
112                     case HSB:
113                         newState = new HSBType(string);
114                         break;
115                 }
116             }
117         }
118
119         state = newState;
120         return newState;
121     }
122
123     private String hsbToString(HSBType state) {
124         switch (channelConfig.colorMode) {
125             case RGB:
126                 PercentType[] rgb = state.toRGB();
127                 return String.format("%1$d,%2$d,%3$d", rgb[0].toBigDecimal().multiply(BYTE_FACTOR).intValue(),
128                         rgb[1].toBigDecimal().multiply(BYTE_FACTOR).intValue(),
129                         rgb[2].toBigDecimal().multiply(BYTE_FACTOR).intValue());
130             case HSB:
131                 return state.toString();
132         }
133         throw new IllegalStateException("Invalid colorMode setting");
134     }
135
136     public enum ColorMode {
137         RGB,
138         HSB
139     }
140 }