]> git.basschouten.com Git - openhab-addons.git/blob
5902933e6dcacf2ad5905219dabf476dec8462aa
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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
17 import org.openhab.core.library.types.OnOffType;
18 import org.openhab.core.thing.ChannelUID;
19 import org.openhab.core.thing.Thing;
20 import org.openhab.core.types.Command;
21 import org.openhab.core.types.RefreshType;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.gson.JsonElement;
26
27 /**
28  * The {@link TumbleDryerHandler} is responsible for handling commands,
29  * which are sent to one of the channels
30  *
31  * @author Karel Goderis - Initial contribution
32  * @author Kai Kreuzer - fixed handling of REFRESH commands
33  * @author Martin Lepsy - fixed handling of empty JSON results
34  */
35 public class TumbleDryerHandler extends MieleApplianceHandler<TumbleDryerChannelSelector> {
36
37     private final Logger logger = LoggerFactory.getLogger(TumbleDryerHandler.class);
38
39     public TumbleDryerHandler(Thing thing) {
40         super(thing, TumbleDryerChannelSelector.class, "TumbleDryer");
41     }
42
43     @Override
44     public void handleCommand(ChannelUID channelUID, Command command) {
45         super.handleCommand(channelUID, command);
46
47         String channelID = channelUID.getId();
48         String uid = (String) getThing().getConfiguration().getProperties().get(APPLIANCE_ID);
49
50         TumbleDryerChannelSelector selector = (TumbleDryerChannelSelector) getValueSelectorFromChannelID(channelID);
51         JsonElement result = null;
52
53         try {
54             if (selector != null) {
55                 switch (selector) {
56                     case SWITCH: {
57                         if (command.equals(OnOffType.ON)) {
58                             result = bridgeHandler.invokeOperation(uid, modelID, "start");
59                         } else if (command.equals(OnOffType.OFF)) {
60                             result = bridgeHandler.invokeOperation(uid, modelID, "stop");
61                         }
62                         break;
63                     }
64                     default: {
65                         if (!(command instanceof RefreshType)) {
66                             logger.debug("{} is a read-only channel that does not accept commands",
67                                     selector.getChannelID());
68                         }
69                     }
70                 }
71             }
72             // process result
73             if (isResultProcessable(result)) {
74                 logger.debug("Result of operation is {}", result.getAsString());
75             }
76         } catch (IllegalArgumentException e) {
77             logger.warn(
78                     "An error occurred while trying to set the read-only variable associated with channel '{}' to '{}'",
79                     channelID, command.toString());
80         }
81     }
82 }