2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.miele.internal.handler;
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;
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.DecimalType;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.OpenClosedType;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.Type;
29 import org.openhab.core.types.UnDefType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import com.google.gson.JsonElement;
36 * The {@link ApplianceChannelSelector} for tumble dryers
38 * @author Karel Goderis - Initial contribution
39 * @author Kai Kreuzer - Changed START_TIME to DateTimeType
41 public enum TumbleDryerChannelSelector implements ApplianceChannelSelector {
43 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
44 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
45 BRAND_ID("brandId", "brandId", StringType.class, true),
46 COMPANY_ID("companyId", "companyId", StringType.class, true),
47 STATE("state", "state", StringType.class, false),
48 PROGRAMID("programId", "program", StringType.class, false),
49 PROGRAMTYPE("programType", "type", StringType.class, false),
50 PROGRAMPHASE("phase", "phase", StringType.class, false),
51 START_TIME("startTime", "start", DateTimeType.class, false) {
53 public State getState(String s, DeviceMetaData dmd) {
54 Date date = new Date();
55 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
56 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
58 date.setTime(Long.valueOf(s) * 60000);
59 } catch (Exception e) {
62 return getState(dateFormatter.format(date));
65 DURATION("duration", "duration", DateTimeType.class, false) {
67 public State getState(String s, DeviceMetaData dmd) {
68 Date date = new Date();
69 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
70 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
72 date.setTime(Long.valueOf(s) * 60000);
73 } catch (Exception e) {
76 return getState(dateFormatter.format(date));
79 ELAPSED_TIME("elapsedTime", "elapsed", DateTimeType.class, false) {
81 public State getState(String s, DeviceMetaData dmd) {
82 Date date = new Date();
83 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
84 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
86 date.setTime(Long.valueOf(s) * 60000);
87 } catch (Exception e) {
90 return getState(dateFormatter.format(date));
93 FINISH_TIME("finishTime", "finish", DateTimeType.class, false) {
95 public State getState(String s, DeviceMetaData dmd) {
96 Date date = new Date();
97 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
98 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
100 date.setTime(Long.valueOf(s) * 60000);
101 } catch (Exception e) {
104 return getState(dateFormatter.format(date));
107 DRYING_STEP("dryingStep", "step", DecimalType.class, false) {
109 public State getState(String s, DeviceMetaData dmd) {
113 DOOR("signalDoor", "door", OpenClosedType.class, false) {
116 public State getState(String s, DeviceMetaData dmd) {
117 if ("true".equals(s)) {
118 return getState("OPEN");
121 if ("false".equals(s)) {
122 return getState("CLOSED");
125 return UnDefType.UNDEF;
128 SWITCH(null, "switch", OnOffType.class, false);
130 private final Logger logger = LoggerFactory.getLogger(TumbleDryerChannelSelector.class);
132 private final String mieleID;
133 private final String channelID;
134 private final Class<? extends Type> typeClass;
135 private final boolean isProperty;
137 TumbleDryerChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass,
138 boolean isProperty) {
139 this.mieleID = propertyID;
140 this.channelID = channelID;
141 this.typeClass = typeClass;
142 this.isProperty = isProperty;
146 public String toString() {
151 public String getMieleID() {
156 public String getChannelID() {
161 public Class<? extends Type> getTypeClass() {
166 public boolean isProperty() {
171 public State getState(String s, DeviceMetaData dmd) {
173 String localizedValue = getMieleEnum(s, dmd);
174 if (localizedValue == null) {
175 localizedValue = dmd.LocalizedValue;
177 if (localizedValue == null) {
181 return getState(localizedValue);
187 public State getState(String s) {
189 Method valueOf = typeClass.getMethod("valueOf", String.class);
190 State state = (State) valueOf.invoke(typeClass, s);
194 } catch (Exception e) {
195 logger.error("An exception occurred while converting '{}' into a State", s);
201 public String getMieleEnum(String s, DeviceMetaData dmd) {
202 if (dmd.MieleEnum != null) {
203 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
204 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
205 return enumEntry.getKey();