Skip to content

Commit 531a217

Browse files
committed
Merge branch 'develop'
2 parents 5aca617 + 759f87c commit 531a217

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
<modelVersion>4.0.0</modelVersion>
1717
<artifactId>mqtt-client</artifactId>
18-
<version>0.1.7</version>
18+
<version>0.1.8</version>
1919
<name>MqttClient</name>
2020
<packaging>jar</packaging>
2121

src/main/java/info/unterrainer/commons/mqttclient/MqttClient.java

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)