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

} }
}); });


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

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

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

} }


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


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




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

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



import com.example.greenwatch.models.Device; import com.example.greenwatch.models.Device;
import com.example.greenwatch.repositories.DeviceRepository; 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; import java.util.List;


public class AccelerometerViewModel extends ViewModel implements ViewModelInterface { public class AccelerometerViewModel extends ViewModel implements ViewModelInterface {


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


@Override @Override
public void init() { public void init() {
if (mDeviceList == null) {
if (mDeviceRepository == null) {
mDeviceRepository = DeviceRepository.getInstance(); mDeviceRepository = DeviceRepository.getInstance();
}
if (mAccelerometerSensor == null) {
mAccelerometerSensor = AccelerometerSensor.getInstance();
}
if (mDeviceList == null) {
mDeviceList = mDeviceRepository.getConnectedDeviceList(); mDeviceList = mDeviceRepository.getConnectedDeviceList();
}
if (mAlarmHistoryList == null) {
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList(); mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList();
}
if (mStartAlarmRecording == null) {
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording(); 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) { 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() { public void meanValueCalculation() {
for (float element : Gesamt_be) {
meanValue += Math.abs(element);
}
meanValue = meanValue/arraySize;
mAccelerometerSensor.meanValueCalculation();
} }


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


public void checkAlarmCondition() { 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 @Override

Loading…
Cancel
Save