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