]> git.basschouten.com Git - openhab-addons.git/blob
76909cb463e0f0db1dbdcfb2a8bf462351e8804e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.sinope.internal.core.base;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.Arrays;
18
19 import org.openhab.binding.sinope.internal.core.appdata.SinopeAppData;
20 import org.openhab.binding.sinope.internal.util.ByteUtil;
21
22 /**
23  * The Class SinopeDataAnswer.
24  *
25  * @author Pascal Larin - Initial contribution
26  */
27 public abstract class SinopeDataAnswer extends SinopeAnswer {
28
29     /** The Constant SEQ_SIZE. */
30     protected static final int SEQ_SIZE = 4;
31
32     /** The Constant STATUS_SIZE. */
33     protected static final int STATUS_SIZE = 1;
34
35     /** The Constant ATTEMPT_NBR_SIZE. */
36     protected static final int ATTEMPT_NBR_SIZE = 1;
37
38     /** The Constant MORE_SIZE. */
39     protected static final int MORE_SIZE = 1;
40
41     /** The Constant SRC_DEVICE_ID_SIZE. */
42     protected static final int SRC_DEVICE_ID_SIZE = 4;
43
44     /** The Constant APP_DATA_SIZE_SIZE. */
45     protected static final int APP_DATA_SIZE_SIZE = 1;
46
47     /** The app data. */
48     private SinopeAppData appData;
49
50     /**
51      * Instantiates a new sinope data answer.
52      *
53      * @param r the r
54      * @param appData the app data
55      * @throws IOException Signals that an I/O exception has occurred.
56      */
57     public SinopeDataAnswer(InputStream r, SinopeAppData appData) throws IOException {
58         super(r);
59         byte[] data = getData();
60         this.appData = appData;
61         this.appData.read(data);
62     }
63
64     /**
65      * Gets the app data.
66      *
67      * @return the app data
68      */
69     public SinopeAppData getAppData() {
70         return appData;
71     }
72
73     /**
74      * Gets the seq.
75      *
76      * @return the seq
77      */
78     public byte[] getSeq() {
79         byte[] b = this.getFrameData();
80         return Arrays.copyOfRange(b, 0, SEQ_SIZE);
81     }
82
83     /**
84      * Gets the status.
85      *
86      * @return the status
87      */
88     public byte getStatus() {
89         byte[] b = this.getFrameData();
90         return b[SEQ_SIZE];
91     }
92
93     /**
94      * Gets the attempt nbr.
95      *
96      * @return the attempt nbr
97      */
98     public byte getAttemptNbr() {
99         byte[] b = this.getFrameData();
100         return b[SEQ_SIZE + STATUS_SIZE];
101     }
102
103     /**
104      * Gets the more.
105      *
106      * @return the more
107      */
108     public byte getMore() {
109         byte[] b = this.getFrameData();
110         return b[SEQ_SIZE + STATUS_SIZE + ATTEMPT_NBR_SIZE];
111     }
112
113     /**
114      * Gets the src device id.
115      *
116      * @return the src device id
117      */
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));
123     }
124
125     /**
126      * Gets the data.
127      *
128      * @return the data
129      */
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);
135     }
136
137     /**
138      * @see org.openhab.binding.sinope.internal.core.base.SinopeFrame#toString()
139      */
140     /*
141      *
142      *
143      * @see ca.tulip.sinope.core.internal.SinopeFrame#toString()
144      */
145     @Override
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()));
154
155         sb.append(String.format("\n\tSrcDeviceId: %s", ByteUtil.toString(getSrcDeviceId())));
156
157         sb.append(appData);
158
159         return sb.toString();
160     }
161 }