]> git.basschouten.com Git - openhab-addons.git/blob
4cc08c55a57332bfec08b2ac42c83b5e979f37e6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.cm11a.internal.handler;
14
15 import java.io.IOException;
16
17 import org.openhab.binding.cm11a.internal.InvalidAddressException;
18 import org.openhab.binding.cm11a.internal.X10Interface;
19 import org.openhab.core.library.types.OnOffType;
20 import org.openhab.core.thing.Bridge;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.RefreshType;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Handler for Appliance (also called Switch) modules. These modules only support ON and OFF states
33  *
34  * @author Bob Raker - Initial contribution
35  *
36  */
37 public class Cm11aApplianceHandler extends Cm11aAbstractHandler {
38
39     private final Logger logger = LoggerFactory.getLogger(Cm11aApplianceHandler.class);
40
41     private State desiredState = UnDefType.UNDEF;
42
43     public Cm11aApplianceHandler(Thing thing) {
44         super(thing);
45     }
46
47     @Override
48     public void handleCommand(ChannelUID channelUID, Command command) {
49         logger.debug("**** Cm11aApplianceHandler handleCommand command = {}, channelUID = {}", command,
50                 channelUID.getAsString());
51
52         x10Function = 0;
53         Bridge bridge = getBridge();
54         if (bridge == null) {
55             logger.debug("Unable to handle command. Bridge is null.");
56             return;
57         }
58         this.channelUID = channelUID;
59
60         Cm11aBridgeHandler cm11aHandler = (Cm11aBridgeHandler) bridge.getHandler();
61         if (cm11aHandler != null && cm11aHandler.getThing().getStatus().equals(ThingStatus.ONLINE)) {
62             if (command == OnOffType.ON) {
63                 x10Function = X10Interface.FUNC_ON;
64                 desiredState = OnOffType.ON;
65             } else if (command == OnOffType.OFF) {
66                 x10Function = X10Interface.FUNC_OFF;
67                 desiredState = OnOffType.OFF;
68             } else if (command instanceof RefreshType) {
69                 x10Function = X10Interface.FUNC_OFF;
70                 desiredState = OnOffType.OFF;
71                 logger.info("Received REFRESH command for switch {}", houseUnitCode);
72             }
73
74             if (x10Function > 0) {
75                 X10Interface x10Interface = cm11aHandler.getX10Interface();
76                 x10Interface.scheduleHWUpdate(this);
77             } else {
78                 logger.debug("Received invalid command for switch {} command: {}", houseUnitCode, command);
79             }
80         } else {
81             logger.debug("Attempted to change switch to {} for {} because the cm11a is not online", command,
82                     houseUnitCode);
83         }
84     }
85
86     @Override
87     public void updateHardware(X10Interface x10Interface) throws IOException, InvalidAddressException {
88         if (!desiredState.equals(currentState)) {
89             if (x10Interface.sendFunction(houseUnitCode, x10Function)) {
90                 // Hardware update was successful so update openHAB
91                 updateState(channelUID, desiredState);
92                 setCurrentState(desiredState);
93             }
94         }
95     }
96 }