]> git.basschouten.com Git - openhab-addons.git/blob
946fb6bfcec038af1a3a13ac24b51405bee4aada
[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;
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 SinopeApiLoginAnswer.
24  * 
25  * @author Pascal Larin - Initial contribution
26  */
27 public class SinopeApiLoginAnswer 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 SW_REV_MAJ_SIZE. */
36     protected static final int SW_REV_MAJ_SIZE = 1;
37
38     /** The Constant SW_REV_MIN_SIZE. */
39     protected static final int SW_REV_MIN_SIZE = 1;
40
41     /** The Constant SW_REV_BUG_SIZE. */
42     protected static final int SW_REV_BUG_SIZE = 1;
43
44     /** The Constant DEVICE_ID_SIZE. */
45     protected static final int DEVICE_ID_SIZE = 4;
46
47     /**
48      * Instantiates a new sinope api login answer.
49      *
50      * @param r the r
51      * @throws IOException Signals that an I/O exception has occurred.
52      */
53     public SinopeApiLoginAnswer(InputStream r) throws IOException {
54         super(r);
55     }
56
57     /**
58      * @see org.openhab.binding.sinope.internal.core.base.SinopeFrame#getCommand()
59      */
60     @Override
61     protected byte[] getCommand() {
62         return new byte[] { 0x01, 0x11 };
63     }
64
65     /**
66      * Gets the status.
67      *
68      * @return the status
69      */
70     public byte getStatus() {
71         byte[] b = this.getFrameData();
72         return b[0];
73     }
74
75     /**
76      * Gets the backoff.
77      *
78      * @return the backoff
79      */
80     public byte[] getBackoff() {
81         byte[] b = this.getFrameData();
82         b = Arrays.copyOfRange(b, STATUS_SIZE, STATUS_SIZE + BACKOFF_SIZE);
83         return b;
84     }
85
86     /**
87      * Gets the sw rev maj.
88      *
89      * @return the sw rev maj
90      */
91     public byte getSwRevMaj() {
92         byte[] b = this.getFrameData();
93         return b[STATUS_SIZE + BACKOFF_SIZE];
94     }
95
96     /**
97      * Gets the sw rev min.
98      *
99      * @return the sw rev min
100      */
101     public byte getSwRevMin() {
102         byte[] b = this.getFrameData();
103         return b[STATUS_SIZE + BACKOFF_SIZE + SW_REV_MAJ_SIZE];
104     }
105
106     /**
107      * Gets the sw rev bug.
108      *
109      * @return the sw rev bug
110      */
111     public byte getSwRevBug() {
112         byte[] b = this.getFrameData();
113         return b[STATUS_SIZE + BACKOFF_SIZE + SW_REV_MAJ_SIZE + SW_REV_MIN_SIZE];
114     }
115
116     /**
117      * Gets the device id.
118      *
119      * @return the device id
120      */
121     public byte[] getDeviceId() {
122         byte[] b = this.getFrameData();
123         b = Arrays.copyOfRange(b, STATUS_SIZE + BACKOFF_SIZE + SW_REV_MAJ_SIZE + SW_REV_MIN_SIZE + SW_REV_BUG_SIZE,
124                 STATUS_SIZE + BACKOFF_SIZE + SW_REV_MAJ_SIZE + SW_REV_MIN_SIZE + SW_REV_BUG_SIZE + DEVICE_ID_SIZE);
125         return ByteUtil.reverse(b);
126     }
127
128     /**
129      * @see org.openhab.binding.sinope.internal.core.base.SinopeFrame#toString()
130      */
131     @Override
132     public String toString() {
133         StringBuilder sb = new StringBuilder();
134         sb.append(super.toString());
135         sb.append(String.format("\nData: %s", ByteUtil.toString(getFrameData())));
136         sb.append(String.format("\n\tStatus: 0x%02X ", getStatus()));
137         sb.append(String.format("\n\tBackoff: %s", ByteUtil.toString(getBackoff())));
138         sb.append(String.format("\n\tSwRevMaj: 0x%02X ", getSwRevMaj()));
139         sb.append(String.format("\n\tSwRevMin: 0x%02X ", getSwRevMin()));
140         sb.append(String.format("\n\tSwRevBug: 0x%02X ", getSwRevBug()));
141         sb.append(String.format("\n\tDeviceID: %s", ByteUtil.toString(getDeviceId())));
142
143         return sb.toString();
144     }
145 }