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) | |||||
{ | |||||
} | |||||
} |
package com.example.ueberwachungssystem; | |||||
package com.example.ueberwachungssystem.Detection; | |||||
import static java.lang.Math.sqrt; | import static java.lang.Math.sqrt; | ||||
import android.hardware.SensorEvent; | import android.hardware.SensorEvent; | ||||
import android.hardware.SensorEventListener; | import android.hardware.SensorEventListener; | ||||
import android.hardware.SensorManager; | import android.hardware.SensorManager; | ||||
import android.widget.TextView; | |||||
import java.util.concurrent.LinkedBlockingQueue; | |||||
/** | /** | ||||
public class Accelerometer extends Detector implements SensorEventListener { | public class Accelerometer extends Detector implements SensorEventListener { | ||||
Logger logger; | |||||
public SensorManager sensorManager; | public SensorManager sensorManager; | ||||
private static final int sensorType = Sensor.TYPE_LINEAR_ACCELERATION; | private static final int sensorType = Sensor.TYPE_LINEAR_ACCELERATION; | ||||
private Sensor accelerometer; | private Sensor accelerometer; | ||||
float z; | float z; | ||||
float betrag; //Betrag aller drei Achsen sqrt(x*x + y*y + z*z) | float betrag; //Betrag aller drei Achsen sqrt(x*x + y*y + z*z) | ||||
private DetectionReport detectionReport; | 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 | // 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 | super(context); //von Detektor | ||||
logger = mainLogger; | |||||
this.context = context; | this.context = context; | ||||
this.textView = this.textView; | |||||
//Logger logger = new Logger(this.getClass().getSimpleName(), textViewLog, ""); | |||||
} | } | ||||
public void getSensor(){ | public void getSensor(){ | ||||
sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE); | sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE); | ||||
if(sensorManager.getSensorList(sensorType).size()==0) { | if(sensorManager.getSensorList(sensorType).size()==0) { | ||||
logger.log("Es gibt den gewünschten Sensor nicht"); | |||||
accelerometer = null; | accelerometer = null; | ||||
} | } | ||||
else { | else { | ||||
accelerometer = sensorManager.getSensorList(sensorType).get(0); | accelerometer = sensorManager.getSensorList(sensorType).get(0); | ||||
logger.log("Sensor gefunden"); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
StringBuffer stringBuffer = new StringBuffer(); | |||||
public void checkAlarm (SensorEvent event) throws InterruptedException { | public void checkAlarm (SensorEvent event) throws InterruptedException { | ||||
x = event.values[0]; | x = event.values[0]; | ||||
y = event.values[1]; | y = event.values[1]; | ||||
alarm = true; | alarm = true; | ||||
detectionReport = new DetectionReport("Accelerometer1", "Bewegung", betrag); | detectionReport = new DetectionReport("Accelerometer1", "Bewegung", betrag); | ||||
reportViolation("Accelo1", "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 { | } else { | ||||
if (betrag < threshold) { | 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; | alarm = false; | ||||
//logger.log("Alarm" + alarm); | |||||
//logger.log("Alarm wieder ausgeschaltet"); | |||||
stringBuffer.append("\nAlarm = ").append(alarm); | |||||
} else { | } else { | ||||
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 startDetection() { | public void startDetection() { | ||||
// entspricht void start() | // entspricht void start() | ||||
getSensor(); | |||||
//getSensor(); | |||||
if (accelerometer != null) { | if (accelerometer != null) { | ||||
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME); | sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME); | ||||
logger.log("Sensor registriert"); | |||||
} | } | ||||
} | } | ||||
public void stopDetection() { | public void stopDetection() { | ||||
// entspricht void stop() | // entspricht void stop() | ||||
sensorManager.unregisterListener(this, accelerometer); | sensorManager.unregisterListener(this, accelerometer); | ||||
logger.log("Vom Sensor abgemeldet"); | |||||
} | } | ||||
} | } |
package com.example.ueberwachungssystem; | |||||
package com.example.ueberwachungssystem.Detection; | |||||
import android.util.Log; | import android.util.Log; | ||||
package com.example.ueberwachungssystem; | |||||
package com.example.ueberwachungssystem.Detection; | |||||
import android.content.Context; | import android.content.Context; | ||||
import androidx.annotation.NonNull; | import androidx.annotation.NonNull; |
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(); | |||||
} | |||||
} | |||||
package com.example.ueberwachungssystem; | package com.example.ueberwachungssystem; | ||||
import static java.lang.Boolean.TRUE; | |||||
import androidx.annotation.NonNull; | |||||
import androidx.appcompat.app.AppCompatActivity; | import androidx.appcompat.app.AppCompatActivity; | ||||
import android.content.Context; | |||||
import android.hardware.SensorEvent; | |||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.text.method.ScrollingMovementMethod; | import android.text.method.ScrollingMovementMethod; | ||||
import android.view.View; | import android.view.View; | ||||
import android.widget.TextView; | import android.widget.TextView; | ||||
import android.widget.ToggleButton; | 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 { | public class MainActivity extends AppCompatActivity implements View.OnClickListener { | ||||
private Logger logger; | |||||
private Accelerometer beschleunigungssensor; | private Accelerometer beschleunigungssensor; | ||||
private TextView textViewLog; | private TextView textViewLog; | ||||
private Button button1; | private Button button1; | ||||
setTitle(this.getClass().getSimpleName()); | setTitle(this.getClass().getSimpleName()); | ||||
setContentView(R.layout.activity_main); | 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 = findViewById(R.id.toggleButton1); //togglebutton um Detektion zu steuern | ||||
toggleButton1.setOnClickListener(this); | toggleButton1.setOnClickListener(this); | ||||
button1.setOnClickListener(this); | button1.setOnClickListener(this); | ||||
//Accelerometer Setup | //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 | @Override | ||||
@Override | @Override | ||||
public void onClick(View v) { | public void onClick(View v) { | ||||
logger.log("toggleButton1 is clicked"); | |||||
if(v == toggleButton1) { | if(v == toggleButton1) { | ||||
if (toggleButton1.isChecked()) { | if (toggleButton1.isChecked()) { | ||||
logger.log("ToggleButton is ON"); | |||||
beschleunigungssensor.startDetection(); | beschleunigungssensor.startDetection(); | ||||
logger.log("Detection started"); | |||||
} else { | } else { | ||||
logger.log("ToggleButton is OFF"); | |||||
beschleunigungssensor.stopDetection(); | beschleunigungssensor.stopDetection(); | ||||
logger.log("Detection stopped"); | |||||
} | } | ||||
} else if (v == button1) { | } else if (v == button1) { | ||||
logger.clearLog(); //just to clear log manually if it's too full | |||||
} | } | ||||
} | } | ||||
} | } |
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(); | |||||
} | |||||
} | |||||
} | |||||
} | |||||
} |
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(); | |||||
} | |||||
} | |||||
} | |||||
} | |||||
} |