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.partition;
15 import java.nio.ByteBuffer;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.paradoxalarm.internal.communication.messages.IPayload;
21 * The {@link PartitionCommandPayload} Class that structures the payload for partition commands.
23 * @author Konstantin Polihronov - Initial contribution
26 public class PartitionCommandPayload implements IPayload {
28 private static final int BYTES_LENGTH = 15;
30 private static final byte MESSAGE_START = 0x40;
31 private static final byte PAYLOAD_SIZE = 0x0f;
32 private static final byte[] EMPTY_FOUR_BYTES = { 0, 0, 0, 0 };
33 private static final byte CHECKSUM = 0;
35 private final int partitionNumber;
36 private final PartitionCommand command;
38 public PartitionCommandPayload(int partitionNumber, PartitionCommand command) {
39 this.partitionNumber = partitionNumber;
40 this.command = command;
44 public byte[] getBytes() {
45 byte[] bufferArray = new byte[BYTES_LENGTH];
46 ByteBuffer buf = ByteBuffer.wrap(bufferArray);
47 buf.put(MESSAGE_START);
48 buf.put(PAYLOAD_SIZE);
49 buf.put(EMPTY_FOUR_BYTES);
50 buf.put(calculateMessageBytes());
51 buf.put(EMPTY_FOUR_BYTES);
57 * The message bytes contain nibbles of command information. First byte, first nibble is partition 1, first byte,
58 * second nibble is partition 2, second byte, first nibble is partition 3, etc...
60 * For command values that are set in byte nibbles, see PartitionCommand enum
62 private byte[] calculateMessageBytes() {
63 byte[] result = { 0, 0, 0, 0 };
64 int index = (partitionNumber - 1) / 2;
65 result[index] = (byte) (calculateNibbleToSet() & 0xff);
69 private int calculateNibbleToSet() {
70 if ((partitionNumber - 1) % 2 == 0) {
71 return (command.getCommand() << 4) & 0xF0;
73 return command.getCommand() & 0x0F;