]> git.basschouten.com Git - openhab-addons.git/blob
cf0354bbe040c67611b955ab31b4503c11c29bff
[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.units;
14
15 import static org.openhab.binding.omnilink.internal.OmnilinkBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.omnilink.internal.handler.UnitHandler;
20 import org.openhab.core.library.types.DecimalType;
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.thing.ThingStatus;
25 import org.openhab.core.thing.ThingStatusDetail;
26 import org.openhab.core.types.Command;
27 import org.openhab.core.types.RefreshType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.digitaldan.jomnilinkII.MessageTypes.CommandMessage;
32 import com.digitaldan.jomnilinkII.MessageTypes.statuses.ExtendedUnitStatus;
33
34 /**
35  * The {@link FlagHandler} defines some methods that are used to
36  * interface with an OmniLink Flag. This by extension also defines the
37  * Flag thing that openHAB will be able to pick up and interface with.
38  *
39  * @author Craig Hamilton - Initial contribution
40  * @author Ethan Dye - openHAB3 rewrite
41  */
42 @NonNullByDefault
43 public class FlagHandler extends UnitHandler {
44     private final Logger logger = LoggerFactory.getLogger(FlagHandler.class);
45     private final int thingID = getThingNumber();
46     public @Nullable String number;
47
48     public FlagHandler(Thing thing) {
49         super(thing);
50     }
51
52     @Override
53     public void handleCommand(ChannelUID channelUID, Command command) {
54         logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
55
56         if (command instanceof RefreshType) {
57             retrieveStatus().ifPresentOrElse(this::updateChannels, () -> updateStatus(ThingStatus.OFFLINE,
58                     ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Received null status update!"));
59             return;
60         }
61
62         switch (channelUID.getId()) {
63             case CHANNEL_FLAG_VALUE:
64                 if (command instanceof DecimalType) {
65                     sendOmnilinkCommand(CommandMessage.CMD_UNIT_SET_COUNTER, ((DecimalType) command).intValue(),
66                             thingID);
67                 } else {
68                     logger.debug("Invalid command: {}, must be DecimalType", command);
69                 }
70                 break;
71             case CHANNEL_FLAG_SWITCH:
72                 if (command instanceof OnOffType) {
73                     handleOnOff(channelUID, (OnOffType) command);
74                 } else {
75                     logger.debug("Invalid command: {}, must be OnOffType", command);
76                 }
77                 break;
78             default:
79                 logger.debug("Unknown channel for Flag thing: {}", channelUID);
80                 super.handleCommand(channelUID, command);
81         }
82     }
83
84     @Override
85     public void updateChannels(ExtendedUnitStatus status) {
86         logger.debug("updateChannels called for Flag status: {}", status);
87         updateState(CHANNEL_FLAG_VALUE, DecimalType.valueOf(Integer.toString(status.getStatus())));
88         updateState(CHANNEL_FLAG_SWITCH, OnOffType.from(status.getStatus() == 1));
89     }
90 }