]> git.basschouten.com Git - openhab-addons.git/blob
c7952402f66a84b87e9735463309a11833ddfc50
[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 decimalCommand) {
65                     sendOmnilinkCommand(CommandMessage.CMD_UNIT_SET_COUNTER, decimalCommand.intValue(), thingID);
66                 } else {
67                     logger.debug("Invalid command: {}, must be DecimalType", command);
68                 }
69                 break;
70             case CHANNEL_FLAG_SWITCH:
71                 if (command instanceof OnOffType onOffCommand) {
72                     handleOnOff(channelUID, onOffCommand);
73                 } else {
74                     logger.debug("Invalid command: {}, must be OnOffType", command);
75                 }
76                 break;
77             default:
78                 logger.debug("Unknown channel for Flag thing: {}", channelUID);
79                 super.handleCommand(channelUID, command);
80         }
81     }
82
83     @Override
84     public void updateChannels(ExtendedUnitStatus status) {
85         logger.debug("updateChannels called for Flag status: {}", status);
86         updateState(CHANNEL_FLAG_VALUE, DecimalType.valueOf(Integer.toString(status.getStatus())));
87         updateState(CHANNEL_FLAG_SWITCH, OnOffType.from(status.getStatus() == 1));
88     }
89 }