]> git.basschouten.com Git - openhab-addons.git/blob
291d94296aa5229563247a3a09c800efd3649e01
[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 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;
23
24 /**
25  * The {@link VelbusMemoHandler} is responsible for handling commands, which are
26  * sent to one of the channels.
27  *
28  * @author Cedric Boon - Initial contribution
29  */
30 @NonNullByDefault
31 public abstract class VelbusMemoHandler extends VelbusThermostatHandler {
32     public static final int MEMO_TEXT_MAX_LENGTH = 63;
33
34     private final ChannelUID memoChannel = new ChannelUID(thing.getUID(), "oledDisplay", "MEMO");
35
36     public VelbusMemoHandler(Thing thing) {
37         super(thing, 4, new ChannelUID(thing.getUID(), "input", "CH33"));
38     }
39
40     @Override
41     public void handleCommand(ChannelUID channelUID, Command command) {
42         super.handleCommand(channelUID, command);
43
44         VelbusBridgeHandler velbusBridgeHandler = getVelbusBridgeHandler();
45         if (velbusBridgeHandler == null) {
46             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
47             return;
48         }
49
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.....)");
54
55             for (int i = 0; i < splittedMemoText.length; i++) {
56                 VelbusMemoTextPacket packet = new VelbusMemoTextPacket(getModuleAddress().getAddress(), (byte) (i * 5),
57                         splittedMemoText[i].toCharArray());
58
59                 byte[] packetBytes = packet.getBytes();
60                 velbusBridgeHandler.sendPacket(packetBytes);
61
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),
65                             new char[0]);
66
67                     packetBytes = packet.getBytes();
68                     velbusBridgeHandler.sendPacket(packetBytes);
69                 }
70             }
71         }
72     }
73 }