Browse Source

Changed to a (hopefully) working version of the Accelerometer

lm
Leon Market 1 year ago
parent
commit
8a5d720685

+ 26
- 21
app/src/main/java/com/example/ueberwachungssystem/Accelerometer.java View File

@@ -11,6 +11,26 @@ import android.widget.TextView;

import java.util.concurrent.LinkedBlockingQueue;


/**
* Accelerometer inherits some methods from abstract Detector class (more info there)
*
*
* USE FROM MAIN ACTIVITY:
*
* Accelerometer beschleunigungssensor = new Accelerometer(this);
* onCreate:
* //Accelerometer Setup
* beschleunigungssensor = new Accelerometer(this, logger, textViewLog); //logger and textview only for debugging necessary
* beschleunigungssensor.getSensor();
*
* //Starting Detection:
* beschleunigungssensor.startDetection();
* //Stopping Detection: also recommended at onPause to avoid unnecessary battery consumption
* beschleunigungssensor.stopDetection();
*
* */

public class Accelerometer extends Detector implements SensorEventListener {

Logger logger;
@@ -25,18 +45,18 @@ 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 textViewLog;
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 textViewLog){
public Accelerometer(Context context, Logger mainLogger, TextView textView){
super(context); //von Detektor
logger = mainLogger;
this.context = context;
this.textViewLog = textViewLog;
this.textView = this.textView;
//Logger logger = new Logger(this.getClass().getSimpleName(), textViewLog, "");
}

@@ -52,22 +72,6 @@ public class Accelerometer extends Detector implements SensorEventListener {
}
}


// void start wird überflüssig mit void startDetection()
//void start() {
// // register the sensor before using //
// sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
// logger.log("Accelerometer, registered");
//}

// void stop wird überflüssig mit void stopDetection()
//void stop() {
// // unregister from the sensor to stop using it //
// sensorManager.unregisterListener(this, accelerometer);
// logger.log("Accelerometer unregistered!");
//}


@Override
public void onSensorChanged(SensorEvent event) {
try {
@@ -78,6 +82,7 @@ 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];
@@ -89,8 +94,8 @@ public class Accelerometer extends Detector implements SensorEventListener {
if (betrag > threshold) {
alarm = true;
detectionReport = new DetectionReport("Accelerometer1", "Bewegung", betrag);
stringBuffer.append("\nDetectionReport = ").append(detectionReport).append("\nAlarm an").append(betrag).append(event.timestamp);
linkedBlockingQueue.put(betrag);
stringBuffer.append("\nDetectionReport = ").append(detectionReport).append("\nAlarm an");
logger.clearLog();
logger.log(stringBuffer.toString());
stringBuffer.delete(0, stringBuffer.length());

+ 5
- 13
app/src/main/java/com/example/ueberwachungssystem/MainActivity.java View File

@@ -16,18 +16,11 @@ import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

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

ToggleButton toggleButton1;

boolean currentAlarm;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -35,20 +28,19 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
setContentView(R.layout.activity_main);

// Logger setup
textViewLog = (TextView) findViewById(R.id.textViewLog);
textViewLog = findViewById(R.id.textViewLog);
textViewLog.setMovementMethod(new ScrollingMovementMethod());
logger = new Logger(this.getClass().getSimpleName(), textViewLog, "");

toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1); //togglebutton um Thread zu steuern
toggleButton1 = findViewById(R.id.toggleButton1); //togglebutton um Detektion zu steuern
toggleButton1.setOnClickListener(this);
textViewWorkerThread = (TextView) findViewById(R.id.textViewWorkerThread); //TextView um Thread zu überwachen
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 = new Accelerometer(this, logger, textViewLog); //logger and textview only for debugging necessary
beschleunigungssensor.getSensor();

logger.log("onCreate");
@@ -79,7 +71,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
logger.log("Detection stopped");
}
} else if (v == button1) {
logger.clearLog();
logger.clearLog(); //just to clear log manually if it's too full
}
}
}

+ 4
- 2
app/src/main/java/com/example/ueberwachungssystem/SensorValueThread.java View File

@@ -10,6 +10,7 @@ public class SensorValueThread implements Runnable {
private Thread thread;
private String threadname = "sensorValueThread";
Logger logger;
float x;
LinkedBlockingQueue<Float> linkedBlockingQueue;
//AppCompatActivity activity;

@@ -21,9 +22,10 @@ public class SensorValueThread implements Runnable {
@Override
public void run() {
while (running){
//Code to execute while running == true, i.e. reading queue and
//Code to execute while running == true, i.e. reading queue and calculate sth.
try {
linkedBlockingQueue.take();
x = linkedBlockingQueue.take();

Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();

Loading…
Cancel
Save