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.appdata;
15 import java.nio.ByteBuffer;
17 import org.openhab.binding.sinope.internal.util.ByteUtil;
20 * The Class SinopeAppData.
22 * @author Pascal Larin - Initial contribution
24 public class SinopeAppData {
26 /** The Constant DATA_ID_SIZE. */
27 protected static final int DATA_ID_SIZE = 4;
29 /** The Constant DATA_SIZE. */
30 protected static final int DATA_SIZE = 1;
32 /** The internal data. */
33 protected byte[] internal_data; // Full App object
36 private byte[] dataId;
40 * data == null ? Do not send data size and payload
42 private byte[] data; // Data field of the app object
45 * Instantiates a new sinope app data.
47 * @param dataId the data id
48 * @param data the data
50 public SinopeAppData(byte[] dataId, byte[] data) {
60 public void read(byte[] d) {
61 this.internal_data = d;
62 if (d.length >= DATA_ID_SIZE) {
63 ByteBuffer bb = ByteBuffer.wrap(d);
64 this.dataId = new byte[DATA_ID_SIZE];
67 this.dataId = ByteUtil.reverse(dataId);
68 if (d.length > DATA_ID_SIZE) {
69 int len = bb.get() & 0xff;
71 this.data = new byte[len];
78 * Gets the internal data.
80 * @return the internal data
82 public byte[] getInternalData() {
83 if (internal_data == null) {
84 int len = data != null ? 1 + data.length : 0;
85 byte[] b = new byte[DATA_ID_SIZE + len];
87 ByteBuffer bb = ByteBuffer.wrap(b);
88 bb.put(ByteUtil.reverse(dataId));
89 if (data != null) { // Special case, Data null, don't put app size
90 bb.put((byte) data.length);
93 this.internal_data = bb.array();
96 return this.internal_data;
104 public byte[] getData() {
109 * @see java.lang.Object#toString()
114 * @see java.lang.Object#toString()
117 public String toString() {
118 StringBuilder sb = new StringBuilder();
120 sb.append(String.format("\n\tData ID: %s", ByteUtil.toString(this.dataId)));
122 sb.append(String.format("\n\t\tData Size: 0x%02X ", getData().length));
123 sb.append(String.format("\n\t\tData: %s", ByteUtil.toString(getData())));
125 return sb.toString();
130 * It nullifies the data field. Tells to not send data size and payload.
132 public void cleanData() {