2 * Copyright (c) 2010-2020 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.satel.internal.command;
15 import org.apache.commons.lang.StringUtils;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.openhab.binding.satel.internal.protocol.SatelMessage;
20 * Base class for all commands that return result code in the response.
22 * @author Krzysztof Goworek - Initial contribution
25 public abstract class ControlCommand extends SatelCommandBase {
28 * Creates new command class instance.
30 * @param commandCode command code
31 * @param payload command bytes
33 public ControlCommand(byte commandCode, byte[] payload) {
34 super(commandCode, payload);
38 protected boolean isResponseValid(SatelMessage response) {
42 protected static byte[] userCodeToBytes(String userCode) {
43 if (StringUtils.isEmpty(userCode)) {
44 throw new IllegalArgumentException("User code is empty");
46 if (userCode.length() > 8) {
47 throw new IllegalArgumentException("User code too long");
49 byte[] bytes = new byte[8];
50 int digitsNbr = 2 * bytes.length;
51 for (int i = 0; i < digitsNbr; ++i) {
52 if (i < userCode.length()) {
53 char digit = userCode.charAt(i);
54 if (!Character.isDigit(digit)) {
55 throw new IllegalArgumentException("User code must contain digits only");
58 bytes[i / 2] = (byte) ((digit - '0') << 4);
60 bytes[i / 2] |= (byte) (digit - '0');
62 } else if (i % 2 == 0) {
63 bytes[i / 2] = (byte) 0xff;
64 } else if (i == userCode.length()) {