@@ -1,123 +0,0 @@ | |||
package com.example.ueberwachungssystem; | |||
import static java.lang.Math.sqrt; | |||
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.annotation.NonNull; | |||
import androidx.annotation.Nullable; | |||
import androidx.appcompat.app.AppCompatActivity; | |||
import android.widget.TextView; | |||
import java.util.ArrayDeque; | |||
import java.util.Arrays; | |||
import java.util.Collection; | |||
import java.util.Deque; | |||
import java.util.Iterator; | |||
import java.util.OptionalDouble; | |||
import java.util.Queue; | |||
public class Beschleunigungssensor extends AppCompatActivity implements SensorEventListener | |||
{ | |||
private Logger logger; | |||
private SensorManager sensorManager; | |||
private static final int sensorType = Sensor.TYPE_LINEAR_ACCELERATION; | |||
private Sensor sensor; | |||
boolean alarm = false; | |||
//Preallocate memory for the data of each axis of the acceleration sensor | |||
double x; | |||
double y; | |||
double z; | |||
double betrag; //Betrag aller drei Achsen sqrt(x*x + y*y + z*z) | |||
@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); | |||
logger.log("Accelerometer gefunden."); | |||
} | |||
} | |||
@Override | |||
protected void onResume() | |||
{ | |||
super.onResume(); | |||
if(sensor != null) | |||
{ | |||
if(sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL)) | |||
{ | |||
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"); | |||
} | |||
} | |||
private void checkAlarm (SensorEvent event) { | |||
x = event.values[0]; | |||
y = event.values[1]; | |||
z = event.values[2]; | |||
betrag = sqrt(x*x + y*y + z*z); | |||
double schwelle = 0.05; | |||
if (!alarm) { | |||
if (betrag > schwelle) { | |||
logger.log("Betragswert über Schwelle erkannt, Alarm wird gleich angeschaltet"); | |||
alarm = true; | |||
logger.log("Alarm an"); | |||
logger.log("Betrag: " + betrag + event.timestamp); | |||
} | |||
} else { | |||
if (betrag < schwelle) { | |||
logger.log("Alarm ist noch an; Neuer Betragswert unter Schwellwert: " + betrag); | |||
alarm = false; | |||
logger.log("Alarm wieder ausgeschaltet"); | |||
} else { | |||
logger.log("Betragswert immer noch über Schwellwert: " + betrag + "; Alarm bleibt an."); | |||
} | |||
} | |||
} | |||
@Override | |||
public void onSensorChanged(SensorEvent event) | |||
{ | |||
checkAlarm(event); | |||
} | |||
@Override | |||
public void onAccuracyChanged(Sensor sensor, int accuracy) | |||
{ | |||
} | |||
} |
@@ -1,4 +1,4 @@ | |||
package com.example.ueberwachungssystem; | |||
package com.example.ueberwachungssystem.Detection; | |||
import static java.lang.Math.sqrt; | |||
@@ -7,9 +7,6 @@ import android.hardware.Sensor; | |||
import android.hardware.SensorEvent; | |||
import android.hardware.SensorEventListener; | |||
import android.hardware.SensorManager; | |||
import android.widget.TextView; | |||
import java.util.concurrent.LinkedBlockingQueue; | |||
/** | |||
@@ -33,7 +30,6 @@ import java.util.concurrent.LinkedBlockingQueue; | |||
public class Accelerometer extends Detector implements SensorEventListener { | |||
Logger logger; | |||
public SensorManager sensorManager; | |||
private static final int sensorType = Sensor.TYPE_LINEAR_ACCELERATION; | |||
private Sensor accelerometer; | |||
@@ -45,31 +41,20 @@ public class Accelerometer extends Detector implements SensorEventListener { | |||
float z; | |||
float betrag; //Betrag aller drei Achsen sqrt(x*x + y*y + z*z) | |||
private DetectionReport detectionReport; | |||
TextView textView; | |||
//Queue for taking in values | |||
LinkedBlockingQueue<Float> linkedBlockingQueue = new LinkedBlockingQueue<>(); | |||
// In constructor pass Activity, Context and TextView from MainActivity in Accelerometer class | |||
public Accelerometer(Context context, Logger mainLogger, TextView textView){ | |||
public Accelerometer(Context context){ | |||
super(context); //von Detektor | |||
logger = mainLogger; | |||
this.context = context; | |||
this.textView = this.textView; | |||
//Logger logger = new Logger(this.getClass().getSimpleName(), textViewLog, ""); | |||
} | |||
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"); | |||
} | |||
} | |||
@@ -82,8 +67,6 @@ public class Accelerometer extends Detector implements SensorEventListener { | |||
} | |||
} | |||
StringBuffer stringBuffer = new StringBuffer(); | |||
public void checkAlarm (SensorEvent event) throws InterruptedException { | |||
x = event.values[0]; | |||
y = event.values[1]; | |||
@@ -96,26 +79,12 @@ public class Accelerometer extends Detector implements SensorEventListener { | |||
alarm = true; | |||
detectionReport = new DetectionReport("Accelerometer1", "Bewegung", betrag); | |||
reportViolation("Accelo1", "Bewegung", betrag); | |||
stringBuffer.append("\nDetectionReport = ").append(detectionReport).append("\nAlarm an"); | |||
logger.clearLog(); | |||
logger.log(stringBuffer.toString()); | |||
stringBuffer.delete(0, stringBuffer.length()); | |||
} | |||
} else { | |||
if (betrag < threshold) { | |||
stringBuffer.append("\nAlarm noch an; Neuer Betragswert unter Schwellwert:").append(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 = ").append(alarm); | |||
} else { | |||
logger.log("Betragswert immer noch über Schwellwert: " + betrag + "; Alarm bleibt an."); | |||
} | |||
//logger.clearLog(); | |||
logger.log(stringBuffer.toString()); | |||
stringBuffer.delete(0, stringBuffer.length()); | |||
} | |||
} | |||
@@ -126,10 +95,9 @@ public class Accelerometer extends Detector implements SensorEventListener { | |||
@Override | |||
public void startDetection() { | |||
// entspricht void start() | |||
getSensor(); | |||
//getSensor(); | |||
if (accelerometer != null) { | |||
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME); | |||
logger.log("Sensor registriert"); | |||
} | |||
} | |||
@@ -137,6 +105,5 @@ public class Accelerometer extends Detector implements SensorEventListener { | |||
public void stopDetection() { | |||
// entspricht void stop() | |||
sensorManager.unregisterListener(this, accelerometer); | |||
logger.log("Vom Sensor abgemeldet"); | |||
} | |||
} |
@@ -1,4 +1,4 @@ | |||
package com.example.ueberwachungssystem; | |||
package com.example.ueberwachungssystem.Detection; | |||
import android.util.Log; | |||
@@ -1,4 +1,4 @@ | |||
package com.example.ueberwachungssystem; | |||
package com.example.ueberwachungssystem.Detection; | |||
import android.content.Context; | |||
import androidx.annotation.NonNull; |
@@ -1,44 +0,0 @@ | |||
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(); | |||
} | |||
} | |||
@@ -1,11 +1,8 @@ | |||
package com.example.ueberwachungssystem; | |||
import static java.lang.Boolean.TRUE; | |||
import androidx.annotation.NonNull; | |||
import androidx.appcompat.app.AppCompatActivity; | |||
import android.content.Context; | |||
import android.hardware.SensorEvent; | |||
import android.os.Bundle; | |||
import android.text.method.ScrollingMovementMethod; | |||
import android.view.View; | |||
@@ -13,9 +10,12 @@ import android.widget.Button; | |||
import android.widget.TextView; | |||
import android.widget.ToggleButton; | |||
import com.example.ueberwachungssystem.Detection.Accelerometer; | |||
import com.example.ueberwachungssystem.Detection.DetectionReport; | |||
import com.example.ueberwachungssystem.Detection.Detector; | |||
public class MainActivity extends AppCompatActivity implements View.OnClickListener { | |||
private Logger logger; | |||
private Accelerometer beschleunigungssensor; | |||
private TextView textViewLog; | |||
private Button button1; | |||
@@ -27,10 +27,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe | |||
setTitle(this.getClass().getSimpleName()); | |||
setContentView(R.layout.activity_main); | |||
// Logger setup | |||
textViewLog = findViewById(R.id.textViewLog); | |||
textViewLog.setMovementMethod(new ScrollingMovementMethod()); | |||
logger = new Logger(this.getClass().getSimpleName(), textViewLog, ""); | |||
toggleButton1 = findViewById(R.id.toggleButton1); //togglebutton um Detektion zu steuern | |||
toggleButton1.setOnClickListener(this); | |||
@@ -40,10 +36,15 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe | |||
button1.setOnClickListener(this); | |||
//Accelerometer Setup | |||
beschleunigungssensor = new Accelerometer(this, logger, textViewLog); //logger and textview only for debugging necessary | |||
//beschleunigungssensor.getSensor(); | |||
beschleunigungssensor = new Accelerometer(this); //logger and textview only for debugging necessary | |||
beschleunigungssensor.getSensor(); | |||
beschleunigungssensor.setOnDetectionListener(new Detector.OnDetectionListener() { | |||
@Override | |||
public void onDetection(@NonNull DetectionReport detectionReport) { | |||
} | |||
}); | |||
logger.log("onCreate"); | |||
} | |||
@Override | |||
@@ -58,20 +59,14 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe | |||
@Override | |||
public void onClick(View v) { | |||
logger.log("toggleButton1 is clicked"); | |||
if(v == toggleButton1) { | |||
if (toggleButton1.isChecked()) { | |||
logger.log("ToggleButton is ON"); | |||
beschleunigungssensor.startDetection(); | |||
logger.log("Detection started"); | |||
} else { | |||
logger.log("ToggleButton is OFF"); | |||
beschleunigungssensor.stopDetection(); | |||
logger.log("Detection stopped"); | |||
} | |||
} else if (v == button1) { | |||
logger.clearLog(); //just to clear log manually if it's too full | |||
} | |||
} | |||
} |
@@ -1,63 +0,0 @@ | |||
package com.example.ueberwachungssystem; | |||
import androidx.appcompat.app.AppCompatActivity; | |||
import java.util.concurrent.LinkedBlockingQueue; | |||
public class SensorValueThread implements Runnable { | |||
private volatile boolean running = false; | |||
private Thread thread; | |||
private String threadname = "sensorValueThread"; | |||
Logger logger; | |||
float x; | |||
LinkedBlockingQueue<Float> linkedBlockingQueue; | |||
//AppCompatActivity activity; | |||
//Constructor muss die LinkedBlockingQueue übergeben bekommen | |||
public SensorValueThread(LinkedBlockingQueue linkedBlockingQueue){ | |||
this.linkedBlockingQueue = linkedBlockingQueue; | |||
} | |||
@Override | |||
public void run() { | |||
while (running){ | |||
//Code to execute while running == true, i.e. reading queue and calculate sth. | |||
try { | |||
x = linkedBlockingQueue.take(); | |||
Thread.sleep(100); | |||
} catch (InterruptedException e) { | |||
e.printStackTrace(); | |||
} | |||
} | |||
} | |||
void start() { | |||
logger.log("Starting " + threadname + "..."); | |||
running = true; | |||
thread = new Thread(this); | |||
thread.setName(threadname); | |||
thread.start(); | |||
logger.log("..." + threadname + " started"); | |||
} | |||
void stop() { | |||
if (!running) { | |||
logger.log(threadname + " not running"); | |||
} else { | |||
logger.log("Stopping " + threadname + "..."); | |||
running = false; | |||
while(true){ | |||
try { | |||
thread.join(); | |||
logger.log("... " + threadname + " stopped"); | |||
break; | |||
} catch (InterruptedException e) { | |||
e.printStackTrace(); | |||
} | |||
} | |||
} | |||
} | |||
} |
@@ -1,70 +0,0 @@ | |||
package com.example.ueberwachungssystem; | |||
import android.content.Context; | |||
import android.widget.TextView; | |||
import androidx.appcompat.app.AppCompatActivity; | |||
public class ThreadDemo implements Runnable { | |||
private volatile boolean running = false; | |||
private Thread thread; | |||
private String threadname = "testThread"; | |||
Logger logger; | |||
// Passing Activity's instance as argument on worker thread | |||
AppCompatActivity activity; | |||
public ThreadDemo(AppCompatActivity activity){ | |||
this.activity = activity; | |||
} | |||
//Method print which delegates access on MainActivity to runOnUiThread | |||
private void print(final String s) { | |||
activity.runOnUiThread(new Runnable() { | |||
@Override | |||
public void run() { | |||
//textViewWorkerThread.setText(s); | |||
} | |||
}); | |||
} | |||
@Override | |||
public void run() { | |||
int i = 0; | |||
while (running) { | |||
i++; | |||
print(String.valueOf(i)); | |||
try { | |||
Thread.sleep(100); | |||
} catch (InterruptedException e) { | |||
e.printStackTrace(); | |||
} | |||
} | |||
//print(activity.getString(R.string.workerThread) + " endet mit " + i); | |||
} | |||
void start() { | |||
logger.log("Starting " + threadname + "..."); | |||
running = true; | |||
thread = new Thread(this); | |||
thread.setName(threadname); | |||
thread.start(); | |||
logger.log("..." + threadname + " started"); | |||
} | |||
void stop() { | |||
if (!running) { | |||
logger.log(threadname + " not running"); | |||
} else { | |||
logger.log("Stopping " + threadname + "..."); | |||
running = false; | |||
while(true){ | |||
try { | |||
thread.join(); | |||
logger.log("... " + threadname + " stopped"); | |||
break; | |||
} catch (InterruptedException e) { | |||
e.printStackTrace(); | |||
} | |||
} | |||
} | |||
} | |||
} |