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.upb.internal.handler;
15 import static org.openhab.binding.upb.internal.message.Command.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.upb.internal.Constants;
20 import org.openhab.binding.upb.internal.message.MessageBuilder;
21 import org.openhab.binding.upb.internal.message.UPBMessage;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.thing.Channel;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.RefreshType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Thing handler for a virtual device.
35 * @author Marcus Better - Initial contribution
39 public class VirtualThingHandler extends UPBThingHandler {
41 private final Logger logger = LoggerFactory.getLogger(VirtualThingHandler.class);
43 public VirtualThingHandler(final Thing device, final @Nullable Byte defaultNetworkId) {
44 super(device, defaultNetworkId);
48 protected void pingDevice() {
49 // always succeeds for virtual device
50 updateStatus(ThingStatus.ONLINE);
54 public void handleCommand(final ChannelUID channelUID, final Command cmd) {
55 final PIMHandler pimHandler = getPIMHandler();
56 if (pimHandler == null) {
57 logger.info("DEV {}: received cmd {} but no bridge handler", unitId, cmd);
61 if (cmd == RefreshType.REFRESH) {
62 // there is no way to read the currently active scene
64 } else if (!(cmd instanceof DecimalType)) {
65 logger.info("channel {}: unsupported cmd {}", channelUID, cmd);
69 final MessageBuilder message;
70 if (channelUID.getId().equals(Constants.LINK_ACTIVATE_CHANNEL_ID)) {
71 message = MessageBuilder.forCommand(ACTIVATE);
72 } else if (channelUID.getId().equals(Constants.LINK_DEACTIVATE_CHANNEL_ID)) {
73 message = MessageBuilder.forCommand(DEACTIVATE);
75 logger.warn("channel {}: unexpected channel type", channelUID);
78 final byte dst = ((DecimalType) cmd).byteValue();
79 message.network(networkId).destination(dst).link(true);
80 pimHandler.sendPacket(message);
84 public void onMessageReceived(final UPBMessage msg) {
85 final byte linkId = msg.getDestination();
86 final String channelId;
87 switch (msg.getCommand()) {
89 channelId = Constants.LINK_ACTIVATE_CHANNEL_ID;
93 channelId = Constants.LINK_DEACTIVATE_CHANNEL_ID;
97 logger.info("DEV {}: Message {} ignored for link {}", unitId, linkId & 0xff, msg.getCommand());
100 final Channel ch = getThing().getChannel(channelId);
104 updateState(ch.getUID(), new DecimalType(linkId));