|
|
|
|
|
|
|
|
|
|
|
|
|
|
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> mAccelerometerAlarmDetected; |
|
|
|
|
|
|
|
|
private MutableLiveData<Boolean> mMovementDetected = new MutableLiveData<>(); |
|
|
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 (mDeviceRepository == null) { |
|
|
|
|
|
mDeviceRepository = DeviceRepository.getInstance(); |
|
|
|
|
|
} |
|
|
|
|
|
if (mAccelerometerSensor == null) { |
|
|
|
|
|
mAccelerometerSensor = AccelerometerSensor.getInstance(); |
|
|
|
|
|
} |
|
|
|
|
|
if (mDeviceList == null) { |
|
|
if (mDeviceList == null) { |
|
|
|
|
|
mDeviceRepository = DeviceRepository.getInstance(); |
|
|
mDeviceList = mDeviceRepository.getConnectedDeviceList(); |
|
|
mDeviceList = mDeviceRepository.getConnectedDeviceList(); |
|
|
} |
|
|
|
|
|
if (mAlarmHistoryList == null) { |
|
|
|
|
|
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList(); |
|
|
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList(); |
|
|
} |
|
|
|
|
|
if (mStartAlarmRecording == null) { |
|
|
|
|
|
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording(); |
|
|
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) { |
|
|
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() { |
|
|
public void meanValueCalculation() { |
|
|
mAccelerometerSensor.meanValueCalculation(); |
|
|
|
|
|
|
|
|
for (float element : Gesamt_be) { |
|
|
|
|
|
meanValue += Math.abs(element); |
|
|
|
|
|
} |
|
|
|
|
|
meanValue = meanValue/arraySize; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void calibrateAccelerometerSensor() { |
|
|
public void calibrateAccelerometerSensor() { |
|
|
mAccelerometerSensor.calibrateAccelerometerSensor(); |
|
|
|
|
|
|
|
|
if (functionCallCount <= arraySize) { |
|
|
|
|
|
offsetValue = meanValue; |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
startMeasuring = true; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void checkAlarmCondition() { |
|
|
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 |
|
|
@Override |