2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.omnilink.internal.handler.units;
15 import static org.openhab.binding.omnilink.internal.OmnilinkBindingConstants.*;
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;
31 import com.digitaldan.jomnilinkII.MessageTypes.CommandMessage;
32 import com.digitaldan.jomnilinkII.MessageTypes.statuses.ExtendedUnitStatus;
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.
39 * @author Craig Hamilton - Initial contribution
40 * @author Ethan Dye - openHAB3 rewrite
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;
48 public FlagHandler(Thing thing) {
53 public void handleCommand(ChannelUID channelUID, Command command) {
54 logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
56 if (command instanceof RefreshType) {
57 retrieveStatus().ifPresentOrElse(this::updateChannels, () -> updateStatus(ThingStatus.OFFLINE,
58 ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Received null status update!"));
62 switch (channelUID.getId()) {
63 case CHANNEL_FLAG_VALUE:
64 if (command instanceof DecimalType) {
65 sendOmnilinkCommand(CommandMessage.CMD_UNIT_SET_COUNTER, ((DecimalType) command).intValue(),
68 logger.debug("Invalid command: {}, must be DecimalType", command);
71 case CHANNEL_FLAG_SWITCH:
72 if (command instanceof OnOffType) {
73 handleOnOff(channelUID, (OnOffType) command);
75 logger.debug("Invalid command: {}, must be OnOffType", command);
79 logger.debug("Unknown channel for Flag thing: {}", channelUID);
80 super.handleCommand(channelUID, command);
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));