Browse Source

Test

DoNotMergeAddCommunicationStrigDependencies
Christian Tinz 1 year ago
parent
commit
c57d277dfb

+ 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.getAccelerometerAlarmDetected().observe(this, new Observer<Boolean>() {
mAccelerometerViewModel.getMovementDetectedValue().observe(this, new Observer<Boolean>() {
@Override
public void onChanged(Boolean aBoolean) {
if (aBoolean) {

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

@@ -6,59 +6,86 @@ 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> mAccelerometerAlarmDetected;
private MutableLiveData<Boolean> mMovementDetected = new MutableLiveData<>();
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 (mDeviceRepository == null) {
mDeviceRepository = DeviceRepository.getInstance();
}
if (mAccelerometerSensor == null) {
mAccelerometerSensor = AccelerometerSensor.getInstance();
}
if (mDeviceList == null) {
mDeviceRepository = DeviceRepository.getInstance();
mDeviceList = mDeviceRepository.getConnectedDeviceList();
}
if (mAlarmHistoryList == null) {
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList();
}
if (mStartAlarmRecording == null) {
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording();
}
if (mAccelerometerAlarmDetected == null) {
mAccelerometerAlarmDetected = mAccelerometerSensor.getAccelerometerAlarmDetected();
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);
}
}

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

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

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

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

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

@Override

Loading…
Cancel
Save