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.ihc.internal.converters;
15 import java.util.HashMap;
18 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSBooleanValue;
19 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSDateValue;
20 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSEnumValue;
21 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSFloatingPointValue;
22 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSIntegerValue;
23 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSResourceValue;
24 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSTimeValue;
25 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSTimerValue;
26 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSWeekdayValue;
27 import org.openhab.core.library.types.DateTimeType;
28 import org.openhab.core.library.types.DecimalType;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.library.types.OpenClosedType;
31 import org.openhab.core.library.types.PercentType;
32 import org.openhab.core.library.types.StringType;
33 import org.openhab.core.library.types.UpDownType;
34 import org.openhab.core.types.Type;
37 * IHC / ELKO {@literal <->} openHAB data type converter factory.
39 * @author Pauli Anttila - Initial contribution
41 public enum ConverterFactory {
44 private static final String ITEM_TYPE_NUMBER = "Number";
45 private static final String ITEM_TYPE_SWITCH = "Switch";
46 private static final String ITEM_TYPE_CONTACT = "Contact";
47 private static final String ITEM_TYPE_DIMMER = "Dimmer";
48 private static final String ITEM_TYPE_DATETIME = "DateTime";
49 private static final String ITEM_TYPE_STRING = "String";
50 private static final String ITEM_TYPE_ROLLERSHUTTER = "Rollershutter";
53 Class<? extends WSResourceValue> ihcType;
54 Class<? extends Type> openhabType;
56 Key(Class<? extends WSResourceValue> ihcType, Class<? extends Type> openhabType) {
57 this.ihcType = ihcType;
58 this.openhabType = openhabType;
62 public int hashCode() {
63 return ihcType.getClass().hashCode() + openhabType.getClass().hashCode();
67 public boolean equals(Object o) {
71 if (!(o instanceof Key)) {
76 return key.ihcType.equals(ihcType) && key.openhabType.equals(openhabType);
80 private Map<Key, Converter<? extends WSResourceValue, ? extends Type>> converters;
82 private ConverterFactory() {
83 converters = new HashMap<>();
85 converters.put(new Key(WSDateValue.class, DateTimeType.class), new DateTimeTypeWSDateValueConverter());
86 converters.put(new Key(WSTimeValue.class, DateTimeType.class), new DateTimeTypeWSTimeValueConverter());
87 converters.put(new Key(WSBooleanValue.class, DecimalType.class), new DecimalTypeWSBooleanValueConverter());
88 converters.put(new Key(WSEnumValue.class, DecimalType.class), new DecimalTypeWSEnumValueConverter());
89 converters.put(new Key(WSFloatingPointValue.class, DecimalType.class),
90 new DecimalTypeWSFloatingPointValueConverter());
91 converters.put(new Key(WSIntegerValue.class, DecimalType.class), new DecimalTypeWSIntegerValueConverter());
92 converters.put(new Key(WSTimerValue.class, DecimalType.class), new DecimalTypeWSTimerValueConverter());
93 converters.put(new Key(WSWeekdayValue.class, DecimalType.class), new DecimalTypeWSWeekdayValueConverter());
94 converters.put(new Key(WSBooleanValue.class, OnOffType.class), new OnOffTypeWSBooleanValueConverter());
95 converters.put(new Key(WSIntegerValue.class, OnOffType.class), new OnOffTypeWSIntegerValueConverter());
96 converters.put(new Key(WSBooleanValue.class, OpenClosedType.class),
97 new OpenClosedTypeWSBooleanValueConverter());
98 converters.put(new Key(WSIntegerValue.class, OpenClosedType.class),
99 new OpenClosedTypeWSIntegerValueConverter());
100 converters.put(new Key(WSIntegerValue.class, PercentType.class), new PercentTypeWSIntegerValueConverter());
101 converters.put(new Key(WSEnumValue.class, StringType.class), new StringTypeWSEnumValueConverter());
102 converters.put(new Key(WSBooleanValue.class, UpDownType.class), new UpDownTypeWSBooleanValueConverter());
103 converters.put(new Key(WSIntegerValue.class, UpDownType.class), new UpDownTypeWSIntegerValueConverter());
106 public static ConverterFactory getInstance() {
110 @SuppressWarnings("unchecked")
111 public Converter<WSResourceValue, Type> getConverter(Class<? extends WSResourceValue> resourceValueType,
112 Class<? extends Type> type) {
113 return (Converter<WSResourceValue, Type>) converters.get(new Key(resourceValueType, type));
116 @SuppressWarnings("unchecked")
117 public Converter<WSResourceValue, Type> getConverter(Class<? extends WSResourceValue> resourceValueType,
119 Class<? extends Type> type = null;
121 case ITEM_TYPE_SWITCH:
122 type = OnOffType.class;
124 case ITEM_TYPE_ROLLERSHUTTER:
125 case ITEM_TYPE_DIMMER:
126 type = PercentType.class;
128 case ITEM_TYPE_CONTACT:
129 type = OpenClosedType.class;
131 case ITEM_TYPE_STRING:
132 type = StringType.class;
134 case ITEM_TYPE_NUMBER:
135 type = DecimalType.class;
137 case ITEM_TYPE_DATETIME:
138 type = DateTimeType.class;
141 return (Converter<WSResourceValue, Type>) converters.get(new Key(resourceValueType, type));