]> git.basschouten.com Git - openhab-addons.git/blob
2395f847091e8041cb09b599bb176e16a865f8f3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.miele.internal.handler;
14
15 import java.lang.reflect.Method;
16 import java.util.Map.Entry;
17
18 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceMetaData;
19 import org.openhab.core.library.types.OnOffType;
20 import org.openhab.core.library.types.OpenClosedType;
21 import org.openhab.core.library.types.StringType;
22 import org.openhab.core.types.State;
23 import org.openhab.core.types.Type;
24 import org.openhab.core.types.UnDefType;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import com.google.gson.JsonElement;
29
30 /**
31  * The {@link ApplianceChannelSelector} for coffee machines
32  *
33  * @author Stephan Esch - Initial contribution
34  */
35 public enum CoffeeMachineChannelSelector implements ApplianceChannelSelector {
36
37     PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
38     DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
39     BRAND_ID("brandId", "brandId", StringType.class, true),
40     COMPANY_ID("companyId", "companyId", StringType.class, true),
41     STATE("state", "state", StringType.class, false),
42     PROGRAMID("programId", "program", StringType.class, false),
43     PROGRAMTYPE("programType", "type", StringType.class, false),
44     PROGRAMPHASE("phase", "phase", StringType.class, false),
45     // lightingStatus signalFailure signalInfo
46     DOOR("signalDoor", "door", OpenClosedType.class, false) {
47         @Override
48         public State getState(String s, DeviceMetaData dmd) {
49             if ("true".equals(s)) {
50                 return getState("OPEN");
51             }
52
53             if ("false".equals(s)) {
54                 return getState("CLOSED");
55             }
56
57             return UnDefType.UNDEF;
58         }
59     },
60     SWITCH(null, "switch", OnOffType.class, false);
61
62     private final Logger logger = LoggerFactory.getLogger(CoffeeMachineChannelSelector.class);
63
64     private final String mieleID;
65     private final String channelID;
66     private final Class<? extends Type> typeClass;
67     private final boolean isProperty;
68
69     CoffeeMachineChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass,
70             boolean isProperty) {
71         this.mieleID = propertyID;
72         this.channelID = channelID;
73         this.typeClass = typeClass;
74         this.isProperty = isProperty;
75     }
76
77     @Override
78     public String toString() {
79         return mieleID;
80     }
81
82     @Override
83     public String getMieleID() {
84         return mieleID;
85     }
86
87     @Override
88     public String getChannelID() {
89         return channelID;
90     }
91
92     @Override
93     public Class<? extends Type> getTypeClass() {
94         return typeClass;
95     }
96
97     @Override
98     public boolean isProperty() {
99         return isProperty;
100     }
101
102     @Override
103     public boolean isExtendedState() {
104         return false;
105     }
106
107     @Override
108     public State getState(String s, DeviceMetaData dmd) {
109         if (dmd != null) {
110             String localizedValue = getMieleEnum(s, dmd);
111             if (localizedValue == null) {
112                 localizedValue = dmd.LocalizedValue;
113             }
114             if (localizedValue == null) {
115                 localizedValue = s;
116             }
117
118             return getState(localizedValue);
119         } else {
120             return getState(s);
121         }
122     }
123
124     public State getState(String s) {
125         try {
126             Method valueOf = typeClass.getMethod("valueOf", String.class);
127             State state = (State) valueOf.invoke(typeClass, s);
128             if (state != null) {
129                 return state;
130             }
131         } catch (Exception e) {
132             logger.error("An exception occurred while converting '{}' into a State", s);
133         }
134
135         return null;
136     }
137
138     public String getMieleEnum(String s, DeviceMetaData dmd) {
139         if (dmd.MieleEnum != null) {
140             for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
141                 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
142                     return enumEntry.getKey();
143                 }
144             }
145         }
146
147         return null;
148     }
149 }