2 * Copyright (c) 2010-2024 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.sinope.internal.core.base;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.nio.ByteBuffer;
18 import java.nio.ByteOrder;
19 import java.util.Arrays;
21 import org.openhab.binding.sinope.internal.util.ByteUtil;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * The Class SinopeAnswer.
28 * @author Pascal Larin - Initial contribution
30 public abstract class SinopeAnswer extends SinopeRequest {
32 /** The Constant logger. */
33 private static final Logger logger = LoggerFactory.getLogger(SinopeAnswer.class);
36 * Instantiates a new sinope answer.
39 * @throws IOException Signals that an I/O exception has occurred.
41 public SinopeAnswer(InputStream r) throws IOException {
42 byte[] header = new byte[SinopeFrame.PREAMBLE_SIZE + SinopeFrame.FRAME_CTL_SIZE + SinopeFrame.SIZE_SIZE];
44 r.read(header, 0, header.length);
46 if (header[0] != 0x55) {
47 throw new IOException(String.format("Invalid header PREAMBLE: %02x", header[0]));
49 int startSizeIndex = SinopeFrame.PREAMBLE_SIZE + SinopeFrame.FRAME_CTL_SIZE;
50 int endSizeIndex = startSizeIndex + SinopeFrame.SIZE_SIZE;
51 byte[] sizeInByte = Arrays.copyOfRange(header, startSizeIndex, endSizeIndex);
52 ByteBuffer bb = ByteBuffer.allocate(SIZE_SIZE);
53 bb.order(ByteOrder.LITTLE_ENDIAN);
55 short size = bb.getShort(0);
57 byte[] payload = new byte[SinopeRequest.HEADER_COMMAND_CRC_SIZE + size];
58 byte[] remain = new byte[size + 1];
59 r.read(remain, 0, size + 1);
60 bb = ByteBuffer.wrap(payload);
64 this.setInternal_payload(bb.array());
68 * @see org.openhab.binding.sinope.internal.core.base.SinopeRequest#getPayload()
73 * @see ca.tulip.sinope.core.internal.SinopeRequest#getPayload()
76 public byte[] getPayload() {
77 return getInternal_payload();
81 * @see org.openhab.binding.sinope.internal.core.base.SinopeRequest#getReplyAnswer(java.io.InputStream)
86 * @see ca.tulip.sinope.core.internal.SinopeRequest#getReplyAnswer(java.io.InputStream)
89 public SinopeAnswer getReplyAnswer(InputStream r) {
90 throw new NotSupportedException();
94 * @see org.openhab.binding.sinope.internal.core.base.SinopeFrame#getFrameData()
99 * @see ca.tulip.sinope.core.internal.SinopeFrame#getFrameData()
102 protected final byte[] getFrameData() {
103 byte[] b = this.getInternal_payload();
104 int headerSize = SinopeFrame.PREAMBLE_SIZE + SinopeFrame.FRAME_CTL_SIZE + SinopeFrame.COMMAND_SIZE
105 + SinopeFrame.SIZE_SIZE;
106 return Arrays.copyOfRange(b, headerSize, b.length - SinopeFrame.CRC_SIZE);
110 * @see org.openhab.binding.sinope.internal.core.base.SinopeRequest#setInternal_payload(byte[])
113 protected void setInternal_payload(byte[] internal_payload) {
114 logger.debug("Answer Frame: {}", ByteUtil.toString(internal_payload));
115 super.setInternal_payload(internal_payload);