package org.fraunhofer.cese.funf_sensor.Probe;

import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;

import edu.mit.media.funf.probe.Probe;

/**
 *
 */
public class AccelerometerProbe extends Probe.Base implements Probe.ContinuousProbe, SensorEventListener {

    private static final String TAG = "AccelerometerProbe: ";
    private SensorManager sensorManager;
    private Sensor acclerometerSensor;
    private static long lastUpdate;

    @Override
    protected void onEnable() {
        super.onStart();
        sensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);
        acclerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(this, acclerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
        lastUpdate = System.currentTimeMillis();
        Log.i(TAG, " enabled");
    }

    @Override
    protected void onDisable() {
        super.onDisable();
        sensorManager.unregisterListener(this, acclerometerSensor);
        Log.i(TAG, "disabled.");
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

            long now = System.currentTimeMillis();

            if((now-lastUpdate)>200) {
                float[] sensorData = event.values;
                long timestamp = event.timestamp;
                Intent intent = new Intent();
                intent.putExtra(TAG, timestamp);
                intent.putExtra(TAG, sensorData);
                sendData(intent);
            }
        }

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        Log.i(TAG, "accuracy changed.");
    }

    public void sendData(Intent intent) {
        sendData(getGson().toJsonTree(intent).getAsJsonObject());
        Log.i(TAG, "sent.");
    }
}

@@ -41,16 +41,17 @@
 Probe;%0A%0A
+%0A
 import a
@@ -284,16 +284,17 @@
 l.Log;%0A%0A
+%0A
 import e
@@ -566,17 +566,23 @@
  Sensor 
-a
+linearA
 cclerome
@@ -620,22 +620,135 @@
 ong last
-Update
+ProbeStart;%0A    private static final long measureInterval = 30000;%0A    private static final long measureDuration = 1000
 ;%0A%0A    @
@@ -916,17 +916,23 @@
         
-a
+linearA
 cclerome
@@ -1039,33 +1039,39 @@
 rListener(this, 
-a
+linearA
 cclerometerSenso
@@ -1104,14 +1104,10 @@
 LAY_
-NORMAL
+UI
 );%0A 
@@ -1117,22 +1117,26 @@
     last
-Update
+ProbeStart
  = Syste
@@ -1318,17 +1318,23 @@
 r(this, 
-a
+linearA
 cclerome
@@ -1590,29 +1590,68 @@
   if
+ 
 ((now
--
+ - 
 last
-Update)%3E200
+ProbeStart) %3E (measureInterval-measureDuration)
 ) %7B%0A
@@ -1930,16 +1930,163 @@
 ntent);%0A
+                if ((now - lastProbeStart) %3E measureInterval) %7B%0A                    lastProbeStart = System.currentTimeMillis();%0A                %7D%0A
         
@@ -2274,24 +2274,53 @@
 t intent) %7B%0A
+        Log.i(TAG, %22sent.%22);%0A
         send
@@ -2377,41 +2377,11 @@
 ));%0A
-        Log.i(TAG, %22sent.%22);%0A
     %7D%0A%7D
-%0A

set measureInterval to 30 seconds and measureDuration to 1 second #37