2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.satel.internal.command;
15 import java.nio.charset.Charset;
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;
23 * Command class for command that reads description for specific event code.
25 * @author Krzysztof Goworek - Initial contribution
28 public class ReadEventDescCommand extends SatelCommandBase {
30 private final Logger logger = LoggerFactory.getLogger(ReadEventDescCommand.class);
32 public static final byte COMMAND_CODE = (byte) 0x8f;
35 * Creates new command class instance to read description for given parameters.
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
41 public ReadEventDescCommand(int eventCode, boolean restore, boolean longDescription) {
42 super(COMMAND_CODE, buildPayload(eventCode, restore, longDescription));
45 private static byte[] buildPayload(int eventCode, boolean restore, boolean longDescription) {
50 if (longDescription) {
53 firstByte |= ((eventCode >> 8) & 0x03);
54 return new byte[] { (byte) firstByte, (byte) (eventCode & 0xff) };
58 * Returns type of requested description, either long or short.
60 * @return <code>true</code> if long description has been requested
62 public boolean isLongDescription() {
63 return (getRequest().getPayload()[0] & 0x80) != 0;
67 * Returns text of the description decoded using given encoding.
68 * Encoding depends on firmware language and must be specified in the binding configuration.
70 * @param encoding encoding for the text
71 * @return text of the description
73 public String getText(Charset encoding) {
74 final int length = isLongDescription() ? 46 : 16;
75 return new String(getResponse().getPayload(), 5, length, encoding).trim();
79 * Returns kind of description, either short or long, depending on the request.
81 * @return kind of description
83 public int getKind() {
84 final byte[] payload = getResponse().getPayload();
85 if (isLongDescription()) {
86 return payload[2] & 0xff;
88 return ((payload[3] & 0xff) << 8) + (payload[4] & 0xff);
93 protected boolean isResponseValid(SatelMessage response) {
95 int properLength = isLongDescription() ? 51 : 21;
96 if (response.getPayload().length != properLength) {
97 logger.debug("Invalid payload length: {}", response.getPayload().length);