2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.webthing.internal.link;
16 import java.math.BigDecimal;
17 import java.util.Collection;
18 import java.util.Locale;
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;
26 * Helper class to create a TypeConverter
28 * @author Gregor Roth - Initial contribution
31 class TypeConverters {
34 * create a TypeConverter for a given Item type and property type
36 * @param itemType the item type
37 * @param propertyType the property type
38 * @return the type converter
40 static TypeConverter create(String itemType, String propertyType) {
41 switch (itemType.toLowerCase(Locale.ENGLISH)) {
43 return new SwitchTypeConverter();
45 return new DimmerTypeConverter();
47 return new ContactTypeConverter();
49 return new ColorTypeConverter();
51 if (propertyType.toLowerCase(Locale.ENGLISH).equals("integer")) {
52 return new IntegerTypeConverter();
54 return new NumberTypeConverter();
57 return new StringTypeConverter();
61 private static boolean toBoolean(Object propertyValue) {
62 return Boolean.parseBoolean(propertyValue.toString());
65 private static BigDecimal toDecimal(Object propertyValue) {
66 return new BigDecimal(propertyValue.toString());
69 private static final class ColorTypeConverter implements TypeConverter {
72 public Command toStateCommand(Object propertyValue) {
73 var value = propertyValue.toString();
74 if (!value.contains("#")) {
77 Color rgb = Color.decode(value);
78 return HSBType.fromRGB(rgb.getRed(), rgb.getGreen(), rgb.getBlue());
82 public Object toPropertyValue(State state) {
83 var hsb = ((HSBType) state);
86 Float hue = hsb.getHue().floatValue();
87 Float saturation = hsb.getSaturation().floatValue();
88 Float brightness = hsb.getBrightness().floatValue();
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());
96 private static final class SwitchTypeConverter implements TypeConverter {
99 public Command toStateCommand(Object propertyValue) {
100 return toBoolean(propertyValue) ? OnOffType.ON : OnOffType.OFF;
104 public Object toPropertyValue(State state) {
105 return state == OnOffType.ON;
109 private static final class ContactTypeConverter implements TypeConverter {
112 public Command toStateCommand(Object propertyValue) {
113 return toBoolean(propertyValue) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
117 public Object toPropertyValue(State state) {
118 return state == OpenClosedType.OPEN;
122 private static final class DimmerTypeConverter implements TypeConverter {
125 public Command toStateCommand(Object propertyValue) {
126 return new PercentType(toDecimal(propertyValue));
130 public Object toPropertyValue(State state) {
131 return ((DecimalType) state).toBigDecimal().intValue();
135 private static final class NumberTypeConverter implements TypeConverter {
138 public Command toStateCommand(Object propertyValue) {
139 return new DecimalType(toDecimal(propertyValue));
143 public Object toPropertyValue(State state) {
144 return ((DecimalType) state).doubleValue();
148 private static final class IntegerTypeConverter implements TypeConverter {
151 public Command toStateCommand(Object propertyValue) {
152 return new DecimalType(toDecimal(propertyValue));
156 public Object toPropertyValue(State state) {
157 return ((DecimalType) state).intValue();
161 private static final class StringTypeConverter implements TypeConverter {
163 @SuppressWarnings("unchecked")
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();
171 return StringType.valueOf(textValue);
175 public Object toPropertyValue(State state) {
176 return state.toString();