2 * Copyright (c) 2010-2023 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;
15 import java.awt.Color;
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.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;
31 * Helper class to create a TypeConverter
33 * @author Gregor Roth - Initial contribution
36 class TypeConverters {
39 * create a TypeConverter for a given Item type and property type
41 * @param itemType the item type
42 * @param propertyType the property type
43 * @return the type converter
45 static TypeConverter create(String itemType, String propertyType) {
46 switch (itemType.toLowerCase(Locale.ENGLISH)) {
48 return new SwitchTypeConverter();
50 return new DimmerTypeConverter();
52 return new ContactTypeConverter();
54 return new ColorTypeConverter();
56 if ("integer".equals(propertyType.toLowerCase(Locale.ENGLISH))) {
57 return new IntegerTypeConverter();
59 return new NumberTypeConverter();
62 return new StringTypeConverter();
66 private static boolean toBoolean(Object propertyValue) {
67 return Boolean.parseBoolean(propertyValue.toString());
70 private static BigDecimal toDecimal(Object propertyValue) {
71 return new BigDecimal(propertyValue.toString());
74 private static final class ColorTypeConverter implements TypeConverter {
77 public Command toStateCommand(Object propertyValue) {
78 var value = propertyValue.toString();
79 if (!value.contains("#")) {
82 Color rgb = Color.decode(value);
83 return HSBType.fromRGB(rgb.getRed(), rgb.getGreen(), rgb.getBlue());
87 public Object toPropertyValue(State state) {
88 var hsb = ((HSBType) state);
91 Float hue = hsb.getHue().floatValue();
92 Float saturation = hsb.getSaturation().floatValue();
93 Float brightness = hsb.getBrightness().floatValue();
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());
101 private static final class SwitchTypeConverter implements TypeConverter {
104 public Command toStateCommand(Object propertyValue) {
105 return toBoolean(propertyValue) ? OnOffType.ON : OnOffType.OFF;
109 public Object toPropertyValue(State state) {
110 return state == OnOffType.ON;
114 private static final class ContactTypeConverter implements TypeConverter {
117 public Command toStateCommand(Object propertyValue) {
118 return toBoolean(propertyValue) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
122 public Object toPropertyValue(State state) {
123 return state == OpenClosedType.OPEN;
127 private static final class DimmerTypeConverter implements TypeConverter {
130 public Command toStateCommand(Object propertyValue) {
131 return new PercentType(toDecimal(propertyValue));
135 public Object toPropertyValue(State state) {
136 return ((DecimalType) state).toBigDecimal().intValue();
140 private static final class NumberTypeConverter implements TypeConverter {
143 public Command toStateCommand(Object propertyValue) {
144 return new DecimalType(toDecimal(propertyValue));
148 public Object toPropertyValue(State state) {
149 return ((DecimalType) state).doubleValue();
153 private static final class IntegerTypeConverter implements TypeConverter {
156 public Command toStateCommand(Object propertyValue) {
157 return new DecimalType(toDecimal(propertyValue));
161 public Object toPropertyValue(State state) {
162 return ((DecimalType) state).intValue();
166 private static final class StringTypeConverter implements TypeConverter {
168 @SuppressWarnings("unchecked")
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();
176 return StringType.valueOf(textValue);
180 public Object toPropertyValue(State state) {
181 return state.toString();