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.apache.commons.lang.StringUtils;
22 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceMetaData;
23 import org.openhab.core.library.types.DateTimeType;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.OpenClosedType;
27 import org.openhab.core.library.types.StringType;
28 import org.openhab.core.types.State;
29 import org.openhab.core.types.Type;
30 import org.openhab.core.types.UnDefType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import com.google.gson.JsonElement;
37 * The {@link ApplianceChannelSelector} for washing machines
39 * @author Karel Goderis - Initial contribution
40 * @author Kai Kreuzer - Changed START_TIME to DateTimeType
42 public enum WashingMachineChannelSelector implements ApplianceChannelSelector {
44 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
45 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
46 BRAND_ID("brandId", "brandId", StringType.class, true),
47 COMPANY_ID("companyId", "companyId", StringType.class, true),
48 STATE("state", "state", StringType.class, false),
49 PROGRAMID("programId", "program", StringType.class, false),
50 PROGRAMTYPE("programType", "type", StringType.class, false),
51 PROGRAMPHASE("phase", "phase", StringType.class, false),
52 START_TIME("startTime", "start", DateTimeType.class, false) {
54 public State getState(String s, DeviceMetaData dmd) {
55 Date date = new Date();
56 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
57 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
59 date.setTime(Long.valueOf(s) * 60000);
60 } catch (Exception e) {
63 return getState(dateFormatter.format(date));
66 DURATION("duration", "duration", DateTimeType.class, false) {
68 public State getState(String s, DeviceMetaData dmd) {
69 Date date = new Date();
70 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
71 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
73 date.setTime(Long.valueOf(StringUtils.trim(s)) * 60000);
74 } catch (Exception e) {
77 return getState(dateFormatter.format(date));
80 ELAPSED_TIME("elapsedTime", "elapsed", DateTimeType.class, false) {
82 public State getState(String s, DeviceMetaData dmd) {
83 Date date = new Date();
84 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
85 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
87 date.setTime(Long.valueOf(s) * 60000);
88 } catch (Exception e) {
91 return getState(dateFormatter.format(date));
94 FINISH_TIME("finishTime", "finish", DateTimeType.class, false) {
96 public State getState(String s, DeviceMetaData dmd) {
97 Date date = new Date();
98 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
99 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
101 date.setTime(Long.valueOf(s) * 60000);
102 } catch (Exception e) {
105 return getState(dateFormatter.format(date));
108 TARGET_TEMP("targetTemperature", "target", DecimalType.class, false) {
110 public State getState(String s, DeviceMetaData dmd) {
114 SPINNING_SPEED("spinningSpeed", "spinningspeed", StringType.class, false) {
116 public State getState(String s, DeviceMetaData dmd) {
118 return getState("Without spinning");
120 if ("256".equals(s)) {
121 return getState("Rinsing");
123 return getState(Integer.toString((Integer.valueOf(s) * 10)));
126 DOOR("signalDoor", "door", OpenClosedType.class, false) {
129 public State getState(String s, DeviceMetaData dmd) {
130 if ("true".equals(s)) {
131 return getState("OPEN");
134 if ("false".equals(s)) {
135 return getState("CLOSED");
138 return UnDefType.UNDEF;
141 SWITCH(null, "switch", OnOffType.class, false);
143 private final Logger logger = LoggerFactory.getLogger(WashingMachineChannelSelector.class);
145 private final String mieleID;
146 private final String channelID;
147 private final Class<? extends Type> typeClass;
148 private final boolean isProperty;
150 WashingMachineChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass,
151 boolean isProperty) {
152 this.mieleID = propertyID;
153 this.channelID = channelID;
154 this.typeClass = typeClass;
155 this.isProperty = isProperty;
159 public String toString() {
164 public String getMieleID() {
169 public String getChannelID() {
174 public Class<? extends Type> getTypeClass() {
179 public boolean isProperty() {
184 public State getState(String s, DeviceMetaData dmd) {
186 String localizedValue = getMieleEnum(s, dmd);
187 if (localizedValue == null) {
188 localizedValue = dmd.LocalizedValue;
190 if (localizedValue == null) {
194 return getState(localizedValue);
200 public State getState(String s) {
202 Method valueOf = typeClass.getMethod("valueOf", String.class);
203 State state = (State) valueOf.invoke(typeClass, s);
207 } catch (Exception e) {
208 logger.error("An exception occurred while converting '{}' into a State", s);
214 public String getMieleEnum(String s, DeviceMetaData dmd) {
215 if (dmd.MieleEnum != null) {
216 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
217 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
218 return enumEntry.getKey();