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