@@ -1,20 +1,42 @@ | |||
package com.example.greenwatch; | |||
import androidx.appcompat.app.AppCompatActivity; | |||
import androidx.lifecycle.Observer; | |||
import androidx.lifecycle.ViewModelProvider; | |||
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 AccelerometerActivity extends AppCompatActivity { | |||
import com.example.greenwatch.models.Device; | |||
import com.example.greenwatch.viewmodels.AccelerometerViewModel; | |||
import java.util.List; | |||
public class AccelerometerActivity extends AppCompatActivity implements SensorEventListener { | |||
private SensorManager accelerometerManager; | |||
private Sensor accelerometerSensor; | |||
private TextView accelerometerStatusMessage; | |||
private TextView accelerometerDataTV; | |||
private TextView accelerometerWarningTV; | |||
private Button backToMainActivityButton; | |||
private AccelerometerViewModel mAccelerometerViewModel; | |||
@Override | |||
protected void onCreate(Bundle savedInstanceState) { | |||
super.onCreate(savedInstanceState); | |||
setContentView(R.layout.activity_accelerometer); | |||
accelerometerStatusMessage = (TextView) findViewById(R.id.tvAccelerometerStatusmessage); | |||
accelerometerDataTV = (TextView) findViewById(R.id.tvAccelerometerData); | |||
accelerometerWarningTV = (TextView) findViewById(R.id.tvAccelerometerWarning); | |||
backToMainActivityButton = (Button) findViewById(R.id.accelerometerBackToMainActivity); | |||
backToMainActivityButton.setOnClickListener(new View.OnClickListener() { | |||
@@ -23,6 +45,93 @@ public class AccelerometerActivity extends AppCompatActivity { | |||
finish(); | |||
} | |||
}); | |||
mAccelerometerViewModel = new ViewModelProvider(this).get(AccelerometerViewModel.class); | |||
mAccelerometerViewModel.init(); | |||
mAccelerometerViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() { | |||
@Override | |||
public void onChanged(List<Device> devices) { | |||
StringBuilder sb = new StringBuilder(); | |||
sb.append("Time Stamp: "); | |||
sb.append(mAccelerometerViewModel.getTimeStamp(mAccelerometerViewModel.getLocalDeviceUUID())); | |||
sb.append(";\n"); | |||
sb.append("DeviceID: "); | |||
sb.append(mAccelerometerViewModel.getDeviceID(mAccelerometerViewModel.getLocalDeviceUUID())); | |||
sb.append(";\n"); | |||
sb.append("Sensor Status: "); | |||
sb.append(mAccelerometerViewModel.getSensorStatus(mAccelerometerViewModel.getLocalDeviceUUID())); | |||
sb.append(";\n"); | |||
sb.append("Sensor Type: "); | |||
sb.append(mAccelerometerViewModel.getSensorType(mAccelerometerViewModel.getLocalDeviceUUID())); | |||
sb.append(";\n"); | |||
sb.append("Sensor Massage: "); | |||
sb.append(mAccelerometerViewModel.getSensorMassage(mAccelerometerViewModel.getLocalDeviceUUID())); | |||
accelerometerStatusMessage.setText(sb); | |||
Toast.makeText(AccelerometerActivity.this, "onChanged", Toast.LENGTH_LONG).show(); | |||
} | |||
}); | |||
mAccelerometerViewModel.getMovementDetectedValue().observe(this, new Observer<Boolean>() { | |||
@Override | |||
public void onChanged(Boolean aBoolean) { | |||
if (aBoolean) { | |||
mAccelerometerViewModel.setDevice(mAccelerometerViewModel.getLocalDeviceUUID(), mAccelerometerViewModel.getSystemTimeStamp(), mAccelerometerViewModel.getLocalDeviceUUID(), true, "Accelerometer", 10); | |||
} | |||
else { | |||
mAccelerometerViewModel.setDevice(mAccelerometerViewModel.getLocalDeviceUUID(), mAccelerometerViewModel.getSystemTimeStamp(), mAccelerometerViewModel.getLocalDeviceUUID(), false, "Accelerometer", 0); | |||
} | |||
} | |||
}); | |||
accelerometerManager = (SensorManager) getSystemService(SENSOR_SERVICE); | |||
if (accelerometerManager.getSensorList(Sensor.TYPE_GYROSCOPE).size() == 0) { | |||
accelerometerSensor = null; | |||
Toast.makeText(AccelerometerActivity.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]); | |||
accelerometerDataTV.setText(sb.toString()); | |||
mAccelerometerViewModel.addValueToGesamtBE(event.values[0] + event.values[1] + event.values[2]); | |||
mAccelerometerViewModel.meanValueCalculation(); | |||
mAccelerometerViewModel.calibrateAccelerometerSensor(); | |||
mAccelerometerViewModel.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(AccelerometerActivity.this, "We registered to the sensor", Toast.LENGTH_LONG).show(); | |||
} else { | |||
Toast.makeText(AccelerometerActivity.this, "Registration did not work", Toast.LENGTH_LONG).show(); | |||
} | |||
} | |||
} | |||
@Override | |||
protected void onPause() { | |||
super.onPause(); | |||
if (accelerometerSensor != null) { | |||
accelerometerManager.unregisterListener(this, accelerometerSensor); | |||
} | |||
} | |||
} |
@@ -84,9 +84,8 @@ public class MainActivity extends AppCompatActivity { | |||
accelerometerButton.setOnClickListener(new View.OnClickListener() { | |||
@Override | |||
public void onClick(View v) { | |||
//openAccelerometerActivity(); | |||
openAccelerometerActivity(); | |||
mMainActivityViewModel.setSensorType(mMainActivityViewModel.getLocalDeviceUUID(), "Accelerometer"); | |||
mMainActivityViewModel.setTimeStamp(mMainActivityViewModel.getLocalDeviceUUID(), "24:00"); | |||
} | |||
}); | |||
audiodetectionAndAccelerometerButton.setOnClickListener(new View.OnClickListener() { |
@@ -0,0 +1,143 @@ | |||
package com.example.greenwatch.viewmodels; | |||
import androidx.lifecycle.LiveData; | |||
import androidx.lifecycle.MutableLiveData; | |||
import androidx.lifecycle.ViewModel; | |||
import com.example.greenwatch.models.Device; | |||
import com.example.greenwatch.repositories.DeviceRepository; | |||
import java.text.SimpleDateFormat; | |||
import java.util.ArrayList; | |||
import java.util.Date; | |||
import java.util.List; | |||
public class AccelerometerViewModel extends ViewModel { | |||
private MutableLiveData<List<Device>> mDeviceList; | |||
private MutableLiveData<Boolean> mMovementDetected = new MutableLiveData<>(); | |||
private DeviceRepository mDeviceRepository; | |||
private ArrayList<Float> Gesamt_be; | |||
int arraySize = 500; | |||
int functionCallCount; | |||
float meanValue; | |||
float offsetValue; | |||
float thresholdValue; | |||
boolean startMeasuring; | |||
public void init() { | |||
if (mDeviceList == null) { | |||
mDeviceRepository = DeviceRepository.getInstance(); | |||
mDeviceList = mDeviceRepository.getConnectedDeviceList(); | |||
} | |||
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) { | |||
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 && !mMovementDetected.getValue()) { | |||
mMovementDetected.setValue(true); | |||
} | |||
else if (meanValue < (thresholdValue + offsetValue) && mMovementDetected.getValue()){ | |||
mMovementDetected.setValue(false); | |||
} | |||
} | |||
public String getSystemTimeStamp() { | |||
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); | |||
Date date = new Date(System.currentTimeMillis()); | |||
return formatter.format(date); | |||
} | |||
public LiveData<Boolean> getMovementDetectedValue() { | |||
return mMovementDetected; | |||
} | |||
public LiveData<List<Device>> getConnectedDeviceList() { | |||
return mDeviceList; | |||
} | |||
public void setDevice(String deviceID, String timeStamp, String newDeviceID, boolean sensorStatus, String sensorType, int sensorMassage) { | |||
mDeviceRepository.setDevice(deviceID, timeStamp,newDeviceID, sensorStatus, sensorType, sensorMassage); | |||
} | |||
public void setTimeStamp(String deviceID, String timeStamp) { | |||
mDeviceRepository.setTimeStamp(deviceID, timeStamp); | |||
} | |||
public String getTimeStamp(String deviceID) { | |||
return mDeviceRepository.getTimeStamp(deviceID); | |||
} | |||
public void setDeviceID(String deviceID, String newDeviceID) { | |||
mDeviceRepository.setDeviceID(deviceID, newDeviceID); | |||
} | |||
public String getDeviceID(String deviceID) { | |||
return mDeviceRepository.getDeviceID(deviceID); | |||
} | |||
public void setSensorStatus(String deviceID, boolean sensorStatus) { | |||
mDeviceRepository.setSensorStatus(deviceID, sensorStatus); | |||
} | |||
public boolean getSensorStatus(String deviceID) { | |||
return mDeviceRepository.getSensorStatus(deviceID); | |||
} | |||
public void setSensorType(String deviceID, String sensorType) { | |||
mDeviceRepository.setSensorType(deviceID, sensorType); | |||
} | |||
public String getSensorType(String deviceID) { | |||
return mDeviceRepository.getSensorType(deviceID); | |||
} | |||
public void setSensorMassage(String deviceID, int sensorMessage) { | |||
mDeviceRepository.setSensorMassage(deviceID, sensorMessage); | |||
} | |||
public int getSensorMassage(String deviceID) { | |||
return mDeviceRepository.getSensorMassage(deviceID); | |||
} | |||
public String getLocalDeviceUUID() { | |||
return mDeviceRepository.getLocalDeviceUUID(); | |||
} | |||
} |
@@ -12,7 +12,6 @@ import java.util.List; | |||
public class MainActivityViewModel extends ViewModel { | |||
private MutableLiveData<List<Device>> mDeviceList; | |||
private MutableLiveData<Device> mLocalDevice; | |||
private DeviceRepository mDeviceRepository; | |||
public void init() { |
@@ -12,7 +12,21 @@ | |||
android:id="@+id/tvAccelerometerStatusmessage" | |||
android:layout_width="match_parent" | |||
android:layout_height="wrap_content" | |||
android:text="Accelerometer Activity"> | |||
android:text=""> | |||
</TextView> | |||
<TextView | |||
android:id="@+id/tvAccelerometerData" | |||
android:layout_width="match_parent" | |||
android:layout_height="wrap_content" | |||
android:text=""> | |||
</TextView> | |||
<TextView | |||
android:id="@+id/tvAccelerometerWarning" | |||
android:layout_width="match_parent" | |||
android:layout_height="wrap_content" | |||
android:text=""> | |||
</TextView> | |||
<Button |