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.alarmdecoder.internal.protocol;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
19 * The {@link ADCommand} class represents an alarm decoder command, and contains the static methods and definitions
20 * used to construct one. Not all supported AD commands are necessarily used by the current binding.
22 * @author Bob Adair - Initial contribution
25 public final class ADCommand {
27 public static final String SPECIAL_KEY_1 = "\u0001\u0001\u0001";
28 public static final String SPECIAL_KEY_2 = "\u0002\u0002\u0002";
29 public static final String SPECIAL_KEY_3 = "\u0003\u0003\u0003";
30 public static final String SPECIAL_KEY_4 = "\u0004\u0004\u0004";
31 public static final String SPECIAL_KEY_5 = "\u0005\u0005\u0005";
32 public static final String SPECIAL_KEY_6 = "\u0006\u0006\u0006";
33 public static final String SPECIAL_KEY_7 = "\u0007\u0007\u0007";
34 public static final String SPECIAL_KEY_8 = "\u0008\u0008\u0008";
36 public static final int ZONE_OPEN = 1;
37 public static final int ZONE_CLOSED = 0;
39 // public static final String KEYPAD_COMMAND_CHARACTERS = "0123456789*#<>";
40 public static final String KEYPAD_COMMAND_REGEX = "^[0-9A-H*#<>]+$";
42 private static final String TERM = "\r\n";
44 private static final String COMMAND_REBOOT = "=";
45 private static final String COMMAND_CONFIG = "C";
46 private static final String COMMAND_ZONE = "L";
47 private static final String COMMAND_ERROR = "E";
48 private static final String COMMAND_VERSION = "V";
49 private static final String COMMAND_ADDRMSG = "K";
50 private static final String COMMAND_ACKCRC = "R";
52 public final String command;
54 public ADCommand(String command) {
55 this.command = command + TERM;
59 public String toString() {
63 public static ADCommand reboot() {
64 return new ADCommand(COMMAND_REBOOT);
68 * Construct an AD configuration command. If configParam is null, a query configuration command will be created.
69 * If configParam consists of one or more NAME=value pairs (separated by {@code '&'} characters), a set
70 * configuration command will be created. The validity of configParam is not checked.
72 * @param configParam String containing parameters to set or null
73 * @return ADCommand object containing the constructed command
75 public static ADCommand config(@Nullable String configParam) {
76 if (configParam == null) {
77 return new ADCommand(COMMAND_CONFIG);
79 return new ADCommand(COMMAND_CONFIG + configParam);
84 * Construct an AD command to set the state of an emulated zone.
86 * @param zone The emulated zone number (0-99) for the command.
87 * @param state The new state (0 or 1) for the emulated zone.
88 * @return ADCommand object containing the constructed command
89 * @throws IllegalArgumentException
91 public static ADCommand setZone(int zone, int state) throws IllegalArgumentException {
92 if (zone < 0 || zone > 99 || state < 0 || state > 1) {
93 throw new IllegalArgumentException("Invalid parameter(s)");
95 return new ADCommand(String.format("%s%02d%d", COMMAND_ZONE, zone, state));
99 * Construct an AD command to get and clear the error counters.
101 * @return ADCommand object containing the constructed command
103 public static ADCommand getErrors() {
104 return new ADCommand(COMMAND_ERROR);
108 * Construct an AD command to request a version info message.
110 * @return ADCommand object containing the constructed command
112 public static ADCommand getVersion() {
113 return new ADCommand(COMMAND_VERSION);
117 * Construct an AD command to send a message from a specific partition or keypad address, rather than from the Alarm
118 * Decoder unit's configured address.
120 * @param address The keypad address or partition (0-99) from which to send the command
121 * @param message A String containing the message to send. Length must be > 0.
122 * @return ADCommand object containing the constructed command
123 * @throws IllegalArgumentException
125 public static ADCommand addressedMessage(int address, String message) throws IllegalArgumentException {
126 if (address < 0 || address > 99 || message.length() < 1) {
127 throw new IllegalArgumentException("Invalid parameter(s)");
129 return new ADCommand(String.format("%s%02d%s", COMMAND_ADDRMSG, address, message));
133 * Construct an AD command to acknowledge that a received CRC message was valid.
135 * @return ADCommand object containing the constructed command
137 public static ADCommand ackCRC() {
138 return new ADCommand(COMMAND_ACKCRC);