aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/org/traccar/forward/EventForwarderMqtt.java3
-rw-r--r--src/main/java/org/traccar/forward/MqttClient.java10
-rw-r--r--src/main/java/org/traccar/forward/PositionForwarderMqtt.java17
3 files changed, 12 insertions, 18 deletions
diff --git a/src/main/java/org/traccar/forward/EventForwarderMqtt.java b/src/main/java/org/traccar/forward/EventForwarderMqtt.java
index 7d1c7dd3c..ea075fc6c 100644
--- a/src/main/java/org/traccar/forward/EventForwarderMqtt.java
+++ b/src/main/java/org/traccar/forward/EventForwarderMqtt.java
@@ -36,9 +36,8 @@ public class EventForwarderMqtt implements EventForwarder {
@Override
public void forward(EventData eventData, ResultHandler resultHandler) {
- String payload;
try {
- payload = objectMapper.writeValueAsString(eventData);
+ String payload = objectMapper.writeValueAsString(eventData);
mqttClient.publish(topic, payload, (message, e) -> resultHandler.onResult(e == null, e));
} catch (JsonProcessingException e) {
resultHandler.onResult(false, e);
diff --git a/src/main/java/org/traccar/forward/MqttClient.java b/src/main/java/org/traccar/forward/MqttClient.java
index ad3065b58..416a167ec 100644
--- a/src/main/java/org/traccar/forward/MqttClient.java
+++ b/src/main/java/org/traccar/forward/MqttClient.java
@@ -28,7 +28,9 @@ import com.hivemq.client.mqtt.mqtt5.message.auth.Mqtt5SimpleAuth;
import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5PublishResult;
public class MqttClient {
+
private final Mqtt5AsyncClient client;
+
MqttClient(String url) {
URI uri;
try {
@@ -50,8 +52,7 @@ public class MqttClient {
});
}
-
- private Mqtt5SimpleAuth getSimpleAuth(final URI uri) {
+ private Mqtt5SimpleAuth getSimpleAuth(URI uri) {
String userInfo = uri.getUserInfo();
Mqtt5SimpleAuth simpleAuth = null;
if (userInfo != null) {
@@ -66,9 +67,8 @@ public class MqttClient {
return simpleAuth;
}
- public void publish(final String pubTopic, final String payload,
- final BiConsumer<? super Mqtt5PublishResult,
- ? super Throwable> whenComplete) {
+ public void publish(
+ String pubTopic, String payload, BiConsumer<? super Mqtt5PublishResult, ? super Throwable> whenComplete) {
client.publishWith().topic(pubTopic).qos(MqttQos.AT_LEAST_ONCE).payload(payload.getBytes()).send()
.whenComplete(whenComplete);
}
diff --git a/src/main/java/org/traccar/forward/PositionForwarderMqtt.java b/src/main/java/org/traccar/forward/PositionForwarderMqtt.java
index abbdc538a..53f0ced19 100644
--- a/src/main/java/org/traccar/forward/PositionForwarderMqtt.java
+++ b/src/main/java/org/traccar/forward/PositionForwarderMqtt.java
@@ -22,16 +22,11 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class PositionForwarderMqtt implements PositionForwarder {
- private final MqttClient mqttClient;
- @Override
- public void forward(final PositionData positionData, final ResultHandler resultHandler) {
- publish(topic, positionData, resultHandler);
- }
+ private final MqttClient mqttClient;
private final ObjectMapper objectMapper;
- protected final String topic;
-
+ private final String topic;
public PositionForwarderMqtt(final Config config, final ObjectMapper objectMapper) {
this.topic = config.getString(Keys.FORWARD_TOPIC);
@@ -39,11 +34,11 @@ public class PositionForwarderMqtt implements PositionForwarder {
this.objectMapper = objectMapper;
}
- protected void publish(final String pubTopic, final Object object, final ResultHandler resultHandler) {
- final String payload;
+ @Override
+ public void forward(PositionData positionData, ResultHandler resultHandler) {
try {
- payload = objectMapper.writeValueAsString(object);
- mqttClient.publish(pubTopic, payload, (message, e) -> resultHandler.onResult(e == null, e));
+ String payload = objectMapper.writeValueAsString(topic);
+ mqttClient.publish(topic, payload, (message, e) -> resultHandler.onResult(e == null, e));
} catch (JsonProcessingException e) {
resultHandler.onResult(false, e);
}