|
|
@@ -0,0 +1,109 @@ |
|
|
|
package com.example.ueberwachungssystem.Detection; |
|
|
|
|
|
|
|
import static java.lang.Math.sqrt; |
|
|
|
|
|
|
|
import android.content.Context; |
|
|
|
import android.hardware.Sensor; |
|
|
|
import android.hardware.SensorEvent; |
|
|
|
import android.hardware.SensorEventListener; |
|
|
|
import android.hardware.SensorManager; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Accelerometer inherits some methods from abstract Detector class (more info there) |
|
|
|
* |
|
|
|
* |
|
|
|
* USE FROM MAIN ACTIVITY: |
|
|
|
* |
|
|
|
* Accelerometer beschleunigungssensor = new Accelerometer(this); |
|
|
|
* onCreate: |
|
|
|
* //Accelerometer Setup |
|
|
|
* beschleunigungssensor = new Accelerometer(this, logger, textViewLog); //logger and textview only for debugging necessary |
|
|
|
* beschleunigungssensor.getSensor(); |
|
|
|
* |
|
|
|
* //Starting Detection: |
|
|
|
* beschleunigungssensor.startDetection(); |
|
|
|
* //Stopping Detection: also recommended at onPause to avoid unnecessary battery consumption |
|
|
|
* beschleunigungssensor.stopDetection(); |
|
|
|
* |
|
|
|
* */ |
|
|
|
|
|
|
|
public class Accelerometer extends Detector implements SensorEventListener { |
|
|
|
|
|
|
|
public SensorManager sensorManager; |
|
|
|
private static final int sensorType = Sensor.TYPE_LINEAR_ACCELERATION; |
|
|
|
private Sensor accelerometer; |
|
|
|
private Context context; |
|
|
|
boolean alarm = false; |
|
|
|
//Preallocate memory for the data of each axis of the acceleration sensor |
|
|
|
float x; |
|
|
|
float y; |
|
|
|
float z; |
|
|
|
float betrag; //Betrag aller drei Achsen sqrt(x*x + y*y + z*z) |
|
|
|
private DetectionReport detectionReport; |
|
|
|
|
|
|
|
// In constructor pass Activity, Context and TextView from MainActivity in Accelerometer class |
|
|
|
public Accelerometer(Context context){ |
|
|
|
super(context); //von Detektor |
|
|
|
this.context = context; |
|
|
|
} |
|
|
|
|
|
|
|
public void getSensor(){ |
|
|
|
sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE); |
|
|
|
if(sensorManager.getSensorList(sensorType).size()==0) { |
|
|
|
accelerometer = null; |
|
|
|
} |
|
|
|
else { |
|
|
|
accelerometer = sensorManager.getSensorList(sensorType).get(0); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onSensorChanged(SensorEvent event) { |
|
|
|
try { |
|
|
|
checkAlarm(event); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void checkAlarm (SensorEvent event) throws InterruptedException { |
|
|
|
x = event.values[0]; |
|
|
|
y = event.values[1]; |
|
|
|
z = event.values[2]; |
|
|
|
betrag = (float) sqrt(x*x + y*y + z*z); |
|
|
|
float threshold = 1.5F; |
|
|
|
|
|
|
|
if (!alarm) { |
|
|
|
if (betrag > threshold) { |
|
|
|
alarm = true; |
|
|
|
detectionReport = new DetectionReport("Accelerometer1", "Bewegung", betrag); |
|
|
|
reportViolation("Accelo1", "Bewegung", betrag); |
|
|
|
} |
|
|
|
} else { |
|
|
|
if (betrag < threshold) { |
|
|
|
alarm = false; |
|
|
|
} else { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onAccuracyChanged(Sensor sensor, int accuracy) { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void startDetection() { |
|
|
|
// entspricht void start() |
|
|
|
//getSensor(); |
|
|
|
if (accelerometer != null) { |
|
|
|
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void stopDetection() { |
|
|
|
// entspricht void stop() |
|
|
|
sensorManager.unregisterListener(this, accelerometer); |
|
|
|
} |
|
|
|
} |