2 * Copyright (c) 2010-2021 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 static org.openhab.binding.miele.internal.MieleBindingConstants.EXTENDED_DEVICE_STATE_PROPERTY_NAME;
16 import static org.openhab.binding.miele.internal.MieleBindingConstants.POWER_CONSUMPTION_CHANNEL_ID;
17 import static org.openhab.binding.miele.internal.MieleBindingConstants.WATER_CONSUMPTION_CHANNEL_ID;
19 import java.lang.reflect.Method;
20 import java.text.SimpleDateFormat;
21 import java.util.Date;
22 import java.util.Map.Entry;
23 import java.util.TimeZone;
25 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceMetaData;
26 import org.openhab.core.library.types.DateTimeType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.OpenClosedType;
29 import org.openhab.core.library.types.QuantityType;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.core.types.State;
32 import org.openhab.core.types.Type;
33 import org.openhab.core.types.UnDefType;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import com.google.gson.JsonElement;
40 * The {@link ApplianceChannelSelector} for dishwashers
42 * @author Karel Goderis - Initial contribution
43 * @author Kai Kreuzer - Changed START_TIME to DateTimeType
44 * @author Jacob Laursen - Added power/water consumption channels
46 public enum DishwasherChannelSelector implements ApplianceChannelSelector {
48 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true, false),
49 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true, false),
50 BRAND_ID("brandId", "brandId", StringType.class, true, false),
51 COMPANY_ID("companyId", "companyId", StringType.class, true, false),
52 STATE("state", "state", StringType.class, false, false),
53 PROGRAMID("programId", "program", StringType.class, false, false),
54 PROGRAMPHASE("phase", "phase", StringType.class, false, false),
55 START_TIME("startTime", "start", DateTimeType.class, false, false) {
57 public State getState(String s, DeviceMetaData dmd) {
58 Date date = new Date();
59 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
60 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
62 date.setTime(Long.valueOf(s) * 60000);
63 } catch (Exception e) {
66 return getState(dateFormatter.format(date));
69 DURATION("duration", "duration", DateTimeType.class, false, false) {
71 public State getState(String s, DeviceMetaData dmd) {
72 Date date = new Date();
73 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
74 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
76 date.setTime(Long.valueOf(s) * 60000);
77 } catch (Exception e) {
80 return getState(dateFormatter.format(date));
83 ELAPSED_TIME("elapsedTime", "elapsed", DateTimeType.class, false, false) {
85 public State getState(String s, DeviceMetaData dmd) {
86 Date date = new Date();
87 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
88 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
90 date.setTime(Long.valueOf(s) * 60000);
91 } catch (Exception e) {
94 return getState(dateFormatter.format(date));
97 FINISH_TIME("finishTime", "finish", DateTimeType.class, false, false) {
99 public State getState(String s, DeviceMetaData dmd) {
100 Date date = new Date();
101 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
102 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
104 date.setTime(Long.valueOf(s) * 60000);
105 } catch (Exception e) {
108 return getState(dateFormatter.format(date));
111 DOOR("signalDoor", "door", OpenClosedType.class, false, false) {
113 public State getState(String s, DeviceMetaData dmd) {
114 if ("true".equals(s)) {
115 return getState("OPEN");
118 if ("false".equals(s)) {
119 return getState("CLOSED");
122 return UnDefType.UNDEF;
125 SWITCH(null, "switch", OnOffType.class, false, false),
126 POWER_CONSUMPTION(EXTENDED_DEVICE_STATE_PROPERTY_NAME, POWER_CONSUMPTION_CHANNEL_ID, QuantityType.class, false,
128 WATER_CONSUMPTION(EXTENDED_DEVICE_STATE_PROPERTY_NAME, WATER_CONSUMPTION_CHANNEL_ID, QuantityType.class, false,
131 private final Logger logger = LoggerFactory.getLogger(DishwasherChannelSelector.class);
133 private final String mieleID;
134 private final String channelID;
135 private final Class<? extends Type> typeClass;
136 private final boolean isProperty;
137 private final boolean isExtendedState;
139 DishwasherChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty,
140 boolean isExtendedState) {
141 this.mieleID = propertyID;
142 this.channelID = channelID;
143 this.typeClass = typeClass;
144 this.isProperty = isProperty;
145 this.isExtendedState = isExtendedState;
149 public String toString() {
154 public String getMieleID() {
159 public String getChannelID() {
164 public Class<? extends Type> getTypeClass() {
169 public boolean isProperty() {
174 public boolean isExtendedState() {
175 return isExtendedState;
179 public State getState(String s, DeviceMetaData dmd) {
181 String localizedValue = getMieleEnum(s, dmd);
182 if (localizedValue == null) {
183 localizedValue = dmd.LocalizedValue;
185 if (localizedValue == null) {
189 return getState(localizedValue);
195 public State getState(String s) {
197 Method valueOf = typeClass.getMethod("valueOf", String.class);
198 State state = (State) valueOf.invoke(typeClass, s);
202 } catch (Exception e) {
203 logger.error("An exception occurred while converting '{}' into a State", s);
209 public String getMieleEnum(String s, DeviceMetaData dmd) {
210 if (dmd.MieleEnum != null) {
211 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
212 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
213 return enumEntry.getKey();