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 information about specific device
24 * (partition, zone, user, etc).
26 * @author Krzysztof Goworek - Initial contribution
29 public class ReadDeviceInfoCommand extends SatelCommandBase {
31 private final Logger logger = LoggerFactory.getLogger(ReadDeviceInfoCommand.class);
33 public static final byte COMMAND_CODE = (byte) 0xee;
36 * Device type: partition, zone, expander, etc.
38 * @author Krzysztof Goworek - Initial contribution
41 public enum DeviceType {
48 ZONE_WITH_PARTITION(5, true),
52 PARTITION_WITH_OBJECT(16, true);
55 boolean additionalInfo;
57 DeviceType(int code) {
61 DeviceType(int code, boolean additionalInfo) {
63 this.additionalInfo = additionalInfo;
70 boolean hasAdditionalInfo() {
71 return additionalInfo;
76 * Creates new command class instance to read description for given
79 * @param deviceType type of the device
80 * @param deviceNumber device number
82 public ReadDeviceInfoCommand(DeviceType deviceType, int deviceNumber) {
83 super(COMMAND_CODE, new byte[] { (byte) deviceType.getCode(), getDeviceNumber(deviceType, deviceNumber) });
86 private static byte getDeviceNumber(DeviceType deviceType, int deviceNumber) {
89 if (deviceNumber < 128) {
90 return (byte) (deviceNumber + 128);
94 if (deviceNumber < 128) {
95 return (byte) (deviceNumber + 192);
99 case ZONE_WITH_PARTITION:
100 return (byte) (deviceNumber == 256 ? 0 : deviceNumber);
104 return (byte) deviceNumber;
108 * Returns device model or function depending on device type:
110 * <li>partition - partition type</li>
111 * <li>zone - zone reaction</li>
113 * <li>object - 0</li>
114 * <li>expander - expander model, CA-64 PP, CA-64 E, etc</li>
115 * <li>LCD - LCD model, INT-KLCD, INT-KLCDR, etc</li>
116 * <li>output - output function</li>
119 * @return kind of the device
121 public int getDeviceKind() {
122 return getResponse().getPayload()[2] & 0xff;
126 * Returns name of the device decoded using given encoding. Encoding
127 * depends on firmware language and must be specified in the binding
130 * @param encoding encoding for the text
131 * @return device name
133 public String getName(Charset encoding) {
134 return new String(getResponse().getPayload(), 3, 16, encoding).trim();
138 * Returns additional info for some types of device.
140 * @return additional info
142 public int getAdditionalInfo() {
143 final byte[] payload = getResponse().getPayload();
144 return (payload.length == 20) ? (payload[19] & 0xff) : 0;
148 protected boolean isResponseValid(SatelMessage response) {
150 if (response.getPayload().length < 19 || response.getPayload().length > 20) {
151 logger.debug("Invalid payload length: {}", response.getPayload().length);