]> git.basschouten.com Git - openhab-addons.git/blob
82bd5d11f5110af83704c18a2ab49ef297d73b43
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.miele.internal.handler;
14
15 import java.lang.reflect.Method;
16 import java.util.Map.Entry;
17
18 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceMetaData;
19 import org.openhab.core.library.types.DecimalType;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.library.types.StringType;
22 import org.openhab.core.types.State;
23 import org.openhab.core.types.Type;
24 import org.openhab.core.types.UnDefType;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import com.google.gson.JsonElement;
29
30 /**
31  * The {@link ApplianceChannelSelector} for ventilation hoods
32  *
33  * @author Karel Goderis - Initial contribution
34  */
35 public enum HoodChannelSelector implements ApplianceChannelSelector {
36
37     PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
38     DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
39     BRAND_ID("brandId", "brandId", StringType.class, true),
40     COMPANY_ID("companyId", "companyId", StringType.class, true),
41     STATE("state", "state", StringType.class, false),
42     VENTILATION("ventilationPower", "ventilation", DecimalType.class, false),
43     LIGHT("lightingStatus", "light", OnOffType.class, false) {
44         @Override
45
46         public State getState(String s, DeviceMetaData dmd) {
47             if ("true".equals(s)) {
48                 return getState("ON");
49             }
50
51             if ("false".equals(s)) {
52                 return getState("OFF");
53             }
54
55             return UnDefType.UNDEF;
56         }
57     },
58     STOP(null, "stop", OnOffType.class, false);
59
60     private final Logger logger = LoggerFactory.getLogger(HoodChannelSelector.class);
61
62     private final String mieleID;
63     private final String channelID;
64     private final Class<? extends Type> typeClass;
65     private final boolean isProperty;
66
67     HoodChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty) {
68         this.mieleID = propertyID;
69         this.channelID = channelID;
70         this.typeClass = typeClass;
71         this.isProperty = isProperty;
72     }
73
74     @Override
75     public String toString() {
76         return mieleID;
77     }
78
79     @Override
80     public String getMieleID() {
81         return mieleID;
82     }
83
84     @Override
85     public String getChannelID() {
86         return channelID;
87     }
88
89     @Override
90     public Class<? extends Type> getTypeClass() {
91         return typeClass;
92     }
93
94     @Override
95     public boolean isProperty() {
96         return isProperty;
97     }
98
99     @Override
100     public State getState(String s, DeviceMetaData dmd) {
101         if (dmd != null) {
102             String localizedValue = getMieleEnum(s, dmd);
103             if (localizedValue == null) {
104                 localizedValue = dmd.LocalizedValue;
105             }
106             if (localizedValue == null) {
107                 localizedValue = s;
108             }
109
110             return getState(localizedValue);
111         } else {
112             return getState(s);
113         }
114     }
115
116     public State getState(String s) {
117         try {
118             Method valueOf = typeClass.getMethod("valueOf", String.class);
119             State state = (State) valueOf.invoke(typeClass, s);
120             if (state != null) {
121                 return state;
122             }
123         } catch (Exception e) {
124             logger.error("An exception occurred while converting '{}' into a State", s);
125         }
126
127         return null;
128     }
129
130     public String getMieleEnum(String s, DeviceMetaData dmd) {
131         if (dmd.MieleEnum != null) {
132             for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
133                 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
134                     return enumEntry.getKey();
135                 }
136             }
137         }
138
139         return null;
140     }
141 }