Browse Source

Move the acceleration sensor to its own class

DoNotMergeAddCommunicationStringDependencies
Christian Tinz 1 year ago
parent
commit
6c4b6b67a4

+ 1
- 1
app/src/main/java/com/example/greenwatch/AccelerometerActivity.java View File

@@ -92,7 +92,7 @@ public class AccelerometerActivity extends AppCompatActivity implements SensorEv
}
});

mAccelerometerViewModel.getMovementDetectedValue().observe(this, new Observer<Boolean>() {
mAccelerometerViewModel.getAccelerometerAlarmDetected().observe(this, new Observer<Boolean>() {
@Override
public void onChanged(Boolean aBoolean) {
if (aBoolean) {

+ 85
- 0
app/src/main/java/com/example/greenwatch/sensors/AccelerometerSensor.java View File

@@ -0,0 +1,85 @@
package com.example.greenwatch.sensors;

import androidx.lifecycle.MutableLiveData;

import java.util.ArrayList;

public class AccelerometerSensor {
private MutableLiveData<Boolean> mAccelerometerAlarmDetected = new MutableLiveData<>();
private static AccelerometerSensor accelerometerSensorInstance;
private boolean accelerometerAlarmDetected;
private ArrayList<Float> Gesamt_be;
int arraySize = 500;
int functionCallCount;
float meanValue;
float offsetValue;
float thresholdValue;
boolean startMeasuring;

private AccelerometerSensor() {
initGesamtBE();
accelerometerAlarmDetected = false;
setMutableLiveDataAccelerometerAlarmDetected();
functionCallCount = 0;
meanValue = 0.0f;
offsetValue = 0.1f;
thresholdValue = 0.15f;
startMeasuring = false;
}

public static synchronized AccelerometerSensor getInstance() {
if (accelerometerSensorInstance == null){
accelerometerSensorInstance = new AccelerometerSensor();
}
return accelerometerSensorInstance;
}

public MutableLiveData<Boolean> getAccelerometerAlarmDetected() {
setMutableLiveDataAccelerometerAlarmDetected();
return mAccelerometerAlarmDetected;
}

private void setMutableLiveDataAccelerometerAlarmDetected() {
mAccelerometerAlarmDetected.setValue(accelerometerAlarmDetected);
}

private void initGesamtBE() {
Gesamt_be = new ArrayList<>(arraySize);
for (int i = 0; i < arraySize; i++) {
Gesamt_be.add(0.0f);
}
}

public void addValueToGesamtBE(float newValue) {
if (Gesamt_be.size() == arraySize) {
Gesamt_be.remove(Gesamt_be.size() -1);
}
Gesamt_be.add(0, newValue);
functionCallCount++;
}

public void meanValueCalculation() {
for (float element : Gesamt_be) {
meanValue += Math.abs(element);
}
meanValue = meanValue/arraySize;
}

public void calibrateAccelerometerSensor() {
if (functionCallCount <= arraySize) {
offsetValue = meanValue;
}
else {
startMeasuring = true;
}
}

public void checkAlarmCondition() {
if (meanValue > (thresholdValue + offsetValue) && startMeasuring && !mAccelerometerAlarmDetected.getValue()) {
mAccelerometerAlarmDetected.setValue(true);
}
else if (meanValue < (thresholdValue + offsetValue) && mAccelerometerAlarmDetected.getValue()){
mAccelerometerAlarmDetected.setValue(false);
}
}
}

+ 4
- 4
app/src/main/java/com/example/greenwatch/sensors/CameraSensor.java View File

@@ -34,11 +34,11 @@ public class CameraSensor {
}

public MutableLiveData<Boolean> getVideoAlarmDetectedValue() {
setMutableLiveDataVideoMovementDetected();
setMutableLiveDataVideoAlarmDetected();
return mVideoAlarmDetected;
}

private void setMutableLiveDataVideoMovementDetected() {
private void setMutableLiveDataVideoAlarmDetected() {
mVideoAlarmDetected.setValue(videoAlarmDetected);
}

@@ -117,10 +117,10 @@ public class CameraSensor {

public void checkAlarmCondition() {
if (videoAlarmDetected && !mVideoAlarmDetected.getValue()) {
setMutableLiveDataVideoMovementDetected();
setMutableLiveDataVideoAlarmDetected();
}
else if (!videoAlarmDetected && mVideoAlarmDetected.getValue()){
setMutableLiveDataVideoMovementDetected();
setMutableLiveDataVideoAlarmDetected();
}
}
}

+ 21
- 48
app/src/main/java/com/example/greenwatch/viewmodels/AccelerometerViewModel.java View File

@@ -6,86 +6,59 @@ import androidx.lifecycle.ViewModel;

import com.example.greenwatch.models.Device;
import com.example.greenwatch.repositories.DeviceRepository;
import com.example.greenwatch.sensors.AccelerometerSensor;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AccelerometerViewModel extends ViewModel implements ViewModelInterface {

private MutableLiveData<List<Device>> mDeviceList;
private MutableLiveData<Boolean> mMovementDetected = new MutableLiveData<>();
private MutableLiveData<Boolean> mAccelerometerAlarmDetected;
private MutableLiveData<List<Device>> mAlarmHistoryList;
private MutableLiveData<Boolean> mStartAlarmRecording;
private AccelerometerSensor mAccelerometerSensor;
private DeviceRepository mDeviceRepository;
private ArrayList<Float> Gesamt_be;
int arraySize = 500;
int functionCallCount;
float meanValue;
float offsetValue;
float thresholdValue;
boolean startMeasuring;

@Override
public void init() {
if (mDeviceList == null) {
if (mDeviceRepository == null) {
mDeviceRepository = DeviceRepository.getInstance();
}
if (mAccelerometerSensor == null) {
mAccelerometerSensor = AccelerometerSensor.getInstance();
}
if (mDeviceList == null) {
mDeviceList = mDeviceRepository.getConnectedDeviceList();
}
if (mAlarmHistoryList == null) {
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList();
}
if (mStartAlarmRecording == null) {
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording();
}
initGesamtBE();
mMovementDetected.setValue(false);
functionCallCount = 0;
meanValue = 0f;
offsetValue = 0.1f;
thresholdValue = 0.15f;
startMeasuring = false;
}

public void initGesamtBE() {
Gesamt_be = new ArrayList<>(arraySize);
for (int i = 0; i < arraySize; i++) {
Gesamt_be.add(0f);
if (mAccelerometerAlarmDetected == null) {
mAccelerometerAlarmDetected = mAccelerometerSensor.getAccelerometerAlarmDetected();
}
}

public void addValueToGesamtBE(float newValue) {
if (Gesamt_be.size() == arraySize) {
Gesamt_be.remove(Gesamt_be.size() -1);
}
Gesamt_be.add(0, newValue);
functionCallCount++;
mAccelerometerSensor.addValueToGesamtBE(newValue);
}

public void meanValueCalculation() {
for (float element : Gesamt_be) {
meanValue += Math.abs(element);
}
meanValue = meanValue/arraySize;
mAccelerometerSensor.meanValueCalculation();
}

public void calibrateAccelerometerSensor() {
if (functionCallCount <= arraySize) {
offsetValue = meanValue;
}
else {
startMeasuring = true;
}
mAccelerometerSensor.calibrateAccelerometerSensor();
}

public void checkAlarmCondition() {
if (meanValue > (thresholdValue + offsetValue) && startMeasuring && !mMovementDetected.getValue()) {
mMovementDetected.setValue(true);
}
else if (meanValue < (thresholdValue + offsetValue) && mMovementDetected.getValue()){
mMovementDetected.setValue(false);
}
mAccelerometerSensor.checkAlarmCondition();
}

public LiveData<Boolean> getMovementDetectedValue() {
return mMovementDetected;
public LiveData<Boolean> getAccelerometerAlarmDetected() {
return mAccelerometerAlarmDetected;
}

@Override

Loading…
Cancel
Save