2 * Copyright (c) 2010-2022 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.openhab.core.util.HexUtils;
18 * The {@link AbstractResponseParser} is responsible for parsing responses coming from
19 * rego6xx controllers.
21 * @author Boris Krivonog - Initial contribution
23 abstract class AbstractResponseParser<T> implements ResponseParser<T> {
24 private static final byte COMPUTER_ADDRESS = (byte) 0x01;
27 public abstract int responseLength();
29 protected abstract T convert(byte[] responseBytes);
32 public T parse(byte[] buffer) throws Rego6xxProtocolException {
33 if (buffer.length != responseLength()) {
34 throw new Rego6xxProtocolException(
35 "Expected size does not match: " + buffer.length + " != " + responseLength());
38 if (buffer[0] != COMPUTER_ADDRESS) {
39 throw new Rego6xxProtocolException("Invalid header " + HexUtils.bytesToHex(buffer));
42 if (responseLength() > 1
43 && Checksum.calculate(buffer, 1, responseLength() - 2) != buffer[responseLength() - 1]) {
44 throw new Rego6xxProtocolException("Invalid crc - " + HexUtils.bytesToHex(buffer));
47 return convert(buffer);