Compare commits
3 Commits
8dd4c6d56a
...
e3df8b63b8
Author | SHA1 | Date | |
---|---|---|---|
e3df8b63b8 | |||
6c4b6b67a4 | |||
1094b0f75d |
@ -70,7 +70,6 @@ public class AccelerometerActivity extends AppCompatActivity implements SensorEv
|
||||
@Override
|
||||
public void onChanged(List<Device> devices) {
|
||||
deviceListAdapter.setDevices(devices);
|
||||
Toast.makeText(AccelerometerActivity.this, "onChanged", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
|
||||
@ -81,7 +80,19 @@ public class AccelerometerActivity extends AppCompatActivity implements SensorEv
|
||||
}
|
||||
});
|
||||
|
||||
mAccelerometerViewModel.getMovementDetectedValue().observe(this, new Observer<Boolean>() {
|
||||
mAccelerometerViewModel.getStartAlarmRecording().observe(this, new Observer<Boolean>() {
|
||||
@Override
|
||||
public void onChanged(Boolean aBoolean) {
|
||||
if (aBoolean) {
|
||||
Toast.makeText(AccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
else {
|
||||
Toast.makeText(AccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
mAccelerometerViewModel.getAccelerometerAlarmDetected().observe(this, new Observer<Boolean>() {
|
||||
@Override
|
||||
public void onChanged(Boolean aBoolean) {
|
||||
if (aBoolean) {
|
||||
|
@ -62,7 +62,6 @@ public class MainActivity extends AppCompatActivity {
|
||||
@Override
|
||||
public void onChanged(List<Device> devices) {
|
||||
deviceListAdapter.setDevices(devices);
|
||||
Toast.makeText(MainActivity.this, "onChanged", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
|
||||
@ -73,6 +72,18 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
});
|
||||
|
||||
mMainActivityViewModel.getStartAlarmRecording().observe(this, new Observer<Boolean>() {
|
||||
@Override
|
||||
public void onChanged(Boolean aBoolean) {
|
||||
if (aBoolean) {
|
||||
Toast.makeText(MainActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
else {
|
||||
Toast.makeText(MainActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
audiodetectionButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
@ -89,7 +100,6 @@ public class MainActivity extends AppCompatActivity {
|
||||
else {
|
||||
mMainActivityViewModel.accessRequestCamera(MainActivity.this);
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
accelerometerButton.setOnClickListener(new View.OnClickListener() {
|
||||
@ -138,9 +148,8 @@ public class MainActivity extends AppCompatActivity {
|
||||
startActivity(intent);
|
||||
}
|
||||
public void openVideodetectionAndAccelerometerActivity(){
|
||||
//Intent intent = new Intent(this, VideodetectionAndAccelerometerActivity.class);
|
||||
//startActivity(intent);
|
||||
mMainActivityViewModel.updateDevice(mMainActivityViewModel.getLocalDeviceUUID(), "24:00", false, "Video", 0);
|
||||
Intent intent = new Intent(this, VideodetectionAndAccelerometerActivity.class);
|
||||
startActivity(intent);
|
||||
}
|
||||
public void openConnectionActivity(){
|
||||
//Intent intent = new Intent(this, ConnectionActivity.class);
|
||||
|
@ -62,7 +62,6 @@ public class VideodetectionActivity extends AppCompatActivity {
|
||||
@Override
|
||||
public void onChanged(List<Device> devices) {
|
||||
deviceListAdapter.setDevices(devices);
|
||||
Toast.makeText(VideodetectionActivity.this, "onChanged", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
|
||||
@ -73,6 +72,18 @@ public class VideodetectionActivity extends AppCompatActivity {
|
||||
}
|
||||
});
|
||||
|
||||
mVideoDetectionViewModel.getStartAlarmRecording().observe(this, new Observer<Boolean>() {
|
||||
@Override
|
||||
public void onChanged(Boolean aBoolean) {
|
||||
if (aBoolean) {
|
||||
Toast.makeText(VideodetectionActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
else {
|
||||
Toast.makeText(VideodetectionActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
mVideoDetectionViewModel.getVideoAlarmDetectedValue().observe(this, new Observer<Boolean>() {
|
||||
@Override
|
||||
public void onChanged(Boolean aBoolean) {
|
||||
|
@ -1,27 +1,171 @@
|
||||
package com.example.greenwatch;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.camera.lifecycle.ProcessCameraProvider;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.hardware.SensorManager;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class VideodetectionAndAccelerometerActivity extends AppCompatActivity {
|
||||
import com.example.greenwatch.adapters.AlarmHistoryListAdapter;
|
||||
import com.example.greenwatch.adapters.DeviceListAdapter;
|
||||
import com.example.greenwatch.models.Device;
|
||||
import com.example.greenwatch.viewmodels.AccelerometerViewModel;
|
||||
import com.example.greenwatch.viewmodels.VideodetectionAndAccelerometerViewModel;
|
||||
import com.example.greenwatch.viewmodels.VideodetectionViewModel;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class VideodetectionAndAccelerometerActivity extends AppCompatActivity implements SensorEventListener {
|
||||
private SensorManager accelerometerManager;
|
||||
private Sensor accelerometerSensor;
|
||||
private TextView videodetectionAndAccelerometerStatusMessage;
|
||||
private TextView videodetectionAndAccelerometerDataTV;
|
||||
private TextView videodetectionAndAccelerometerWarningTV;
|
||||
private Button backToMainActivity;
|
||||
private VideodetectionAndAccelerometerViewModel mVideodetectionAndAccelerometerViewModel;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_videodetection_and_accelerometer);
|
||||
|
||||
videodetectionAndAccelerometerStatusMessage = (TextView) findViewById(R.id.tvvideodetectionAndAccelerometerStatusmessage);
|
||||
videodetectionAndAccelerometerDataTV = (TextView) findViewById(R.id.tvvideodetectionAndAccelerometerData);
|
||||
videodetectionAndAccelerometerWarningTV = (TextView) findViewById(R.id.tvvideodetectionAndAccelerometerWarning);
|
||||
backToMainActivity = (Button) findViewById(R.id.videodetectionAndAccelerometerBackToMainActivity);
|
||||
|
||||
RecyclerView recyclerView = findViewById(R.id.deviceListRecyclerView);
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||
recyclerView.setHasFixedSize(true);
|
||||
|
||||
final DeviceListAdapter deviceListAdapter = new DeviceListAdapter();
|
||||
recyclerView.setAdapter(deviceListAdapter);
|
||||
|
||||
RecyclerView alarmHistoryListRecyclerView = findViewById(R.id.alarmHistoryListRecyclerView);
|
||||
alarmHistoryListRecyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||
alarmHistoryListRecyclerView.setHasFixedSize(true);
|
||||
|
||||
final AlarmHistoryListAdapter alarmHistoryListAdapter = new AlarmHistoryListAdapter();
|
||||
alarmHistoryListRecyclerView.setAdapter(alarmHistoryListAdapter);
|
||||
|
||||
backToMainActivity.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
|
||||
mVideodetectionAndAccelerometerViewModel = new ViewModelProvider(this).get(VideodetectionAndAccelerometerViewModel.class);
|
||||
mVideodetectionAndAccelerometerViewModel.init();
|
||||
mVideodetectionAndAccelerometerViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() {
|
||||
@Override
|
||||
public void onChanged(List<Device> devices) {
|
||||
deviceListAdapter.setDevices(devices);
|
||||
}
|
||||
});
|
||||
|
||||
mVideodetectionAndAccelerometerViewModel.getAlarmHistoryList().observe(this, new Observer<List<Device>>() {
|
||||
@Override
|
||||
public void onChanged(List<Device> devices) {
|
||||
alarmHistoryListAdapter.setAlarmHistoryList(devices);
|
||||
}
|
||||
});
|
||||
|
||||
mVideodetectionAndAccelerometerViewModel.getStartAlarmRecording().observe(this, new Observer<Boolean>() {
|
||||
@Override
|
||||
public void onChanged(Boolean aBoolean) {
|
||||
if (aBoolean) {
|
||||
Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
else {
|
||||
Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
mVideodetectionAndAccelerometerViewModel.getVideodetectionAndAccelerometerAlarmDetected().observe(this, new Observer<Boolean>() {
|
||||
@Override
|
||||
public void onChanged(Boolean aBoolean) {
|
||||
if (aBoolean) {
|
||||
mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "Accelerometer", 10);
|
||||
}
|
||||
else {
|
||||
mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), false, "Accelerometer", 0);
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
|
||||
cameraProviderFuture.addListener(() -> {
|
||||
try {
|
||||
mVideodetectionAndAccelerometerViewModel.bindImageAnalysis(cameraProviderFuture.get(), this, this);
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, ContextCompat.getMainExecutor(this));
|
||||
|
||||
accelerometerManager = (SensorManager) getSystemService(SENSOR_SERVICE);
|
||||
if (accelerometerManager.getSensorList(Sensor.TYPE_GYROSCOPE).size() == 0) {
|
||||
accelerometerSensor = null;
|
||||
Toast.makeText(VideodetectionAndAccelerometerActivity.this, "No accelerometer sensor available", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
else {
|
||||
accelerometerSensor = accelerometerManager.getSensorList(Sensor.TYPE_GYROSCOPE).get(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("x=")
|
||||
.append(event.values[0])
|
||||
.append("\ny=")
|
||||
.append(event.values[1])
|
||||
.append("\nz=")
|
||||
.append(event.values[2]);
|
||||
videodetectionAndAccelerometerDataTV.setText(sb.toString());
|
||||
mVideodetectionAndAccelerometerViewModel.addValueToGesamtBE(event.values[0] + event.values[1] + event.values[2]);
|
||||
mVideodetectionAndAccelerometerViewModel.meanValueCalculation();
|
||||
mVideodetectionAndAccelerometerViewModel.calibrateAccelerometerSensor();
|
||||
mVideodetectionAndAccelerometerViewModel.checkAlarmCondition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
|
||||
}
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
if (accelerometerSensor != null) {
|
||||
if (accelerometerManager.registerListener(this, accelerometerSensor, SensorManager.SENSOR_DELAY_GAME)) {
|
||||
Toast.makeText(VideodetectionAndAccelerometerActivity.this, "We registered to the sensor", Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Registration did not work", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
if (accelerometerSensor != null) {
|
||||
accelerometerManager.unregisterListener(this, accelerometerSensor);
|
||||
}
|
||||
}
|
||||
}
|
@ -17,10 +17,12 @@ public class DeviceRepository {
|
||||
|
||||
private final int maxAlarmHistoryListSize = 20;
|
||||
private final String delimiter = ", ";
|
||||
private final String SensorStatusKey = "An";
|
||||
private final String sensorStatusKey = "An";
|
||||
private String localDeviceUUID;
|
||||
private static DeviceRepository deviceRepositoryInstance;
|
||||
private WiFiCommunication mWiFiCommunication;
|
||||
private boolean startAlarmRecordingValue;
|
||||
private MutableLiveData<Boolean> startAlarmRecording = new MutableLiveData<>();
|
||||
private MutableLiveData<List<Device>> deviceList = new MutableLiveData<>();
|
||||
private MutableLiveData<List<Device>> alarmHistoryList = new MutableLiveData<>();
|
||||
private HashMap<String, Device> connectedDevicesList = new HashMap<>();
|
||||
@ -29,6 +31,7 @@ public class DeviceRepository {
|
||||
|
||||
private DeviceRepository() {
|
||||
setLocalDeviceUUID();
|
||||
startAlarmRecordingValue = false;
|
||||
}
|
||||
|
||||
public static synchronized DeviceRepository getInstance() {
|
||||
@ -52,11 +55,21 @@ public class DeviceRepository {
|
||||
return alarmHistoryList;
|
||||
}
|
||||
|
||||
public MutableLiveData<Boolean> getStartAlarmRecording() {
|
||||
setMutableLiveDataStartAlarmRecording();
|
||||
return startAlarmRecording;
|
||||
}
|
||||
|
||||
public void createNewDevice(String timeStamp, String deviceID, boolean sensorStatus, String sensorType, int sensorMassage){
|
||||
Device newDevice = new Device(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage);
|
||||
if (sensorStatus) {
|
||||
setAlarmHistoryDeviceList(newDevice);
|
||||
}
|
||||
boolean newStartAlarmRecordingValue = checkDeviceStatus();
|
||||
if (startAlarmRecordingValue != newStartAlarmRecordingValue) {
|
||||
startAlarmRecordingValue = newStartAlarmRecordingValue;
|
||||
setMutableLiveDataStartAlarmRecording();
|
||||
}
|
||||
addToConnectedDeviceList(newDevice.getDeviceID(), newDevice);
|
||||
setMutableLiveDataDeviceList();
|
||||
}
|
||||
@ -124,6 +137,11 @@ public class DeviceRepository {
|
||||
setAlarmHistoryDeviceList(device);
|
||||
}
|
||||
device.setSensorStatus(sensorStatus);
|
||||
boolean newStartAlarmRecordingValue = checkDeviceStatus();
|
||||
if (startAlarmRecordingValue != newStartAlarmRecordingValue) {
|
||||
startAlarmRecordingValue = newStartAlarmRecordingValue;
|
||||
setMutableLiveDataStartAlarmRecording();
|
||||
}
|
||||
addToConnectedDeviceList(checkedDeviceID, device);
|
||||
setMutableLiveDataDeviceList();
|
||||
}
|
||||
@ -177,6 +195,11 @@ public class DeviceRepository {
|
||||
setAlarmHistoryDeviceList(device);
|
||||
}
|
||||
device.setSensorStatus(sensorStatus);
|
||||
boolean newStartAlarmRecordingValue = checkDeviceStatus();
|
||||
if (startAlarmRecordingValue != newStartAlarmRecordingValue) {
|
||||
startAlarmRecordingValue = newStartAlarmRecordingValue;
|
||||
setMutableLiveDataStartAlarmRecording();
|
||||
}
|
||||
addToConnectedDeviceList(checkedDeviceID, device);
|
||||
setMutableLiveDataDeviceList();
|
||||
|
||||
@ -245,6 +268,10 @@ public class DeviceRepository {
|
||||
alarmHistoryList.setValue(alarmHistoryDeviceList);
|
||||
}
|
||||
|
||||
private void setMutableLiveDataStartAlarmRecording() {
|
||||
startAlarmRecording.setValue(startAlarmRecordingValue);
|
||||
}
|
||||
|
||||
private void addToConnectedDeviceList(String key, Device device) {
|
||||
connectedDevicesList.put(key, device);
|
||||
if (key.equals(checkDeviceID(localDeviceUUID))) {
|
||||
@ -272,7 +299,7 @@ public class DeviceRepository {
|
||||
}
|
||||
|
||||
private boolean convertSensorStatus(String status) {
|
||||
return status.equals(SensorStatusKey);
|
||||
return status.equals(sensorStatusKey);
|
||||
}
|
||||
|
||||
private void setAlarmHistoryDeviceList(Device device) {
|
||||
@ -283,4 +310,13 @@ public class DeviceRepository {
|
||||
alarmHistoryDeviceList.add(0, alarmHistoryDevice);
|
||||
setMutableLiveDataAlarmHistoryList();
|
||||
}
|
||||
|
||||
private boolean checkDeviceStatus() {
|
||||
for (Device device : connectedDevicesList.values()) {
|
||||
if (device.getSensorStatus()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -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() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,84 +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();
|
||||
}
|
||||
initGesamtBE();
|
||||
mMovementDetected.setValue(false);
|
||||
functionCallCount = 0;
|
||||
meanValue = 0f;
|
||||
offsetValue = 0.1f;
|
||||
thresholdValue = 0.15f;
|
||||
startMeasuring = false;
|
||||
if (mStartAlarmRecording == null) {
|
||||
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording();
|
||||
}
|
||||
|
||||
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
|
||||
@ -96,6 +71,11 @@ public class AccelerometerViewModel extends ViewModel implements ViewModelInterf
|
||||
return mAlarmHistoryList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LiveData<Boolean> getStartAlarmRecording() {
|
||||
return mStartAlarmRecording;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) {
|
||||
mDeviceRepository.updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);
|
||||
|
@ -20,8 +20,9 @@ public class MainActivityViewModel extends ViewModel implements ViewModelInterfa
|
||||
|
||||
private MutableLiveData<List<Device>> mDeviceList;
|
||||
private MutableLiveData<List<Device>> mAlarmHistoryList;
|
||||
private MutableLiveData<Boolean> mStartAlarmRecording;
|
||||
private DeviceRepository mDeviceRepository;
|
||||
private static final int RECHTEANFORDERUNG_KAMERA = 10;
|
||||
private static final int RIGHTS_REQUEST_CAMERA = 10;
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
@ -37,6 +38,7 @@ public class MainActivityViewModel extends ViewModel implements ViewModelInterfa
|
||||
mDeviceRepository.createNewDevice(mDeviceRepository.getSystemTimeStamp(), mDeviceRepository.getLocalDeviceUUID(), false, "No Sensor selected", 0);
|
||||
mDeviceList = mDeviceRepository.getConnectedDeviceList();
|
||||
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList();
|
||||
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording();
|
||||
}
|
||||
|
||||
public boolean isCameraAccessAllowed(Context context) {
|
||||
@ -44,7 +46,7 @@ public class MainActivityViewModel extends ViewModel implements ViewModelInterfa
|
||||
}
|
||||
|
||||
public void accessRequestCamera(Activity activity) {
|
||||
ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.CAMERA}, RECHTEANFORDERUNG_KAMERA);
|
||||
ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.CAMERA}, RIGHTS_REQUEST_CAMERA);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -57,6 +59,11 @@ public class MainActivityViewModel extends ViewModel implements ViewModelInterfa
|
||||
return mAlarmHistoryList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LiveData<Boolean> getStartAlarmRecording() {
|
||||
return mStartAlarmRecording;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) {
|
||||
mDeviceRepository.updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);
|
||||
|
@ -0,0 +1,196 @@
|
||||
package com.example.greenwatch.viewmodels;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.camera.lifecycle.ProcessCameraProvider;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
import com.example.greenwatch.models.Device;
|
||||
import com.example.greenwatch.repositories.DeviceRepository;
|
||||
import com.example.greenwatch.sensors.AccelerometerSensor;
|
||||
import com.example.greenwatch.sensors.CameraSensor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class VideodetectionAndAccelerometerViewModel extends ViewModel implements ViewModelInterface{
|
||||
private MutableLiveData<List<Device>> mDeviceList;
|
||||
private MutableLiveData<Boolean> mAccelerometerAlarmDetected;
|
||||
private MutableLiveData<Boolean> mVideoAlarmDetected;
|
||||
private MutableLiveData<Boolean> mVideodetectionAndAccelerometerAlarmDetected = new MutableLiveData<>();
|
||||
private MutableLiveData<List<Device>> mAlarmHistoryList;
|
||||
private MutableLiveData<Boolean> mStartAlarmRecording;
|
||||
private AccelerometerSensor mAccelerometerSensor;
|
||||
private CameraSensor mCameraSensor;
|
||||
private DeviceRepository mDeviceRepository;
|
||||
private boolean VideodetectionAndAccelerometerAlarmDetected;
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
if (mDeviceRepository == null) {
|
||||
mDeviceRepository = DeviceRepository.getInstance();
|
||||
}
|
||||
if (mAccelerometerSensor == null) {
|
||||
mAccelerometerSensor = AccelerometerSensor.getInstance();
|
||||
}
|
||||
if (mCameraSensor == null) {
|
||||
mCameraSensor = CameraSensor.getInstance();
|
||||
}
|
||||
if (mDeviceList == null) {
|
||||
mDeviceList = mDeviceRepository.getConnectedDeviceList();
|
||||
}
|
||||
if (mAlarmHistoryList == null) {
|
||||
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList();
|
||||
}
|
||||
if (mStartAlarmRecording == null) {
|
||||
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording();
|
||||
}
|
||||
if (mAccelerometerAlarmDetected == null) {
|
||||
mAccelerometerAlarmDetected = mAccelerometerSensor.getAccelerometerAlarmDetected();
|
||||
}
|
||||
if (mVideoAlarmDetected == null) {
|
||||
mVideoAlarmDetected = mCameraSensor.getVideoAlarmDetectedValue();
|
||||
}
|
||||
VideodetectionAndAccelerometerAlarmDetected = false;
|
||||
registerAlarmObserver();
|
||||
}
|
||||
|
||||
public void addValueToGesamtBE(float newValue) {
|
||||
mAccelerometerSensor.addValueToGesamtBE(newValue);
|
||||
}
|
||||
|
||||
public void meanValueCalculation() {
|
||||
mAccelerometerSensor.meanValueCalculation();
|
||||
}
|
||||
|
||||
public void calibrateAccelerometerSensor() {
|
||||
mAccelerometerSensor.calibrateAccelerometerSensor();
|
||||
}
|
||||
|
||||
public void checkAlarmCondition() {
|
||||
mAccelerometerSensor.checkAlarmCondition();
|
||||
}
|
||||
|
||||
public void bindImageAnalysis(ProcessCameraProvider cameraProvider, LifecycleOwner lifecycleOwner, Context context) {
|
||||
mCameraSensor.bindImageAnalysis(cameraProvider, lifecycleOwner, context);
|
||||
}
|
||||
|
||||
public LiveData<Boolean> getVideodetectionAndAccelerometerAlarmDetected() {
|
||||
setMutableLiveDataVideodetectionAndAccelerometerAlarmDetected();
|
||||
return mVideodetectionAndAccelerometerAlarmDetected;
|
||||
}
|
||||
|
||||
private void setMutableLiveDataVideodetectionAndAccelerometerAlarmDetected() {
|
||||
mVideodetectionAndAccelerometerAlarmDetected.setValue(VideodetectionAndAccelerometerAlarmDetected);
|
||||
}
|
||||
|
||||
Observer<Boolean> observer = new Observer<Boolean>() {
|
||||
@Override
|
||||
public void onChanged(Boolean aBoolean) {
|
||||
if (mVideoAlarmDetected.getValue() || mAccelerometerAlarmDetected.getValue() && !VideodetectionAndAccelerometerAlarmDetected) {
|
||||
VideodetectionAndAccelerometerAlarmDetected = true;
|
||||
setMutableLiveDataVideodetectionAndAccelerometerAlarmDetected();
|
||||
|
||||
}
|
||||
else if (!mVideoAlarmDetected.getValue() && !mAccelerometerAlarmDetected.getValue() && VideodetectionAndAccelerometerAlarmDetected) {
|
||||
VideodetectionAndAccelerometerAlarmDetected = false;
|
||||
setMutableLiveDataVideodetectionAndAccelerometerAlarmDetected();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private void registerAlarmObserver() {
|
||||
mVideoAlarmDetected.observeForever(observer);
|
||||
mAccelerometerAlarmDetected.observeForever(observer);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCleared() {
|
||||
super.onCleared();
|
||||
mVideoAlarmDetected.removeObserver(observer);
|
||||
mAccelerometerAlarmDetected.removeObserver(observer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LiveData<List<Device>> getConnectedDeviceList() {
|
||||
return mDeviceList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LiveData<List<Device>> getAlarmHistoryList() {
|
||||
return mAlarmHistoryList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LiveData<Boolean> getStartAlarmRecording() {
|
||||
return mStartAlarmRecording;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) {
|
||||
mDeviceRepository.updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTimeStamp(String deviceID, String timeStamp) {
|
||||
mDeviceRepository.setTimeStamp(deviceID, timeStamp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTimeStamp(String deviceID) {
|
||||
return mDeviceRepository.getTimeStamp(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDeviceID(String deviceID, String newDeviceID) {
|
||||
mDeviceRepository.setDeviceID(deviceID, newDeviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDeviceID(String deviceID) {
|
||||
return mDeviceRepository.getDeviceID(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSensorStatus(String deviceID, boolean sensorStatus) {
|
||||
mDeviceRepository.setSensorStatus(deviceID, sensorStatus);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getSensorStatus(String deviceID) {
|
||||
return mDeviceRepository.getSensorStatus(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSensorType(String deviceID, String sensorType) {
|
||||
mDeviceRepository.setSensorType(deviceID, sensorType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSensorType(String deviceID) {
|
||||
return mDeviceRepository.getSensorType(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSensorMassage(String deviceID, int sensorMessage) {
|
||||
mDeviceRepository.setSensorMassage(deviceID, sensorMessage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSensorMassage(String deviceID) {
|
||||
return mDeviceRepository.getSensorMassage(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalDeviceUUID() {
|
||||
return mDeviceRepository.getLocalDeviceUUID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSystemTimeStamp() {
|
||||
return mDeviceRepository.getSystemTimeStamp();
|
||||
}
|
||||
}
|
@ -19,6 +19,7 @@ public class VideodetectionViewModel extends ViewModel implements ViewModelInter
|
||||
private MutableLiveData<List<Device>> mDeviceList;
|
||||
private MutableLiveData<Boolean> mVideoAlarmDetected;
|
||||
private MutableLiveData<List<Device>> mAlarmHistoryList;
|
||||
private MutableLiveData<Boolean> mStartAlarmRecording;
|
||||
private DeviceRepository mDeviceRepository;
|
||||
private CameraSensor mCameraSensor;
|
||||
|
||||
@ -41,6 +42,9 @@ public class VideodetectionViewModel extends ViewModel implements ViewModelInter
|
||||
if (mAlarmHistoryList == null) {
|
||||
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList();
|
||||
}
|
||||
if (mStartAlarmRecording == null) {
|
||||
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording();
|
||||
}
|
||||
if (mVideoAlarmDetected == null) {
|
||||
mVideoAlarmDetected = mCameraSensor.getVideoAlarmDetectedValue();
|
||||
}
|
||||
@ -64,6 +68,11 @@ public class VideodetectionViewModel extends ViewModel implements ViewModelInter
|
||||
return mAlarmHistoryList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LiveData<Boolean> getStartAlarmRecording() {
|
||||
return mStartAlarmRecording;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) {
|
||||
mDeviceRepository.updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);
|
||||
|
@ -10,6 +10,7 @@ public interface ViewModelInterface {
|
||||
void init();
|
||||
LiveData<List<Device>> getConnectedDeviceList();
|
||||
LiveData<List<Device>> getAlarmHistoryList();
|
||||
LiveData<Boolean> getStartAlarmRecording();
|
||||
void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage);
|
||||
void setTimeStamp(String deviceID, String timeStamp);
|
||||
String getTimeStamp(String deviceID);
|
||||
|
@ -9,10 +9,24 @@
|
||||
tools:context=".VideodetectionAndAccelerometerActivity">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvVideodetectionAndAccelerometerStatusmessage"
|
||||
android:id="@+id/tvvideodetectionAndAccelerometerStatusmessage"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Videodetection + Accelerometer Activity">
|
||||
android:text="">
|
||||
</TextView>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvvideodetectionAndAccelerometerData"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="">
|
||||
</TextView>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvvideodetectionAndAccelerometerWarning"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="">
|
||||
</TextView>
|
||||
|
||||
<Button
|
||||
@ -22,4 +36,27 @@
|
||||
android:text="Back to MainActivity">
|
||||
</Button>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/deviceListRecyclerView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:listitem="@layout/device_item"
|
||||
android:layout_weight="1">
|
||||
</androidx.recyclerview.widget.RecyclerView>
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/alarmHistoryListRecyclerView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:listitem="@layout/alarm_history_item"
|
||||
android:layout_weight="1">
|
||||
</androidx.recyclerview.widget.RecyclerView>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
Loading…
x
Reference in New Issue
Block a user