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.nio.ByteBuffer;
19 import org.openhab.binding.sinope.internal.core.appdata.SinopeAppData;
20 import org.openhab.binding.sinope.internal.util.ByteUtil;
23 * The Class SinopeDataRequest.
25 * @author Pascal Larin - Initial contribution
27 public abstract class SinopeDataRequest extends SinopeRequest {
32 /** The request type. */
33 private byte requestType;
47 /** The dst device id. */
48 private byte[] dstDeviceId;
51 private SinopeAppData appData;
54 * Instantiates a new sinope data request.
57 * @param dstDeviceId the dst device id
58 * @param appData the app data
60 public SinopeDataRequest(byte[] seq, byte[] dstDeviceId, SinopeAppData appData) {
66 this.res3 = new byte[] { 0, 0 };
67 this.res4 = new byte[] { 0, 0 };
68 this.dstDeviceId = dstDeviceId;
70 this.appData = appData;
78 public byte[] getSeq() {
83 * Gets the request type.
85 * @return the request type
87 public byte getRequestType() {
96 public byte getRes1() {
105 public byte getRes2() {
114 public byte[] getRes3() {
123 public byte[] getRes4() {
128 * Gets the dst device id.
130 * @return the dst device id
132 public byte[] getDstDeviceId() {
137 * Gets the app data size.
139 * @return the app data size
141 public int getAppDataSize() {
142 return getAppData().getInternalData().length;
148 * @return the app data
150 public SinopeAppData getAppData() {
155 * @see org.openhab.binding.sinope.internal.core.base.SinopeFrame#getFrameData()
160 * @see ca.tulip.sinope.core.internal.SinopeFrame#getFrameData()
163 protected byte[] getFrameData() {
164 int appDataLen = getAppDataSize();
165 byte b[] = new byte[seq.length + 1 + 1 + 1 + res3.length + res4.length + dstDeviceId.length + 1 + appDataLen];
167 ByteBuffer bb = ByteBuffer.wrap(b);
169 bb.put(ByteUtil.reverse(seq));
173 bb.put(ByteUtil.reverse(res3));
174 bb.put(ByteUtil.reverse(res4));
175 bb.put(ByteUtil.reverse(dstDeviceId));
176 bb.put((byte) appDataLen);
177 bb.put(getAppData().getInternalData());
179 // System.out.println(toString(bb.array()));
184 * @see org.openhab.binding.sinope.internal.core.base.SinopeFrame#toString()
189 * @see ca.tulip.sinope.core.internal.SinopeFrame#toString()
192 public String toString() {
193 StringBuilder sb = new StringBuilder();
195 sb.append(String.format("\nData: %s", ByteUtil.toString(getFrameData())));
196 sb.append(String.format("\n\tSeq: %s", ByteUtil.toString(getSeq())));
197 sb.append(String.format("\n\tRequest Type: 0x%02X ", getRequestType()));
198 sb.append(String.format("\n\tRes1: 0x%02X ", getRes1()));
199 sb.append(String.format("\n\tRes2: 0x%02X ", getRes2()));
200 sb.append(String.format("\n\tRes3: %s", ByteUtil.toString(getRes3())));
201 sb.append(String.format("\n\tRes4: %s", ByteUtil.toString(getRes4())));
202 sb.append(String.format("\n\tDstDeviceId: %s", ByteUtil.toString(getDstDeviceId())));
203 sb.append(String.format("\n\tAppDataSize: 0x%02X ", getAppDataSize()));
204 sb.append(String.format("\n\tAppData: %s", getAppData()));
206 return sb.toString();
210 * @see org.openhab.binding.sinope.internal.core.base.SinopeRequest#getReplyAnswer(java.io.InputStream)
215 * @see ca.tulip.sinope.core.internal.SinopeRequest#getReplyAnswer(java.io.InputStream)
218 public abstract SinopeDataAnswer getReplyAnswer(InputStream r) throws IOException;