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.apache.commons.lang3.StringUtils;
26 import org.openhab.binding.miele.internal.ExtendedDeviceStateUtil;
27 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceMetaData;
28 import org.openhab.core.library.types.DateTimeType;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.library.types.OpenClosedType;
31 import org.openhab.core.library.types.QuantityType;
32 import org.openhab.core.library.types.StringType;
33 import org.openhab.core.types.State;
34 import org.openhab.core.types.Type;
35 import org.openhab.core.types.UnDefType;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import com.google.gson.JsonElement;
42 * The {@link ApplianceChannelSelector} for washing machines
44 * @author Karel Goderis - Initial contribution
45 * @author Kai Kreuzer - Changed START_TIME to DateTimeType
46 * @author Jacob Laursen - Added power/water consumption channels
48 public enum WashingMachineChannelSelector implements ApplianceChannelSelector {
50 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true, false),
51 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true, false),
52 BRAND_ID("brandId", "brandId", StringType.class, true, false),
53 COMPANY_ID("companyId", "companyId", StringType.class, true, false),
54 STATE("state", "state", StringType.class, false, false),
55 PROGRAMID("programId", "program", StringType.class, false, false),
56 PROGRAMTYPE("programType", "type", StringType.class, false, false),
57 PROGRAMPHASE("phase", "phase", StringType.class, false, false),
58 START_TIME("startTime", "start", DateTimeType.class, false, false) {
60 public State getState(String s, DeviceMetaData dmd) {
61 Date date = new Date();
62 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
63 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
65 date.setTime(Long.valueOf(s) * 60000);
66 } catch (Exception e) {
69 return getState(dateFormatter.format(date));
72 DURATION("duration", "duration", DateTimeType.class, false, false) {
74 public State getState(String s, DeviceMetaData dmd) {
75 Date date = new Date();
76 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
77 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
79 date.setTime(Long.valueOf(StringUtils.trim(s)) * 60000);
80 } catch (Exception e) {
83 return getState(dateFormatter.format(date));
86 ELAPSED_TIME("elapsedTime", "elapsed", DateTimeType.class, false, false) {
88 public State getState(String s, DeviceMetaData dmd) {
89 Date date = new Date();
90 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
91 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
93 date.setTime(Long.valueOf(s) * 60000);
94 } catch (Exception e) {
97 return getState(dateFormatter.format(date));
100 FINISH_TIME("finishTime", "finish", DateTimeType.class, false, false) {
102 public State getState(String s, DeviceMetaData dmd) {
103 Date date = new Date();
104 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
105 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
107 date.setTime(Long.valueOf(s) * 60000);
108 } catch (Exception e) {
111 return getState(dateFormatter.format(date));
114 TARGET_TEMP("targetTemperature", "target", QuantityType.class, false, false) {
116 public State getState(String s, DeviceMetaData dmd) {
117 return getTemperatureState(s);
120 SPINNING_SPEED("spinningSpeed", "spinningspeed", StringType.class, false, false) {
122 public State getState(String s, DeviceMetaData dmd) {
124 return getState("Without spinning");
126 if ("256".equals(s)) {
127 return getState("Rinsing");
129 return getState(Integer.toString((Integer.valueOf(s))));
132 DOOR("signalDoor", "door", OpenClosedType.class, false, false) {
135 public State getState(String s, DeviceMetaData dmd) {
136 if ("true".equals(s)) {
137 return getState("OPEN");
140 if ("false".equals(s)) {
141 return getState("CLOSED");
144 return UnDefType.UNDEF;
147 SWITCH(null, "switch", OnOffType.class, false, false),
148 POWER_CONSUMPTION(EXTENDED_DEVICE_STATE_PROPERTY_NAME, POWER_CONSUMPTION_CHANNEL_ID, QuantityType.class, false,
150 WATER_CONSUMPTION(EXTENDED_DEVICE_STATE_PROPERTY_NAME, WATER_CONSUMPTION_CHANNEL_ID, QuantityType.class, false,
153 private final Logger logger = LoggerFactory.getLogger(WashingMachineChannelSelector.class);
155 private final String mieleID;
156 private final String channelID;
157 private final Class<? extends Type> typeClass;
158 private final boolean isProperty;
159 private final boolean isExtendedState;
161 WashingMachineChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass,
162 boolean isProperty, boolean isExtendedState) {
163 this.mieleID = propertyID;
164 this.channelID = channelID;
165 this.typeClass = typeClass;
166 this.isProperty = isProperty;
167 this.isExtendedState = isExtendedState;
171 public String toString() {
176 public String getMieleID() {
181 public String getChannelID() {
186 public boolean isProperty() {
191 public boolean isExtendedState() {
192 return isExtendedState;
196 public State getState(String s, DeviceMetaData dmd) {
198 String localizedValue = getMieleEnum(s, dmd);
199 if (localizedValue == null) {
200 localizedValue = dmd.LocalizedValue;
202 if (localizedValue == null) {
206 return getState(localizedValue);
212 public State getState(String s) {
214 Method valueOf = typeClass.getMethod("valueOf", String.class);
215 State state = (State) valueOf.invoke(typeClass, s);
219 } catch (Exception e) {
220 logger.error("An exception occurred while converting '{}' into a State", s);
226 public State getTemperatureState(String s) {
228 return ExtendedDeviceStateUtil.getTemperatureState(s);
229 } catch (NumberFormatException e) {
230 logger.warn("An exception occurred while converting '{}' into a State", s);
231 return UnDefType.UNDEF;
235 public String getMieleEnum(String s, DeviceMetaData dmd) {
236 if (dmd.MieleEnum != null) {
237 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
238 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
239 return enumEntry.getKey();