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