2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.homematic.internal.discovery.eq3udp;
15 import org.apache.commons.lang.builder.ToStringBuilder;
16 import org.apache.commons.lang.builder.ToStringStyle;
19 * Extracts a UDP response from a Homematic CCU gateway.
21 * @author Gerhard Riegler - Initial contribution
24 public class Eq3UdpResponse {
26 private String deviceTypeId;
27 private String serialNumber;
30 * Extracts the received UDP response.
32 public Eq3UdpResponse(byte[] buffer) throws IndexOutOfBoundsException {
34 byte protocolVersion = buffer[(index++)];
35 if (protocolVersion == 2) {
36 senderId = readInt(buffer, index, 3);
39 deviceTypeId = readString(buffer, index++);
40 index += deviceTypeId.length();
41 serialNumber = readString(buffer, index);
45 * Returns the device type of the gateway.
47 public String getDeviceTypeId() {
52 * Returns the serial number of the gateway.
54 public String getSerialNumber() {
59 * Returns true, if this response is from a Homematic CCU gateway.
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());
67 private String readString(byte[] data, int index) throws IndexOutOfBoundsException {
69 for (int i = index; i < data.length; i++) {
73 result += (char) data[i];
78 private int readInt(byte[] data, int index, int length) throws IndexOutOfBoundsException {
80 for (int n = index; n < index + length; n++) {
82 result |= data[n] & 0xFF;
88 public String toString() {
89 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("deviceTypeId", deviceTypeId)
90 .append("serialNumber", serialNumber).toString();