]> git.basschouten.com Git - openhab-addons.git/blob
03f63382d57c3913f26a6be23138de2a0b5a3207
[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.webthing.internal.link;
14
15 import java.awt.Color;
16 import java.math.BigDecimal;
17 import java.util.Collection;
18 import java.util.Locale;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.HSBType;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.OpenClosedType;
25 import org.openhab.core.library.types.PercentType;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.State;
29
30 /**
31  * Helper class to create a TypeConverter
32  *
33  * @author Gregor Roth - Initial contribution
34  */
35 @NonNullByDefault
36 class TypeConverters {
37
38     /**
39      * create a TypeConverter for a given Item type and property type
40      *
41      * @param itemType the item type
42      * @param propertyType the property type
43      * @return the type converter
44      */
45     static TypeConverter create(String itemType, String propertyType) {
46         switch (itemType.toLowerCase(Locale.ENGLISH)) {
47             case "switch":
48                 return new SwitchTypeConverter();
49             case "dimmer":
50                 return new DimmerTypeConverter();
51             case "contact":
52                 return new ContactTypeConverter();
53             case "color":
54                 return new ColorTypeConverter();
55             case "number":
56                 if ("integer".equals(propertyType.toLowerCase(Locale.ENGLISH))) {
57                     return new IntegerTypeConverter();
58                 } else {
59                     return new NumberTypeConverter();
60                 }
61             default:
62                 return new StringTypeConverter();
63         }
64     }
65
66     private static boolean toBoolean(Object propertyValue) {
67         return Boolean.parseBoolean(propertyValue.toString());
68     }
69
70     private static BigDecimal toDecimal(Object propertyValue) {
71         return new BigDecimal(propertyValue.toString());
72     }
73
74     private static final class ColorTypeConverter implements TypeConverter {
75
76         @Override
77         public Command toStateCommand(Object propertyValue) {
78             var value = propertyValue.toString();
79             if (!value.contains("#")) {
80                 value = "#" + value;
81             }
82             Color rgb = Color.decode(value);
83             return HSBType.fromRGB(rgb.getRed(), rgb.getGreen(), rgb.getBlue());
84         }
85
86         @Override
87         public Object toPropertyValue(State state) {
88             var hsb = ((HSBType) state);
89
90             // Get HSB values
91             Float hue = hsb.getHue().floatValue();
92             Float saturation = hsb.getSaturation().floatValue();
93             Float brightness = hsb.getBrightness().floatValue();
94
95             // Convert HSB to RGB and then to HTML hex
96             Color rgb = Color.getHSBColor(hue / 360, saturation / 100, brightness / 100);
97             return String.format("#%02x%02x%02x", rgb.getRed(), rgb.getGreen(), rgb.getBlue());
98         }
99     }
100
101     private static final class SwitchTypeConverter implements TypeConverter {
102
103         @Override
104         public Command toStateCommand(Object propertyValue) {
105             return toBoolean(propertyValue) ? OnOffType.ON : OnOffType.OFF;
106         }
107
108         @Override
109         public Object toPropertyValue(State state) {
110             return state == OnOffType.ON;
111         }
112     }
113
114     private static final class ContactTypeConverter implements TypeConverter {
115
116         @Override
117         public Command toStateCommand(Object propertyValue) {
118             return toBoolean(propertyValue) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
119         }
120
121         @Override
122         public Object toPropertyValue(State state) {
123             return state == OpenClosedType.OPEN;
124         }
125     }
126
127     private static final class DimmerTypeConverter implements TypeConverter {
128
129         @Override
130         public Command toStateCommand(Object propertyValue) {
131             return new PercentType(toDecimal(propertyValue));
132         }
133
134         @Override
135         public Object toPropertyValue(State state) {
136             return ((DecimalType) state).toBigDecimal().intValue();
137         }
138     }
139
140     private static final class NumberTypeConverter implements TypeConverter {
141
142         @Override
143         public Command toStateCommand(Object propertyValue) {
144             return new DecimalType(toDecimal(propertyValue));
145         }
146
147         @Override
148         public Object toPropertyValue(State state) {
149             return ((DecimalType) state).doubleValue();
150         }
151     }
152
153     private static final class IntegerTypeConverter implements TypeConverter {
154
155         @Override
156         public Command toStateCommand(Object propertyValue) {
157             return new DecimalType(toDecimal(propertyValue));
158         }
159
160         @Override
161         public Object toPropertyValue(State state) {
162             return ((DecimalType) state).intValue();
163         }
164     }
165
166     private static final class StringTypeConverter implements TypeConverter {
167
168         @SuppressWarnings("unchecked")
169         @Override
170         public Command toStateCommand(Object propertyValue) {
171             String textValue = propertyValue.toString();
172             if (propertyValue instanceof Collection collection) {
173                 textValue = collection.stream()
174                         .reduce("", (entry1, entry2) -> entry1.toString() + "\n" + entry2.toString()).toString();
175             }
176             return StringType.valueOf(textValue);
177         }
178
179         @Override
180         public Object toPropertyValue(State state) {
181             return state.toString();
182         }
183     }
184 }