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;
20 import org.openhab.binding.sinope.internal.util.ByteUtil;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * The Class SinopeRequest.
27 * @author Pascal Larin - Initial contribution
29 public abstract class SinopeRequest extends SinopeFrame {
31 /** The Constant HEADER_COMMAND_CRC_SIZE. */
32 protected static final int HEADER_COMMAND_CRC_SIZE = SinopeFrame.PREAMBLE_SIZE + SinopeFrame.FRAME_CTL_SIZE
33 + SinopeFrame.SIZE_SIZE + SinopeFrame.COMMAND_SIZE + SinopeFrame.CRC_SIZE;
35 /** The Constant logger. */
36 private static final Logger logger = LoggerFactory.getLogger(SinopeRequest.class);
39 * @see org.openhab.binding.sinope.internal.core.base.SinopeFrame#getPayload()
44 * @see ca.tulip.sinope.core.internal.SinopeFrame#getPayload()
47 public byte[] getPayload() {
48 if (getInternal_payload() == null) {
49 byte[] command = getCommand();
50 byte[] data = getFrameData();
51 int len = HEADER_COMMAND_CRC_SIZE + data.length;
52 byte[] buffer = new byte[len];
53 ByteBuffer bb = ByteBuffer.wrap(buffer);
56 bb.order(ByteOrder.LITTLE_ENDIAN);
57 bb.putShort((short) (SinopeFrame.COMMAND_SIZE + data.length));
59 bb.put(ByteUtil.reverse(command));
62 bb.put(getCRC8(bb.array()));
64 setInternal_payload(bb.array());
66 return getInternal_payload();
70 * Gets the reply answer.
73 * @return the reply answer
74 * @throws IOException Signals that an I/O exception has occurred.
76 public abstract SinopeAnswer getReplyAnswer(InputStream r) throws IOException;
79 * @see org.openhab.binding.sinope.internal.core.base.SinopeFrame#setInternal_payload(byte[])
82 protected void setInternal_payload(byte[] internal_payload) {
83 logger.debug("Request Frame: {}", ByteUtil.toString(internal_payload));
84 super.setInternal_payload(internal_payload);