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.paradoxalarm.internal.communication.messages.zone;
15 import java.nio.ByteBuffer;
17 import org.openhab.binding.paradoxalarm.internal.communication.messages.IPayload;
18 import org.openhab.binding.paradoxalarm.internal.util.ParadoxUtil;
21 * @author Konstantin Polihronov - Initial contribution
23 public class ZoneCommandPayload implements IPayload {
25 private static final int BYTES_LENGTH = 31;
27 private static final byte MESSAGE_START = (byte) 0xD0;
28 private static final byte PAYLOAD_SIZE = 0x1f;
29 private static final byte ZONE_FLAG = 0x08; // "bypassed" flag (5th bit)
30 private static final byte[] EMPTY_TWO_BYTES = { 0, 0 };
31 private static final byte CHECKSUM = 0;
33 private int zoneNumber;
34 private ZoneCommand command;
36 public ZoneCommandPayload(int zoneNumber, ZoneCommand command) {
37 this.zoneNumber = zoneNumber;
38 this.command = command;
42 public byte[] getBytes() {
43 byte[] bufferArray = new byte[BYTES_LENGTH];
44 ByteBuffer buf = ByteBuffer.wrap(bufferArray);
45 buf.put(MESSAGE_START);
46 buf.put(PAYLOAD_SIZE);
48 buf.put(command.getCommand());
49 buf.put(EMPTY_TWO_BYTES);
50 buf.put(calculateMessageBytes());
51 buf.put(ParadoxUtil.calculateChecksum(bufferArray));
56 * The total zone message consists of 24 bytes (8 bits each) which results of 192 bits for each zone in case of
58 * The low nible of each byte represents the first 4 zones of each group as a bit, the high nible is
59 * the second 4 zones. The zone groups are considered every 8 zones represented by a byte in this array (1-8,9-16,
62 * Example: So if we address zone 1 for example the value of first byte will be 0x01, for zone 2 - 0x02, for zone 3
64 * (third bit set to 1), for zone 4 - 0x08(fourth bit set to 1),<br>
65 * for zone 5 - 0x10, for zone 6 - 0x20, for zone 7 - 0x40, for zone 8 - 0x80.<br>
66 * For examples see TestGetBytes.java
68 * @return 24 bytes array with the needed zone to be set to bypass/clear bypass
70 private byte[] calculateMessageBytes() {
71 byte[] zoneMessage = new byte[24];
72 int byteIndex = (zoneNumber - 1) / 8;
73 byte zoneByteGroup = zoneMessage[byteIndex];
74 int bitNumber = calculateBitNumber();
75 zoneMessage[byteIndex] = ParadoxUtil.setBit(zoneByteGroup, bitNumber - 1, 1);
79 private int calculateBitNumber() {
80 int residual = zoneNumber % 8;
81 return residual != 0 ? residual : 8;