]> git.basschouten.com Git - openhab-addons.git/blob
7bfbd55523b709bdfcd54dd43c8d91d9a6a85949
[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.alarmdecoder.internal.protocol;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17
18 /**
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.
21  *
22  * @author Bob Adair - Initial contribution
23  */
24 @NonNullByDefault
25 public final class ADCommand {
26
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";
35
36     public static final int ZONE_OPEN = 1;
37     public static final int ZONE_CLOSED = 0;
38
39     // public static final String KEYPAD_COMMAND_CHARACTERS = "0123456789*#<>";
40     public static final String KEYPAD_COMMAND_REGEX = "^[0-9A-H*#<>]+$";
41
42     private static final String TERM = "\r\n";
43
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";
51
52     public final String command;
53
54     public ADCommand(String command) {
55         this.command = command + TERM;
56     }
57
58     @Override
59     public String toString() {
60         return command;
61     }
62
63     public static ADCommand reboot() {
64         return new ADCommand(COMMAND_REBOOT);
65     }
66
67     /**
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.
71      *
72      * @param configParam String containing parameters to set or null
73      * @return ADCommand object containing the constructed command
74      */
75     public static ADCommand config(@Nullable String configParam) {
76         if (configParam == null) {
77             return new ADCommand(COMMAND_CONFIG);
78         } else {
79             return new ADCommand(COMMAND_CONFIG + configParam);
80         }
81     }
82
83     /**
84      * Construct an AD command to set the state of an emulated zone.
85      *
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
90      */
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)");
94         }
95         return new ADCommand(String.format("%s%02d%d", COMMAND_ZONE, zone, state));
96     }
97
98     /**
99      * Construct an AD command to get and clear the error counters.
100      *
101      * @return ADCommand object containing the constructed command
102      */
103     public static ADCommand getErrors() {
104         return new ADCommand(COMMAND_ERROR);
105     }
106
107     /**
108      * Construct an AD command to request a version info message.
109      *
110      * @return ADCommand object containing the constructed command
111      */
112     public static ADCommand getVersion() {
113         return new ADCommand(COMMAND_VERSION);
114     }
115
116     /**
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.
119      *
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
124      */
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)");
128         }
129         return new ADCommand(String.format("%s%02d%s", COMMAND_ADDRMSG, address, message));
130     }
131
132     /**
133      * Construct an AD command to acknowledge that a received CRC message was valid.
134      *
135      * @return ADCommand object containing the constructed command
136      */
137     public static ADCommand ackCRC() {
138         return new ADCommand(COMMAND_ACKCRC);
139     }
140 }