]> git.basschouten.com Git - openhab-addons.git/blob
70340edb48bace34e7ca1a8de1879bcfdca10171
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.omnilink.internal.handler;
14
15 import static org.openhab.binding.omnilink.internal.OmnilinkBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.library.types.DecimalType;
19 import org.openhab.core.library.types.StringType;
20 import org.openhab.core.thing.ChannelUID;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.thing.ThingStatus;
23 import org.openhab.core.thing.ThingStatusDetail;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.RefreshType;
26 import org.openhab.core.types.UnDefType;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The {@link ConsoleHandler} defines some methods that are used to
32  * interface with an OmniLink Console. This by extension also defines the
33  * Console thing that openHAB will be able to pick up and interface with.
34  *
35  * @author Craig Hamilton - Initial contribution
36  * @author Ethan Dye - openHAB3 rewrite
37  */
38 @NonNullByDefault
39 public class ConsoleHandler extends AbstractOmnilinkHandler {
40     private final Logger logger = LoggerFactory.getLogger(ConsoleHandler.class);
41     private final int thingID = getThingNumber();
42
43     public ConsoleHandler(Thing thing) {
44         super(thing);
45     }
46
47     @Override
48     public void initialize() {
49         if (getOmnilinkBridgeHandler() != null) {
50             updateStatus(ThingStatus.ONLINE);
51             updateChannels();
52         } else {
53             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
54                     "Received null bridge while initializing Console!");
55         }
56     }
57
58     @Override
59     public void handleCommand(ChannelUID channelUID, Command command) {
60         logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
61
62         if (command instanceof RefreshType) {
63             updateChannels();
64             return;
65         }
66
67         switch (channelUID.getId()) {
68             case CHANNEL_CONSOLE_ENABLE_DISABLE_BEEPER:
69                 if (command instanceof StringType) {
70                     sendOmnilinkCommand(OmniLinkCmd.CMD_CONSOLE_ENABLE_DISABLE_BEEPER.getNumber(),
71                             ((StringType) command).equals(StringType.valueOf("OFF")) ? 0 : 1, thingID);
72                 } else {
73                     logger.debug("Invalid command: {}, must be StringType", command);
74                 }
75                 break;
76             case CHANNEL_CONSOLE_BEEP:
77                 if (command instanceof DecimalType) {
78                     sendOmnilinkCommand(OmniLinkCmd.CMD_CONSOLE_BEEP.getNumber(), ((DecimalType) command).intValue(),
79                             thingID);
80                 } else {
81                     logger.debug("Invalid command: {}, must be DecimalType", command);
82                 }
83                 break;
84             default:
85                 logger.warn("Unknown channel for Console thing: {}", channelUID);
86         }
87         updateChannels();
88     }
89
90     public void updateChannels() {
91         updateState(CHANNEL_CONSOLE_ENABLE_DISABLE_BEEPER, UnDefType.UNDEF);
92         updateState(CHANNEL_CONSOLE_BEEP, UnDefType.UNDEF);
93     }
94 }