]> git.basschouten.com Git - openhab-addons.git/blob
406a7bb044ae32bdb289be39fdd6d174e5c6f55d
[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.zone;
14
15 import java.nio.ByteBuffer;
16
17 import org.openhab.binding.paradoxalarm.internal.communication.messages.IPayload;
18 import org.openhab.binding.paradoxalarm.internal.util.ParadoxUtil;
19
20 /**
21  * @author Konstantin Polihronov - Initial contribution
22  */
23 public class ZoneCommandPayload implements IPayload {
24
25     private static final int BYTES_LENGTH = 31;
26
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;
32
33     private int zoneNumber;
34     private ZoneCommand command;
35
36     public ZoneCommandPayload(int zoneNumber, ZoneCommand command) {
37         this.zoneNumber = zoneNumber;
38         this.command = command;
39     }
40
41     @Override
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);
47         buf.put(ZONE_FLAG);
48         buf.put(command.getCommand());
49         buf.put(EMPTY_TWO_BYTES);
50         buf.put(calculateMessageBytes());
51         buf.put(ParadoxUtil.calculateChecksum(bufferArray));
52         return bufferArray;
53     }
54
55     /**
56      * The total zone message consists of 24 bytes (8 bits each) which results of 192 bits for each zone in case of
57      * Evo192 (i.e. 24x8).
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,
60      * 17-24, etc)<br>
61      *
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
63      * - 0x04
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
67      *
68      * @return 24 bytes array with the needed zone to be set to bypass/clear bypass
69      */
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);
76         return zoneMessage;
77     }
78
79     private int calculateBitNumber() {
80         int residual = zoneNumber % 8;
81         return residual != 0 ? residual : 8;
82     }
83 }