]> git.basschouten.com Git - openhab-addons.git/blob
2c494d28e2f5a1d8111b78fcd0dd212a999c45bd
[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.satel.internal.command;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.satel.internal.protocol.SatelMessage;
17
18 /**
19  * Base class for all commands that return result code in the response.
20  *
21  * @author Krzysztof Goworek - Initial contribution
22  */
23 @NonNullByDefault
24 public abstract class ControlCommand extends SatelCommandBase {
25
26     /**
27      * Creates new command class instance.
28      *
29      * @param commandCode command code
30      * @param payload command bytes
31      */
32     public ControlCommand(byte commandCode, byte[] payload) {
33         super(commandCode, payload);
34     }
35
36     /**
37      * Creates new command class instance.
38      *
39      * @param commandCode command code
40      * @param payload command bytes
41      * @param userCode user code
42      */
43     public ControlCommand(byte commandCode, byte[] payload, String userCode) {
44         super(commandCode, appendUserCode(payload, userCode));
45     }
46
47     @Override
48     protected boolean isResponseValid(SatelMessage response) {
49         return true;
50     }
51
52     protected static byte[] appendUserCode(byte[] payload, String userCode) {
53         byte[] userCodeBytes = userCodeToBytes(userCode);
54         byte[] result = new byte[userCodeBytes.length + payload.length];
55         System.arraycopy(userCodeBytes, 0, result, 0, userCodeBytes.length);
56         System.arraycopy(payload, 0, result, userCodeBytes.length, payload.length);
57         return result;
58     }
59
60     protected static byte[] userCodeToBytes(String userCode) {
61         if (userCode.isEmpty()) {
62             throw new IllegalArgumentException("User code is empty");
63         }
64         if (userCode.length() > 8) {
65             throw new IllegalArgumentException("User code too long");
66         }
67         byte[] bytes = new byte[8];
68         int digitsNbr = 2 * bytes.length;
69         for (int i = 0; i < digitsNbr; ++i) {
70             if (i < userCode.length()) {
71                 char digit = userCode.charAt(i);
72                 if (!Character.isDigit(digit)) {
73                     throw new IllegalArgumentException("User code must contain digits only");
74                 }
75                 if (i % 2 == 0) {
76                     bytes[i / 2] = (byte) ((digit - '0') << 4);
77                 } else {
78                     bytes[i / 2] |= (byte) (digit - '0');
79                 }
80             } else if (i % 2 == 0) {
81                 bytes[i / 2] = (byte) 0xff;
82             } else if (i == userCode.length()) {
83                 bytes[i / 2] |= 0x0f;
84             }
85         }
86
87         return bytes;
88     }
89 }