From 40acf3bd81ca74684ebdba57357dc5b112559a69 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 16 Mar 2024 07:43:42 -0700 Subject: Clean up formatting --- .../java/org/traccar/forward/EventForwarderMqtt.java | 3 +-- src/main/java/org/traccar/forward/MqttClient.java | 10 +++++----- .../java/org/traccar/forward/PositionForwarderMqtt.java | 17 ++++++----------- 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 whenComplete) { + public void publish( + String pubTopic, String payload, BiConsumer 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); } -- cgit v1.2.3