23
23
import org .eclipse .jdt .annotation .Nullable ;
24
24
import org .openhab .core .library .types .DecimalType ;
25
25
import org .openhab .core .library .types .OnOffType ;
26
- import org .openhab .core .library . types . StringType ;
26
+ import org .openhab .core .thing . Thing ;
27
27
import org .slf4j .Logger ;
28
28
import org .slf4j .LoggerFactory ;
29
29
35
35
*/
36
36
@ NonNullByDefault
37
37
public class AnthemCommandParser {
38
- private static final Pattern NUM_AVAILABLE_INPUTS_PATTERN = Pattern .compile ("ICN([0-9])" );
38
+ private static final Pattern NUM_AVAILABLE_INPUTS_PATTERN = Pattern .compile ("ICN([0-9]{1,2} )" );
39
39
private static final Pattern INPUT_SHORT_NAME_PATTERN = Pattern .compile ("ISN([0-9][0-9])(\\ p{ASCII}*)" );
40
40
private static final Pattern INPUT_LONG_NAME_PATTERN = Pattern .compile ("ILN([0-9][0-9])(\\ p{ASCII}*)" );
41
41
private static final Pattern POWER_PATTERN = Pattern .compile ("Z([0-9])POW([01])" );
@@ -45,39 +45,27 @@ public class AnthemCommandParser {
45
45
46
46
private Logger logger = LoggerFactory .getLogger (AnthemCommandParser .class );
47
47
48
- private AnthemHandler handler ;
48
+ private Map <String , String > inputShortNamesMap = new HashMap <>();
49
+ private Map <String , String > inputLongNamesMap = new HashMap <>();
49
50
50
- private Map <Integer , String > inputShortNamesMap = new HashMap <>();
51
- private Map <Integer , String > inputLongNamesMap = new HashMap <>();
52
-
53
- private int numAvailableInputs ;
54
-
55
- public AnthemCommandParser (AnthemHandler anthemHandler ) {
56
- this .handler = anthemHandler ;
57
- }
58
-
59
- public int getNumAvailableInputs () {
60
- return numAvailableInputs ;
61
- }
62
-
63
- public void parseMessage (String command ) {
51
+ public @ Nullable AnthemUpdate parseCommand (String command ) {
64
52
if (!isValidCommand (command )) {
65
- return ;
53
+ return null ;
66
54
}
67
55
// Strip off the termination char and any whitespace
68
56
String cmd = command .substring (0 , command .indexOf (COMMAND_TERMINATION_CHAR )).trim ();
69
57
70
58
// Zone command
71
59
if (cmd .startsWith ("Z" )) {
72
- parseZoneCommand (cmd );
60
+ return parseZoneCommand (cmd );
73
61
}
74
62
// Information command
75
63
else if (cmd .startsWith ("ID" )) {
76
- parseInformationCommand (cmd );
64
+ return parseInformationCommand (cmd );
77
65
}
78
66
// Number of inputs
79
67
else if (cmd .startsWith ("ICN" )) {
80
- parseNumberOfAvailableInputsCommand (cmd );
68
+ return parseNumberOfAvailableInputsCommand (cmd );
81
69
}
82
70
// Input short name
83
71
else if (cmd .startsWith ("ISN" )) {
@@ -95,6 +83,15 @@ else if (cmd.startsWith("!")) {
95
83
else {
96
84
logger .trace ("Command parser doesn't know how to handle command: '{}'" , cmd );
97
85
}
86
+ return null ;
87
+ }
88
+
89
+ public @ Nullable String getInputShortName (String input ) {
90
+ return inputShortNamesMap .get (input );
91
+ }
92
+
93
+ public @ Nullable String getInputLongName (String input ) {
94
+ return inputLongNamesMap .get (input );
98
95
}
99
96
100
97
private boolean isValidCommand (String command ) {
@@ -106,45 +103,47 @@ private boolean isValidCommand(String command) {
106
103
return true ;
107
104
}
108
105
109
- private void parseZoneCommand (String command ) {
106
+ private @ Nullable AnthemUpdate parseZoneCommand (String command ) {
110
107
// Power update
111
108
if (command .contains ("POW" )) {
112
- parsePower (command );
109
+ return parsePower (command );
113
110
}
114
111
// Volume update
115
112
else if (command .contains ("VOL" )) {
116
- parseVolume (command );
113
+ return parseVolume (command );
117
114
}
118
115
// Mute update
119
116
else if (command .contains ("MUT" )) {
120
- parseMute (command );
117
+ return parseMute (command );
121
118
}
122
119
// Active input
123
120
else if (command .contains ("INP" )) {
124
- parseActiveInput (command );
121
+ return parseActiveInput (command );
125
122
}
123
+ return null ;
126
124
}
127
125
128
- private void parseInformationCommand (String command ) {
126
+ private @ Nullable AnthemUpdate parseInformationCommand (String command ) {
129
127
String value = command .substring (3 , command .length ());
128
+ AnthemUpdate update = null ;
130
129
switch (command .substring (2 , 3 )) {
131
130
case "M" :
132
- handler . setModel ( value );
131
+ update = AnthemUpdate . createPropertyUpdate ( Thing . PROPERTY_MODEL_ID , value );
133
132
break ;
134
133
case "R" :
135
- handler . setRegion ( value );
134
+ update = AnthemUpdate . createPropertyUpdate ( PROPERTY_REGION , value );
136
135
break ;
137
136
case "S" :
138
- handler . setSoftwareVersion ( value );
137
+ update = AnthemUpdate . createPropertyUpdate ( Thing . PROPERTY_FIRMWARE_VERSION , value );
139
138
break ;
140
139
case "B" :
141
- handler . setSoftwareBuildDate ( value );
140
+ update = AnthemUpdate . createPropertyUpdate ( PROPERTY_SOFTWARE_BUILD_DATE , value );
142
141
break ;
143
142
case "H" :
144
- handler . setHardwareVersion ( value );
143
+ update = AnthemUpdate . createPropertyUpdate ( Thing . PROPERTY_HARDWARE_VERSION , value );
145
144
break ;
146
145
case "N" :
147
- handler . setMacAddress ( value );
146
+ update = AnthemUpdate . createPropertyUpdate ( Thing . PROPERTY_MAC_ADDRESS , value );
148
147
break ;
149
148
case "Q" :
150
149
// Ignore
@@ -153,21 +152,20 @@ private void parseInformationCommand(String command) {
153
152
logger .debug ("Unknown info type" );
154
153
break ;
155
154
}
155
+ return update ;
156
156
}
157
157
158
- private void parseNumberOfAvailableInputsCommand (String command ) {
158
+ private @ Nullable AnthemUpdate parseNumberOfAvailableInputsCommand (String command ) {
159
159
Matcher matcher = NUM_AVAILABLE_INPUTS_PATTERN .matcher (command );
160
160
if (matcher != null ) {
161
161
try {
162
162
matcher .find ();
163
- String numAvailableInputsStr = matcher .group (1 );
164
- DecimalType numAvailableInputs = DecimalType .valueOf (numAvailableInputsStr );
165
- handler .setNumAvailableInputs (numAvailableInputs .intValue ());
166
- this .numAvailableInputs = numAvailableInputs .intValue ();
167
- } catch (NumberFormatException | IndexOutOfBoundsException | IllegalStateException e ) {
163
+ return AnthemUpdate .createPropertyUpdate (PROPERTY_NUM_AVAILABLE_INPUTS , matcher .group (1 ));
164
+ } catch (IndexOutOfBoundsException | IllegalStateException e ) {
168
165
logger .debug ("Parsing exception on command: {}" , command , e );
169
166
}
170
167
}
168
+ return null ;
171
169
}
172
170
173
171
private void parseInputShortNameCommand (String command ) {
@@ -182,11 +180,11 @@ private void parseErrorCommand(String command) {
182
180
logger .info ("Command was not processed successfully by the device: '{}'" , command );
183
181
}
184
182
185
- private void parseInputName (String command , @ Nullable Matcher matcher , Map <Integer , String > map ) {
183
+ private void parseInputName (String command , @ Nullable Matcher matcher , Map <String , String > map ) {
186
184
if (matcher != null ) {
187
185
try {
188
186
matcher .find ();
189
- int input = Integer . parseInt ( matcher .group (1 ) );
187
+ String input = matcher .group (1 );
190
188
String inputName = matcher .group (2 );
191
189
map .putIfAbsent (input , inputName );
192
190
} catch (NumberFormatException | IndexOutOfBoundsException | IllegalStateException e ) {
@@ -195,69 +193,65 @@ private void parseInputName(String command, @Nullable Matcher matcher, Map<Integ
195
193
}
196
194
}
197
195
198
- private void parsePower (String command ) {
196
+ private @ Nullable AnthemUpdate parsePower (String command ) {
199
197
Matcher mmatcher = POWER_PATTERN .matcher (command );
200
198
if (mmatcher != null ) {
201
199
try {
202
200
mmatcher .find ();
203
201
String zone = mmatcher .group (1 );
204
202
String power = mmatcher .group (2 );
205
- handler . updateChannelState (zone , CHANNEL_POWER , "1" . equals ( power ) ? OnOffType . ON : OnOffType . OFF );
206
- handler . checkPowerStatusChange ( zone , power );
203
+ return AnthemUpdate . createStateUpdate (zone , CHANNEL_POWER ,
204
+ "1" . equals ( power ) ? OnOffType . ON : OnOffType . OFF );
207
205
} catch (IndexOutOfBoundsException | IllegalStateException e ) {
208
206
logger .debug ("Parsing exception on command: {}" , command , e );
209
207
}
210
208
}
209
+ return null ;
211
210
}
212
211
213
- private void parseVolume (String command ) {
212
+ private @ Nullable AnthemUpdate parseVolume (String command ) {
214
213
Matcher matcher = VOLUME_PATTERN .matcher (command );
215
214
if (matcher != null ) {
216
215
try {
217
216
matcher .find ();
218
217
String zone = matcher .group (1 );
219
218
String volume = matcher .group (2 );
220
- handler . updateChannelState (zone , CHANNEL_VOLUME_DB , DecimalType .valueOf (volume ));
219
+ return AnthemUpdate . createStateUpdate (zone , CHANNEL_VOLUME_DB , DecimalType .valueOf (volume ));
221
220
} catch (NumberFormatException | IndexOutOfBoundsException | IllegalStateException e ) {
222
221
logger .debug ("Parsing exception on command: {}" , command , e );
223
222
}
224
223
}
224
+ return null ;
225
225
}
226
226
227
- private void parseMute (String command ) {
227
+ private @ Nullable AnthemUpdate parseMute (String command ) {
228
228
Matcher matcher = MUTE_PATTERN .matcher (command );
229
229
if (matcher != null ) {
230
230
try {
231
231
matcher .find ();
232
232
String zone = matcher .group (1 );
233
233
String mute = matcher .group (2 );
234
- handler .updateChannelState (zone , CHANNEL_MUTE , "1" .equals (mute ) ? OnOffType .ON : OnOffType .OFF );
234
+ return AnthemUpdate .createStateUpdate (zone , CHANNEL_MUTE ,
235
+ "1" .equals (mute ) ? OnOffType .ON : OnOffType .OFF );
235
236
} catch (IndexOutOfBoundsException | IllegalStateException e ) {
236
237
logger .debug ("Parsing exception on command: {}" , command , e );
237
238
}
238
239
}
240
+ return null ;
239
241
}
240
242
241
- private void parseActiveInput (String command ) {
243
+ private @ Nullable AnthemUpdate parseActiveInput (String command ) {
242
244
Matcher matcher = ACTIVE_INPUT_PATTERN .matcher (command );
243
245
if (matcher != null ) {
244
246
try {
245
247
matcher .find ();
246
248
String zone = matcher .group (1 );
247
249
DecimalType activeInput = DecimalType .valueOf (matcher .group (2 ));
248
- handler .updateChannelState (zone , CHANNEL_ACTIVE_INPUT , activeInput );
249
- String name ;
250
- name = inputShortNamesMap .get (activeInput .intValue ());
251
- if (name != null ) {
252
- handler .updateChannelState (zone , CHANNEL_ACTIVE_INPUT_SHORT_NAME , new StringType (name ));
253
- }
254
- name = inputShortNamesMap .get (activeInput .intValue ());
255
- if (name != null ) {
256
- handler .updateChannelState (zone , CHANNEL_ACTIVE_INPUT_LONG_NAME , new StringType (name ));
257
- }
250
+ return AnthemUpdate .createStateUpdate (zone , CHANNEL_ACTIVE_INPUT , activeInput );
258
251
} catch (NumberFormatException | IndexOutOfBoundsException | IllegalStateException e ) {
259
252
logger .debug ("Parsing exception on command: {}" , command , e );
260
253
}
261
254
}
255
+ return null ;
262
256
}
263
257
}
0 commit comments