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