]> git.basschouten.com Git - openhab-addons.git/blob
46942aca0cfbe31168b9869067c861ba39a29d9f
[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.velbus.internal.handler;
14
15 import static org.openhab.binding.velbus.internal.VelbusBindingConstants.*;
16
17 import java.util.Arrays;
18 import java.util.HashSet;
19 import java.util.Set;
20
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;
31
32 /**
33  * The {@link VelbusVMBGPOHandler} is responsible for handling commands, which are
34  * sent to one of the channels.
35  *
36  * @author Cedric Boon - Initial contribution
37  */
38 @NonNullByDefault
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));
42
43     public static final int MODULESETTINGS_MEMORY_ADDRESS = 0x02F0;
44     public static final int LAST_MEMORY_LOCATION_ADDRESS = 0x1A03;
45
46     private final ChannelUID screensaverChannel = new ChannelUID(thing.getUID(), "oledDisplay", "SCREENSAVER");
47
48     private byte moduleSettings;
49
50     public VelbusVMBGPOHandler(Thing thing) {
51         super(thing);
52     }
53
54     @Override
55     public void handleCommand(ChannelUID channelUID, Command command) {
56         super.handleCommand(channelUID, command);
57
58         VelbusBridgeHandler velbusBridgeHandler = getVelbusBridgeHandler();
59         if (velbusBridgeHandler == null) {
60             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
61             return;
62         }
63
64         if (channelUID.equals(screensaverChannel) && command instanceof RefreshType) {
65             sendReadMemoryPacket(velbusBridgeHandler, MODULESETTINGS_MEMORY_ADDRESS);
66         }
67
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);
73         }
74     }
75
76     @Override
77     public void onPacketReceived(byte[] packet) {
78         super.onPacketReceived(packet);
79
80         logger.trace("onPacketReceived() was called");
81
82         if (packet[0] == VelbusPacket.STX && packet.length >= 5) {
83             byte command = packet[4];
84
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] };
93
94                 for (int i = 0; i < data.length; i++) {
95
96                     if ((memoryAddress + i) == MODULESETTINGS_MEMORY_ADDRESS) {
97                         this.moduleSettings = data[i];
98                         OnOffType state = ((this.moduleSettings & 0x80) != 0x00) ? OnOffType.ON : OnOffType.OFF;
99                         updateState(screensaverChannel, state);
100                     }
101                 }
102             }
103         }
104     }
105 }