2 * Copyright (c) 2010-2023 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.elerotransmitterstick.internal.stick;
15 import java.nio.ByteBuffer;
18 * @author Volker Bier - Initial contribution
20 public class CommandUtil {
22 * Create the two channel bytes for the given channel IDs
24 * @param channelIds channel ids (starting from 1)
26 private static byte[] createChannelBits(Integer... channelIds) {
29 for (int id : channelIds) {
30 channels = channels + (1 << (id - 1));
33 ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES);
34 buffer.putShort((short) (channels % 32768));
36 return buffer.array();
39 public static CommandPacket createPacket(Command cmd, Integer channelId) {
40 return createPacket(cmd.getCommandType(), new Integer[] { channelId });
43 public static CommandPacket createPacket(Command cmd) {
44 return createPacket(cmd.getCommandType(), cmd.getChannelIds());
47 public static CommandPacket createPacket(CommandType cmd, Integer... channelIds) {
48 if (cmd == CommandType.INFO) {
49 byte[] channelBits = createChannelBits(channelIds);
51 return new CommandPacket(
52 new byte[] { (byte) 0xAA, 0x04, CommandPacket.EASY_INFO, channelBits[0], channelBits[1] });
55 if (cmd == CommandType.CHECK) {
56 return new CommandPacket(new byte[] { (byte) 0xaa, (byte) 0x02, CommandPacket.EASY_CHECK });
59 byte[] channelBits = createChannelBits(channelIds);
60 byte cmdByte = getCommandByte(cmd);
62 return new CommandPacket(
63 new byte[] { (byte) 0xAA, 0x05, CommandPacket.EASY_SEND, channelBits[0], channelBits[1], cmdByte });
66 private static byte getCommandByte(CommandType command) {
79 throw new IllegalArgumentException("Unhandled command type " + command);