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