]> git.basschouten.com Git - openhab-addons.git/blob
d4a34e4723c429a6f49f52be8fc638e6d013b84b
[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.satel.internal.command;
14
15 import java.nio.charset.Charset;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.satel.internal.protocol.SatelMessage;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Command class for command that reads description for specific event code.
24  *
25  * @author Krzysztof Goworek - Initial contribution
26  */
27 @NonNullByDefault
28 public class ReadEventDescCommand extends SatelCommandBase {
29
30     private final Logger logger = LoggerFactory.getLogger(ReadEventDescCommand.class);
31
32     public static final byte COMMAND_CODE = (byte) 0x8f;
33
34     /**
35      * Creates new command class instance to read description for given parameters.
36      *
37      * @param eventCode event code
38      * @param restore <code>true</code> if this is restoration
39      * @param longDescription <code>true</code> for long description, <code>false</code> for short one
40      */
41     public ReadEventDescCommand(int eventCode, boolean restore, boolean longDescription) {
42         super(COMMAND_CODE, buildPayload(eventCode, restore, longDescription));
43     }
44
45     private static byte[] buildPayload(int eventCode, boolean restore, boolean longDescription) {
46         int firstByte = 0;
47         if (restore) {
48             firstByte |= 0x04;
49         }
50         if (longDescription) {
51             firstByte |= 0x80;
52         }
53         firstByte |= ((eventCode >> 8) & 0x03);
54         return new byte[] { (byte) firstByte, (byte) (eventCode & 0xff) };
55     }
56
57     /**
58      * Returns type of requested description, either long or short.
59      *
60      * @return <code>true</code> if long description has been requested
61      */
62     public boolean isLongDescription() {
63         return (getRequest().getPayload()[0] & 0x80) != 0;
64     }
65
66     /**
67      * Returns text of the description decoded using given encoding.
68      * Encoding depends on firmware language and must be specified in the binding configuration.
69      *
70      * @param encoding encoding for the text
71      * @return text of the description
72      */
73     public String getText(Charset encoding) {
74         final int length = isLongDescription() ? 46 : 16;
75         return new String(getResponse().getPayload(), 5, length, encoding).trim();
76     }
77
78     /**
79      * Returns kind of description, either short or long, depending on the request.
80      *
81      * @return kind of description
82      */
83     public int getKind() {
84         final byte[] payload = getResponse().getPayload();
85         if (isLongDescription()) {
86             return payload[2] & 0xff;
87         } else {
88             return ((payload[3] & 0xff) << 8) + (payload[4] & 0xff);
89         }
90     }
91
92     @Override
93     protected boolean isResponseValid(SatelMessage response) {
94         // validate response
95         int properLength = isLongDescription() ? 51 : 21;
96         if (response.getPayload().length != properLength) {
97             logger.debug("Invalid payload length: {}", response.getPayload().length);
98             return false;
99         }
100         return true;
101     }
102 }