]> git.basschouten.com Git - openhab-addons.git/blob
7d853ce8ba9b897d2cb4e75cd562527273a6b3a5
[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.homematic.internal.discovery.eq3udp;
14
15 import java.io.ByteArrayOutputStream;
16 import java.io.IOException;
17 import java.util.Random;
18
19 /**
20  * Generates a UDP request to discover Homematic CCU gateways.
21  *
22  * @author Gerhard Riegler - Initial contribution
23  */
24
25 public class Eq3UdpRequest {
26     private static final byte UDP_IDENTIFY = 73;
27     private static final byte UDP_SEPARATOR = 0;
28
29     private static final int senderId = new Random().nextInt() & 0xFFFFFF;
30     private static final String EQ3_DEVICE_TYPE = "eQ3-*";
31     private static final String EQ3_SERIAL_NUMBER = "*";
32
33     /**
34      * Returns the Eq3 Serialnumber.
35      */
36     public static String getEq3SerialNumber() {
37         return EQ3_SERIAL_NUMBER;
38     }
39
40     /**
41      * Returns the sender id.
42      */
43     public static int getSenderId() {
44         return senderId;
45     }
46
47     /**
48      * Creates the UDP request.
49      */
50     public byte[] getBytes() throws IOException {
51         ByteArrayOutputStream baos = new ByteArrayOutputStream();
52         baos.write(2);
53         for (int i = 2; i >= 0; i--) {
54             byte temp = (byte) (senderId >> i * 8 & 0xFF);
55             baos.write(temp);
56         }
57         baos.write(UDP_SEPARATOR);
58         baos.write(EQ3_DEVICE_TYPE.getBytes());
59         baos.write(UDP_SEPARATOR);
60         baos.write(EQ3_SERIAL_NUMBER.getBytes());
61         baos.write(UDP_SEPARATOR);
62         baos.write(UDP_IDENTIFY);
63         return baos.toByteArray();
64     }
65 }