Leon Market 1 year ago
parent
commit
8275d8bd5a

+ 0
- 0
Neues Textdokument.txt View File


+ 1
- 1
app/src/main/AndroidManifest.xml View File

android:theme="@style/Theme.Ueberwachungssystem" android:theme="@style/Theme.Ueberwachungssystem"
tools:targetApi="31"> tools:targetApi="31">
<activity <activity
android:name=".MainActivity"
android:name=".Beschleunigungssensor"
android:exported="true"> android:exported="true">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />

+ 139
- 7
app/src/main/java/com/example/ueberwachungssystem/Beschleunigungssensor.java View File

package com.example.ueberwachungssystem; package com.example.ueberwachungssystem;


import static java.lang.Math.sqrt;

import android.content.Context; import android.content.Context;
import android.hardware.Sensor; import android.hardware.Sensor;
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.os.Bundle; import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView; 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 public class Beschleunigungssensor extends AppCompatActivity implements SensorEventListener
{ {
private Logger logger; private Logger logger;
private SensorManager sensorManager; private SensorManager sensorManager;
private int sensorType = Sensor.TYPE_ACCELEROMETER;
private static final int sensorType = Sensor.TYPE_LINEAR_ACCELERATION;
private Sensor sensor; private Sensor sensor;


//Matrizen für Datenverarbeitung:
double[] linear_acceleration = new double[3];
int numberOfValues = 100;
double[][] calibrationMatrix = new double[3][numberOfValues];
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 @Override
protected void onCreate(Bundle savedInstanceState) protected void onCreate(Bundle savedInstanceState)
{ {
super.onResume(); super.onResume();
if(sensor != null) if(sensor != null)
{ {
if(sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME))
if(sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL))
{ {
logger.log("Wir haben uns beim Sensor angemeldet."); logger.log("Wir haben uns beim Sensor angemeldet.");
} }
} }
} }


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 = 1.5;

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.");
}
}
}
int i = 0;
boolean stopp_mal = false;

@Override @Override
public void onSensorChanged(SensorEvent event) public void onSensorChanged(SensorEvent event)
{ {
String sb = "t=" + event.timestamp +
"\nx=" + event.values[0] + // Wert legend: x = 0.04
checkAlarm(event);

//Alter Code, erstmal wild mit if statement ausgeschlossen
if (stopp_mal) {
if (i<numberOfValues) {
calibrationMatrix[0][i] = event.values[0];
calibrationMatrix[1][i] = event.values[1];
calibrationMatrix[2][i] = event.values[2];
i++;
}

double sumX = 0;
double sumY = 0;
double sumZ = 0;
for (int j = 0; j<numberOfValues;j++){
sumX += calibrationMatrix[0][j];
sumY += calibrationMatrix[1][j];
sumZ += calibrationMatrix[2][j];
}

double averageX = sumX/numberOfValues;
double averageY = sumY/numberOfValues;
double averageZ = sumZ/numberOfValues;

String sb2 = "t=" + event.timestamp +
"\nx=" + event.values[0] + // Wert liegend: x = 0.04
"\ny=" + event.values[1] + // Wert liegend: y = 0.44 "\ny=" + event.values[1] + // Wert liegend: y = 0.44
"\nz=" + event.values[2]; // Wert liegend: z = 9.90 = Erdbeschleunigung --> Wenn Ausrichtung unbekannt ist, müsste kalibrierung bei Start der Bewegungsüberwachung vorgenommen werden
logger.clearLog();
logger.log(sb);
"\nz=" + event.values[2] + // Wert liegend: z = 9.90 = Erdbeschleunigung
"\nlin_x=" + linear_acceleration[0] +
"\nlin_x=" + linear_acceleration[1] +
"\nlin_x=" + linear_acceleration[2] +
"\naverageX=" + averageX +
"\naverageY=" + averageY +
"\naverageZ=" + averageZ +
"\ni=" + i;

double schwelle = 5.0;

if (alarm = false) {
if (averageX + schwelle < event.values[0]) {
alarm = true;
}
}
if (alarm = false) {
if (averageY + schwelle < event.values[1]) {
alarm = true;
}
}
if (alarm = false) {
if (averageZ + schwelle < event.values[2]) {
alarm = true;
}
}
if (alarm = true) {
if (averageX + schwelle > event.values[0]) {
alarm = false;
}
if (averageY + schwelle > event.values[1]) {
alarm = false;
}
if (averageZ + schwelle > event.values[2]) {
alarm = false;
}
}


logger.clearLog();
logger.log(sb2);
}

} }


@Override @Override
{ {
} }
} }

//Sample code for deque usage. Might be not useful.
class MovingAverage {
int size, windowSum = 0, count = 0;
Deque queue = new ArrayDeque<Integer>();
public MovingAverage(int size) {
this.size = size;
}
public double next(int val) {
++count;
// calculate the new sum by shifting the window
queue.add(val);
int tail = count > size ? (int)queue.poll() : 0;
windowSum = windowSum - tail + val;
return windowSum * 1.0 / Math.min(size, count);
}
}

+ 42
- 2
app/src/main/java/com/example/ueberwachungssystem/MainActivity.java View File

package com.example.ueberwachungssystem; package com.example.ueberwachungssystem;


import static java.lang.Boolean.TRUE;

import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;


import android.os.Bundle; import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;


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


private Logger logger; private Logger logger;
private Beschleunigungssensor beschleunigungssensor;
//private Accelerometer beschleunigungssensor;
//private SensorManager sensorManager;
private TextView textViewLog;
private TextView textViewWorkerThread;
//private WorkerUsingThread workerUsingThread;
ToggleButton toggleButton1;


@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
setTitle(this.getClass().getSimpleName()); setTitle(this.getClass().getSimpleName());
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);


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

toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1); //togglebutton um Thread zu steuern
toggleButton1.setOnClickListener(this);
textViewWorkerThread = (TextView) findViewById(R.id.textViewWorkerThread); //TextView um Thread zu überwachen

logger.log("onCreate");
}

@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
//beschleunigungssensor.stop();
}


@Override
public void onClick(View v) {
logger.log("toggleButton1 is clicked");
if (toggleButton1.isChecked()) {
logger.log("ToggleButton is ON");
} else {
logger.log("ToggleButton is OFF");
}
} }
} }

+ 25
- 0
app/src/main/res/layout/activity_main.xml View File

tools:context=".MainActivityBackup"> tools:context=".MainActivityBackup">


<TextView <TextView
android:id="@+id/textViewLog"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Hello World!" android:text="Hello World!"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />


<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="161dp"
android:layout_marginTop="115dp"
android:layout_marginEnd="162dp"
android:text="ToggleButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textViewWorkerThread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="174dp"
android:layout_marginTop="85dp"
android:layout_marginEnd="179dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toggleButton1" />

</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

+ 2
- 2
build.gradle View File

// Top-level build file where you can add configuration options common to all sub-projects/modules. // Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins { plugins {
id 'com.android.application' version '7.4.2' apply false
id 'com.android.library' version '7.4.2' apply false
id 'com.android.application' version '8.0.0' apply false
id 'com.android.library' version '8.0.0' apply false
} }

+ 3
- 1
gradle.properties View File

# Enables namespacing of each library's R class so that its R class includes only the # Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies, # resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library # thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
android.nonTransitiveRClass=true
android.defaults.buildfeatures.buildconfig=true
android.nonFinalResIds=false

+ 1
- 1
gradle/wrapper/gradle-wrapper.properties View File

#Thu May 11 15:04:30 CEST 2023 #Thu May 11 15:04:30 CEST 2023
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME

Loading…
Cancel
Save