]> git.basschouten.com Git - openhab-addons.git/blob
2667598bc1ce38818280ad9e29565847459b2174
[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 /**
16  * Extracts a UDP response from a Homematic CCU gateway.
17  *
18  * @author Gerhard Riegler - Initial contribution
19  */
20
21 public class Eq3UdpResponse {
22     private int senderId;
23     private String deviceTypeId;
24     private String serialNumber;
25
26     /**
27      * Extracts the received UDP response.
28      */
29     public Eq3UdpResponse(byte[] buffer) throws IndexOutOfBoundsException {
30         int index = 0;
31         byte protocolVersion = buffer[(index++)];
32         if (protocolVersion == 2) {
33             senderId = readInt(buffer, index, 3);
34             index += 4;
35         }
36         deviceTypeId = readString(buffer, index++);
37         index += deviceTypeId.length();
38         serialNumber = readString(buffer, index);
39     }
40
41     /**
42      * Returns the device type of the gateway.
43      */
44     public String getDeviceTypeId() {
45         return deviceTypeId;
46     }
47
48     /**
49      * Returns the serial number of the gateway.
50      */
51     public String getSerialNumber() {
52         return serialNumber;
53     }
54
55     /**
56      * Returns true, if this response is from a Homematic CCU gateway.
57      */
58     public boolean isValid() {
59         return this.senderId == Eq3UdpRequest.getSenderId()
60                 && (deviceTypeId.startsWith("eQ3-HM-CCU") || deviceTypeId.startsWith("eQ3-HmIP-CCU3"))
61                 && !serialNumber.contains(Eq3UdpRequest.getEq3SerialNumber());
62     }
63
64     private String readString(byte[] data, int index) throws IndexOutOfBoundsException {
65         String result = "";
66         for (int i = index; i < data.length; i++) {
67             if (data[i] == 0) {
68                 break;
69             }
70             result += (char) data[i];
71         }
72         return result;
73     }
74
75     private int readInt(byte[] data, int index, int length) throws IndexOutOfBoundsException {
76         int result = 0;
77         for (int n = index; n < index + length; n++) {
78             result <<= 8;
79             result |= data[n] & 0xFF;
80         }
81         return result;
82     }
83
84     @Override
85     public String toString() {
86         return String.format("%s[deviceTypeId=%s,serialNumber=%s]", getClass().getSimpleName(), deviceTypeId,
87                 serialNumber);
88     }
89 }