]> git.basschouten.com Git - openhab-addons.git/blob
8c99b2e7f8b8ecaef0d211c332de5085bb012892
[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.elerotransmitterstick.internal.stick;
14
15 import org.openhab.core.util.HexUtils;
16
17 /**
18  * @author Volker Bier - Initial contribution
19  */
20 public class CommandPacket {
21     public static final byte EASY_CHECK = (byte) 0x4A;
22     public static final byte EASY_SEND = (byte) 0x4C;
23     public static final byte EASY_INFO = (byte) 0x4E;
24
25     byte[] data;
26
27     public CommandPacket(byte[] bytes) {
28         data = new byte[bytes.length + 1];
29         System.arraycopy(bytes, 0, data, 0, bytes.length);
30
31         data[bytes.length] = checksum(bytes);
32     }
33
34     public byte[] getBytes() {
35         return data;
36     }
37
38     public long getResponseTimeout() {
39         if (isEasyCheck()) {
40             return 1000;
41         }
42
43         return 4000;
44     }
45
46     private byte checksum(byte[] data) {
47         long val = 0;
48
49         for (byte b : data) {
50             val += b;
51         }
52
53         val = val % 256;
54         return (byte) (256 - val);
55     }
56
57     public boolean isEasyCheck() {
58         return data[2] == EASY_CHECK;
59     }
60
61     @Override
62     public String toString() {
63         return HexUtils.bytesToHex(data);
64     }
65 }