]> git.basschouten.com Git - openhab-addons.git/blob
dcc85e99a8244d0b270bc1c1a4b53a341cfc8166
[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.util.Arrays;
16
17 /**
18  * @author Volker Bier - Initial contribution
19  */
20 public class ResponseUtil {
21
22     public static Response createResponse(byte upperChannelByte, byte lowerChannelByte) {
23         return new Response(getChannelIds(upperChannelByte, lowerChannelByte));
24     }
25
26     public static Response createResponse(byte upperChannelByte, byte lowerChannelByte, byte responseType) {
27         return new Response(ResponseStatus.getFor(responseType), getChannelIds(upperChannelByte, lowerChannelByte));
28     }
29
30     /**
31      * returns the list of channels (starting with 1)
32      */
33     public static int[] getChannelIds(byte upperChannelByte, byte lowerChannelByte) {
34         int[] result = new int[16];
35         int idx = 0;
36
37         byte b = lowerChannelByte;
38         for (int i = 0; i < 8; i++) {
39             if ((b & 1) > 0) {
40                 result[idx++] = i + 1;
41             }
42             b = (byte) (b >> 1);
43         }
44
45         b = upperChannelByte;
46         for (int i = 0; i < 8; i++) {
47             if ((b & 1) > 0) {
48                 result[idx++] = i + 9;
49             }
50             b = (byte) (b >> 1);
51         }
52
53         return Arrays.copyOfRange(result, 0, idx);
54     }
55 }