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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.velbus.internal.packets.VelbusMemoTextPacket;
17 import org.openhab.core.library.types.StringType;
18 import org.openhab.core.thing.ChannelUID;
19 import org.openhab.core.thing.Thing;
20 import org.openhab.core.thing.ThingStatus;
21 import org.openhab.core.thing.ThingStatusDetail;
22 import org.openhab.core.types.Command;
25 * The {@link VelbusMemoHandler} is responsible for handling commands, which are
26 * sent to one of the channels.
28 * @author Cedric Boon - Initial contribution
31 public abstract class VelbusMemoHandler extends VelbusThermostatHandler {
32 public static final int MEMO_TEXT_MAX_LENGTH = 63;
34 private final ChannelUID memoChannel = new ChannelUID(thing.getUID(), "oledDisplay", "MEMO");
36 public VelbusMemoHandler(Thing thing) {
37 super(thing, 4, new ChannelUID(thing.getUID(), "input", "CH33"));
41 public void handleCommand(ChannelUID channelUID, Command command) {
42 super.handleCommand(channelUID, command);
44 VelbusBridgeHandler velbusBridgeHandler = getVelbusBridgeHandler();
45 if (velbusBridgeHandler == null) {
46 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
50 if (channelUID.equals(memoChannel) && command instanceof StringType) {
51 String memoText = ((StringType) command).toFullString();
52 String trucatedMemoText = memoText.substring(0, Math.min(memoText.length(), MEMO_TEXT_MAX_LENGTH));
53 String[] splittedMemoText = trucatedMemoText.split("(?<=\\G.....)");
55 for (int i = 0; i < splittedMemoText.length; i++) {
56 VelbusMemoTextPacket packet = new VelbusMemoTextPacket(getModuleAddress().getAddress(), (byte) (i * 5),
57 splittedMemoText[i].toCharArray());
59 byte[] packetBytes = packet.getBytes();
60 velbusBridgeHandler.sendPacket(packetBytes);
62 // The last character must be zero
63 if ((((i * 5) + 5) >= trucatedMemoText.length()) && (splittedMemoText[i].length() == 5)) {
64 packet = new VelbusMemoTextPacket(getModuleAddress().getAddress(), (byte) ((i + 1) * 5),
67 packetBytes = packet.getBytes();
68 velbusBridgeHandler.sendPacket(packetBytes);