aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/forward
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/traccar/forward')
-rw-r--r--src/main/java/org/traccar/forward/AmqpClient.java58
-rw-r--r--src/main/java/org/traccar/forward/EventData.java78
-rw-r--r--src/main/java/org/traccar/forward/EventForwarder.java20
-rw-r--r--src/main/java/org/traccar/forward/EventForwarderAmqp.java48
-rw-r--r--src/main/java/org/traccar/forward/EventForwarderJson.java68
-rw-r--r--src/main/java/org/traccar/forward/EventForwarderKafka.java58
-rw-r--r--src/main/java/org/traccar/forward/EventForwarderMqtt.java100
-rw-r--r--src/main/java/org/traccar/forward/NetworkForwarder.java77
-rw-r--r--src/main/java/org/traccar/forward/PositionData.java45
-rw-r--r--src/main/java/org/traccar/forward/PositionForwarder.java20
-rw-r--r--src/main/java/org/traccar/forward/PositionForwarderAmqp.java48
-rw-r--r--src/main/java/org/traccar/forward/PositionForwarderJson.java86
-rw-r--r--src/main/java/org/traccar/forward/PositionForwarderKafka.java58
-rw-r--r--src/main/java/org/traccar/forward/PositionForwarderRedis.java50
-rw-r--r--src/main/java/org/traccar/forward/PositionForwarderUrl.java166
-rw-r--r--src/main/java/org/traccar/forward/ResultHandler.java20
16 files changed, 1000 insertions, 0 deletions
diff --git a/src/main/java/org/traccar/forward/AmqpClient.java b/src/main/java/org/traccar/forward/AmqpClient.java
new file mode 100644
index 000000000..361cfffee
--- /dev/null
+++ b/src/main/java/org/traccar/forward/AmqpClient.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2023 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.forward;
+
+import com.rabbitmq.client.BuiltinExchangeType;
+import com.rabbitmq.client.Channel;
+import com.rabbitmq.client.Connection;
+import com.rabbitmq.client.ConnectionFactory;
+import com.rabbitmq.client.MessageProperties;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.util.concurrent.TimeoutException;
+
+public class AmqpClient {
+ private final Channel channel;
+ private final String exchange;
+ private final String topic;
+
+ AmqpClient(String connectionUrl, String exchange, String topic) {
+ this.exchange = exchange;
+ this.topic = topic;
+
+ ConnectionFactory factory = new ConnectionFactory();
+ try {
+ factory.setUri(connectionUrl);
+ } catch (NoSuchAlgorithmException | URISyntaxException | KeyManagementException e) {
+ throw new RuntimeException("Error while setting URI for RabbitMQ connection factory", e);
+ }
+
+ try {
+ Connection connection = factory.newConnection();
+ channel = connection.createChannel();
+ channel.exchangeDeclare(exchange, BuiltinExchangeType.TOPIC, true);
+ } catch (IOException | TimeoutException e) {
+ throw new RuntimeException("Error while creating and configuring RabbitMQ channel", e);
+ }
+ }
+
+ public void publishMessage(String message) throws IOException {
+ channel.basicPublish(exchange, topic, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
+ }
+}
diff --git a/src/main/java/org/traccar/forward/EventData.java b/src/main/java/org/traccar/forward/EventData.java
new file mode 100644
index 000000000..4471b10b3
--- /dev/null
+++ b/src/main/java/org/traccar/forward/EventData.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2022 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.forward;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import org.traccar.model.Device;
+import org.traccar.model.Event;
+import org.traccar.model.Geofence;
+import org.traccar.model.Maintenance;
+import org.traccar.model.Position;
+
+@JsonInclude(JsonInclude.Include.NON_NULL)
+public class EventData {
+
+ private Event event;
+
+ public Event getEvent() {
+ return event;
+ }
+
+ public void setEvent(Event event) {
+ this.event = event;
+ }
+
+ private Position position;
+
+ public Position getPosition() {
+ return position;
+ }
+
+ public void setPosition(Position position) {
+ this.position = position;
+ }
+
+ private Device device;
+
+ public Device getDevice() {
+ return device;
+ }
+
+ public void setDevice(Device device) {
+ this.device = device;
+ }
+
+ private Geofence geofence;
+
+ public Geofence getGeofence() {
+ return geofence;
+ }
+
+ public void setGeofence(Geofence geofence) {
+ this.geofence = geofence;
+ }
+
+ private Maintenance maintenance;
+
+ public Maintenance getMaintenance() {
+ return maintenance;
+ }
+
+ public void setMaintenance(Maintenance maintenance) {
+ this.maintenance = maintenance;
+ }
+
+}
diff --git a/src/main/java/org/traccar/forward/EventForwarder.java b/src/main/java/org/traccar/forward/EventForwarder.java
new file mode 100644
index 000000000..1f991c0b5
--- /dev/null
+++ b/src/main/java/org/traccar/forward/EventForwarder.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2022 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.forward;
+
+public interface EventForwarder {
+ void forward(EventData eventData, ResultHandler resultHandler);
+}
diff --git a/src/main/java/org/traccar/forward/EventForwarderAmqp.java b/src/main/java/org/traccar/forward/EventForwarderAmqp.java
new file mode 100644
index 000000000..5c38a4459
--- /dev/null
+++ b/src/main/java/org/traccar/forward/EventForwarderAmqp.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2023 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.forward;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import org.traccar.config.Config;
+import org.traccar.config.Keys;
+
+import java.io.IOException;
+
+public class EventForwarderAmqp implements EventForwarder {
+
+ private final AmqpClient amqpClient;
+ private final ObjectMapper objectMapper;
+
+ public EventForwarderAmqp(Config config, ObjectMapper objectMapper) {
+ String connectionUrl = config.getString(Keys.EVENT_FORWARD_URL);
+ String exchange = config.getString(Keys.EVENT_FORWARD_EXCHANGE);
+ String topic = config.getString(Keys.EVENT_FORWARD_TOPIC);
+ this.objectMapper = objectMapper;
+ amqpClient = new AmqpClient(connectionUrl, exchange, topic);
+ }
+
+ @Override
+ public void forward(EventData eventData, ResultHandler resultHandler) {
+ try {
+ String value = objectMapper.writeValueAsString(eventData);
+ amqpClient.publishMessage(value);
+ resultHandler.onResult(true, null);
+ } catch (IOException e) {
+ resultHandler.onResult(false, e);
+ }
+ }
+}
diff --git a/src/main/java/org/traccar/forward/EventForwarderJson.java b/src/main/java/org/traccar/forward/EventForwarderJson.java
new file mode 100644
index 000000000..df53d3d46
--- /dev/null
+++ b/src/main/java/org/traccar/forward/EventForwarderJson.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2022 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.forward;
+
+import org.traccar.config.Config;
+import org.traccar.config.Keys;
+
+import jakarta.ws.rs.client.Client;
+import jakarta.ws.rs.client.Entity;
+import jakarta.ws.rs.client.InvocationCallback;
+import jakarta.ws.rs.core.Response;
+
+public class EventForwarderJson implements EventForwarder {
+
+ private final String url;
+ private final String header;
+
+ private final Client client;
+
+ public EventForwarderJson(Config config, Client client) {
+ this.client = client;
+ url = config.getString(Keys.EVENT_FORWARD_URL);
+ header = config.getString(Keys.EVENT_FORWARD_HEADERS);
+ }
+
+ @Override
+ public void forward(EventData eventData, ResultHandler resultHandler) {
+ var requestBuilder = client.target(url).request();
+
+ if (header != null && !header.isEmpty()) {
+ for (String line: header.split("\\r?\\n")) {
+ String[] values = line.split(":", 2);
+ requestBuilder.header(values[0].trim(), values[1].trim());
+ }
+ }
+
+ requestBuilder.async().post(Entity.json(eventData), new InvocationCallback<Response>() {
+ @Override
+ public void completed(Response response) {
+ if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) {
+ resultHandler.onResult(true, null);
+ } else {
+ int code = response.getStatusInfo().getStatusCode();
+ resultHandler.onResult(false, new RuntimeException("HTTP code " + code));
+ }
+ }
+
+ @Override
+ public void failed(Throwable throwable) {
+ resultHandler.onResult(false, throwable);
+ }
+ });
+ }
+
+}
diff --git a/src/main/java/org/traccar/forward/EventForwarderKafka.java b/src/main/java/org/traccar/forward/EventForwarderKafka.java
new file mode 100644
index 000000000..e65c3a51b
--- /dev/null
+++ b/src/main/java/org/traccar/forward/EventForwarderKafka.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2022 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.forward;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.clients.producer.Producer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.traccar.config.Config;
+import org.traccar.config.Keys;
+
+import java.util.Properties;
+
+public class EventForwarderKafka implements EventForwarder {
+
+ private final Producer<String, String> producer;
+ private final ObjectMapper objectMapper;
+
+ private final String topic;
+
+ public EventForwarderKafka(Config config, ObjectMapper objectMapper) {
+ this.objectMapper = objectMapper;
+ Properties properties = new Properties();
+ properties.put("bootstrap.servers", config.getString(Keys.EVENT_FORWARD_URL));
+ properties.put("acks", "all");
+ properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
+ properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
+ producer = new KafkaProducer<>(properties);
+ topic = config.getString(Keys.EVENT_FORWARD_TOPIC);
+ }
+
+ @Override
+ public void forward(EventData eventData, ResultHandler resultHandler) {
+ try {
+ String key = Long.toString(eventData.getDevice().getId());
+ String value = objectMapper.writeValueAsString(eventData);
+ producer.send(new ProducerRecord<>(topic, key, value));
+ resultHandler.onResult(true, null);
+ } catch (JsonProcessingException e) {
+ resultHandler.onResult(false, e);
+ }
+ }
+
+}
diff --git a/src/main/java/org/traccar/forward/EventForwarderMqtt.java b/src/main/java/org/traccar/forward/EventForwarderMqtt.java
new file mode 100644
index 000000000..7f4e29384
--- /dev/null
+++ b/src/main/java/org/traccar/forward/EventForwarderMqtt.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2022 - 2023 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.forward;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.hivemq.client.mqtt.datatypes.MqttQos;
+import com.hivemq.client.mqtt.mqtt5.Mqtt5AsyncClient;
+import com.hivemq.client.mqtt.mqtt5.Mqtt5Client;
+import com.hivemq.client.mqtt.mqtt5.message.auth.Mqtt5SimpleAuth;
+import org.traccar.config.Config;
+import org.traccar.config.Keys;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.UUID;
+
+public class EventForwarderMqtt implements EventForwarder {
+
+ private final Mqtt5AsyncClient client;
+ private final ObjectMapper objectMapper;
+
+ private final String topic;
+
+ public EventForwarderMqtt(Config config, ObjectMapper objectMapper) {
+ URI url;
+ try {
+ url = new URI(config.getString(Keys.EVENT_FORWARD_URL));
+ } catch (URISyntaxException e) {
+ throw new RuntimeException(e);
+ }
+
+ String userInfo = url.getUserInfo();
+ Mqtt5SimpleAuth simpleAuth = null;
+ if (userInfo != null) {
+ int delimiter = userInfo.indexOf(':');
+ if (delimiter == -1) {
+ throw new IllegalArgumentException("Wrong credentials. Should be in format \"username:password\"");
+ } else {
+ simpleAuth = Mqtt5SimpleAuth.builder()
+ .username(userInfo.substring(0, delimiter++))
+ .password(userInfo.substring(delimiter).getBytes())
+ .build();
+ }
+ }
+
+ String host = url.getHost();
+ int port = url.getPort();
+ client = Mqtt5Client.builder()
+ .identifier("traccar-" + UUID.randomUUID())
+ .serverHost(host)
+ .serverPort(port)
+ .simpleAuth(simpleAuth)
+ .automaticReconnectWithDefaultConfig()
+ .buildAsync();
+
+ client.connectWith()
+ .send()
+ .whenComplete((message, e) -> {
+ if (e != null) {
+ throw new RuntimeException(e);
+ }
+ });
+
+ this.objectMapper = objectMapper;
+ topic = config.getString(Keys.EVENT_FORWARD_TOPIC);
+ }
+
+ @Override
+ public void forward(EventData eventData, ResultHandler resultHandler) {
+ byte[] payload;
+ try {
+ payload = objectMapper.writeValueAsString(eventData).getBytes();
+ } catch (JsonProcessingException e) {
+ resultHandler.onResult(false, e);
+ return;
+ }
+
+ client.publishWith()
+ .topic(topic)
+ .qos(MqttQos.AT_LEAST_ONCE)
+ .payload(payload)
+ .send()
+ .whenComplete((message, e) -> resultHandler.onResult(e == null, e));
+ }
+
+}
diff --git a/src/main/java/org/traccar/forward/NetworkForwarder.java b/src/main/java/org/traccar/forward/NetworkForwarder.java
new file mode 100644
index 000000000..86c9a77f3
--- /dev/null
+++ b/src/main/java/org/traccar/forward/NetworkForwarder.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2023 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.forward;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.traccar.config.Config;
+import org.traccar.config.Keys;
+
+import jakarta.inject.Inject;
+import jakarta.inject.Singleton;
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.util.HashMap;
+import java.util.Map;
+
+@Singleton
+public class NetworkForwarder {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(NetworkForwarder.class);
+
+ private final InetAddress destination;
+ private final DatagramSocket connectionUdp;
+ private final Map<InetSocketAddress, Socket> connectionsTcp = new HashMap<>();
+
+ @Inject
+ public NetworkForwarder(Config config) throws IOException {
+ destination = InetAddress.getByName(config.getString(Keys.SERVER_FORWARD));
+ connectionUdp = new DatagramSocket();
+ }
+
+ public void forward(InetSocketAddress source, int port, boolean datagram, byte[] data) {
+ try {
+ if (datagram) {
+ connectionUdp.send(new DatagramPacket(data, data.length, destination, port));
+ } else {
+ Socket connectionTcp = connectionsTcp.get(source);
+ if (connectionTcp == null || connectionTcp.isClosed()) {
+ connectionTcp = new Socket(destination, port);
+ connectionsTcp.put(source, connectionTcp);
+ }
+ connectionTcp.getOutputStream().write(data);
+ }
+ } catch (IOException e) {
+ LOGGER.warn("Network forwarding error", e);
+ }
+ }
+
+ public void disconnect(InetSocketAddress source) {
+ Socket connectionTcp = connectionsTcp.remove(source);
+ if (connectionTcp != null) {
+ try {
+ connectionTcp.close();
+ } catch (IOException e) {
+ LOGGER.warn("Connection close error", e);
+ }
+ }
+ }
+
+}
diff --git a/src/main/java/org/traccar/forward/PositionData.java b/src/main/java/org/traccar/forward/PositionData.java
new file mode 100644
index 000000000..784cf52f5
--- /dev/null
+++ b/src/main/java/org/traccar/forward/PositionData.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2022 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.forward;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import org.traccar.model.Device;
+import org.traccar.model.Position;
+
+@JsonInclude(JsonInclude.Include.NON_NULL)
+public class PositionData {
+
+ private Position position;
+
+ public Position getPosition() {
+ return position;
+ }
+
+ public void setPosition(Position position) {
+ this.position = position;
+ }
+
+ private Device device;
+
+ public Device getDevice() {
+ return device;
+ }
+
+ public void setDevice(Device device) {
+ this.device = device;
+ }
+
+}
diff --git a/src/main/java/org/traccar/forward/PositionForwarder.java b/src/main/java/org/traccar/forward/PositionForwarder.java
new file mode 100644
index 000000000..58bd1dcc7
--- /dev/null
+++ b/src/main/java/org/traccar/forward/PositionForwarder.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2022 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.forward;
+
+public interface PositionForwarder {
+ void forward(PositionData positionData, ResultHandler resultHandler);
+}
diff --git a/src/main/java/org/traccar/forward/PositionForwarderAmqp.java b/src/main/java/org/traccar/forward/PositionForwarderAmqp.java
new file mode 100644
index 000000000..3996bda15
--- /dev/null
+++ b/src/main/java/org/traccar/forward/PositionForwarderAmqp.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2023 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.forward;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import org.traccar.config.Config;
+import org.traccar.config.Keys;
+
+import java.io.IOException;
+
+public class PositionForwarderAmqp implements PositionForwarder {
+
+ private final AmqpClient amqpClient;
+ private final ObjectMapper objectMapper;
+
+ public PositionForwarderAmqp(Config config, ObjectMapper objectMapper) {
+ String connectionUrl = config.getString(Keys.FORWARD_URL);
+ String exchange = config.getString(Keys.FORWARD_EXCHANGE);
+ String topic = config.getString(Keys.FORWARD_TOPIC);
+ amqpClient = new AmqpClient(connectionUrl, exchange, topic);
+ this.objectMapper = objectMapper;
+ }
+
+ @Override
+ public void forward(PositionData positionData, ResultHandler resultHandler) {
+ try {
+ String value = objectMapper.writeValueAsString(positionData);
+ amqpClient.publishMessage(value);
+ resultHandler.onResult(true, null);
+ } catch (IOException e) {
+ resultHandler.onResult(false, e);
+ }
+ }
+}
diff --git a/src/main/java/org/traccar/forward/PositionForwarderJson.java b/src/main/java/org/traccar/forward/PositionForwarderJson.java
new file mode 100644
index 000000000..a0ad8ffd0
--- /dev/null
+++ b/src/main/java/org/traccar/forward/PositionForwarderJson.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2022 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.forward;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.traccar.config.Config;
+import org.traccar.config.Keys;
+
+import jakarta.ws.rs.client.Client;
+import jakarta.ws.rs.client.Entity;
+import jakarta.ws.rs.client.InvocationCallback;
+import jakarta.ws.rs.core.HttpHeaders;
+import jakarta.ws.rs.core.MediaType;
+import jakarta.ws.rs.core.Response;
+
+public class PositionForwarderJson implements PositionForwarder {
+
+ private final String url;
+ private final String header;
+
+ private final Client client;
+ private final ObjectMapper objectMapper;
+
+ public PositionForwarderJson(Config config, Client client, ObjectMapper objectMapper) {
+ this.client = client;
+ this.objectMapper = objectMapper;
+ this.url = config.getString(Keys.FORWARD_URL);
+ this.header = config.getString(Keys.FORWARD_HEADER);
+ }
+
+ @Override
+ public void forward(PositionData positionData, ResultHandler resultHandler) {
+ var requestBuilder = client.target(url).request();
+
+ MediaType mediaType = MediaType.APPLICATION_JSON_TYPE;
+ if (header != null && !header.isEmpty()) {
+ for (String line: header.split("\\r?\\n")) {
+ String[] values = line.split(":", 2);
+ String headerName = values[0].trim();
+ String headerValue = values[1].trim();
+ if (headerName.equals(HttpHeaders.CONTENT_TYPE)) {
+ mediaType = MediaType.valueOf(headerValue);
+ } else {
+ requestBuilder.header(headerName, headerValue);
+ }
+ }
+ }
+
+ try {
+ var entity = Entity.entity(objectMapper.writeValueAsString(positionData), mediaType);
+ requestBuilder.async().post(entity, new InvocationCallback<Response>() {
+ @Override
+ public void completed(Response response) {
+ if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) {
+ resultHandler.onResult(true, null);
+ } else {
+ int code = response.getStatusInfo().getStatusCode();
+ resultHandler.onResult(false, new RuntimeException("HTTP code " + code));
+ }
+ }
+
+ @Override
+ public void failed(Throwable throwable) {
+ resultHandler.onResult(false, throwable);
+ }
+ });
+ } catch (JsonProcessingException e) {
+ resultHandler.onResult(false, e);
+ }
+ }
+
+}
diff --git a/src/main/java/org/traccar/forward/PositionForwarderKafka.java b/src/main/java/org/traccar/forward/PositionForwarderKafka.java
new file mode 100644
index 000000000..7432e9364
--- /dev/null
+++ b/src/main/java/org/traccar/forward/PositionForwarderKafka.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2022 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.forward;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.clients.producer.Producer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.traccar.config.Config;
+import org.traccar.config.Keys;
+
+import java.util.Properties;
+
+public class PositionForwarderKafka implements PositionForwarder {
+
+ private final Producer<String, String> producer;
+ private final ObjectMapper objectMapper;
+
+ private final String topic;
+
+ public PositionForwarderKafka(Config config, ObjectMapper objectMapper) {
+ this.objectMapper = objectMapper;
+ Properties properties = new Properties();
+ properties.put("bootstrap.servers", config.getString(Keys.FORWARD_URL));
+ properties.put("acks", "all");
+ properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
+ properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
+ producer = new KafkaProducer<>(properties);
+ topic = config.getString(Keys.FORWARD_TOPIC);
+ }
+
+ @Override
+ public void forward(PositionData positionData, ResultHandler resultHandler) {
+ try {
+ String key = Long.toString(positionData.getDevice().getId());
+ String value = objectMapper.writeValueAsString(positionData);
+ producer.send(new ProducerRecord<>(topic, key, value));
+ resultHandler.onResult(true, null);
+ } catch (JsonProcessingException e) {
+ resultHandler.onResult(false, e);
+ }
+ }
+
+}
diff --git a/src/main/java/org/traccar/forward/PositionForwarderRedis.java b/src/main/java/org/traccar/forward/PositionForwarderRedis.java
new file mode 100644
index 000000000..539d247b6
--- /dev/null
+++ b/src/main/java/org/traccar/forward/PositionForwarderRedis.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2023 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.forward;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.traccar.config.Config;
+import org.traccar.config.Keys;
+import redis.clients.jedis.Jedis;
+
+public class PositionForwarderRedis implements PositionForwarder {
+
+ private final String url;
+
+ private final ObjectMapper objectMapper;
+
+ public PositionForwarderRedis(Config config, ObjectMapper objectMapper) {
+ this.objectMapper = objectMapper;
+ this.url = config.getString(Keys.FORWARD_URL);
+ }
+
+ @Override
+ public void forward(PositionData positionData, ResultHandler resultHandler) {
+
+ try {
+ String key = "positions." + positionData.getDevice().getUniqueId();
+ String value = objectMapper.writeValueAsString(positionData.getPosition());
+ try (Jedis jedis = new Jedis(url)) {
+ jedis.lpush(key, value);
+ }
+ resultHandler.onResult(true, null);
+ } catch (JsonProcessingException e) {
+ resultHandler.onResult(false, e);
+ }
+ }
+
+}
diff --git a/src/main/java/org/traccar/forward/PositionForwarderUrl.java b/src/main/java/org/traccar/forward/PositionForwarderUrl.java
new file mode 100644
index 000000000..33474d40b
--- /dev/null
+++ b/src/main/java/org/traccar/forward/PositionForwarderUrl.java
@@ -0,0 +1,166 @@
+/*
+ * Copyright 2022 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.forward;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.traccar.config.Config;
+import org.traccar.config.Keys;
+import org.traccar.helper.Checksum;
+import org.traccar.model.Device;
+import org.traccar.model.Position;
+
+import jakarta.ws.rs.client.Client;
+import jakarta.ws.rs.client.InvocationCallback;
+import jakarta.ws.rs.core.Response;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.util.Calendar;
+import java.util.Formatter;
+import java.util.Locale;
+import java.util.TimeZone;
+
+public class PositionForwarderUrl implements PositionForwarder {
+
+ private final String url;
+ private final String header;
+
+ private final Client client;
+ private final ObjectMapper objectMapper;
+
+ public PositionForwarderUrl(Config config, Client client, ObjectMapper objectMapper) {
+ this.client = client;
+ this.objectMapper = objectMapper;
+ this.url = config.getString(Keys.FORWARD_URL);
+ this.header = config.getString(Keys.FORWARD_HEADER);
+ }
+
+ @Override
+ public void forward(PositionData positionData, ResultHandler resultHandler) {
+ try {
+ String url = formatRequest(positionData);
+ var requestBuilder = client.target(url).request();
+
+ if (header != null && !header.isEmpty()) {
+ for (String line: header.split("\\r?\\n")) {
+ String[] values = line.split(":", 2);
+ String headerName = values[0].trim();
+ String headerValue = values[1].trim();
+ requestBuilder.header(headerName, headerValue);
+ }
+ }
+
+ requestBuilder.async().get(new InvocationCallback<Response>() {
+ @Override
+ public void completed(Response response) {
+ if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) {
+ resultHandler.onResult(true, null);
+ } else {
+ int code = response.getStatusInfo().getStatusCode();
+ resultHandler.onResult(false, new RuntimeException("HTTP code " + code));
+ }
+ }
+
+ @Override
+ public void failed(Throwable throwable) {
+ resultHandler.onResult(false, throwable);
+ }
+ });
+ } catch (UnsupportedEncodingException | JsonProcessingException e) {
+ resultHandler.onResult(false, e);
+ }
+ }
+
+ public String formatRequest(
+ PositionData positionData) throws UnsupportedEncodingException, JsonProcessingException {
+
+ Position position = positionData.getPosition();
+ Device device = positionData.getDevice();
+
+ String request = url
+ .replace("{name}", URLEncoder.encode(device.getName(), StandardCharsets.UTF_8))
+ .replace("{uniqueId}", device.getUniqueId())
+ .replace("{status}", device.getStatus())
+ .replace("{deviceId}", String.valueOf(position.getDeviceId()))
+ .replace("{protocol}", String.valueOf(position.getProtocol()))
+ .replace("{deviceTime}", String.valueOf(position.getDeviceTime().getTime()))
+ .replace("{fixTime}", String.valueOf(position.getFixTime().getTime()))
+ .replace("{valid}", String.valueOf(position.getValid()))
+ .replace("{latitude}", String.valueOf(position.getLatitude()))
+ .replace("{longitude}", String.valueOf(position.getLongitude()))
+ .replace("{altitude}", String.valueOf(position.getAltitude()))
+ .replace("{speed}", String.valueOf(position.getSpeed()))
+ .replace("{course}", String.valueOf(position.getCourse()))
+ .replace("{accuracy}", String.valueOf(position.getAccuracy()))
+ .replace("{statusCode}", calculateStatus(position));
+
+ if (position.getAddress() != null) {
+ request = request.replace(
+ "{address}", URLEncoder.encode(position.getAddress(), StandardCharsets.UTF_8));
+ }
+
+ if (request.contains("{attributes}")) {
+ String attributes = objectMapper.writeValueAsString(position.getAttributes());
+ request = request.replace(
+ "{attributes}", URLEncoder.encode(attributes, StandardCharsets.UTF_8));
+ }
+
+ if (request.contains("{gprmc}")) {
+ request = request.replace("{gprmc}", formatSentence(position));
+ }
+
+ return request;
+ }
+
+ private static String formatSentence(Position position) {
+
+ StringBuilder s = new StringBuilder("$GPRMC,");
+
+ try (Formatter f = new Formatter(s, Locale.ENGLISH)) {
+
+ Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ENGLISH);
+ calendar.setTimeInMillis(position.getFixTime().getTime());
+
+ f.format("%1$tH%1$tM%1$tS.%1$tL,A,", calendar);
+
+ double lat = position.getLatitude();
+ double lon = position.getLongitude();
+
+ f.format("%02d%07.4f,%c,", (int) Math.abs(lat), Math.abs(lat) % 1 * 60, lat < 0 ? 'S' : 'N');
+ f.format("%03d%07.4f,%c,", (int) Math.abs(lon), Math.abs(lon) % 1 * 60, lon < 0 ? 'W' : 'E');
+
+ f.format("%.2f,%.2f,", position.getSpeed(), position.getCourse());
+ f.format("%1$td%1$tm%1$ty,,", calendar);
+ }
+
+ s.append(Checksum.nmea(s.substring(1)));
+
+ return s.toString();
+ }
+
+ // OpenGTS status code
+ private String calculateStatus(Position position) {
+ if (position.hasAttribute(Position.KEY_ALARM)) {
+ return "0xF841"; // STATUS_PANIC_ON
+ } else if (position.getSpeed() < 1.0) {
+ return "0xF020"; // STATUS_LOCATION
+ } else {
+ return "0xF11C"; // STATUS_MOTION_MOVING
+ }
+ }
+
+}
diff --git a/src/main/java/org/traccar/forward/ResultHandler.java b/src/main/java/org/traccar/forward/ResultHandler.java
new file mode 100644
index 000000000..009daf495
--- /dev/null
+++ b/src/main/java/org/traccar/forward/ResultHandler.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2022 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.forward;
+
+public interface ResultHandler {
+ void onResult(boolean success, Throwable throwable);
+}