|
|
@@ -1,6 +1,6 @@ |
|
|
|
package com.example.ueberwachungssystem; |
|
|
|
|
|
|
|
import static java.lang.Math.sqrt; |
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context; |
|
|
|
import android.hardware.Sensor; |
|
|
@@ -11,122 +11,114 @@ import android.widget.TextView; |
|
|
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity; |
|
|
|
|
|
|
|
public class Accelerometer extends Detector implements SensorEventListener { |
|
|
|
|
|
|
|
public class Accelerometer implements Runnable, SensorEventListener { |
|
|
|
|
|
|
|
Logger logger; |
|
|
|
private volatile boolean running = false; |
|
|
|
public SensorManager sensorManager; |
|
|
|
private static final int sensorType = Sensor.TYPE_LINEAR_ACCELERATION; |
|
|
|
private int sensorType = Sensor.TYPE_ACCELEROMETER; |
|
|
|
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; |
|
|
|
TextView textViewLog; |
|
|
|
|
|
|
|
|
|
|
|
// In constructor pass Activity, Context and TextView from MainActivity in Accelerometer class |
|
|
|
public Accelerometer(Context context, Logger mainLogger, TextView textViewLog){ |
|
|
|
super(context); //von Detektor |
|
|
|
logger = mainLogger; |
|
|
|
private Thread thread; |
|
|
|
private String threadName = "Accelerometer Thread"; |
|
|
|
double[] gravity = new double[3]; |
|
|
|
double[] linear_acceleration = new double[3]; |
|
|
|
private TextView textViewWorkerThread; |
|
|
|
|
|
|
|
// In constructor pass Context from MainActivity in Accelerometer class |
|
|
|
public Accelerometer(Context context, TextView textViewWorkerThread){ |
|
|
|
this.context = context; |
|
|
|
this.textViewLog = textViewLog; |
|
|
|
//Logger logger = new Logger(this.getClass().getSimpleName(), textViewLog, ""); |
|
|
|
this.textViewWorkerThread = textViewWorkerThread; |
|
|
|
} |
|
|
|
// Passing Activity's instance as argument on worker thread |
|
|
|
AppCompatActivity activity; |
|
|
|
public Accelerometer(AppCompatActivity activity){ |
|
|
|
this.activity = activity; |
|
|
|
} |
|
|
|
|
|
|
|
public void getSensor(){ |
|
|
|
sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE); |
|
|
|
if(sensorManager.getSensorList(sensorType).size()==0) { |
|
|
|
logger.log("Es gibt den gewünschten Sensor nicht"); |
|
|
|
accelerometer = null; |
|
|
|
} |
|
|
|
else { |
|
|
|
accelerometer = sensorManager.getSensorList(sensorType).get(0); |
|
|
|
logger.log("Sensor gefunden"); |
|
|
|
} |
|
|
|
private void print (final String s){ |
|
|
|
activity.runOnUiThread(new Runnable() { |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
textViewWorkerThread.setText(s); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
while (running) { |
|
|
|
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); |
|
|
|
if (sensorManager.getSensorList(sensorType).size() == 0) { |
|
|
|
logger.log("Es gibt den gewünschten Sensor nicht"); |
|
|
|
accelerometer = null; |
|
|
|
} else { |
|
|
|
accelerometer = sensorManager.getSensorList(sensorType).get(0); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
void start(){ |
|
|
|
logger.log("Starting" + threadName); |
|
|
|
running = true; |
|
|
|
thread = new Thread(this); |
|
|
|
thread.setName(threadName); |
|
|
|
thread.start(); |
|
|
|
logger.log("alright, " + threadName + " started!"); |
|
|
|
|
|
|
|
void start() { |
|
|
|
// register the sensor before using // |
|
|
|
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL); |
|
|
|
logger.log("Accelerometer, start!"); |
|
|
|
sensorManager.registerListener((SensorEventListener) context, accelerometer, SensorManager.SENSOR_DELAY_NORMAL); |
|
|
|
logger.log("Accelerometer, du bist dran!"); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void stop() { |
|
|
|
// unregister from the sensor to stop using it // |
|
|
|
sensorManager.unregisterListener(this, accelerometer); |
|
|
|
logger.log("Accelerometer unregistered!"); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public void onSensorChanged(SensorEvent event) { |
|
|
|
checkAlarm(event); |
|
|
|
} |
|
|
|
|
|
|
|
StringBuffer stringBuffer = new StringBuffer(); |
|
|
|
public void checkAlarm (SensorEvent event) { |
|
|
|
x = event.values[0]; |
|
|
|
y = event.values[1]; |
|
|
|
z = event.values[2]; |
|
|
|
betrag = (float) sqrt(x*x + y*y + z*z); |
|
|
|
double schwelle = 1.5; |
|
|
|
|
|
|
|
if (!alarm) { |
|
|
|
if (betrag > schwelle) { |
|
|
|
stringBuffer.append("\n Betragswert über Schwelle, Detection Report wird gleich aufgerufen"); |
|
|
|
//logger.log("Betragswert über Schwelle erkannt, Alarm wird gleich angeschaltet"); |
|
|
|
alarm = true; |
|
|
|
stringBuffer.append("\nAlarm = " + alarm); |
|
|
|
detectionReport = new DetectionReport("Accelerometer1", "Bewegung", betrag); |
|
|
|
stringBuffer.append("\nDetectionReport = " + detectionReport) |
|
|
|
.append("\nAlarm an" + betrag + event.timestamp); |
|
|
|
//logger.log("Alarm an"); |
|
|
|
//logger.log("Betrag: " + betrag + event.timestamp); |
|
|
|
logger.clearLog(); |
|
|
|
logger.log(stringBuffer.toString()); |
|
|
|
stringBuffer.delete(0, stringBuffer.length()); |
|
|
|
} |
|
|
|
sensorManager.unregisterListener((SensorEventListener) context, accelerometer); |
|
|
|
logger.log("Das reicht Accelerometer, komm zurück!"); |
|
|
|
if (!running){ |
|
|
|
logger.log(threadName + " not running."); |
|
|
|
} else { |
|
|
|
if (betrag < schwelle) { |
|
|
|
stringBuffer.append("\nAlarm noch an; Neuer Betragswert unter Schwellwert:" + betrag); |
|
|
|
//logger.log("Alarm ist noch an; Neuer Betragswert unter Schwellwert: " + betrag); |
|
|
|
alarm = false; |
|
|
|
//logger.log("Alarm" + alarm); |
|
|
|
//logger.log("Alarm wieder ausgeschaltet"); |
|
|
|
stringBuffer.append("\nAlarm = " + alarm); |
|
|
|
} else { |
|
|
|
logger.log("Betragswert immer noch über Schwellwert: " + betrag + "; Alarm bleibt an."); |
|
|
|
logger.log("Betragswert immer noch über Schwellwert: " + betrag + "; Alarm bleibt an."); |
|
|
|
logger.log("Stopping " + threadName); |
|
|
|
running = false; |
|
|
|
while (true) { |
|
|
|
try { |
|
|
|
thread.join(); |
|
|
|
logger.log("..." + threadName + " stopped"); |
|
|
|
break; |
|
|
|
} catch (InterruptedException e){ |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
//logger.clearLog(); |
|
|
|
logger.log(stringBuffer.toString()); |
|
|
|
stringBuffer.delete(0, stringBuffer.length()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onAccuracyChanged(Sensor sensor, int accuracy) { |
|
|
|
public void onSensorChanged(SensorEvent event) { |
|
|
|
// Highpass filter to filter out gravity influence |
|
|
|
final double alpha = 0.8; |
|
|
|
|
|
|
|
gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0]; |
|
|
|
gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1]; |
|
|
|
gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2]; |
|
|
|
linear_acceleration[0] = event.values[0] - gravity[0]; |
|
|
|
|
|
|
|
linear_acceleration[1] = event.values[1] - gravity[1]; |
|
|
|
linear_acceleration[2] = event.values[2] - gravity[2]; |
|
|
|
|
|
|
|
String sb = "t=" + event.timestamp + |
|
|
|
"\nx=" + event.values[0] + // Wert liegend: x = 0.04 |
|
|
|
"\ny=" + event.values[1] + // Wert liegend: y = 0.44 |
|
|
|
"\nz=" + event.values[2] + // Wert liegend: z = 9.90 = Erdbeschleunigung --> Wenn Ausrichtung unbekannt ist, müsste kalibrierung bei Start der Bewegungsüberwachung vorgenommen werden |
|
|
|
"\nlin_x=" + linear_acceleration[0] + |
|
|
|
"\nlin_x=" + linear_acceleration[1] + |
|
|
|
"\nlin_x=" + linear_acceleration[2]; |
|
|
|
|
|
|
|
logger.clearLog(); |
|
|
|
logger.log(sb); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void startDetection() { |
|
|
|
// entspricht void start() |
|
|
|
if (accelerometer != null) { |
|
|
|
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME); |
|
|
|
logger.log("Sensor registriert"); |
|
|
|
} |
|
|
|
} |
|
|
|
public void onAccuracyChanged(Sensor sensor, int accuracy) { |
|
|
|
|
|
|
|
@Override |
|
|
|
public void stopDetection() { |
|
|
|
// entspricht void stop() |
|
|
|
sensorManager.unregisterListener(this, accelerometer); |
|
|
|
logger.log("Vom Sensor abgemeldet"); |
|
|
|
} |
|
|
|
} |