Browse Source

added Detector, DetectorReport, Logger

lm
Leon Market 11 months ago
parent
commit
4999f1b998

+ 83
- 0
app/src/main/java/com/example/ueberwachungssystem/Beschleunigungssensor.java View File

@@ -0,0 +1,83 @@
package com.example.ueberwachungssystem;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;

public class Beschleunigungssensor extends AppCompatActivity implements SensorEventListener
{
private Logger logger;
private SensorManager sensorManager;
private int sensorType = Sensor.TYPE_ACCELEROMETER;
private Sensor sensor;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setTitle(this.getClass().getSimpleName());
TextView textView = new TextView(this);
setContentView(textView);

logger = new Logger(this.getClass().getSimpleName(),textView,"");

sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
if(sensorManager.getSensorList(sensorType).size()==0)
{
logger.log("Es gibt den gewünschten Sensor nicht");
sensor = null;
}
else
{
sensor = sensorManager.getSensorList(sensorType).get(0);
}
}

@Override
protected void onResume()
{
super.onResume();
if(sensor != null)
{
if(sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME))
{
logger.log("Wir haben uns beim Sensor angemeldet.");
}
else
{
logger.log("Das Anmelden beim Sensor hat nicht so gut geklappt");
}
}
}

@Override
protected void onPause()
{
super.onPause();
if(sensor != null) {
sensorManager.unregisterListener(this, sensor);
logger.log("Wir haben uns beim Sensor abgemeldet");
}
}

@Override
public void onSensorChanged(SensorEvent event)
{
String sb = "t=" + event.timestamp +
"\nx=" + event.values[0] + // Wert legend: 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
logger.clearLog();
logger.log(sb);
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
}

+ 36
- 0
app/src/main/java/com/example/ueberwachungssystem/DetectionReport.java View File

@@ -0,0 +1,36 @@
package com.example.ueberwachungssystem;

import android.util.Log;

import java.util.Calendar;

/** Detection Report Class */
public class DetectionReport {
public String timeStamp;
public String detectionType;
public float detectedValue;
public String detectorID;

public DetectionReport(String detectorID, String detectionType, float detectedAmplitude) {
this.timeStamp = String.valueOf(Calendar.getInstance().getTime());
this.detectionType = detectionType;
this.detectedValue = detectedAmplitude;
this.detectorID = detectorID;
}


/** Get Detection Report in String format */
public String toString() {
String time = "Time: " + "[" + this.timeStamp + "]";
String type = "Type: " + "[" + this.detectionType + "]";
String value = "Value: " + "[" + this.detectedValue + "]";
String id = "ID: " + "[" + this.detectorID + "]";

return String.join("\t", time, type, value, id);
}

/** Debug Report */
public void log(String tag) {
Log.d(tag, this.toString());
}
}

+ 39
- 0
app/src/main/java/com/example/ueberwachungssystem/Detector.java View File

@@ -0,0 +1,39 @@
package com.example.ueberwachungssystem;

import android.content.Context;
import androidx.annotation.NonNull;


abstract public class Detector {
private OnDetectionListener listener;

/** Constructor - takes context of current activity */
public Detector(Context context) {};


/** On Detection Listener - runs when violation is reported */
public interface OnDetectionListener {
void onDetection(@NonNull DetectionReport detectionReport);
}
public void setOnDetectionListener(@NonNull OnDetectionListener listener) {
this.listener = listener;
}


/** Triggers onDetectionListener - call this to trigger violation/alarm */
private void reportViolation(String detectorID, String detectionType, float amplitude) {
if (listener != null) {
DetectionReport detectionReport = new DetectionReport(detectorID, detectionType, amplitude);
listener.onDetection(detectionReport);
} else {
throw new IllegalStateException("No listener set for violation reporting");
}
}


/** Starts Detection (abstract method: needs to be overridden in child class) */
public abstract void startDetection();

/** Stops Detection (abstract method: needs to be overridden in child class) */
public abstract void stopDetection();
}

+ 44
- 0
app/src/main/java/com/example/ueberwachungssystem/Logger.java View File

@@ -0,0 +1,44 @@
package com.example.ueberwachungssystem;

import android.widget.TextView;
import android.util.Log;
import java.io.PrintWriter;
import java.io.StringWriter;

public class Logger {
private TextView textView;
private StringBuffer sb = new StringBuffer();
private String tag;

public Logger(String tag, TextView textView, String logInitText) {
this.tag = tag;
this.textView = textView;
sb.append(logInitText);
}

public void log(String s) {
Log.d(tag, s);
sb.append(s).append("\n");
if (textView != null) {
textView.setText(sb.toString());
}
}
public void log(Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
log(sw.toString());
}

public void clearLog() {
sb.setLength(0);
if (textView != null) {
textView.setText("");
}
}

public String getLoggedText() {
return sb.toString();
}
}



+ 6
- 1
app/src/main/java/com/example/ueberwachungssystem/MainActivity.java View File

@@ -4,11 +4,16 @@ import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity{

private Logger logger;
private Beschleunigungssensor beschleunigungssensor;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(this.getClass().getSimpleName());
setContentView(R.layout.activity_main);

}
}

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

@@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
tools:context=".MainActivityBackup">

<TextView
android:layout_width="wrap_content"

Loading…
Cancel
Save