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 = (String) getThing().getConfiguration().getProperties().get(APPLIANCE_ID);
147 String modelID = StringUtils.right(dco.DeviceClass,
148 dco.DeviceClass.length() - new String("com.miele.xgw3000.gateway.hdm.deviceclasses.Miele").length());
150 if (myUID.equals(UID)) {
151 if (modelID.equals(this.modelID)) {
152 for (JsonElement prop : dco.Properties.getAsJsonArray()) {
154 DeviceProperty dp = gson.fromJson(prop, DeviceProperty.class);
155 dp.Value = StringUtils.trim(dp.Value);
156 dp.Value = StringUtils.strip(dp.Value);
158 onAppliancePropertyChanged(UID, dp);
159 } catch (Exception p) {
160 // Ignore - this is due to an unrecognized and not yet reverse-engineered array property
168 public void onAppliancePropertyChanged(String UID, DeviceProperty dp) {
169 String myUID = (String) getThing().getConfiguration().getProperties().get(APPLIANCE_ID);
171 if (myUID.equals(UID)) {
173 DeviceMetaData dmd = null;
174 if (dp.Metadata == null) {
175 String metadata = metaDataCache.get(new StringBuilder().append(dp.Name).toString().trim());
176 if (metadata != null) {
177 JsonObject jsonMetaData = (JsonObject) JsonParser.parseString(metadata);
178 dmd = gson.fromJson(jsonMetaData, DeviceMetaData.class);
179 // only keep the enum, if any - that's all we care for events we receive via multicast
180 // all other fields are nulled
181 dmd.LocalizedID = null;
182 dmd.LocalizedValue = null;
184 dmd.description = null;
187 if (dp.Metadata != null) {
188 String metadata = StringUtils.replace(dp.Metadata.toString(), "enum", "MieleEnum");
189 JsonObject jsonMetaData = (JsonObject) JsonParser.parseString(metadata);
190 dmd = gson.fromJson(jsonMetaData, DeviceMetaData.class);
191 metaDataCache.put(new StringBuilder().append(dp.Name).toString().trim(), metadata);
194 ApplianceChannelSelector selector = null;
196 selector = getValueSelectorFromMieleID(dp.Name);
197 } catch (Exception h) {
198 logger.trace("{} is not a valid channel for a {}", dp.Name, modelID);
201 String dpValue = StringUtils.trim(StringUtils.strip(dp.Value));
203 if (selector != null) {
204 if (!selector.isProperty()) {
205 ChannelUID theChannelUID = new ChannelUID(getThing().getUID(), selector.getChannelID());
207 if (dp.Value != null) {
208 logger.trace("Update state of {} with getState '{}'", theChannelUID,
209 selector.getState(dpValue, dmd));
210 updateState(theChannelUID, selector.getState(dpValue, dmd));
212 updateState(theChannelUID, UnDefType.UNDEF);
214 } else if (dpValue != null) {
215 logger.debug("Updating the property '{}' of '{}' to '{}'", selector.getChannelID(),
216 getThing().getUID(), selector.getState(dpValue, dmd).toString());
217 Map<String, String> properties = editProperties();
218 properties.put(selector.getChannelID(), selector.getState(dpValue, dmd).toString());
219 updateProperties(properties);
222 } catch (IllegalArgumentException e) {
223 logger.error("An exception occurred while processing a changed device property :'{}'", e.getMessage());
229 public void onApplianceRemoved(HomeDevice appliance) {
231 if (uid.equals(appliance.getApplianceId())) {
232 updateStatus(ThingStatus.OFFLINE);
238 public void onApplianceAdded(HomeDevice appliance) {
240 if (uid.equals(appliance.getApplianceId())) {
241 Map<String, String> properties = editProperties();
242 properties.put(PROTOCOL_PROPERTY_NAME, appliance.getProtocol());
243 updateProperties(properties);
245 updateStatus(ThingStatus.ONLINE);
250 private synchronized MieleBridgeHandler getMieleBridgeHandler() {
251 if (this.bridgeHandler == null) {
252 Bridge bridge = getBridge();
253 if (bridge == null) {
256 ThingHandler handler = bridge.getHandler();
257 if (handler instanceof MieleBridgeHandler) {
258 this.bridgeHandler = (MieleBridgeHandler) handler;
259 this.bridgeHandler.registerApplianceStatusListener(this);
264 return this.bridgeHandler;
267 protected boolean isResultProcessable(JsonElement result) {
268 return result != null && !result.isJsonNull();