I'm basically working with android sensors that provide new data in milliseconds. So for example on every rotation (gyroscope sensor) that the listener gets im collecting the data in a string (as this is the approach to write on an influxdb) compressing it into a gzip file after 2000 lines. Im also checking about file size and depending on that or on a timer i sent the data on the server. Ofcourse i know this is not thread safe or optimized as the thread might get blocked if the post requests takes too long or the writing of data aswell. What would be the best approach to tackle this and how? Maybe a thread pool executor that handles the compression and the requests? Im not really sure how i would implement that.
The following implementation is inside of my service
gyroscope.setListener(new Gyroscope.Listener() {
final StringBuilder str = new StringBuilder();
int count = 0;
byte[] b ;
// on rotation method of gyroscope
@Override
public void onRotation(long timestamp,float rx, float ry, float rz) {
try {
//get string of new lines of the write data for the sensor
str.append("gyroTest,userTag=testUser,deviceTag="+deviceName+" rx="+rx+",ry="+ry+",rz="+rz+" "+timestamp+"\n");
if(count % 2000 == 0 && count != 0){
b = GZIPCompression.compress(str);
if(FileHandling.isFileBig(sensor1_file)){
gyroSend=true;
}else{
FileHandling.writegzipFile(sensor1_file,b);
Log.i(FILE_TAG,"Write gyroscope file");
str.setLength(0);
}
}
if(gyroSend){
if(isConnectedToInternet()) {
if (!FileHandling.isFileEmpty( sensor1_file)){
//Send the data to the server
Log.i("SERVER","Sending gyroscope with timer");
req.dataInsert(MonitorService.this,FileHandling.readgzipFile(sensor1_file),sensor1_file);
count=0;
}
gyroSend = false;
}
}
count++;
if(count>950000){
if(isConnectedToInternet()) {
Log.i("SERVER","Sending gyroscope with lines");
req.dataInsert(MonitorService.this,FileHandling.readgzipFile(sensor1_file),sensor1_file);
count=0;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
Sensor class:
public class Gyroscope {
// create an interface with one method
public interface Listener {
// create method with all 3
// axis translation as argument
void onRotation(long timestamp,float tx, float ty, float ts);
}
// create an instance
private Listener listener;
private HandlerThread mSensorThread;
private Handler mSensorHandler;
// method to set the instance
public void setListener(Listener l) {
listener = l;
}
private final SensorManager sensorManager;
private final Sensor sensor;
private final SensorEventListener sensorEventListener;
// create constructor with context as argument
public Gyroscope(Context context) {
// create instance of sensor manager
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
// create instance of sensor with type gyroscope
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
// create the sensor listener
sensorEventListener = new SensorEventListener() {
// this method is called when
// the device's position changes
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
// check if listener is different from null
if (listener != null) {
// pass the three floats in listener on rotation of axis
listener.onRotation((new Date()).getTime() + (sensorEvent.timestamp - System.nanoTime()) / 1000000L,sensorEvent.values[0], sensorEvent.values[1], sensorEvent.values[2]);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
}
// create register method
// for sensor notifications
public void register() {
// call sensor manger's register listener and pass the required arguments
mSensorThread = new HandlerThread("Gyroscope Sensor thread", Thread.MAX_PRIORITY);
mSensorThread.start();
mSensorHandler = new Handler(mSensorThread.getLooper()); //Blocks until looper is prepared, which is fairly quick
sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL, mSensorHandler);
}
// create method to unregister
// from sensor notifications
public void unregister() {
// call sensor manger's unregister listener
// and pass the required arguments
mSensorThread.quitSafely();
sensorManager.unregisterListener(sensorEventListener);
}
}