]> git.basschouten.com Git - openhab-addons.git/blob
35491260ea583575b2d3501f72f77924ce89f530
[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.MIELE_DEVICE_CLASS_HOOD;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.miele.internal.exceptions.MieleRpcException;
19 import org.openhab.core.i18n.LocaleProvider;
20 import org.openhab.core.i18n.TranslationProvider;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.types.Command;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import com.google.gson.JsonElement;
29
30 /**
31  * The {@link HoodHandler} is responsible for handling commands,
32  * which are sent to one of the channels
33  *
34  * @author Karel Goderis - Initial contribution
35  * @author Kai Kreuzer - fixed handling of REFRESH commands
36  * @author Martin Lepsy - fixed handling of empty JSON results
37  * @author Jacob Laursen - Fixed multicast and protocol support (ZigBee/LAN)
38  **/
39 @NonNullByDefault
40 public class HoodHandler extends MieleApplianceHandler<HoodChannelSelector> {
41
42     private final Logger logger = LoggerFactory.getLogger(HoodHandler.class);
43
44     public HoodHandler(Thing thing, TranslationProvider i18nProvider, LocaleProvider localeProvider) {
45         super(thing, i18nProvider, localeProvider, HoodChannelSelector.class, MIELE_DEVICE_CLASS_HOOD);
46     }
47
48     @Override
49     public void handleCommand(ChannelUID channelUID, Command command) {
50         super.handleCommand(channelUID, command);
51
52         String channelID = channelUID.getId();
53         String applianceId = this.applianceId;
54         if (applianceId == null) {
55             logger.warn("Command '{}' failed, appliance id is unknown", command);
56             return;
57         }
58
59         HoodChannelSelector selector = (HoodChannelSelector) getValueSelectorFromChannelID(channelID);
60         JsonElement result = null;
61
62         try {
63             MieleBridgeHandler bridgeHandler = getMieleBridgeHandler();
64             if (bridgeHandler == null) {
65                 logger.warn("Command '{}' failed, missing bridge handler", command);
66                 return;
67             }
68             switch (selector) {
69                 case LIGHT: {
70                     if (command.equals(OnOffType.ON)) {
71                         result = bridgeHandler.invokeOperation(applianceId, modelID, "startLighting");
72                     } else if (command.equals(OnOffType.OFF)) {
73                         result = bridgeHandler.invokeOperation(applianceId, modelID, "stopLighting");
74                     }
75                     break;
76                 }
77                 case STOP: {
78                     if (command.equals(OnOffType.ON)) {
79                         result = bridgeHandler.invokeOperation(applianceId, modelID, "stop");
80                     }
81                     break;
82                 }
83                 default: {
84                     logger.debug("{} is a read-only channel that does not accept commands", selector.getChannelID());
85                 }
86             }
87             // process result
88             if (result != null && isResultProcessable(result)) {
89                 logger.debug("Result of operation is {}", result.getAsString());
90             }
91         } catch (IllegalArgumentException e) {
92             logger.warn(
93                     "An error occurred while trying to set the read-only variable associated with channel '{}' to '{}'",
94                     channelID, command.toString());
95         } catch (MieleRpcException e) {
96             Throwable cause = e.getCause();
97             if (cause == null) {
98                 logger.warn("An error occurred while trying to invoke operation: {}", e.getMessage());
99             } else {
100                 logger.warn("An error occurred while trying to invoke operation: {} -> {}", e.getMessage(),
101                         cause.getMessage());
102             }
103         }
104     }
105 }