]> git.basschouten.com Git - openhab-addons.git/blob
f44b5008436a38d9366a181288d3639303284700
[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.text.SimpleDateFormat;
17 import java.util.Date;
18 import java.util.Map.Entry;
19 import java.util.TimeZone;
20
21 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceMetaData;
22 import org.openhab.core.library.types.DateTimeType;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.OpenClosedType;
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 dishwashers
36  *
37  * @author Karel Goderis - Initial contribution
38  * @author Kai Kreuzer - Changed START_TIME to DateTimeType
39  */
40 public enum DishwasherChannelSelector implements ApplianceChannelSelector {
41
42     PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
43     DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
44     BRAND_ID("brandId", "brandId", StringType.class, true),
45     COMPANY_ID("companyId", "companyId", StringType.class, true),
46     STATE("state", "state", StringType.class, false),
47     PROGRAMID("programId", "program", StringType.class, false),
48     PROGRAMPHASE("phase", "phase", StringType.class, false),
49     START_TIME("startTime", "start", DateTimeType.class, false) {
50         @Override
51         public State getState(String s, DeviceMetaData dmd) {
52             Date date = new Date();
53             SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
54             dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
55             try {
56                 date.setTime(Long.valueOf(s) * 60000);
57             } catch (Exception e) {
58                 date.setTime(0);
59             }
60             return getState(dateFormatter.format(date));
61         }
62     },
63     DURATION("duration", "duration", DateTimeType.class, false) {
64         @Override
65         public State getState(String s, DeviceMetaData dmd) {
66             Date date = new Date();
67             SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
68             dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
69             try {
70                 date.setTime(Long.valueOf(s) * 60000);
71             } catch (Exception e) {
72                 date.setTime(0);
73             }
74             return getState(dateFormatter.format(date));
75         }
76     },
77     ELAPSED_TIME("elapsedTime", "elapsed", DateTimeType.class, false) {
78         @Override
79         public State getState(String s, DeviceMetaData dmd) {
80             Date date = new Date();
81             SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
82             dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
83             try {
84                 date.setTime(Long.valueOf(s) * 60000);
85             } catch (Exception e) {
86                 date.setTime(0);
87             }
88             return getState(dateFormatter.format(date));
89         }
90     },
91     FINISH_TIME("finishTime", "finish", DateTimeType.class, false) {
92         @Override
93         public State getState(String s, DeviceMetaData dmd) {
94             Date date = new Date();
95             SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
96             dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
97             try {
98                 date.setTime(Long.valueOf(s) * 60000);
99             } catch (Exception e) {
100                 date.setTime(0);
101             }
102             return getState(dateFormatter.format(date));
103         }
104     },
105     DOOR("signalDoor", "door", OpenClosedType.class, false) {
106         @Override
107         public State getState(String s, DeviceMetaData dmd) {
108             if ("true".equals(s)) {
109                 return getState("OPEN");
110             }
111
112             if ("false".equals(s)) {
113                 return getState("CLOSED");
114             }
115
116             return UnDefType.UNDEF;
117         }
118     },
119     SWITCH(null, "switch", OnOffType.class, false);
120
121     private final Logger logger = LoggerFactory.getLogger(DishwasherChannelSelector.class);
122
123     private final String mieleID;
124     private final String channelID;
125     private final Class<? extends Type> typeClass;
126     private final boolean isProperty;
127
128     DishwasherChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass,
129             boolean isProperty) {
130         this.mieleID = propertyID;
131         this.channelID = channelID;
132         this.typeClass = typeClass;
133         this.isProperty = isProperty;
134     }
135
136     @Override
137     public String toString() {
138         return mieleID;
139     }
140
141     @Override
142     public String getMieleID() {
143         return mieleID;
144     }
145
146     @Override
147     public String getChannelID() {
148         return channelID;
149     }
150
151     @Override
152     public Class<? extends Type> getTypeClass() {
153         return typeClass;
154     }
155
156     @Override
157     public boolean isProperty() {
158         return isProperty;
159     }
160
161     @Override
162     public State getState(String s, DeviceMetaData dmd) {
163         if (dmd != null) {
164             String localizedValue = getMieleEnum(s, dmd);
165             if (localizedValue == null) {
166                 localizedValue = dmd.LocalizedValue;
167             }
168             if (localizedValue == null) {
169                 localizedValue = s;
170             }
171
172             return getState(localizedValue);
173         } else {
174             return getState(s);
175         }
176     }
177
178     public State getState(String s) {
179         try {
180             Method valueOf = typeClass.getMethod("valueOf", String.class);
181             State state = (State) valueOf.invoke(typeClass, s);
182             if (state != null) {
183                 return state;
184             }
185         } catch (Exception e) {
186             logger.error("An exception occurred while converting '{}' into a State", s);
187         }
188
189         return null;
190     }
191
192     public String getMieleEnum(String s, DeviceMetaData dmd) {
193         if (dmd.MieleEnum != null) {
194             for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
195                 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
196                     return enumEntry.getKey();
197                 }
198             }
199         }
200
201         return null;
202     }
203 }