]> git.basschouten.com Git - openhab-addons.git/blob
ceb8706356b363c7ad626c5fe5e8271afdd2831a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.OpenClosedType;
22 import org.openhab.core.library.types.StringType;
23 import org.openhab.core.types.State;
24 import org.openhab.core.types.Type;
25 import org.openhab.core.types.UnDefType;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import com.google.gson.JsonElement;
30
31 /**
32  * The {@link ApplianceChannelSelector} for fridges
33  *
34  * @author Karel Goderis - Initial contribution
35  */
36 public enum FridgeChannelSelector implements ApplianceChannelSelector {
37
38     PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
39     DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
40     BRAND_ID("brandId", "brandId", StringType.class, true),
41     COMPANY_ID("companyId", "companyId", StringType.class, true),
42     STATE("state", "state", StringType.class, false),
43     SUPERCOOL(null, "supercool", OnOffType.class, false),
44     FRIDGECURRENTTEMP("currentTemperature", "current", DecimalType.class, false) {
45         @Override
46         public State getState(String s, DeviceMetaData dmd) {
47             return getState(s);
48         }
49     },
50     FRIDGETARGETTEMP("targetTemperature", "target", DecimalType.class, false) {
51         @Override
52         public State getState(String s, DeviceMetaData dmd) {
53             return getState(s);
54         }
55     },
56     DOOR("signalDoor", "door", OpenClosedType.class, false) {
57         @Override
58
59         public State getState(String s, DeviceMetaData dmd) {
60             if ("true".equals(s)) {
61                 return getState("OPEN");
62             }
63
64             if ("false".equals(s)) {
65                 return getState("CLOSED");
66             }
67
68             return UnDefType.UNDEF;
69         }
70     },
71     START(null, "start", OnOffType.class, false);
72
73     private final Logger logger = LoggerFactory.getLogger(FridgeChannelSelector.class);
74
75     private final String mieleID;
76     private final String channelID;
77     private final Class<? extends Type> typeClass;
78     private final boolean isProperty;
79
80     FridgeChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty) {
81         this.mieleID = propertyID;
82         this.channelID = channelID;
83         this.typeClass = typeClass;
84         this.isProperty = isProperty;
85     }
86
87     @Override
88     public String toString() {
89         return mieleID;
90     }
91
92     @Override
93     public String getMieleID() {
94         return mieleID;
95     }
96
97     @Override
98     public String getChannelID() {
99         return channelID;
100     }
101
102     @Override
103     public Class<? extends Type> getTypeClass() {
104         return typeClass;
105     }
106
107     @Override
108     public boolean isProperty() {
109         return isProperty;
110     }
111
112     @Override
113     public boolean isExtendedState() {
114         return false;
115     }
116
117     @Override
118     public State getState(String s, DeviceMetaData dmd) {
119         if (dmd != null) {
120             String localizedValue = getMieleEnum(s, dmd);
121             if (localizedValue == null) {
122                 localizedValue = dmd.LocalizedValue;
123             }
124             if (localizedValue == null) {
125                 localizedValue = s;
126             }
127
128             return getState(localizedValue);
129         } else {
130             return getState(s);
131         }
132     }
133
134     public State getState(String s) {
135         try {
136             Method valueOf = typeClass.getMethod("valueOf", String.class);
137             State state = (State) valueOf.invoke(typeClass, s);
138             if (state != null) {
139                 return state;
140             }
141         } catch (Exception e) {
142             logger.error("An exception occurred while converting '{}' into a State", s);
143         }
144
145         return null;
146     }
147
148     public String getMieleEnum(String s, DeviceMetaData dmd) {
149         if (dmd.MieleEnum != null) {
150             for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
151                 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
152                     return enumEntry.getKey();
153                 }
154             }
155         }
156
157         return null;
158     }
159 }