]> git.basschouten.com Git - openhab-addons.git/blob
16300670b06f0eaaed4775e090129e17ae00a53b
[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 information about specific device
24  * (partition, zone, user, etc).
25  *
26  * @author Krzysztof Goworek - Initial contribution
27  */
28 @NonNullByDefault
29 public class ReadDeviceInfoCommand extends SatelCommandBase {
30
31     private final Logger logger = LoggerFactory.getLogger(ReadDeviceInfoCommand.class);
32
33     public static final byte COMMAND_CODE = (byte) 0xee;
34
35     /**
36      * Device type: partition, zone, expander, etc.
37      *
38      * @author Krzysztof Goworek - Initial contribution
39      *
40      */
41     public enum DeviceType {
42         PARTITION(0),
43         ZONE(1),
44         USER(2),
45         EXPANDER(3),
46         KEYPAD(3),
47         OUTPUT(4),
48         ZONE_WITH_PARTITION(5, true),
49         TIMER(6),
50         TELEPHONE(7),
51         OBJECT(15),
52         PARTITION_WITH_OBJECT(16, true);
53
54         int code;
55         boolean additionalInfo;
56
57         DeviceType(int code) {
58             this(code, false);
59         }
60
61         DeviceType(int code, boolean additionalInfo) {
62             this.code = code;
63             this.additionalInfo = additionalInfo;
64         }
65
66         int getCode() {
67             return code;
68         }
69
70         boolean hasAdditionalInfo() {
71             return additionalInfo;
72         }
73     }
74
75     /**
76      * Creates new command class instance to read description for given
77      * parameters.
78      *
79      * @param deviceType type of the device
80      * @param deviceNumber device number
81      */
82     public ReadDeviceInfoCommand(DeviceType deviceType, int deviceNumber) {
83         super(COMMAND_CODE, new byte[] { (byte) deviceType.getCode(), getDeviceNumber(deviceType, deviceNumber) });
84     }
85
86     private static byte getDeviceNumber(DeviceType deviceType, int deviceNumber) {
87         switch (deviceType) {
88             case EXPANDER:
89                 if (deviceNumber < 128) {
90                     return (byte) (deviceNumber + 128);
91                 }
92                 break;
93             case KEYPAD:
94                 if (deviceNumber < 128) {
95                     return (byte) (deviceNumber + 192);
96                 }
97                 break;
98             case ZONE:
99             case ZONE_WITH_PARTITION:
100                 return (byte) (deviceNumber == 256 ? 0 : deviceNumber);
101             default:
102                 break;
103         }
104         return (byte) deviceNumber;
105     }
106
107     /**
108      * Returns device model or function depending on device type:
109      * <ul>
110      * <li>partition - partition type</li>
111      * <li>zone - zone reaction</li>
112      * <li>user - 0</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>
117      * </ul>
118      *
119      * @return kind of the device
120      */
121     public int getDeviceKind() {
122         return getResponse().getPayload()[2] & 0xff;
123     }
124
125     /**
126      * Returns name of the device decoded using given encoding. Encoding
127      * depends on firmware language and must be specified in the binding
128      * configuration.
129      *
130      * @param encoding encoding for the text
131      * @return device name
132      */
133     public String getName(Charset encoding) {
134         return new String(getResponse().getPayload(), 3, 16, encoding).trim();
135     }
136
137     /**
138      * Returns additional info for some types of device.
139      *
140      * @return additional info
141      */
142     public int getAdditionalInfo() {
143         final byte[] payload = getResponse().getPayload();
144         return (payload.length == 20) ? (payload[19] & 0xff) : 0;
145     }
146
147     @Override
148     protected boolean isResponseValid(SatelMessage response) {
149         // validate response
150         if (response.getPayload().length < 19 || response.getPayload().length > 20) {
151             logger.debug("Invalid payload length: {}", response.getPayload().length);
152             return false;
153         }
154         return true;
155     }
156 }