]> git.basschouten.com Git - openhab-addons.git/blob
48fc72e19444bd119f12446e584ae32051114a95
[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.ihc.internal.converters;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
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;
35
36 /**
37  * IHC / ELKO <-> openHAB data type converter factory.
38  *
39  * @author Pauli Anttila - Initial contribution
40  */
41 public enum ConverterFactory {
42     INSTANCE;
43
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";
51
52     private class Key {
53         Class<? extends WSResourceValue> ihcType;
54         Class<? extends Type> openhabType;
55
56         Key(Class<? extends WSResourceValue> ihcType, Class<? extends Type> openhabType) {
57             this.ihcType = ihcType;
58             this.openhabType = openhabType;
59         }
60
61         @Override
62         public int hashCode() {
63             return ihcType.getClass().hashCode() + openhabType.getClass().hashCode();
64         }
65
66         @Override
67         public boolean equals(Object o) {
68             if (o == this) {
69                 return true;
70             }
71             if (!(o instanceof Key)) {
72                 return false;
73             }
74             Key key = (Key) o;
75
76             return key.ihcType.equals(ihcType) && key.openhabType.equals(openhabType);
77         }
78     }
79
80     private Map<Key, Converter<? extends WSResourceValue, ? extends Type>> converters;
81
82     private ConverterFactory() {
83         converters = new HashMap<>();
84
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());
104     }
105
106     public static ConverterFactory getInstance() {
107         return INSTANCE;
108     }
109
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));
114     }
115
116     @SuppressWarnings("unchecked")
117     public Converter<WSResourceValue, Type> getConverter(Class<? extends WSResourceValue> resourceValueType,
118             String itemType) {
119         Class<? extends Type> type = null;
120         switch (itemType) {
121             case ITEM_TYPE_SWITCH:
122                 type = OnOffType.class;
123                 break;
124             case ITEM_TYPE_ROLLERSHUTTER:
125             case ITEM_TYPE_DIMMER:
126                 type = PercentType.class;
127                 break;
128             case ITEM_TYPE_CONTACT:
129                 type = OpenClosedType.class;
130                 break;
131             case ITEM_TYPE_STRING:
132                 type = StringType.class;
133                 break;
134             case ITEM_TYPE_NUMBER:
135                 type = DecimalType.class;
136                 break;
137             case ITEM_TYPE_DATETIME:
138                 type = DateTimeType.class;
139                 break;
140         }
141         return (Converter<WSResourceValue, Type>) converters.get(new Key(resourceValueType, type));
142     }
143 }