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 static org.openhab.binding.miele.internal.MieleBindingConstants.*;
17 import java.util.HashMap;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
23 import org.apache.commons.lang3.StringUtils;
24 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceClassObject;
25 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceMetaData;
26 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceProperty;
27 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.HomeDevice;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingStatusInfo;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.binding.BaseThingHandler;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.types.Command;
37 import org.openhab.core.types.RefreshType;
38 import org.openhab.core.types.UnDefType;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import com.google.gson.Gson;
43 import com.google.gson.JsonElement;
44 import com.google.gson.JsonObject;
45 import com.google.gson.JsonParser;
48 * The {@link MieleApplianceHandler} is an abstract class
49 * responsible for handling commands, which are sent to one
50 * of the channels of the appliance that understands/"talks"
51 * the {@link ApplianceChannelSelector} datapoints
53 * @author Karel Goderis - Initial contribution
54 * @author Martin Lepsy - Added check for JsonNull result
56 public abstract class MieleApplianceHandler<E extends Enum<E> & ApplianceChannelSelector> extends BaseThingHandler
57 implements ApplianceStatusListener {
59 private final Logger logger = LoggerFactory.getLogger(MieleApplianceHandler.class);
61 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Stream
62 .of(THING_TYPE_DISHWASHER, THING_TYPE_OVEN, THING_TYPE_FRIDGE, THING_TYPE_DRYER, THING_TYPE_HOB,
63 THING_TYPE_FRIDGEFREEZER, THING_TYPE_HOOD, THING_TYPE_WASHINGMACHINE, THING_TYPE_COFFEEMACHINE)
64 .collect(Collectors.toSet());
66 protected Gson gson = new Gson();
69 protected MieleBridgeHandler bridgeHandler;
70 private Class<E> selectorType;
71 protected String modelID;
73 protected Map<String, String> metaDataCache = new HashMap<>();
75 public MieleApplianceHandler(Thing thing, Class<E> selectorType, String modelID) {
77 this.selectorType = selectorType;
78 this.modelID = modelID;
81 public ApplianceChannelSelector getValueSelectorFromChannelID(String valueSelectorText)
82 throws IllegalArgumentException {
83 for (ApplianceChannelSelector c : selectorType.getEnumConstants()) {
84 if (c.getChannelID() != null && c.getChannelID().equals(valueSelectorText)) {
89 throw new IllegalArgumentException("Not valid value selector");
92 public ApplianceChannelSelector getValueSelectorFromMieleID(String valueSelectorText)
93 throws IllegalArgumentException {
94 for (ApplianceChannelSelector c : selectorType.getEnumConstants()) {
95 if (c.getMieleID() != null && c.getMieleID().equals(valueSelectorText)) {
100 throw new IllegalArgumentException("Not valid value selector");
104 public void initialize() {
105 logger.debug("Initializing Miele appliance handler.");
106 final String uid = (String) getThing().getConfiguration().getProperties().get(APPLIANCE_ID);
109 if (getMieleBridgeHandler() != null) {
110 ThingStatusInfo statusInfo = getBridge().getStatusInfo();
111 updateStatus(statusInfo.getStatus(), statusInfo.getStatusDetail(), statusInfo.getDescription());
116 public void onBridgeConnectionResumed() {
117 if (getMieleBridgeHandler() != null) {
118 ThingStatusInfo statusInfo = getBridge().getStatusInfo();
119 updateStatus(statusInfo.getStatus(), statusInfo.getStatusDetail(), statusInfo.getDescription());
124 public void dispose() {
125 logger.debug("Handler disposes. Unregistering listener.");
127 MieleBridgeHandler bridgeHandler = getMieleBridgeHandler();
128 if (bridgeHandler != null) {
129 getMieleBridgeHandler().unregisterApplianceStatusListener(this);
136 public void handleCommand(ChannelUID channelUID, Command command) {
137 // Here we could handle commands that are common to all Miele Appliances, but so far I don't know of any
138 if (command instanceof RefreshType) {
139 // Placeholder for future refinement
145 public void onApplianceStateChanged(String UID, DeviceClassObject dco) {
146 String myUID = (getThing().getProperties().get(PROTOCOL_PROPERTY_NAME))
147 + (String) getThing().getConfiguration().getProperties().get(APPLIANCE_ID);
148 String modelID = StringUtils.right(dco.DeviceClass,
149 dco.DeviceClass.length() - new String("com.miele.xgw3000.gateway.hdm.deviceclasses.Miele").length());
151 if (myUID.equals(UID)) {
152 if (modelID.equals(this.modelID)) {
153 for (JsonElement prop : dco.Properties.getAsJsonArray()) {
155 DeviceProperty dp = gson.fromJson(prop, DeviceProperty.class);
156 dp.Value = StringUtils.trim(dp.Value);
157 dp.Value = StringUtils.strip(dp.Value);
159 onAppliancePropertyChanged(UID, dp);
160 } catch (Exception p) {
161 // Ignore - this is due to an unrecognized and not yet reverse-engineered array property
169 public void onAppliancePropertyChanged(String UID, DeviceProperty dp) {
170 String myUID = (getThing().getProperties().get(PROTOCOL_PROPERTY_NAME))
171 + (String) getThing().getConfiguration().getProperties().get(APPLIANCE_ID);
173 if (myUID.equals(UID)) {
175 DeviceMetaData dmd = null;
176 if (dp.Metadata == null) {
177 String metadata = metaDataCache.get(new StringBuilder().append(dp.Name).toString().trim());
178 if (metadata != null) {
179 JsonObject jsonMetaData = (JsonObject) JsonParser.parseString(metadata);
180 dmd = gson.fromJson(jsonMetaData, DeviceMetaData.class);
181 // only keep the enum, if any - that's all we care for events we receive via multicast
182 // all other fields are nulled
183 dmd.LocalizedID = null;
184 dmd.LocalizedValue = null;
186 dmd.description = null;
189 if (dp.Metadata != null) {
190 String metadata = StringUtils.replace(dp.Metadata.toString(), "enum", "MieleEnum");
191 JsonObject jsonMetaData = (JsonObject) JsonParser.parseString(metadata);
192 dmd = gson.fromJson(jsonMetaData, DeviceMetaData.class);
193 metaDataCache.put(new StringBuilder().append(dp.Name).toString().trim(), metadata);
196 ApplianceChannelSelector selector = null;
198 selector = getValueSelectorFromMieleID(dp.Name);
199 } catch (Exception h) {
200 logger.trace("{} is not a valid channel for a {}", dp.Name, modelID);
203 String dpValue = StringUtils.trim(StringUtils.strip(dp.Value));
205 if (selector != null) {
206 if (!selector.isProperty()) {
207 ChannelUID theChannelUID = new ChannelUID(getThing().getUID(), selector.getChannelID());
209 if (dp.Value != null) {
210 logger.trace("Update state of {} with getState '{}'", theChannelUID,
211 selector.getState(dpValue, dmd));
212 updateState(theChannelUID, selector.getState(dpValue, dmd));
214 updateState(theChannelUID, UnDefType.UNDEF);
216 } else if (dpValue != null) {
217 logger.debug("Updating the property '{}' of '{}' to '{}'", selector.getChannelID(),
218 getThing().getUID(), selector.getState(dpValue, dmd).toString());
219 Map<String, String> properties = editProperties();
220 properties.put(selector.getChannelID(), selector.getState(dpValue, dmd).toString());
221 updateProperties(properties);
224 } catch (IllegalArgumentException e) {
225 logger.error("An exception occurred while processing a changed device property :'{}'", e.getMessage());
231 public void onApplianceRemoved(HomeDevice appliance) {
233 if (uid.equals(appliance.UID)) {
234 updateStatus(ThingStatus.OFFLINE);
240 public void onApplianceAdded(HomeDevice appliance) {
242 if (uid.equals(appliance.UID)) {
243 updateStatus(ThingStatus.ONLINE);
248 private synchronized MieleBridgeHandler getMieleBridgeHandler() {
249 if (this.bridgeHandler == null) {
250 Bridge bridge = getBridge();
251 if (bridge == null) {
254 ThingHandler handler = bridge.getHandler();
255 if (handler instanceof MieleBridgeHandler) {
256 this.bridgeHandler = (MieleBridgeHandler) handler;
257 this.bridgeHandler.registerApplianceStatusListener(this);
262 return this.bridgeHandler;
265 protected boolean isResultProcessable(JsonElement result) {
266 return result != null && !result.isJsonNull();