|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()); |