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.sinope.internal.core.base;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.Arrays;
19 import org.openhab.binding.sinope.internal.core.appdata.SinopeAppData;
20 import org.openhab.binding.sinope.internal.util.ByteUtil;
23 * The Class SinopeDataAnswer.
25 * @author Pascal Larin - Initial contribution
27 public abstract class SinopeDataAnswer extends SinopeAnswer {
29 /** The Constant SEQ_SIZE. */
30 protected static final int SEQ_SIZE = 4;
32 /** The Constant STATUS_SIZE. */
33 protected static final int STATUS_SIZE = 1;
35 /** The Constant ATTEMPT_NBR_SIZE. */
36 protected static final int ATTEMPT_NBR_SIZE = 1;
38 /** The Constant MORE_SIZE. */
39 protected static final int MORE_SIZE = 1;
41 /** The Constant SRC_DEVICE_ID_SIZE. */
42 protected static final int SRC_DEVICE_ID_SIZE = 4;
44 /** The Constant APP_DATA_SIZE_SIZE. */
45 protected static final int APP_DATA_SIZE_SIZE = 1;
48 private SinopeAppData appData;
51 * Instantiates a new sinope data answer.
54 * @param appData the app data
55 * @throws IOException Signals that an I/O exception has occurred.
57 public SinopeDataAnswer(InputStream r, SinopeAppData appData) throws IOException {
59 byte[] data = getData();
60 this.appData = appData;
61 this.appData.read(data);
67 * @return the app data
69 public SinopeAppData getAppData() {
78 public byte[] getSeq() {
79 byte[] b = this.getFrameData();
80 return Arrays.copyOfRange(b, 0, SEQ_SIZE);
88 public byte getStatus() {
89 byte[] b = this.getFrameData();
94 * Gets the attempt nbr.
96 * @return the attempt nbr
98 public byte getAttemptNbr() {
99 byte[] b = this.getFrameData();
100 return b[SEQ_SIZE + STATUS_SIZE];
108 public byte getMore() {
109 byte[] b = this.getFrameData();
110 return b[SEQ_SIZE + STATUS_SIZE + ATTEMPT_NBR_SIZE];
114 * Gets the src device id.
116 * @return the src device id
118 public byte[] getSrcDeviceId() {
119 byte[] b = this.getFrameData();
120 int start = SEQ_SIZE + STATUS_SIZE + ATTEMPT_NBR_SIZE + MORE_SIZE;
121 int end = start + SRC_DEVICE_ID_SIZE;
122 return ByteUtil.reverse(Arrays.copyOfRange(b, start, end));
130 public byte[] getData() {
131 byte[] b = this.getFrameData();
132 int start = SEQ_SIZE + STATUS_SIZE + ATTEMPT_NBR_SIZE + MORE_SIZE + SRC_DEVICE_ID_SIZE;
133 int end = start + 1 + (b[start] & 0xff);
134 return Arrays.copyOfRange(b, start + 1, end);
138 * @see org.openhab.binding.sinope.internal.core.base.SinopeFrame#toString()
143 * @see ca.tulip.sinope.core.internal.SinopeFrame#toString()
146 public String toString() {
147 StringBuilder sb = new StringBuilder();
148 sb.append(super.toString());
149 sb.append(String.format("\nData: %s", ByteUtil.toString(getFrameData())));
150 sb.append(String.format("\n\tSeq: %s", ByteUtil.toString(getSeq())));
151 sb.append(String.format("\n\tStatus: 0x%02X ", getStatus()));
152 sb.append(String.format("\n\tAttempt Nbr: 0x%02X ", getAttemptNbr()));
153 sb.append(String.format("\n\tMore: 0x%02X ", getMore()));
155 sb.append(String.format("\n\tSrcDeviceId: %s", ByteUtil.toString(getSrcDeviceId())));
159 return sb.toString();