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 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 PROGRAMTYPE("programType", "type", StringType.class, false),
50 PROGRAMPHASE("phase", "phase", StringType.class, false),
51 START_TIME("startTime", "start", DateTimeType.class, false) {
53 public State getState(String s, DeviceMetaData dmd) {
54 Date date = new Date();
55 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
56 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
58 date.setTime(Long.valueOf(s) * 60000);
59 } catch (Exception e) {
62 return getState(dateFormatter.format(date));
65 DURATION("duration", "duration", DateTimeType.class, false) {
67 public State getState(String s, DeviceMetaData dmd) {
68 Date date = new Date();
69 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
70 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
72 date.setTime(Long.valueOf(s) * 60000);
73 } catch (Exception e) {
76 return getState(dateFormatter.format(date));
79 ELAPSED_TIME("elapsedTime", "elapsed", DateTimeType.class, false) {
81 public State getState(String s, DeviceMetaData dmd) {
82 Date date = new Date();
83 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
84 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
86 date.setTime(Long.valueOf(s) * 60000);
87 } catch (Exception e) {
90 return getState(dateFormatter.format(date));
93 FINISH_TIME("finishTime", "finish", DateTimeType.class, false) {
95 public State getState(String s, DeviceMetaData dmd) {
96 Date date = new Date();
97 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
98 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
100 date.setTime(Long.valueOf(s) * 60000);
101 } catch (Exception e) {
104 return getState(dateFormatter.format(date));
107 TARGET_TEMP("targetTemperature", "target", DecimalType.class, false) {
109 public State getState(String s, DeviceMetaData dmd) {
113 MEASURED_TEMP("measuredTemperature", "measured", DecimalType.class, false) {
115 public State getState(String s, DeviceMetaData dmd) {
119 DEVICE_TEMP_ONE("deviceTemperature1", "temp1", DecimalType.class, false) {
121 public State getState(String s, DeviceMetaData dmd) {
125 DEVICE_TEMP_TWO("deviceTemperature2", "temp2", DecimalType.class, false) {
127 public State getState(String s, DeviceMetaData dmd) {
131 DOOR("signalDoor", "door", OpenClosedType.class, false) {
134 public State getState(String s, DeviceMetaData dmd) {
135 if ("true".equals(s)) {
136 return getState("OPEN");
139 if ("false".equals(s)) {
140 return getState("CLOSED");
143 return UnDefType.UNDEF;
146 STOP(null, "stop", OnOffType.class, false),
147 SWITCH(null, "switch", OnOffType.class, false);
149 private final Logger logger = LoggerFactory.getLogger(OvenChannelSelector.class);
151 private final String mieleID;
152 private final String channelID;
153 private final Class<? extends Type> typeClass;
154 private final boolean isProperty;
156 OvenChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty) {
157 this.mieleID = propertyID;
158 this.channelID = channelID;
159 this.typeClass = typeClass;
160 this.isProperty = isProperty;
164 public String toString() {
169 public String getMieleID() {
174 public String getChannelID() {
179 public Class<? extends Type> getTypeClass() {
184 public boolean isProperty() {
189 public boolean isExtendedState() {
194 public State getState(String s, DeviceMetaData dmd) {
196 String localizedValue = getMieleEnum(s, dmd);
197 if (localizedValue == null) {
198 localizedValue = dmd.LocalizedValue;
200 if (localizedValue == null) {
204 return getState(localizedValue);
210 public State getState(String s) {
212 Method valueOf = typeClass.getMethod("valueOf", String.class);
213 State state = (State) valueOf.invoke(typeClass, s);
217 } catch (Exception e) {
218 logger.error("An exception occurred while converting '{}' into a State", s);
224 public String getMieleEnum(String s, DeviceMetaData dmd) {
225 if (dmd.MieleEnum != null) {
226 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
227 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
228 return enumEntry.getKey();