]> git.basschouten.com Git - openhab-addons.git/blob
37c18cbcdc485237068a3360df639e2f9b272b65
[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.paradoxalarm.internal.communication.messages.partition;
14
15 import java.nio.ByteBuffer;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.paradoxalarm.internal.communication.messages.IPayload;
19
20 /**
21  * The {@link PartitionCommandPayload} Class that structures the payload for partition commands.
22  *
23  * @author Konstantin Polihronov - Initial contribution
24  */
25 @NonNullByDefault
26 public class PartitionCommandPayload implements IPayload {
27
28     private static final int BYTES_LENGTH = 15;
29
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;
34
35     private final int partitionNumber;
36     private final PartitionCommand command;
37
38     public PartitionCommandPayload(int partitionNumber, PartitionCommand command) {
39         this.partitionNumber = partitionNumber;
40         this.command = command;
41     }
42
43     @Override
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);
52         buf.put(CHECKSUM);
53         return bufferArray;
54     }
55
56     /*
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...
59      *
60      * For command values that are set in byte nibbles, see PartitionCommand enum
61      */
62     private byte[] calculateMessageBytes() {
63         byte[] result = { 0, 0, 0, 0 };
64         int index = (partitionNumber - 1) / 2;
65         result[index] = (byte) (calculateNibbleToSet() & 0xff);
66         return result;
67     }
68
69     private int calculateNibbleToSet() {
70         if ((partitionNumber - 1) % 2 == 0) {
71             return (command.getCommand() << 4) & 0xF0;
72         } else {
73             return command.getCommand() & 0x0F;
74         }
75     }
76 }