Move the acceleration sensor to its own class
This commit is contained in:
parent
1094b0f75d
commit
6c4b6b67a4
@ -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
|
@Override
|
||||||
public void onChanged(Boolean aBoolean) {
|
public void onChanged(Boolean aBoolean) {
|
||||||
if (aBoolean) {
|
if (aBoolean) {
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -34,11 +34,11 @@ public class CameraSensor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,10 +117,10 @@ public class CameraSensor {
|
|||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,86 +6,59 @@ import androidx.lifecycle.ViewModel;
|
|||||||
|
|
||||||
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();
|
if (mAccelerometerAlarmDetected == null) {
|
||||||
mMovementDetected.setValue(false);
|
mAccelerometerAlarmDetected = mAccelerometerSensor.getAccelerometerAlarmDetected();
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addValueToGesamtBE(float newValue) {
|
public void addValueToGesamtBE(float newValue) {
|
||||||
if (Gesamt_be.size() == arraySize) {
|
mAccelerometerSensor.addValueToGesamtBE(newValue);
|
||||||
Gesamt_be.remove(Gesamt_be.size() -1);
|
|
||||||
}
|
|
||||||
Gesamt_be.add(0, newValue);
|
|
||||||
functionCallCount++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void meanValueCalculation() {
|
public void meanValueCalculation() {
|
||||||
for (float element : Gesamt_be) {
|
mAccelerometerSensor.meanValueCalculation();
|
||||||
meanValue += Math.abs(element);
|
|
||||||
}
|
|
||||||
meanValue = meanValue/arraySize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void calibrateAccelerometerSensor() {
|
public void calibrateAccelerometerSensor() {
|
||||||
if (functionCallCount <= arraySize) {
|
mAccelerometerSensor.calibrateAccelerometerSensor();
|
||||||
offsetValue = meanValue;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
startMeasuring = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void checkAlarmCondition() {
|
public void checkAlarmCondition() {
|
||||||
if (meanValue > (thresholdValue + offsetValue) && startMeasuring && !mMovementDetected.getValue()) {
|
mAccelerometerSensor.checkAlarmCondition();
|
||||||
mMovementDetected.setValue(true);
|
|
||||||
}
|
|
||||||
else if (meanValue < (thresholdValue + offsetValue) && mMovementDetected.getValue()){
|
|
||||||
mMovementDetected.setValue(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public LiveData<Boolean> getMovementDetectedValue() {
|
public LiveData<Boolean> getAccelerometerAlarmDetected() {
|
||||||
return mMovementDetected;
|
return mAccelerometerAlarmDetected;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user