]> git.basschouten.com Git - openhab-addons.git/blob
8840d895d1e882dd844eae1624fe06ddd7d9d1f1
[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.upb.internal.handler;
14
15 import static org.openhab.binding.upb.internal.message.Command.*;
16
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;
31
32 /**
33  * Thing handler for a virtual device.
34  *
35  * @author Marcus Better - Initial contribution
36  *
37  */
38 @NonNullByDefault
39 public class VirtualThingHandler extends UPBThingHandler {
40
41     private final Logger logger = LoggerFactory.getLogger(VirtualThingHandler.class);
42
43     public VirtualThingHandler(final Thing device, final @Nullable Byte defaultNetworkId) {
44         super(device, defaultNetworkId);
45     }
46
47     @Override
48     protected void pingDevice() {
49         // always succeeds for virtual device
50         updateStatus(ThingStatus.ONLINE);
51     }
52
53     @Override
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);
58             return;
59         }
60
61         if (cmd == RefreshType.REFRESH) {
62             // there is no way to read the currently active scene
63             return;
64         } else if (!(cmd instanceof DecimalType)) {
65             logger.info("channel {}: unsupported cmd {}", channelUID, cmd);
66             return;
67         }
68
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);
74         } else {
75             logger.warn("channel {}: unexpected channel type", channelUID);
76             return;
77         }
78         final byte dst = ((DecimalType) cmd).byteValue();
79         message.network(networkId).destination(dst).link(true);
80         pimHandler.sendPacket(message);
81     }
82
83     @Override
84     public void onMessageReceived(final UPBMessage msg) {
85         final byte linkId = msg.getDestination();
86         final String channelId;
87         switch (msg.getCommand()) {
88             case ACTIVATE:
89                 channelId = Constants.LINK_ACTIVATE_CHANNEL_ID;
90                 break;
91
92             case DEACTIVATE:
93                 channelId = Constants.LINK_DEACTIVATE_CHANNEL_ID;
94                 break;
95
96             default:
97                 logger.info("DEV {}: Message {} ignored for link {}", unitId, linkId & 0xff, msg.getCommand());
98                 return;
99         }
100         final Channel ch = getThing().getChannel(channelId);
101         if (ch == null) {
102             return;
103         }
104         updateState(ch.getUID(), new DecimalType(linkId));
105     }
106 }