Skip to content

Commit

Permalink
UDP implementation for OpenGTS Background Worker
Browse files Browse the repository at this point in the history
  • Loading branch information
mendhak committed Mar 16, 2024
1 parent b103a63 commit 4a7ecb3
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.mendhak.gpslogger.loggers.opengts;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import com.mendhak.gpslogger.common.PreferenceHelper;
import com.mendhak.gpslogger.common.SerializableLocation;
import com.mendhak.gpslogger.common.Strings;
import com.mendhak.gpslogger.common.events.UploadEvents;
import com.mendhak.gpslogger.common.slf4j.Logs;
import com.mendhak.gpslogger.senders.GpxReader;
import com.mendhak.gpslogger.senders.opengts.OpenGTSManager;

import org.slf4j.Logger;

import java.io.File;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.List;

import de.greenrobot.event.EventBus;

public class OpenGtsUdpWorker extends Worker{

public static final Logger LOG = Logs.of(OpenGtsUdpWorker.class);
public OpenGtsUdpWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {

PreferenceHelper preferenceHelper = PreferenceHelper.getInstance();
String server = preferenceHelper.getOpenGTSServer();
int port = Integer.valueOf(preferenceHelper.getOpenGTSServerPort());
String accountName = preferenceHelper.getOpenGTSAccountName();
String deviceId = preferenceHelper.getOpenGTSDeviceId();

String gpxFilePath = getInputData().getString("gpxFilePath");
String[] serializedLocations = getInputData().getStringArray("locations");

try {
if(!Strings.isNullOrEmpty(gpxFilePath)){
List<SerializableLocation> locations = GpxReader.getPoints(new File(gpxFilePath));
sendRAW(deviceId, accountName, server, port, locations.toArray(new SerializableLocation[0]));
}
else if (serializedLocations != null && serializedLocations.length > 0){
SerializableLocation[] locations = new SerializableLocation[serializedLocations.length];
for (int i = 0; i < serializedLocations.length; i++) {
locations[i] = Strings.deserializeFromJson(serializedLocations[i], SerializableLocation.class);
}
sendRAW(deviceId, accountName, server, port, locations);
}
EventBus.getDefault().post(new UploadEvents.OpenGTS().succeeded());
}
catch(Exception ex){
LOG.error("Could not send to OpenGTS", ex);
EventBus.getDefault().post(new UploadEvents.OpenGTS().failed("Could not send to OpenGTS", ex));
return Result.failure();
}
return Result.success();
}

private void sendRAW(String id, String accountName, String server, int port, SerializableLocation[] locations) throws Exception {
for (SerializableLocation loc : locations) {
if(Strings.isNullOrEmpty(accountName)){
accountName = id;
}
String message = accountName + "/" + id + "/" + OpenGTSManager.gprmcEncode(loc);
DatagramSocket socket = new DatagramSocket();
byte[] buffer = message.getBytes();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName(server), port);
LOG.debug("Sending UDP " + message);
socket.send(packet);
socket.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;

import com.birbit.android.jobqueue.JobManager;
import com.mendhak.gpslogger.common.*;
import com.mendhak.gpslogger.common.slf4j.Logs;
import com.mendhak.gpslogger.loggers.customurl.CustomUrlRequest;
import com.mendhak.gpslogger.loggers.customurl.CustomUrlWorker;
import com.mendhak.gpslogger.loggers.opengts.OpenGtsUdpJob;
import com.mendhak.gpslogger.loggers.opengts.OpenGtsUdpWorker;
import com.mendhak.gpslogger.senders.FileSender;
import com.mendhak.gpslogger.senders.GpxReader;
import org.slf4j.Logger;
Expand Down Expand Up @@ -59,16 +58,28 @@ public void uploadFile(List<File> files) {
// Use only gpx
for (File f : files) {
if (f.getName().endsWith(".gpx")) {
String tag = String.valueOf(Objects.hashCode(f.getName()));

HashMap<String, Object> dataMap = new HashMap<String, Object>(){{
put("gpxFilePath", f.getAbsolutePath());
put("callbackType", "opengts");
}};

OneTimeWorkRequest workRequest = Systems.getBasicOneTimeWorkRequest(CustomUrlWorker.class, dataMap);
WorkManager.getInstance(AppSettings.getInstance())
.enqueueUniqueWork(tag, ExistingWorkPolicy.REPLACE, workRequest);
String communication = preferenceHelper.getOpenGTSServerCommunicationMethod();

if(communication.equalsIgnoreCase("udp")){
String tag = String.valueOf(Objects.hashCode(f.getName()));
HashMap<String, Object> dataMap = new HashMap<String, Object>(){{
put("gpxFilePath", f.getAbsolutePath());
}};
OneTimeWorkRequest workRequest = Systems.getBasicOneTimeWorkRequest(OpenGtsUdpWorker.class, dataMap);
WorkManager.getInstance(AppSettings.getInstance())
.enqueueUniqueWork(tag, ExistingWorkPolicy.REPLACE, workRequest);
}
else {
String tag = String.valueOf(Objects.hashCode(f.getName()));
HashMap<String, Object> dataMap = new HashMap<String, Object>(){{
put("gpxFilePath", f.getAbsolutePath());
put("callbackType", "opengts");
}};
OneTimeWorkRequest workRequest = Systems.getBasicOneTimeWorkRequest(CustomUrlWorker.class, dataMap);
WorkManager.getInstance(AppSettings.getInstance())
.enqueueUniqueWork(tag, ExistingWorkPolicy.REPLACE, workRequest);
}

}
}
Expand All @@ -85,8 +96,18 @@ public void sendLocations(SerializableLocation[] locations){
String communication = preferenceHelper.getOpenGTSServerCommunicationMethod();

if(communication.equalsIgnoreCase("udp")){
JobManager jobManager = AppSettings.getJobManager();
jobManager.addJobInBackground(new OpenGtsUdpJob(server, port, accountName, path, deviceId, communication, locations));
String tag = String.valueOf(Objects.hashCode(locations));
String[] serializedLocations = new String[locations.length];
for (int i = 0; i < locations.length; i++) {
serializedLocations[i] = Strings.serializeTojson(locations[i]);
}
HashMap<String, Object> dataMap = new HashMap<String, Object>(){{
put("locations", serializedLocations);
}};

OneTimeWorkRequest workRequest = Systems.getBasicOneTimeWorkRequest(OpenGtsUdpWorker.class, dataMap);
WorkManager.getInstance(AppSettings.getInstance())
.enqueueUniqueWork(tag, ExistingWorkPolicy.REPLACE, workRequest);
}
else {
sendByHttp(deviceId, accountName, locations, communication, path, server, port);
Expand Down

0 comments on commit 4a7ecb3

Please sign in to comment.