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