]> git.basschouten.com Git - openhab-addons.git/blob
1100b9f8ac26d45ed776f7923d7445d921f9150c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.*;
16
17 import java.math.BigDecimal;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.miele.internal.api.dto.DeviceProperty;
21 import org.openhab.binding.miele.internal.exceptions.MieleRpcException;
22 import org.openhab.core.i18n.LocaleProvider;
23 import org.openhab.core.i18n.TimeZoneProvider;
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 WashingMachineHandler} 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 WashingMachineHandler extends MieleApplianceHandler<WashingMachineChannelSelector>
48         implements ExtendedDeviceStateListener {
49
50     private static final int ENERGY_CONSUMPTION_BYTE_POSITION = 51;
51     private static final int WATER_CONSUMPTION_BYTE_POSITION = 53;
52     private static final int EXTENDED_STATE_MIN_SIZE_BYTES = 54;
53
54     private final Logger logger = LoggerFactory.getLogger(WashingMachineHandler.class);
55
56     public WashingMachineHandler(Thing thing, TranslationProvider i18nProvider, LocaleProvider localeProvider,
57             TimeZoneProvider timeZoneProvider) {
58         super(thing, i18nProvider, localeProvider, timeZoneProvider, WashingMachineChannelSelector.class,
59                 MIELE_DEVICE_CLASS_WASHING_MACHINE);
60     }
61
62     @Override
63     public void handleCommand(ChannelUID channelUID, Command command) {
64         super.handleCommand(channelUID, command);
65
66         String channelID = channelUID.getId();
67         String applianceId = this.applianceId;
68         if (applianceId == null) {
69             logger.warn("Command '{}' failed, appliance id is unknown", command);
70             return;
71         }
72
73         WashingMachineChannelSelector selector = (WashingMachineChannelSelector) getValueSelectorFromChannelID(
74                 channelID);
75         JsonElement result = null;
76
77         try {
78             switch (selector) {
79                 case SWITCH: {
80                     MieleBridgeHandler bridgeHandler = getMieleBridgeHandler();
81                     if (bridgeHandler == null) {
82                         logger.warn("Command '{}' failed, missing bridge handler", command);
83                         return;
84                     }
85                     if (command.equals(OnOffType.ON)) {
86                         result = bridgeHandler.invokeOperation(applianceId, modelID, "start");
87                     } else if (command.equals(OnOffType.OFF)) {
88                         result = bridgeHandler.invokeOperation(applianceId, modelID, "stop");
89                     }
90                     break;
91                 }
92                 default: {
93                     if (!(command instanceof RefreshType)) {
94                         logger.debug("{} is a read-only channel that does not accept commands",
95                                 selector.getChannelID());
96                     }
97                 }
98             }
99             // process result
100             if (result != null && isResultProcessable(result)) {
101                 logger.debug("Result of operation is {}", result.getAsString());
102             }
103         } catch (IllegalArgumentException e) {
104             logger.warn(
105                     "An error occurred while trying to set the read-only variable associated with channel '{}' to '{}'",
106                     channelID, command.toString());
107         } catch (MieleRpcException e) {
108             Throwable cause = e.getCause();
109             if (cause == null) {
110                 logger.warn("An error occurred while trying to invoke operation: {}", e.getMessage());
111             } else {
112                 logger.warn("An error occurred while trying to invoke operation: {} -> {}", e.getMessage(),
113                         cause.getMessage());
114             }
115         }
116     }
117
118     @Override
119     public void onAppliancePropertyChanged(DeviceProperty dp) {
120         super.onAppliancePropertyChanged(dp);
121         updateSwitchStartStopFromState(dp);
122     }
123
124     @Override
125     public void onApplianceExtendedStateChanged(byte[] extendedDeviceState) {
126         if (extendedDeviceState.length < EXTENDED_STATE_MIN_SIZE_BYTES) {
127             logger.debug("Insufficient extended state data to extract consumption values: {}", extendedDeviceState);
128             return;
129         }
130
131         BigDecimal kiloWattHoursTenths = BigDecimal
132                 .valueOf(extendedDeviceState[ENERGY_CONSUMPTION_BYTE_POSITION] & 0xff);
133         var kiloWattHours = new QuantityType<>(kiloWattHoursTenths.divide(BigDecimal.valueOf(10)), Units.KILOWATT_HOUR);
134         updateExtendedState(ENERGY_CONSUMPTION_CHANNEL_ID, kiloWattHours);
135
136         var litres = new QuantityType<>(BigDecimal.valueOf(extendedDeviceState[WATER_CONSUMPTION_BYTE_POSITION] & 0xff),
137                 Units.LITRE);
138         updateExtendedState(WATER_CONSUMPTION_CHANNEL_ID, litres);
139     }
140 }