2 * Copyright (c) 2010-2023 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.regoheatpump.internal.rego6xx;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.core.util.HexUtils;
19 * The {@link AbstractResponseParser} is responsible for parsing responses coming from
20 * rego6xx controllers.
22 * @author Boris Krivonog - Initial contribution
25 abstract class AbstractResponseParser<T> implements ResponseParser<T> {
26 private static final byte COMPUTER_ADDRESS = (byte) 0x01;
29 public abstract int responseLength();
31 protected abstract T convert(byte[] responseBytes);
34 public T parse(byte[] buffer) throws Rego6xxProtocolException {
35 if (buffer.length != responseLength()) {
36 throw new Rego6xxProtocolException(
37 "Expected size does not match: " + buffer.length + " != " + responseLength());
40 if (buffer[0] != COMPUTER_ADDRESS) {
41 throw new Rego6xxProtocolException("Invalid header " + HexUtils.bytesToHex(buffer));
44 if (responseLength() > 1
45 && Checksum.calculate(buffer, 1, responseLength() - 2) != buffer[responseLength() - 1]) {
46 throw new Rego6xxProtocolException("Invalid crc - " + HexUtils.bytesToHex(buffer));
49 return convert(buffer);