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