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 ovens
38 * @author Karel Goderis - Initial contribution
39 * @author Kai Kreuzer - Changed START_TIME to DateTimeType
41 public enum OvenChannelSelector 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 PROGRAMPHASE("phase", "phase", StringType.class, false),
50 START_TIME("startTime", "start", DateTimeType.class, false) {
52 public State getState(String s, DeviceMetaData dmd) {
53 Date date = new Date();
54 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
55 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
57 date.setTime(Long.valueOf(s) * 60000);
58 } catch (Exception e) {
61 return getState(dateFormatter.format(date));
64 DURATION("duration", "duration", DateTimeType.class, false) {
66 public State getState(String s, DeviceMetaData dmd) {
67 Date date = new Date();
68 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
69 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
71 date.setTime(Long.valueOf(s) * 60000);
72 } catch (Exception e) {
75 return getState(dateFormatter.format(date));
78 ELAPSED_TIME("elapsedTime", "elapsed", DateTimeType.class, false) {
80 public State getState(String s, DeviceMetaData dmd) {
81 Date date = new Date();
82 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
83 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
85 date.setTime(Long.valueOf(s) * 60000);
86 } catch (Exception e) {
89 return getState(dateFormatter.format(date));
92 FINISH_TIME("finishTime", "finish", DateTimeType.class, false) {
94 public State getState(String s, DeviceMetaData dmd) {
95 Date date = new Date();
96 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
97 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
99 date.setTime(Long.valueOf(s) * 60000);
100 } catch (Exception e) {
103 return getState(dateFormatter.format(date));
106 TARGET_TEMP("targetTemperature", "target", DecimalType.class, false) {
108 public State getState(String s, DeviceMetaData dmd) {
112 MEASURED_TEMP("measuredTemperature", "measured", DecimalType.class, false) {
114 public State getState(String s, DeviceMetaData dmd) {
118 DEVICE_TEMP_ONE("deviceTemperature1", "temp1", DecimalType.class, false) {
120 public State getState(String s, DeviceMetaData dmd) {
124 DEVICE_TEMP_TWO("deviceTemperature2", "temp2", DecimalType.class, false) {
126 public State getState(String s, DeviceMetaData dmd) {
130 DOOR("signalDoor", "door", OpenClosedType.class, false) {
133 public State getState(String s, DeviceMetaData dmd) {
134 if ("true".equals(s)) {
135 return getState("OPEN");
138 if ("false".equals(s)) {
139 return getState("CLOSED");
142 return UnDefType.UNDEF;
145 STOP(null, "stop", OnOffType.class, false),
146 SWITCH(null, "switch", OnOffType.class, false);
148 private final Logger logger = LoggerFactory.getLogger(OvenChannelSelector.class);
150 private final String mieleID;
151 private final String channelID;
152 private final Class<? extends Type> typeClass;
153 private final boolean isProperty;
155 OvenChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty) {
156 this.mieleID = propertyID;
157 this.channelID = channelID;
158 this.typeClass = typeClass;
159 this.isProperty = isProperty;
163 public String toString() {
168 public String getMieleID() {
173 public String getChannelID() {
178 public Class<? extends Type> getTypeClass() {
183 public boolean isProperty() {
188 public State getState(String s, DeviceMetaData dmd) {
190 String localizedValue = getMieleEnum(s, dmd);
191 if (localizedValue == null) {
192 localizedValue = dmd.LocalizedValue;
194 if (localizedValue == null) {
198 return getState(localizedValue);
204 public State getState(String s) {
206 Method valueOf = typeClass.getMethod("valueOf", String.class);
207 State state = (State) valueOf.invoke(typeClass, s);
211 } catch (Exception e) {
212 logger.error("An exception occurred while converting '{}' into a State", s);
218 public String getMieleEnum(String s, DeviceMetaData dmd) {
219 if (dmd.MieleEnum != null) {
220 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
221 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
222 return enumEntry.getKey();