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



import java.util.concurrent.LinkedBlockingQueue; 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 { public class Accelerometer extends Detector implements SensorEventListener {


Logger logger; Logger logger;
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 textViewLog;
TextView textView;


//Queue for taking in values //Queue for taking in values
LinkedBlockingQueue<Float> linkedBlockingQueue = new LinkedBlockingQueue<>(); 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 textViewLog){
public Accelerometer(Context context, Logger mainLogger, TextView textView){
super(context); //von Detektor super(context); //von Detektor
logger = mainLogger; logger = mainLogger;
this.context = context; this.context = context;
this.textViewLog = textViewLog;
this.textView = this.textView;
//Logger logger = new Logger(this.getClass().getSimpleName(), textViewLog, ""); //Logger logger = new Logger(this.getClass().getSimpleName(), textViewLog, "");
} }


} }
} }



// 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 @Override
public void onSensorChanged(SensorEvent event) { public void onSensorChanged(SensorEvent event) {
try { try {
} }


StringBuffer stringBuffer = new StringBuffer(); 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];
if (betrag > threshold) { if (betrag > threshold) {
alarm = true; alarm = true;
detectionReport = new DetectionReport("Accelerometer1", "Bewegung", betrag); 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.clearLog();
logger.log(stringBuffer.toString()); logger.log(stringBuffer.toString());
stringBuffer.delete(0, stringBuffer.length()); stringBuffer.delete(0, stringBuffer.length());

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener { public class MainActivity extends AppCompatActivity implements View.OnClickListener {


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

ToggleButton toggleButton1; ToggleButton toggleButton1;


boolean currentAlarm;

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


// Logger setup // Logger setup
textViewLog = (TextView) findViewById(R.id.textViewLog);
textViewLog = findViewById(R.id.textViewLog);
textViewLog.setMovementMethod(new ScrollingMovementMethod());
logger = new Logger(this.getClass().getSimpleName(), textViewLog, ""); 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); toggleButton1.setOnClickListener(this);
textViewWorkerThread = (TextView) findViewById(R.id.textViewWorkerThread); //TextView um Thread zu überwachen
textViewWorkerThread.setMovementMethod(new ScrollingMovementMethod());


//Button to clear Log while Debugging //Button to clear Log while Debugging
button1 = (Button) findViewById(R.id.button1); button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this); button1.setOnClickListener(this);


//Accelerometer Setup //Accelerometer Setup
beschleunigungssensor = new Accelerometer(this, logger, textViewWorkerThread);
beschleunigungssensor = new Accelerometer(this, logger, textViewLog); //logger and textview only for debugging necessary
beschleunigungssensor.getSensor(); beschleunigungssensor.getSensor();


logger.log("onCreate"); logger.log("onCreate");
logger.log("Detection stopped"); logger.log("Detection stopped");
} }
} else if (v == button1) { } 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

private Thread thread; private Thread thread;
private String threadname = "sensorValueThread"; private String threadname = "sensorValueThread";
Logger logger; Logger logger;
float x;
LinkedBlockingQueue<Float> linkedBlockingQueue; LinkedBlockingQueue<Float> linkedBlockingQueue;
//AppCompatActivity activity; //AppCompatActivity activity;


@Override @Override
public void run() { public void run() {
while (running){ 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 { try {
linkedBlockingQueue.take();
x = linkedBlockingQueue.take();

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

Loading…
Cancel
Save