@@ -134,42 +134,46 @@ public void unsubscribe(final String topicFilter) {
134134 public <T > void subscribe (final String topic , final Class <?> type , final BiConsumer <String , T > setter ) {
135135
136136 subscribe (topic , (actualTopic , actualMessage ) -> {
137- String stringValue = new String (actualMessage .getPayload ());
138- Object v = stringValue ;
139- log .info ("subscription fired for topic [{}] with value [{}]" , actualTopic , stringValue );
140-
141137 try {
142- v = jsonMapper .fromStringTo (type , stringValue );
143- } catch (Exception e ) {
138+ String stringValue = new String (actualMessage .getPayload ());
139+ Object v = stringValue ;
140+ log .info ("subscription fired for topic [{}] with value [{}]" , actualTopic , stringValue );
141+
144142 try {
145- if (type == Integer .class )
146- v = Integer .parseInt (stringValue );
147- if (type == Long .class )
148- v = Long .parseLong (stringValue );
149- if (type == Float .class )
150- v = Float .parseFloat (stringValue );
151- if (type == Double .class )
152- v = Double .parseDouble (stringValue );
153- if (type == Boolean .class )
154- if ("1" .equals (stringValue .trim ()) || "on" .equalsIgnoreCase (stringValue .trim ())
155- || "true" .equalsIgnoreCase (stringValue .trim ())
156- || "open" .equalsIgnoreCase (stringValue .trim ()))
157- v = true ;
158- else if ("0" .equals (stringValue .trim ()) || "off" .equalsIgnoreCase (stringValue .trim ())
159- || "false" .equalsIgnoreCase (stringValue .trim ())
160- || "close" .equalsIgnoreCase (stringValue .trim ())
161- || "overpower" .equalsIgnoreCase (stringValue .trim ()))
162- v = false ;
163- else
164- v = Boolean .parseBoolean (stringValue );
165- } catch (NumberFormatException e1 ) {
166- log .warn ("Error parsing to type [{}]. Falling back to string." , type .getSimpleName ());
167- // Fallback is String.
168- setter .accept (actualTopic , (T ) String .class .cast (v ));
169- return ;
143+ v = jsonMapper .fromStringTo (type , stringValue );
144+ } catch (Exception e ) {
145+ try {
146+ if (type == Integer .class )
147+ v = Integer .parseInt (stringValue );
148+ if (type == Long .class )
149+ v = Long .parseLong (stringValue );
150+ if (type == Float .class )
151+ v = Float .parseFloat (stringValue );
152+ if (type == Double .class )
153+ v = Double .parseDouble (stringValue );
154+ if (type == Boolean .class )
155+ if ("1" .equals (stringValue .trim ()) || "on" .equalsIgnoreCase (stringValue .trim ())
156+ || "true" .equalsIgnoreCase (stringValue .trim ())
157+ || "open" .equalsIgnoreCase (stringValue .trim ()))
158+ v = true ;
159+ else if ("0" .equals (stringValue .trim ()) || "off" .equalsIgnoreCase (stringValue .trim ())
160+ || "false" .equalsIgnoreCase (stringValue .trim ())
161+ || "close" .equalsIgnoreCase (stringValue .trim ())
162+ || "overpower" .equalsIgnoreCase (stringValue .trim ()))
163+ v = false ;
164+ else
165+ v = Boolean .parseBoolean (stringValue );
166+ } catch (NumberFormatException e1 ) {
167+ log .warn ("Error parsing to type [{}]. Falling back to string." , type .getSimpleName ());
168+ // Fallback is String.
169+ setter .accept (actualTopic , (T ) String .class .cast (v ));
170+ return ;
171+ }
170172 }
173+ setter .accept (actualTopic , (T ) type .cast (v ));
174+ } catch (Throwable e ) {
175+ log .error ("Exception in MQTT subscription." , e );
171176 }
172- setter .accept (actualTopic , (T ) type .cast (v ));
173177 });
174178 }
175179
0 commit comments