|
|
|
|
|
|
|
|
package com.example.ueberwachungssystem; |
|
|
package com.example.ueberwachungssystem; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static java.lang.Math.sqrt; |
|
|
|
|
|
|
|
|
import android.content.Context; |
|
|
import android.content.Context; |
|
|
import android.hardware.Sensor; |
|
|
import android.hardware.Sensor; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity; |
|
|
import androidx.appcompat.app.AppCompatActivity; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class Accelerometer implements Runnable, SensorEventListener { |
|
|
|
|
|
|
|
|
public class Accelerometer extends Detector implements SensorEventListener { |
|
|
|
|
|
|
|
|
Logger logger; |
|
|
Logger logger; |
|
|
|
|
|
|
|
|
private volatile boolean running = false; |
|
|
private volatile boolean running = false; |
|
|
public SensorManager sensorManager; |
|
|
public SensorManager sensorManager; |
|
|
private int sensorType = Sensor.TYPE_ACCELEROMETER; |
|
|
|
|
|
|
|
|
private static final int sensorType = Sensor.TYPE_LINEAR_ACCELERATION; |
|
|
private Sensor accelerometer; |
|
|
private Sensor accelerometer; |
|
|
private Context context; |
|
|
private Context context; |
|
|
private Thread thread; |
|
|
|
|
|
|
|
|
|
|
|
private String threadName = "Accelerometer Thread"; |
|
|
private String threadName = "Accelerometer Thread"; |
|
|
double[] gravity = new double[3]; |
|
|
|
|
|
double[] linear_acceleration = new double[3]; |
|
|
|
|
|
|
|
|
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 TextView textViewWorkerThread; |
|
|
private TextView textViewWorkerThread; |
|
|
|
|
|
private AppCompatActivity activity; |
|
|
|
|
|
private DetectionReport detectionReport; |
|
|
|
|
|
|
|
|
// In constructor pass Context from MainActivity in Accelerometer class |
|
|
|
|
|
public Accelerometer(Context context, TextView textViewWorkerThread){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// In constructor pass Activity, Context and TextView from MainActivity in Accelerometer class |
|
|
|
|
|
public Accelerometer(Context context, Logger mainLogger, TextView textViewWorkerThread){ |
|
|
|
|
|
super(context); //von Detektor |
|
|
|
|
|
logger = mainLogger; |
|
|
this.context = context; |
|
|
this.context = context; |
|
|
this.textViewWorkerThread = textViewWorkerThread; |
|
|
this.textViewWorkerThread = textViewWorkerThread; |
|
|
} |
|
|
|
|
|
// Passing Activity's instance as argument on worker thread |
|
|
|
|
|
AppCompatActivity activity; |
|
|
|
|
|
public Accelerometer(AppCompatActivity activity){ |
|
|
|
|
|
this.activity = activity; |
|
|
|
|
|
|
|
|
Logger logger = new Logger(this.getClass().getSimpleName(), textViewWorkerThread, ""); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
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"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
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 // |
|
|
// register the sensor before using // |
|
|
sensorManager.registerListener((SensorEventListener) context, accelerometer, SensorManager.SENSOR_DELAY_NORMAL); |
|
|
|
|
|
logger.log("Accelerometer, du bist dran!"); |
|
|
|
|
|
|
|
|
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME); |
|
|
|
|
|
logger.log("Accelerometer, start!"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void stop() { |
|
|
void stop() { |
|
|
// unregister from the sensor to stop using it // |
|
|
// unregister from the sensor to stop using it // |
|
|
sensorManager.unregisterListener((SensorEventListener) context, accelerometer); |
|
|
|
|
|
logger.log("Das reicht Accelerometer, komm zurück!"); |
|
|
|
|
|
if (!running){ |
|
|
|
|
|
logger.log(threadName + " not running."); |
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
//stringBuffer.append("\nBetrag =" + betrag); |
|
|
|
|
|
//logger.log("Betrag" + betrag); |
|
|
|
|
|
|
|
|
|
|
|
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()); |
|
|
|
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
logger.log("Stopping " + threadName); |
|
|
|
|
|
running = false; |
|
|
|
|
|
while (true) { |
|
|
|
|
|
try { |
|
|
|
|
|
thread.join(); |
|
|
|
|
|
logger.log("..." + threadName + " stopped"); |
|
|
|
|
|
break; |
|
|
|
|
|
} catch (InterruptedException e){ |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
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.clearLog(); |
|
|
|
|
|
logger.log(stringBuffer.toString()); |
|
|
|
|
|
stringBuffer.delete(0, stringBuffer.length()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
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); |
|
|
|
|
|
|
|
|
public void onAccuracyChanged(Sensor sensor, int accuracy) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onAccuracyChanged(Sensor sensor, int accuracy) { |
|
|
|
|
|
|
|
|
public void startDetection() { |
|
|
|
|
|
// entspricht void start() |
|
|
|
|
|
if (accelerometer != null) { |
|
|
|
|
|
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME); |
|
|
|
|
|
logger.log("Sensor registriert"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void stopDetection() { |
|
|
|
|
|
// entspricht void stop() |
|
|
|
|
|
sensorManager.unregisterListener(this, accelerometer); |
|
|
|
|
|
logger.log("Vom Sensor abgemeldet"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |