Browse Source

Added detection report

lm
Leon Market 1 year ago
parent
commit
2cff334056

+ 94
- 79
app/src/main/java/com/example/ueberwachungssystem/Accelerometer.java View File

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");
} }
} }

+ 2
- 1
app/src/main/java/com/example/ueberwachungssystem/Beschleunigungssensor.java View File

else else
{ {
sensor = sensorManager.getSensorList(sensorType).get(0); sensor = sensorManager.getSensorList(sensorType).get(0);
logger.log("Accelerometer gefunden.");
} }
} }


y = event.values[1]; y = event.values[1];
z = event.values[2]; z = event.values[2];
betrag = sqrt(x*x + y*y + z*z); betrag = sqrt(x*x + y*y + z*z);
double schwelle = 1.5;
double schwelle = 0.05;


if (!alarm) { if (!alarm) {
if (betrag > schwelle) { if (betrag > schwelle) {

+ 37
- 14
app/src/main/java/com/example/ueberwachungssystem/MainActivity.java View File

import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;


import android.content.Context; import android.content.Context;
import android.hardware.SensorEvent;
import android.os.Bundle; import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View; import android.view.View;
import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
import android.widget.ToggleButton; import android.widget.ToggleButton;




private Logger logger; private Logger logger;
private Context context; private Context context;
//private Accelerometer beschleunigungssensor;
private ThreadDemo threadDemo;
private Accelerometer beschleunigungssensor;
//private ThreadDemo threadDemo;
private TextView textViewLog; private TextView textViewLog;
private Button button1;
private TextView textViewWorkerThread; private TextView textViewWorkerThread;
SensorEvent event;


//private WorkerUsingThread workerUsingThread;
ToggleButton toggleButton1; ToggleButton toggleButton1;


boolean currentAlarm;

@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1); //togglebutton um Thread zu steuern toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1); //togglebutton um Thread zu steuern
toggleButton1.setOnClickListener(this); toggleButton1.setOnClickListener(this);
textViewWorkerThread = (TextView) findViewById(R.id.textViewWorkerThread); //TextView um Thread zu überwachen textViewWorkerThread = (TextView) findViewById(R.id.textViewWorkerThread); //TextView um Thread zu überwachen
// beschleunigungssensor = new Accelerometer(this);
threadDemo = new ThreadDemo(this, textViewWorkerThread);
textViewWorkerThread.setMovementMethod(new ScrollingMovementMethod());

//Button to clear Log while Debugging
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);

//Accelerometer Setup
beschleunigungssensor = new Accelerometer(this, logger, textViewWorkerThread);
beschleunigungssensor.getSensor();

logger.log("onCreate"); logger.log("onCreate");
} }


protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
//beschleunigungssensor.stop(); //beschleunigungssensor.stop();
threadDemo.stop();
//threadDemo.stop();


} }



@Override @Override
public void onClick(View v) { public void onClick(View v) {
logger.log("toggleButton1 is clicked"); logger.log("toggleButton1 is clicked");
if (toggleButton1.isChecked()) {
logger.log("ToggleButton is ON");
//beschleunigungssensor.start();
threadDemo.start();
} else {
logger.log("ToggleButton is OFF");
threadDemo.stop();
if(v == toggleButton1) {
if (toggleButton1.isChecked()) {
logger.log("ToggleButton is ON");
//beschleunigungssensor.start();
beschleunigungssensor.startDetection();
logger.log("Detection started");
//logger.log(stringBuffer.toString());

} else {
logger.log("ToggleButton is OFF");
//threadDemo.stop();
//beschleunigungssensor.stop();
beschleunigungssensor.stopDetection();
logger.log("Detection stopped");
}
} else if (v == button1) {
logger.clearLog();
} }
} }
} }

+ 25
- 5
app/src/main/res/layout/activity_main.xml View File

xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".MainActivityBackup">
tools:context=".MainActivity">


<TextView <TextView
android:id="@+id/textViewLog" android:id="@+id/textViewLog"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="60dp"
android:layout_marginEnd="32dp"
android:maxLines="25"
android:scrollbars="vertical"
android:text="TextViewLoggerMain" android:text="TextViewLoggerMain"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toBottomOf="@+id/textViewWorkerThread"
app:layout_constraintVertical_bias="0.0" />


<ToggleButton <ToggleButton
android:id="@+id/toggleButton1" android:id="@+id/toggleButton1"
android:id="@+id/textViewWorkerThread" android:id="@+id/textViewWorkerThread"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="174dp"
android:layout_marginTop="85dp"
android:layout_marginEnd="179dp"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="32dp"
android:text="textViewWorkerThreadTextView" android:text="textViewWorkerThreadTextView"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toggleButton1" /> app:layout_constraintTop_toBottomOf="@+id/toggleButton1" />


<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="35dp"
android:layout_marginTop="31dp"
android:layout_marginEnd="282dp"
android:layout_marginBottom="100dp"
android:text="Clear Log"
app:layout_constraintBottom_toTopOf="@+id/textViewWorkerThread"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

Loading…
Cancel
Save