]> git.basschouten.com Git - openhab-addons.git/blob
a8ae61c0b5667e1ef19cb24885aae81e5b83b55e
[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.StringType;
21 import org.openhab.core.types.State;
22 import org.openhab.core.types.Type;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.google.gson.JsonElement;
27
28 /**
29  * The {@link ApplianceChannelSelector} for hobs
30  *
31  * @author Karel Goderis - Initial contribution
32  */
33 public enum HobChannelSelector implements ApplianceChannelSelector {
34
35     PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
36     DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
37     BRAND_ID("brandId", "brandId", StringType.class, true),
38     COMPANY_ID("companyId", "companyId", StringType.class, true),
39     STATE("state", "state", StringType.class, false),
40     PLATES("plateNumbers", "plates", DecimalType.class, true),
41     PLATE1_POWER("plate1PowerStep", "plate1power", DecimalType.class, false),
42     PLATE1_HEAT("plate1RemainingHeat", "plate1heat", DecimalType.class, false) {
43         @Override
44         public State getState(String s, DeviceMetaData dmd) {
45             // If there is remaining heat, the device metadata contains some informative string which can not be
46             // converted into a DecimalType. We therefore ignore the metadata and return the device property value as a
47             // State
48             return getState(s);
49         }
50     },
51     PLATE1_TIME("plate1RemainingTime", "plate1time", StringType.class, false),
52     PLATE2_POWER("plate2PowerStep", "plate2power", DecimalType.class, false),
53     PLATE2_HEAT("plate2RemainingHeat", "plate2heat", DecimalType.class, false) {
54         @Override
55         public State getState(String s, DeviceMetaData dmd) {
56             return getState(s);
57         }
58     },
59     PLATE2_TIME("plate2RemainingTime", "plate2time", StringType.class, false),
60     PLATE3_POWER("plate3PowerStep", "plate3power", DecimalType.class, false),
61     PLATE3_HEAT("plate3RemainingHeat", "plate3heat", DecimalType.class, false) {
62         @Override
63         public State getState(String s, DeviceMetaData dmd) {
64             return getState(s);
65         }
66     },
67     PLATE3_TIME("plate3RemainingTime", "plate3time", StringType.class, false),
68     PLATE4_POWER("plate4PowerStep", "plate4power", DecimalType.class, false),
69     PLATE4_HEAT("plate4RemainingHeat", "plate4heat", DecimalType.class, false) {
70         @Override
71         public State getState(String s, DeviceMetaData dmd) {
72             return getState(s);
73         }
74     },
75     PLATE4_TIME("plate4RemainingTime", "plate4time", StringType.class, false),
76     PLATE5_POWER("plate5PowerStep", "plate5power", DecimalType.class, false),
77     PLATE5_HEAT("plate5RemainingHeat", "plate5heat", DecimalType.class, false) {
78         @Override
79         public State getState(String s, DeviceMetaData dmd) {
80             return getState(s);
81         }
82     },
83     PLATE5_TIME("plate5RemainingTime", "plate5time", StringType.class, false),
84     PLATE6_POWER("plate6PowerStep", "plate6power", DecimalType.class, false),
85     PLATE6_HEAT("plate6RemainingHeat", "plate6heat", DecimalType.class, false) {
86         @Override
87         public State getState(String s, DeviceMetaData dmd) {
88             return getState(s);
89         }
90     },
91     PLATE6_TIME("plate6RemainingTime", "plate6time", StringType.class, false);
92
93     private final Logger logger = LoggerFactory.getLogger(HobChannelSelector.class);
94
95     private final String mieleID;
96     private final String channelID;
97     private final Class<? extends Type> typeClass;
98     private final boolean isProperty;
99
100     HobChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty) {
101         this.mieleID = propertyID;
102         this.channelID = channelID;
103         this.typeClass = typeClass;
104         this.isProperty = isProperty;
105     }
106
107     @Override
108     public String toString() {
109         return mieleID;
110     }
111
112     @Override
113     public String getMieleID() {
114         return mieleID;
115     }
116
117     @Override
118     public String getChannelID() {
119         return channelID;
120     }
121
122     @Override
123     public Class<? extends Type> getTypeClass() {
124         return typeClass;
125     }
126
127     @Override
128     public boolean isProperty() {
129         return isProperty;
130     }
131
132     @Override
133     public State getState(String s, DeviceMetaData dmd) {
134         if (dmd != null) {
135             String localizedValue = getMieleEnum(s, dmd);
136             if (localizedValue == null) {
137                 localizedValue = dmd.LocalizedValue;
138             }
139             if (localizedValue == null) {
140                 localizedValue = s;
141             }
142
143             return getState(localizedValue);
144         } else {
145             return getState(s);
146         }
147     }
148
149     public State getState(String s) {
150         try {
151             Method valueOf = typeClass.getMethod("valueOf", String.class);
152             State state = (State) valueOf.invoke(typeClass, s);
153             if (state != null) {
154                 return state;
155             }
156         } catch (Exception e) {
157             logger.error("An exception occurred while converting '{}' into a State", s);
158         }
159
160         return null;
161     }
162
163     public String getMieleEnum(String s, DeviceMetaData dmd) {
164         if (dmd.MieleEnum != null) {
165             for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
166                 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
167                     return enumEntry.getKey();
168                 }
169             }
170         }
171
172         return null;
173     }
174 }