]> git.basschouten.com Git - openhab-addons.git/blob
b2db865ac5b3f9cd5137f73ce41ef71dca6e17d0
[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.openthermgateway.internal;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 /**
22  * The {@link GatewayCommand} is used to validate and match commands send through the binding
23  * to the OpenTherm gateway device.
24  *
25  * @author Arjen Korevaar - Initial contribution
26  */
27 @NonNullByDefault
28 public class GatewayCommand {
29     private static final Map<String, @Nullable String> SUPPORTEDCOMMANDS = getSupportedCommands();
30
31     private String code;
32     private String validationSet;
33     private String message;
34
35     public String getCode() {
36         return code;
37     }
38
39     public String getMessage() {
40         return this.message;
41     }
42
43     public String getValidationSet() {
44         return validationSet;
45     }
46
47     public String toFullString() {
48         return this.code + "=" + this.message;
49     }
50
51     private GatewayCommand(String code, String message, String validationSet) throws IllegalArgumentException {
52         this.code = code;
53         this.message = message;
54         this.validationSet = validationSet;
55
56         if (!validate()) {
57             throw new IllegalArgumentException(
58                     String.format("Invalid value '%s' for code '%s'", this.message, this.code));
59         }
60     }
61
62     private boolean validate() {
63         if (this.validationSet.isEmpty()) {
64             return true;
65         }
66
67         String[] validations = this.validationSet.split(",");
68
69         for (String validation : validations) {
70             if (this.message.equals(validation)) {
71                 return true;
72             }
73         }
74
75         return false;
76     }
77
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));
81         }
82
83         if (code != null && code.length() == 2) {
84             String codeUpperCase = code.toUpperCase();
85
86             if (SUPPORTEDCOMMANDS.containsKey(codeUpperCase)) {
87                 String validateSet = SUPPORTEDCOMMANDS.get(codeUpperCase);
88
89                 if (validateSet == null) {
90                     validateSet = "";
91                 }
92
93                 return new GatewayCommand(codeUpperCase, message, validateSet);
94             }
95             throw new IllegalArgumentException(String.format("Unsupported gateway code '%s'", code.toUpperCase()));
96         }
97         throw new IllegalArgumentException(
98                 String.format("Unable to parse gateway command with code '%s' and message '%s'", code, message));
99     }
100
101     private static Map<String, @Nullable String> getSupportedCommands() {
102         Map<String, @Nullable String> c = new HashMap<>();
103
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);
143
144         return c;
145     }
146 }