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.openthermgateway.internal;
15 import java.util.HashMap;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
22 * The {@link GatewayCommand} is used to validate and match commands send through the binding
23 * to the OpenTherm gateway device.
25 * @author Arjen Korevaar - Initial contribution
28 public class GatewayCommand {
29 private static final Map<String, @Nullable String> SUPPORTEDCOMMANDS = getSupportedCommands();
32 private String validationSet;
33 private String message;
35 public String getCode() {
39 public String getMessage() {
43 public String getValidationSet() {
47 public String toFullString() {
48 return this.code + "=" + this.message;
51 private GatewayCommand(String code, String message, String validationSet) throws IllegalArgumentException {
53 this.message = message;
54 this.validationSet = validationSet;
57 throw new IllegalArgumentException(
58 String.format("Invalid value '%s' for code '%s'", this.message, this.code));
62 private boolean validate() {
63 if (this.validationSet.isEmpty()) {
67 String[] validations = this.validationSet.split(",");
69 for (String validation : validations) {
70 if (this.message.equals(validation)) {
78 public static GatewayCommand parse(@Nullable String code, String message) throws IllegalArgumentException {
79 if ((code == null || code.isEmpty()) && message.length() > 2 && message.charAt(2) == '=') {
80 return parse(message.substring(0, 2), message.substring(3));
83 if (code != null && code.length() == 2) {
84 String codeUpperCase = code.toUpperCase();
86 if (SUPPORTEDCOMMANDS.containsKey(codeUpperCase)) {
87 String validateSet = SUPPORTEDCOMMANDS.get(codeUpperCase);
89 if (validateSet == null) {
93 return new GatewayCommand(codeUpperCase, message, validateSet);
95 throw new IllegalArgumentException(String.format("Unsupported gateway code '%s'", code.toUpperCase()));
97 throw new IllegalArgumentException(
98 String.format("Unable to parse gateway command with code '%s' and message '%s'", code, message));
101 private static Map<String, @Nullable String> getSupportedCommands() {
102 Map<String, @Nullable String> c = new HashMap<>();
104 c.put(GatewayCommandCode.TEMPERATURETEMPORARY, null);
105 c.put(GatewayCommandCode.TEMPERATURECONSTANT, null);
106 c.put(GatewayCommandCode.TEMPERATUREOUTSIDE, null);
107 c.put(GatewayCommandCode.SETCLOCK, null);
108 c.put(GatewayCommandCode.HOTWATER, null);
109 c.put(GatewayCommandCode.PRINTREPORT, "A,B,C,G,I,L,M,O,P,R,S,T,V,W");
110 c.put(GatewayCommandCode.PRINTSUMMARY, "0,1");
111 c.put(GatewayCommandCode.GATEWAY, "0,1,R");
112 c.put(GatewayCommandCode.LEDA, "R,X,T,B,O,F,H,W,C,E,M,P");
113 c.put(GatewayCommandCode.LEDB, "R,X,T,B,O,F,H,W,C,E,M,P");
114 c.put(GatewayCommandCode.LEDC, "R,X,T,B,O,F,H,W,C,E,M,P");
115 c.put(GatewayCommandCode.LEDD, "R,X,T,B,O,F,H,W,C,E,M,P");
116 c.put(GatewayCommandCode.LEDE, "R,X,T,B,O,F,H,W,C,E,M,P");
117 c.put(GatewayCommandCode.LEDF, "R,X,T,B,O,F,H,W,C,E,M,P");
118 c.put(GatewayCommandCode.GPIOA, "0,1,2,3,4,5,6,7");
119 c.put(GatewayCommandCode.GPIOB, "0,1,2,3,4,5,6,7");
120 c.put(GatewayCommandCode.SETBACK, null);
121 c.put(GatewayCommandCode.TEMPERATURESENSOR, "O,R");
122 c.put(GatewayCommandCode.ADDALTERNATIVE, null);
123 c.put(GatewayCommandCode.DELETEALTERNATIVE, null);
124 c.put(GatewayCommandCode.UNKNOWNID, null);
125 c.put(GatewayCommandCode.KNOWNID, null);
126 c.put(GatewayCommandCode.PRIORITYMESSAGE, null);
127 c.put(GatewayCommandCode.SETRESPONSE, null);
128 c.put(GatewayCommandCode.CLEARRESPONSE, null);
129 c.put(GatewayCommandCode.SETPOINTHEATING, null);
130 c.put(GatewayCommandCode.SETPOINTWATER, null);
131 c.put(GatewayCommandCode.MAXIMUMMODULATION, null);
132 c.put(GatewayCommandCode.CONTROLSETPOINT, null);
133 c.put(GatewayCommandCode.CONTROLSETPOINT2, null);
134 c.put(GatewayCommandCode.CENTRALHEATING, "0,1");
135 c.put(GatewayCommandCode.CENTRALHEATING2, "0,1");
136 c.put(GatewayCommandCode.VENTILATIONSETPOINT, null);
137 c.put(GatewayCommandCode.RESET, null);
138 c.put(GatewayCommandCode.IGNORETRANSITION, "0,1");
139 c.put(GatewayCommandCode.OVERRIDEHIGHBYTE, "0,1");
140 c.put(GatewayCommandCode.FORCETHERMOSTAT, "0,1");
141 c.put(GatewayCommandCode.VOLTAGEREFERENCE, "0,1,2,3,4,5,6,7,8,9");
142 c.put(GatewayCommandCode.DEBUGPOINTER, null);