Leon Market 1 year ago
parent
commit
3d02d8d4c0

+ 3
- 0
Notiz.txt View File

Sensor als Service einrichten, der aus Activity heraus gestartet werden kann.
Stichwort: Intent
Siehe Skript Teil 1

+ 84
- 92
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 extends Detector implements SensorEventListener {

public class Accelerometer implements Runnable, SensorEventListener {


Logger logger; Logger logger;
private volatile boolean running = false;
public SensorManager sensorManager; public SensorManager sensorManager;
private static final int sensorType = Sensor.TYPE_LINEAR_ACCELERATION;
private int sensorType = Sensor.TYPE_ACCELEROMETER;
private Sensor accelerometer; private Sensor accelerometer;
private Context context; 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.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 // // 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() { void stop() {
// unregister from the sensor to stop using it // // 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 { } 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 @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 @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");
} }
} }

+ 9
- 10
app/src/main/java/com/example/ueberwachungssystem/ThreadDemo.java View File

private volatile boolean running = false; private volatile boolean running = false;
private Thread thread; private Thread thread;
private String threadname = "testThread"; private String threadname = "testThread";
//Logger logger;
TextView textViewWorkerThread;
Logger logger;


// Passing Activity's instance as argument on worker thread // Passing Activity's instance as argument on worker thread
AppCompatActivity activity; AppCompatActivity activity;
public ThreadDemo(AppCompatActivity activity, TextView textViewWorkerThread){
public ThreadDemo(AppCompatActivity activity){
this.activity = activity; this.activity = activity;
this.textViewWorkerThread = textViewWorkerThread;
} }

//Method print which delegates access on MainActivity to runOnUiThread //Method print which delegates access on MainActivity to runOnUiThread
private void print(final String s) { private void print(final String s) {
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
textViewWorkerThread.setText(s);
//textViewWorkerThread.setText(s);
} }
}); });
} }
} }


void start() { void start() {
//logger.log("Starting " + threadname + "...");
logger.log("Starting " + threadname + "...");
running = true; running = true;
thread = new Thread(this); thread = new Thread(this);
thread.setName(threadname); thread.setName(threadname);
thread.start(); thread.start();
//logger.log("..." + threadname + " started");
logger.log("..." + threadname + " started");
} }
void stop() { void stop() {
if (!running) { if (!running) {
//logger.log(threadname + " not running");
logger.log(threadname + " not running");
} else { } else {
//logger.log("Stopping " + threadname + "...");
logger.log("Stopping " + threadname + "...");
running = false; running = false;
while(true){ while(true){
try { try {
thread.join(); thread.join();
//logger.log("... " + threadname + " stopped");
logger.log("... " + threadname + " stopped");
break; break;
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();

Loading…
Cancel
Save