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.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;
32 import com.google.gson.JsonElement;
35 * The {@link ApplianceChannelSelector} for dishwashers
37 * @author Karel Goderis - Initial contribution
38 * @author Kai Kreuzer - Changed START_TIME to DateTimeType
40 public enum DishwasherChannelSelector implements ApplianceChannelSelector {
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) {
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"));
56 date.setTime(Long.valueOf(s) * 60000);
57 } catch (Exception e) {
60 return getState(dateFormatter.format(date));
63 DURATION("duration", "duration", DateTimeType.class, false) {
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"));
70 date.setTime(Long.valueOf(s) * 60000);
71 } catch (Exception e) {
74 return getState(dateFormatter.format(date));
77 ELAPSED_TIME("elapsedTime", "elapsed", DateTimeType.class, false) {
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"));
84 date.setTime(Long.valueOf(s) * 60000);
85 } catch (Exception e) {
88 return getState(dateFormatter.format(date));
91 FINISH_TIME("finishTime", "finish", DateTimeType.class, false) {
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"));
98 date.setTime(Long.valueOf(s) * 60000);
99 } catch (Exception e) {
102 return getState(dateFormatter.format(date));
105 DOOR("signalDoor", "door", OpenClosedType.class, false) {
107 public State getState(String s, DeviceMetaData dmd) {
108 if ("true".equals(s)) {
109 return getState("OPEN");
112 if ("false".equals(s)) {
113 return getState("CLOSED");
116 return UnDefType.UNDEF;
119 SWITCH(null, "switch", OnOffType.class, false);
121 private final Logger logger = LoggerFactory.getLogger(DishwasherChannelSelector.class);
123 private final String mieleID;
124 private final String channelID;
125 private final Class<? extends Type> typeClass;
126 private final boolean isProperty;
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;
137 public String toString() {
142 public String getMieleID() {
147 public String getChannelID() {
152 public Class<? extends Type> getTypeClass() {
157 public boolean isProperty() {
162 public State getState(String s, DeviceMetaData dmd) {
164 String localizedValue = getMieleEnum(s, dmd);
165 if (localizedValue == null) {
166 localizedValue = dmd.LocalizedValue;
168 if (localizedValue == null) {
172 return getState(localizedValue);
178 public State getState(String s) {
180 Method valueOf = typeClass.getMethod("valueOf", String.class);
181 State state = (State) valueOf.invoke(typeClass, s);
185 } catch (Exception e) {
186 logger.error("An exception occurred while converting '{}' into a State", s);
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();