]> git.basschouten.com Git - openhab-addons.git/blob
4db8dbac3c4bd18e22c587db4f3a9ea23154890e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 static org.openhab.binding.miele.internal.MieleBindingConstants.MIELE_DEVICE_CLASS_DISHWASHER;
16 import static org.openhab.binding.miele.internal.MieleBindingConstants.POWER_CONSUMPTION_CHANNEL_ID;
17 import static org.openhab.binding.miele.internal.MieleBindingConstants.WATER_CONSUMPTION_CHANNEL_ID;
18
19 import java.math.BigDecimal;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.miele.internal.exceptions.MieleRpcException;
23 import org.openhab.core.i18n.LocaleProvider;
24 import org.openhab.core.i18n.TranslationProvider;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.RefreshType;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.google.gson.JsonElement;
36
37 /**
38  * The {@link DishWasherHandler} is responsible for handling commands,
39  * which are sent to one of the channels
40  *
41  * @author Karel Goderis - Initial contribution
42  * @author Kai Kreuzer - fixed handling of REFRESH commands
43  * @author Martin Lepsy - fixed handling of empty JSON results
44  * @author Jacob Laursen - Fixed multicast and protocol support (ZigBee/LAN), added power/water consumption channels
45  */
46 @NonNullByDefault
47 public class DishWasherHandler extends MieleApplianceHandler<DishwasherChannelSelector>
48         implements ExtendedDeviceStateListener {
49
50     private static final int POWER_CONSUMPTION_BYTE_POSITION = 16;
51     private static final int WATER_CONSUMPTION_BYTE_POSITION = 18;
52     private static final int EXTENDED_STATE_MIN_SIZE_BYTES = 19;
53
54     private final Logger logger = LoggerFactory.getLogger(DishWasherHandler.class);
55
56     public DishWasherHandler(Thing thing, TranslationProvider i18nProvider, LocaleProvider localeProvider) {
57         super(thing, i18nProvider, localeProvider, DishwasherChannelSelector.class, MIELE_DEVICE_CLASS_DISHWASHER);
58     }
59
60     @Override
61     public void handleCommand(ChannelUID channelUID, Command command) {
62         super.handleCommand(channelUID, command);
63
64         String channelID = channelUID.getId();
65         String applianceId = this.applianceId;
66         if (applianceId == null) {
67             logger.warn("Command '{}' failed, appliance id is unknown", command);
68             return;
69         }
70
71         DishwasherChannelSelector selector = (DishwasherChannelSelector) getValueSelectorFromChannelID(channelID);
72         JsonElement result = null;
73
74         try {
75             switch (selector) {
76                 case SWITCH: {
77                     MieleBridgeHandler bridgeHandler = getMieleBridgeHandler();
78                     if (bridgeHandler == null) {
79                         logger.warn("Command '{}' failed, missing bridge handler", command);
80                         return;
81                     }
82                     if (command.equals(OnOffType.ON)) {
83                         result = bridgeHandler.invokeOperation(applianceId, modelID, "start");
84                     } else if (command.equals(OnOffType.OFF)) {
85                         result = bridgeHandler.invokeOperation(applianceId, modelID, "stop");
86                     }
87                     break;
88                 }
89                 default: {
90                     if (!(command instanceof RefreshType)) {
91                         logger.debug("{} is a read-only channel that does not accept commands",
92                                 selector.getChannelID());
93                     }
94                 }
95             }
96             // process result
97             if (result != null && isResultProcessable(result)) {
98                 logger.debug("Result of operation is {}", result.getAsString());
99             }
100         } catch (IllegalArgumentException e) {
101             logger.warn(
102                     "An error occurred while trying to set the read-only variable associated with channel '{}' to '{}'",
103                     channelID, command.toString());
104         } catch (MieleRpcException e) {
105             Throwable cause = e.getCause();
106             if (cause == null) {
107                 logger.warn("An error occurred while trying to invoke operation: {}", e.getMessage());
108             } else {
109                 logger.warn("An error occurred while trying to invoke operation: {} -> {}", e.getMessage(),
110                         cause.getMessage());
111             }
112         }
113     }
114
115     public void onApplianceExtendedStateChanged(byte[] extendedDeviceState) {
116         if (extendedDeviceState.length < EXTENDED_STATE_MIN_SIZE_BYTES) {
117             logger.warn("Unexpected size of extended state: {}", extendedDeviceState);
118             return;
119         }
120
121         BigDecimal kiloWattHoursTenths = BigDecimal
122                 .valueOf(extendedDeviceState[POWER_CONSUMPTION_BYTE_POSITION] & 0xff);
123         var kiloWattHours = new QuantityType<>(kiloWattHoursTenths.divide(BigDecimal.valueOf(10)), Units.KILOWATT_HOUR);
124         updateExtendedState(POWER_CONSUMPTION_CHANNEL_ID, kiloWattHours);
125
126         BigDecimal decilitres = BigDecimal.valueOf(extendedDeviceState[WATER_CONSUMPTION_BYTE_POSITION] & 0xff);
127         var litres = new QuantityType<>(decilitres.divide(BigDecimal.valueOf(10)), Units.LITRE);
128         updateExtendedState(WATER_CONSUMPTION_CHANNEL_ID, litres);
129     }
130 }