]> git.basschouten.com Git - openhab-addons.git/blob
87a99123b05ce189f119d8d0ac42d1c4fa88ee20
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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;
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.base.SinopeAnswer;
20 import org.openhab.binding.sinope.internal.util.ByteUtil;
21
22 /**
23  * The Class SinopeAuthenticationKeyAnswer.
24  * 
25  * @author Pascal Larin - Initial contribution
26  */
27 public class SinopeAuthenticationKeyAnswer extends SinopeAnswer {
28
29     /** The Constant STATUS_SIZE. */
30     protected static final int STATUS_SIZE = 1;
31
32     /** The Constant BACKOFF_SIZE. */
33     protected static final int BACKOFF_SIZE = 2;
34
35     /** The Constant API_KEY_SIZE. */
36     protected static final int API_KEY_SIZE = 8;
37
38     /**
39      * Instantiates a new sinope authentication key answer.
40      *
41      * @param r the r
42      * @throws IOException Signals that an I/O exception has occurred.
43      */
44     public SinopeAuthenticationKeyAnswer(InputStream r) throws IOException {
45         super(r);
46     }
47
48     /**
49      * @see org.openhab.binding.sinope.internal.core.base.SinopeFrame#getCommand()
50      */
51     @Override
52     protected byte[] getCommand() {
53         return new byte[] { 0x01, 0x11 };
54     }
55
56     /**
57      * Gets the status.
58      *
59      * @return the status
60      */
61     public int getStatus() {
62         byte[] b = this.getFrameData();
63         return b[0];
64     }
65
66     /**
67      * Gets the backoff.
68      *
69      * @return the backoff
70      */
71     public byte[] getBackoff() {
72         byte[] b = this.getFrameData();
73         b = Arrays.copyOfRange(b, STATUS_SIZE, STATUS_SIZE + BACKOFF_SIZE);
74         return b;
75     }
76
77     /**
78      * Gets the api key.
79      *
80      * @return the api key
81      */
82     public byte[] getApiKey() {
83         byte[] b = this.getFrameData();
84         b = Arrays.copyOfRange(b, STATUS_SIZE + BACKOFF_SIZE, STATUS_SIZE + BACKOFF_SIZE + API_KEY_SIZE);
85         return ByteUtil.reverse(b);
86     }
87
88     /**
89      * @see org.openhab.binding.sinope.internal.core.base.SinopeFrame#toString()
90      */
91     @Override
92     public String toString() {
93         StringBuilder sb = new StringBuilder();
94         sb.append(super.toString());
95         sb.append(String.format("\nData: %s", ByteUtil.toString(getFrameData())));
96         sb.append(String.format("\n\tStatus: 0x%02X ", getStatus()));
97         sb.append(String.format("\n\tApi Key: %s", ByteUtil.toString(getApiKey())));
98         sb.append(String.format("\n\tBackoff: %s", ByteUtil.toString(getBackoff())));
99         return sb.toString();
100     }
101 }