]> git.basschouten.com Git - openhab-addons.git/blob
6360b1f1393d7bedf0e505bf5420ff6c001de792
[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 State getState(String s, DeviceMetaData dmd) {
104         if (dmd != null) {
105             String localizedValue = getMieleEnum(s, dmd);
106             if (localizedValue == null) {
107                 localizedValue = dmd.LocalizedValue;
108             }
109             if (localizedValue == null) {
110                 localizedValue = s;
111             }
112
113             return getState(localizedValue);
114         } else {
115             return getState(s);
116         }
117     }
118
119     public State getState(String s) {
120         try {
121             Method valueOf = typeClass.getMethod("valueOf", String.class);
122             State state = (State) valueOf.invoke(typeClass, s);
123             if (state != null) {
124                 return state;
125             }
126         } catch (Exception e) {
127             logger.error("An exception occurred while converting '{}' into a State", s);
128         }
129
130         return null;
131     }
132
133     public String getMieleEnum(String s, DeviceMetaData dmd) {
134         if (dmd.MieleEnum != null) {
135             for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
136                 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
137                     return enumEntry.getKey();
138                 }
139             }
140         }
141
142         return null;
143     }
144 }