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