]> git.basschouten.com Git - openhab-addons.git/blob
17f6e16e7775a8c4ef12d4707e188ad57babfb4a
[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
20 import org.openhab.binding.sinope.internal.util.ByteUtil;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * The Class SinopeRequest.
26  *
27  * @author Pascal Larin - Initial contribution
28  */
29 public abstract class SinopeRequest extends SinopeFrame {
30
31     /** The Constant HEADER_COMMAND_CRC_SIZE. */
32     protected static final int HEADER_COMMAND_CRC_SIZE = SinopeFrame.PREAMBLE_SIZE + SinopeFrame.FRAME_CTL_SIZE
33             + SinopeFrame.SIZE_SIZE + SinopeFrame.COMMAND_SIZE + SinopeFrame.CRC_SIZE;
34
35     /** The Constant logger. */
36     private static final Logger logger = LoggerFactory.getLogger(SinopeRequest.class);
37
38     /**
39      * @see org.openhab.binding.sinope.internal.core.base.SinopeFrame#getPayload()
40      */
41     /*
42      *
43      *
44      * @see ca.tulip.sinope.core.internal.SinopeFrame#getPayload()
45      */
46     @Override
47     public byte[] getPayload() {
48         if (getInternal_payload() == null) {
49             byte[] command = getCommand();
50             byte[] data = getFrameData();
51             int len = HEADER_COMMAND_CRC_SIZE + data.length;
52             byte[] buffer = new byte[len];
53             ByteBuffer bb = ByteBuffer.wrap(buffer);
54             bb.put(PREAMBLE);
55             bb.put(FRAME_CTL);
56             bb.order(ByteOrder.LITTLE_ENDIAN);
57             bb.putShort((short) (SinopeFrame.COMMAND_SIZE + data.length));
58
59             bb.put(ByteUtil.reverse(command));
60             bb.put(data);
61
62             bb.put(getCRC8(bb.array()));
63
64             setInternal_payload(bb.array());
65         }
66         return getInternal_payload();
67     }
68
69     /**
70      * Gets the reply answer.
71      *
72      * @param r the r
73      * @return the reply answer
74      * @throws IOException Signals that an I/O exception has occurred.
75      */
76     public abstract SinopeAnswer getReplyAnswer(InputStream r) throws IOException;
77
78     /**
79      * @see org.openhab.binding.sinope.internal.core.base.SinopeFrame#setInternal_payload(byte[])
80      */
81     @Override
82     protected void setInternal_payload(byte[] internal_payload) {
83         logger.debug("Request Frame: {}", ByteUtil.toString(internal_payload));
84         super.setInternal_payload(internal_payload);
85     }
86 }