]> git.basschouten.com Git - openhab-addons.git/blob
baf03769958c22c3a1d2d7cf7c3c4b90f440969c
[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.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.miele.internal.api.dto.DeviceProperty;
19 import org.openhab.binding.miele.internal.exceptions.MieleRpcException;
20 import org.openhab.core.i18n.LocaleProvider;
21 import org.openhab.core.i18n.TimeZoneProvider;
22 import org.openhab.core.i18n.TranslationProvider;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.types.Command;
27 import org.openhab.core.types.RefreshType;
28 import org.openhab.core.types.State;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import com.google.gson.JsonElement;
33
34 /**
35  * The {@link FridgeHandler} is responsible for handling commands,
36  * which are sent to one of the channels
37  *
38  * @author Karel Goderis - Initial contribution
39  * @author Martin Lepsy - fixed handling of empty JSON results
40  * @author Jacob Laursen - Fixed multicast and protocol support (ZigBee/LAN)
41  */
42 @NonNullByDefault
43 public class FridgeHandler extends MieleApplianceHandler<FridgeChannelSelector> {
44
45     private final Logger logger = LoggerFactory.getLogger(FridgeHandler.class);
46
47     public FridgeHandler(Thing thing, TranslationProvider i18nProvider, LocaleProvider localeProvider,
48             TimeZoneProvider timeZoneProvider) {
49         super(thing, i18nProvider, localeProvider, timeZoneProvider, FridgeChannelSelector.class,
50                 MIELE_DEVICE_CLASS_FRIDGE);
51     }
52
53     @Override
54     public void handleCommand(ChannelUID channelUID, Command command) {
55         super.handleCommand(channelUID, command);
56
57         String channelID = channelUID.getId();
58         String applianceId = this.applianceId;
59         if (applianceId == null) {
60             logger.warn("Command '{}' failed, appliance id is unknown", command);
61             return;
62         }
63
64         FridgeChannelSelector selector = (FridgeChannelSelector) getValueSelectorFromChannelID(channelID);
65         JsonElement result = null;
66
67         try {
68             MieleBridgeHandler bridgeHandler = getMieleBridgeHandler();
69             if (bridgeHandler == null) {
70                 logger.warn("Command '{}' failed, missing bridge handler", command);
71                 return;
72             }
73             switch (selector) {
74                 case SUPERCOOL: {
75                     if (command.equals(OnOffType.ON)) {
76                         result = bridgeHandler.invokeOperation(applianceId, modelID, "startSuperCooling");
77                     } else if (command.equals(OnOffType.OFF)) {
78                         result = bridgeHandler.invokeOperation(applianceId, modelID, "stopSuperCooling");
79                     }
80                     break;
81                 }
82                 case START: {
83                     if (command.equals(OnOffType.ON)) {
84                         result = bridgeHandler.invokeOperation(applianceId, modelID, "start");
85                     }
86                     break;
87                 }
88                 default: {
89                     if (!(command instanceof RefreshType)) {
90                         logger.debug("{} is a read-only channel that does not accept commands",
91                                 selector.getChannelID());
92                     }
93                 }
94             }
95             // process result
96             if (result != null && isResultProcessable(result)) {
97                 logger.debug("Result of operation is {}", result.getAsString());
98             }
99         } catch (IllegalArgumentException e) {
100             logger.warn(
101                     "An error occurred while trying to set the read-only variable associated with channel '{}' to '{}'",
102                     channelID, command.toString());
103         } catch (MieleRpcException e) {
104             Throwable cause = e.getCause();
105             if (cause == null) {
106                 logger.warn("An error occurred while trying to invoke operation: {}", e.getMessage());
107             } else {
108                 logger.warn("An error occurred while trying to invoke operation: {} -> {}", e.getMessage(),
109                         cause.getMessage());
110             }
111         }
112     }
113
114     @Override
115     public void onAppliancePropertyChanged(DeviceProperty dp) {
116         super.onAppliancePropertyChanged(dp);
117
118         if (!STATE_PROPERTY_NAME.equals(dp.Name)) {
119             return;
120         }
121
122         // Supercool is not exposed directly as property, but can be deduced from state.
123         ChannelUID channelUid = new ChannelUID(getThing().getUID(), SUPERCOOL_CHANNEL_ID);
124         State state = dp.Value.equals(String.valueOf(STATE_SUPER_COOLING)) ? OnOffType.ON : OnOffType.OFF;
125         logger.trace("Update state of {} to {} through '{}'", channelUid, state, dp.Name);
126         updateState(channelUid, state);
127     }
128 }