]> git.basschouten.com Git - openhab-addons.git/blob
45c4671fccfadea9f252901c2761e15304845f5d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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 import java.nio.ByteOrder;
19 import java.util.Arrays;
20
21 import org.openhab.binding.sinope.internal.util.ByteUtil;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * The Class SinopeAnswer.
27  *
28  * @author Pascal Larin - Initial contribution
29  */
30 public abstract class SinopeAnswer extends SinopeRequest {
31
32     /** The Constant logger. */
33     private static final Logger logger = LoggerFactory.getLogger(SinopeAnswer.class);
34
35     /**
36      * Instantiates a new sinope answer.
37      *
38      * @param r the r
39      * @throws IOException Signals that an I/O exception has occurred.
40      */
41     public SinopeAnswer(InputStream r) throws IOException {
42         byte[] header = new byte[SinopeFrame.PREAMBLE_SIZE + SinopeFrame.FRAME_CTL_SIZE + SinopeFrame.SIZE_SIZE];
43
44         r.read(header, 0, header.length);
45
46         if (header[0] != 0x55) {
47             throw new IOException(String.format("Invalid header PREAMBLE: %02x", header[0]));
48         }
49         int startSizeIndex = SinopeFrame.PREAMBLE_SIZE + SinopeFrame.FRAME_CTL_SIZE;
50         int endSizeIndex = startSizeIndex + SinopeFrame.SIZE_SIZE;
51         byte[] sizeInByte = Arrays.copyOfRange(header, startSizeIndex, endSizeIndex);
52         ByteBuffer bb = ByteBuffer.allocate(SIZE_SIZE);
53         bb.order(ByteOrder.LITTLE_ENDIAN);
54         bb.put(sizeInByte);
55         short size = bb.getShort(0);
56
57         byte[] payload = new byte[SinopeRequest.HEADER_COMMAND_CRC_SIZE + size];
58         byte[] remain = new byte[size + 1];
59         r.read(remain, 0, size + 1);
60         bb = ByteBuffer.wrap(payload);
61         bb.put(header);
62         bb.put(remain);
63
64         this.setInternal_payload(bb.array());
65     }
66
67     /**
68      * @see org.openhab.binding.sinope.internal.core.base.SinopeRequest#getPayload()
69      */
70     /*
71      *
72      *
73      * @see ca.tulip.sinope.core.internal.SinopeRequest#getPayload()
74      */
75     @Override
76     public byte[] getPayload() {
77         return getInternal_payload();
78     }
79
80     /**
81      * @see org.openhab.binding.sinope.internal.core.base.SinopeRequest#getReplyAnswer(java.io.InputStream)
82      */
83     /*
84      *
85      *
86      * @see ca.tulip.sinope.core.internal.SinopeRequest#getReplyAnswer(java.io.InputStream)
87      */
88     @Override
89     public SinopeAnswer getReplyAnswer(InputStream r) {
90         throw new NotSupportedException();
91     }
92
93     /**
94      * @see org.openhab.binding.sinope.internal.core.base.SinopeFrame#getFrameData()
95      */
96     /*
97      *
98      *
99      * @see ca.tulip.sinope.core.internal.SinopeFrame#getFrameData()
100      */
101     @Override
102     protected final byte[] getFrameData() {
103         byte[] b = this.getInternal_payload();
104         int headerSize = SinopeFrame.PREAMBLE_SIZE + SinopeFrame.FRAME_CTL_SIZE + SinopeFrame.COMMAND_SIZE
105                 + SinopeFrame.SIZE_SIZE;
106         return Arrays.copyOfRange(b, headerSize, b.length - SinopeFrame.CRC_SIZE);
107     }
108
109     /**
110      * @see org.openhab.binding.sinope.internal.core.base.SinopeRequest#setInternal_payload(byte[])
111      */
112     @Override
113     protected void setInternal_payload(byte[] internal_payload) {
114         logger.debug("Answer  Frame: {}", ByteUtil.toString(internal_payload));
115         super.setInternal_payload(internal_payload);
116     }
117 }