Skip to content

Commit

Permalink
Local logfiles implementation (draft) and FTP
Browse files Browse the repository at this point in the history
  • Loading branch information
francescogabbrielli committed Aug 8, 2017
1 parent 0f1e492 commit f589fb4
Show file tree
Hide file tree
Showing 10 changed files with 338 additions and 111 deletions.
5 changes: 5 additions & 0 deletions app/src/main/assets/defaults.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# filenames (TODO: use just prefixes)
filename_data=sensors.csv
filename_frame=frame.jpg


# milliseconds between one sample and the other (frequency in Hz = 1 / (default_logging_rate/1000))
pref_logging_rate=100

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

import java.io.IOException;

/**
* XXX: all callbacks moved to activty...
* TODO: refactor
*/
public class CameraPreview extends SurfaceView {

public CameraPreview(MainActivity activity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class FTPUploader {
public class LogFTPUploader extends LogTarget {

private final static String TAG = FTPUploader.class.getSimpleName();
private final static String TAG = LogFTPUploader.class.getSimpleName();

private ExecutorService exec;

Expand All @@ -26,12 +26,10 @@ public class FTPUploader {
* Creates a new FTP uploader, that is a wrapper around Apache Commons FTPClient with threading
* support and configuration linked to application settings
*
* TODO: add a kind of listener interface support instead of single callbacks
*
* @param context
* the application context
*/
public FTPUploader(Context context) {
public LogFTPUploader(Context context) {
client = new FTPClient();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
address = prefs.getString(Util.PREF_FTP_ADDRESS, "");
Expand Down Expand Up @@ -71,6 +69,7 @@ public void execute(final Runnable command) {
/**
* Closes current connection
*/
@Override
public void close() {
if (client!=null && client.isConnected()) {
exec.execute(new Runnable() {
Expand Down Expand Up @@ -107,6 +106,7 @@ public void run() {
* @param filename
* the filename to write to
*/
@Override
public void send(final byte[] data, final String filename, final Runnable callback) {
if (client.isConnected())
exec.execute(new Runnable() {
Expand All @@ -120,13 +120,13 @@ public void run() {
callback.run();
Log.d(TAG, "Data written to "+filename);
} catch (Exception e) {
Log.e(FTPUploader.class.getSimpleName(), "Transfer Error", e);
Log.e(TAG, "Transfer Error", e);
} finally {
try {
out.close();
client.completePendingCommand();
} catch (Exception e) {
Log.e(FTPUploader.class.getSimpleName(), "Unexpected error", e);
Log.e(LogFTPUploader.class.getSimpleName(), "Unexpected error", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package it.francescogabbrielli.apps.sensorlogger;

import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class LogFileWriter extends LogTarget {

private final static String TAG = LogFTPUploader.class.getSimpleName();

private File folder;
private ExecutorService exec;

public LogFileWriter(File folder) {
this.folder = folder;
exec = Executors.newSingleThreadExecutor();
}

@Override
public void send(final byte[] data, final String filename, final Runnable callback) {
exec.execute(new Runnable() {
@Override
public void run() {
FileOutputStream out = null;
try {
out = new FileOutputStream(new File(folder, filename));
out.write(data);
if (callback!=null)
callback.run();
Log.d(TAG, "Data written to " + filename);
} catch (Exception e) {
Log.e(TAG, "Error writing file", e);
} finally {
try {
out.close();
} catch (Exception e) {

}
}
}
});
}

@Override
public void close() {
exec.execute(new Runnable() {
@Override
public void run() {
new Thread() {
@Override
public void run() {
try {
exec.awaitTermination(5, TimeUnit.SECONDS);
exec.shutdown();
} catch(InterruptedException e) {
Log.e(TAG, "Unexpected interruption", e);
}
}
}.start();
}
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package it.francescogabbrielli.apps.sensorlogger;

/**
* Generic target where to log data
*
* TODO: move executor framework here
*/
public abstract class LogTarget {

/**
* Send data to the target
*
* TODO: add a kind of listener interface support instead of single callbacks
*/
public abstract void send(final byte[] data, final String filename, final Runnable callback);

/**
* Implement final cleanup
*/
public abstract void close();

}
Loading

0 comments on commit f589fb4

Please sign in to comment.