]> git.basschouten.com Git - openhab-addons.git/blob
032e99f355119975318031ac6949b4d2aad9e2b2
[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.nio.ByteBuffer;
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 SinopeDataRequest.
24  *
25  * @author Pascal Larin - Initial contribution
26  */
27 public abstract class SinopeDataRequest extends SinopeRequest {
28
29     /** The seq. */
30     private byte[] seq;
31
32     /** The request type. */
33     private byte requestType;
34
35     /** The res 1. */
36     private byte res1;
37
38     /** The res 2. */
39     private byte res2;
40
41     /** The res 3. */
42     private byte[] res3;
43
44     /** The res 4. */
45     private byte[] res4;
46
47     /** The dst device id. */
48     private byte[] dstDeviceId;
49
50     /** The app data. */
51     private SinopeAppData appData;
52
53     /**
54      * Instantiates a new sinope data request.
55      *
56      * @param seq the seq
57      * @param dstDeviceId the dst device id
58      * @param appData the app data
59      */
60     public SinopeDataRequest(byte[] seq, byte[] dstDeviceId, SinopeAppData appData) {
61         super();
62         this.seq = seq;
63         this.requestType = 0;
64         this.res1 = 0;
65         this.res2 = 0;
66         this.res3 = new byte[] { 0, 0 };
67         this.res4 = new byte[] { 0, 0 };
68         this.dstDeviceId = dstDeviceId;
69
70         this.appData = appData;
71     }
72
73     /**
74      * Gets the seq.
75      *
76      * @return the seq
77      */
78     public byte[] getSeq() {
79         return seq;
80     }
81
82     /**
83      * Gets the request type.
84      *
85      * @return the request type
86      */
87     public byte getRequestType() {
88         return requestType;
89     }
90
91     /**
92      * Gets the res 1.
93      *
94      * @return the res 1
95      */
96     public byte getRes1() {
97         return res1;
98     }
99
100     /**
101      * Gets the res 2.
102      *
103      * @return the res 2
104      */
105     public byte getRes2() {
106         return res2;
107     }
108
109     /**
110      * Gets the res 3.
111      *
112      * @return the res 3
113      */
114     public byte[] getRes3() {
115         return res3;
116     }
117
118     /**
119      * Gets the res 4.
120      *
121      * @return the res 4
122      */
123     public byte[] getRes4() {
124         return res4;
125     }
126
127     /**
128      * Gets the dst device id.
129      *
130      * @return the dst device id
131      */
132     public byte[] getDstDeviceId() {
133         return dstDeviceId;
134     }
135
136     /**
137      * Gets the app data size.
138      *
139      * @return the app data size
140      */
141     public int getAppDataSize() {
142         return getAppData().getInternalData().length;
143     }
144
145     /**
146      * Gets the app data.
147      *
148      * @return the app data
149      */
150     public SinopeAppData getAppData() {
151         return appData;
152     }
153
154     /**
155      * @see org.openhab.binding.sinope.internal.core.base.SinopeFrame#getFrameData()
156      */
157     /*
158      *
159      *
160      * @see ca.tulip.sinope.core.internal.SinopeFrame#getFrameData()
161      */
162     @Override
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];
166
167         ByteBuffer bb = ByteBuffer.wrap(b);
168
169         bb.put(ByteUtil.reverse(seq));
170         bb.put(requestType);
171         bb.put(res1);
172         bb.put(res2);
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());
178         //
179         // System.out.println(toString(bb.array()));
180         return bb.array();
181     }
182
183     /**
184      * @see org.openhab.binding.sinope.internal.core.base.SinopeFrame#toString()
185      */
186     /*
187      *
188      *
189      * @see ca.tulip.sinope.core.internal.SinopeFrame#toString()
190      */
191     @Override
192     public String toString() {
193         StringBuilder sb = new StringBuilder();
194
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()));
205
206         return sb.toString();
207     }
208
209     /**
210      * @see org.openhab.binding.sinope.internal.core.base.SinopeRequest#getReplyAnswer(java.io.InputStream)
211      */
212     /*
213      *
214      *
215      * @see ca.tulip.sinope.core.internal.SinopeRequest#getReplyAnswer(java.io.InputStream)
216      */
217     @Override
218     public abstract SinopeDataAnswer getReplyAnswer(InputStream r) throws IOException;
219 }