2 * Copyright (c) 2010-2022 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()));
99 throw new IllegalArgumentException(
100 String.format("Unable to parse gateway command with code '%s' and message '%s'", code, message));
103 private static Map<String, @Nullable String> getSupportedCommands() {
104 Map<String, @Nullable String> c = new HashMap<>();
106 c.put(GatewayCommandCode.TemperatureTemporary, null);
107 c.put(GatewayCommandCode.TemperatureConstant, null);
108 c.put(GatewayCommandCode.TemperatureOutside, null);
109 c.put(GatewayCommandCode.SetClock, null);
110 c.put(GatewayCommandCode.HotWater, null);
111 c.put(GatewayCommandCode.PrintReport, "A,B,C,G,I,L,M,O,P,R,S,T,V,W");
112 c.put(GatewayCommandCode.PrintSummary, "0,1");
113 c.put(GatewayCommandCode.GateWay, "0,1,R");
114 c.put(GatewayCommandCode.LedA, "R,X,T,B,O,F,H,W,C,E,M,P");
115 c.put(GatewayCommandCode.LedB, "R,X,T,B,O,F,H,W,C,E,M,P");
116 c.put(GatewayCommandCode.LedC, "R,X,T,B,O,F,H,W,C,E,M,P");
117 c.put(GatewayCommandCode.LedD, "R,X,T,B,O,F,H,W,C,E,M,P");
118 c.put(GatewayCommandCode.LedE, "R,X,T,B,O,F,H,W,C,E,M,P");
119 c.put(GatewayCommandCode.LedF, "R,X,T,B,O,F,H,W,C,E,M,P");
120 c.put(GatewayCommandCode.GpioA, "0,1,2,3,4,5,6,7");
121 c.put(GatewayCommandCode.GpioB, "0,1,2,3,4,5,6,7");
122 c.put(GatewayCommandCode.SetBack, null);
123 c.put(GatewayCommandCode.TemperatureSensor, "O,R");
124 c.put(GatewayCommandCode.AddAlternative, null);
125 c.put(GatewayCommandCode.DeleteAlternative, null);
126 c.put(GatewayCommandCode.UnknownID, null);
127 c.put(GatewayCommandCode.KnownID, null);
128 c.put(GatewayCommandCode.PriorityMessage, null);
129 c.put(GatewayCommandCode.SetResponse, null);
130 c.put(GatewayCommandCode.ClearResponse, null);
131 c.put(GatewayCommandCode.SetpointHeating, null);
132 c.put(GatewayCommandCode.SetpointWater, null);
133 c.put(GatewayCommandCode.MaximumModulation, null);
134 c.put(GatewayCommandCode.ControlSetpoint, null);
135 c.put(GatewayCommandCode.ControlSetpoint2, null);
136 c.put(GatewayCommandCode.CentralHeating, "0,1");
137 c.put(GatewayCommandCode.CentralHeating2, "0,1");
138 c.put(GatewayCommandCode.VentilationSetpoint, null);
139 c.put(GatewayCommandCode.Reset, null);
140 c.put(GatewayCommandCode.IgnoreTransition, "0,1");
141 c.put(GatewayCommandCode.OverrideHighbyte, "0,1");
142 c.put(GatewayCommandCode.ForceThermostat, "0,1");
143 c.put(GatewayCommandCode.VoltageReference, "0,1,2,3,4,5,6,7,8,9");
144 c.put(GatewayCommandCode.DebugPointer, null);