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.velbus.internal.handler;
15 import static org.openhab.binding.velbus.internal.VelbusBindingConstants.*;
17 import java.util.Arrays;
18 import java.util.HashSet;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.velbus.internal.packets.VelbusPacket;
23 import org.openhab.core.library.types.OnOffType;
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.thing.ThingStatusDetail;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.RefreshType;
33 * The {@link VelbusVMBGPOHandler} is responsible for handling commands, which are
34 * sent to one of the channels.
36 * @author Cedric Boon - Initial contribution
39 public class VelbusVMBGPOHandler extends VelbusMemoHandler {
40 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>(
41 Arrays.asList(THING_TYPE_VMBGPO, THING_TYPE_VMBGPOD, THING_TYPE_VMBGPOD_2));
43 public static final int MODULESETTINGS_MEMORY_ADDRESS = 0x02F0;
44 public static final int LAST_MEMORY_LOCATION_ADDRESS = 0x1A03;
46 private final ChannelUID screensaverChannel = new ChannelUID(thing.getUID(), "oledDisplay", "SCREENSAVER");
48 private byte moduleSettings;
50 public VelbusVMBGPOHandler(Thing thing) {
55 public void handleCommand(ChannelUID channelUID, Command command) {
56 super.handleCommand(channelUID, command);
58 VelbusBridgeHandler velbusBridgeHandler = getVelbusBridgeHandler();
59 if (velbusBridgeHandler == null) {
60 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
64 if (channelUID.equals(screensaverChannel) && command instanceof RefreshType) {
65 sendReadMemoryPacket(velbusBridgeHandler, MODULESETTINGS_MEMORY_ADDRESS);
68 if (channelUID.equals(screensaverChannel) && command instanceof OnOffType) {
69 byte screenSaverOnOffByte = (byte) ((command == OnOffType.ON) ? 0x80 : 0x00);
70 moduleSettings = (byte) (screenSaverOnOffByte | (moduleSettings & 0x7F));
71 sendWriteMemoryPacket(velbusBridgeHandler, MODULESETTINGS_MEMORY_ADDRESS, moduleSettings);
72 sendWriteMemoryPacket(velbusBridgeHandler, LAST_MEMORY_LOCATION_ADDRESS, (byte) 0xFF);
77 public void onPacketReceived(byte[] packet) {
78 super.onPacketReceived(packet);
80 logger.trace("onPacketReceived() was called");
82 if (packet[0] == VelbusPacket.STX && packet.length >= 5) {
83 byte command = packet[4];
85 if ((command == COMMAND_MEMORY_DATA_BLOCK && packet.length >= 11)
86 || (command == COMMAND_MEMORY_DATA && packet.length >= 8)) {
87 byte highMemoryAddress = packet[5];
88 byte lowMemoryAddress = packet[6];
89 int memoryAddress = ((highMemoryAddress & 0xff) << 8) | (lowMemoryAddress & 0xff);
90 byte[] data = (command == COMMAND_MEMORY_DATA_BLOCK)
91 ? new byte[] { packet[7], packet[8], packet[9], packet[10] }
92 : new byte[] { packet[7] };
94 for (int i = 0; i < data.length; i++) {
96 if ((memoryAddress + i) == MODULESETTINGS_MEMORY_ADDRESS) {
97 this.moduleSettings = data[i];
98 OnOffType state = OnOffType.from((this.moduleSettings & 0x80) != 0x00);
99 updateState(screensaverChannel, state);