]> git.basschouten.com Git - openhab-addons.git/blob
1f04a75347e7fd9e3aa8587670225294eb9b4f68
[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 java.nio.ByteBuffer;
16
17 /**
18  * @author Volker Bier - Initial contribution
19  */
20 public class CommandUtil {
21     /**
22      * Create the two channel bytes for the given channel IDs
23      *
24      * @param channelIds channel ids (starting from 1)
25      */
26     private static byte[] createChannelBits(Integer... channelIds) {
27         long channels = 0;
28
29         for (int id : channelIds) {
30             channels = channels + (1 << (id - 1));
31         }
32
33         ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES);
34         buffer.putShort((short) (channels % 32768));
35
36         return buffer.array();
37     }
38
39     public static CommandPacket createPacket(Command cmd, Integer channelId) {
40         return createPacket(cmd.getCommandType(), new Integer[] { channelId });
41     }
42
43     public static CommandPacket createPacket(Command cmd) {
44         return createPacket(cmd.getCommandType(), cmd.getChannelIds());
45     }
46
47     public static CommandPacket createPacket(CommandType cmd, Integer... channelIds) {
48         if (cmd == CommandType.INFO) {
49             byte[] channelBits = createChannelBits(channelIds);
50
51             return new CommandPacket(
52                     new byte[] { (byte) 0xAA, 0x04, CommandPacket.EASY_INFO, channelBits[0], channelBits[1] });
53         }
54
55         if (cmd == CommandType.CHECK) {
56             return new CommandPacket(new byte[] { (byte) 0xaa, (byte) 0x02, CommandPacket.EASY_CHECK });
57         }
58
59         byte[] channelBits = createChannelBits(channelIds);
60         byte cmdByte = getCommandByte(cmd);
61
62         return new CommandPacket(
63                 new byte[] { (byte) 0xAA, 0x05, CommandPacket.EASY_SEND, channelBits[0], channelBits[1], cmdByte });
64     }
65
66     private static byte getCommandByte(CommandType command) {
67         switch (command) {
68             case DOWN:
69                 return (byte) 0x40;
70             case INTERMEDIATE:
71                 return (byte) 0x44;
72             case STOP:
73                 return (byte) 0x10;
74             case UP:
75                 return (byte) 0x20;
76             case VENTILATION:
77                 return (byte) 0x24;
78             default:
79                 throw new IllegalArgumentException("Unhandled command type " + command);
80         }
81     }
82 }