Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e1b822aa13 |
@@ -4,6 +4,9 @@
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.CAMERA"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
||||
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
|
||||
|
||||
|
||||
<uses-feature android:name="android.hardware.camera"/>
|
||||
|
||||
|
||||
@@ -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
|
||||
public void onChanged(Boolean aBoolean) {
|
||||
if (aBoolean) {
|
||||
|
||||
@@ -1,16 +1,23 @@
|
||||
package com.example.greenwatch;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
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.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.Toast;
|
||||
import android.Manifest;
|
||||
|
||||
import com.example.greenwatch.adapters.AlarmHistoryListAdapter;
|
||||
import com.example.greenwatch.adapters.DeviceListAdapter;
|
||||
@@ -29,6 +36,8 @@ public class MainActivity extends AppCompatActivity {
|
||||
private Button connectionButton;
|
||||
private MainActivityViewModel mMainActivityViewModel;
|
||||
|
||||
private static final int REQUEST_PERMISSION_INTERNET = 123;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@@ -127,7 +136,14 @@ public class MainActivity extends AppCompatActivity {
|
||||
connectionButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
openConnectionActivity();
|
||||
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED
|
||||
|| ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED
|
||||
|| ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CHANGE_WIFI_MULTICAST_STATE) != PackageManager.PERMISSION_GRANTED) {
|
||||
|
||||
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.INTERNET, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.CHANGE_WIFI_MULTICAST_STATE}, REQUEST_PERMISSION_INTERNET);
|
||||
} else {
|
||||
openConnectionActivity();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -157,4 +173,20 @@ public class MainActivity extends AppCompatActivity {
|
||||
mMainActivityViewModel.updateDevice(mMainActivityViewModel.getLocalDeviceUUID(), "10:51", true, "Audio", 10);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
if (requestCode == REQUEST_PERMISSION_INTERNET) {
|
||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
// Berechtigungen wurden gewährt
|
||||
Toast.makeText(this, "Berechtigungen gewährt", Toast.LENGTH_SHORT).show();
|
||||
openConnectionActivity();
|
||||
} else {
|
||||
// Berechtigungen wurden nicht gewährt
|
||||
Toast.makeText(this, "Berechtigungen nicht gewährt", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.example.greenwatch.communication;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.example.greenwatch.repositories.DeviceRepository;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -58,6 +56,23 @@ public class WiFiCommunication {
|
||||
this.isNewMessage = isNewMessage;
|
||||
}
|
||||
|
||||
public static String getLocalIpAddress() {
|
||||
try {
|
||||
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
|
||||
NetworkInterface networkInterface = (NetworkInterface) ((Enumeration<?>) en).nextElement();
|
||||
for (Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); addresses.hasMoreElements();) {
|
||||
InetAddress inetAddress = addresses.nextElement();
|
||||
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
|
||||
return inetAddress.getHostAddress();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SocketException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void stopCommunication() {
|
||||
running = false;
|
||||
socket.close();
|
||||
@@ -74,7 +89,7 @@ public class WiFiCommunication {
|
||||
{
|
||||
isNewMessage = false;
|
||||
//todo: adapt send String
|
||||
String txString = sendMessage;
|
||||
String txString = getLocalIpAddress() + " sends. #" + tmpCnt++ + sendMessage;
|
||||
byte[] txBuffer = txString.getBytes();
|
||||
|
||||
DatagramPacket txPacket = new DatagramPacket(txBuffer, txBuffer.length, address, port);
|
||||
|
||||
@@ -1,21 +1,14 @@
|
||||
package com.example.greenwatch.repositories;
|
||||
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import com.example.greenwatch.models.Device;
|
||||
import com.example.greenwatch.communication.WiFiCommunication;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
@@ -83,23 +76,21 @@ public class DeviceRepository {
|
||||
|
||||
public void getNewReceivedMessage(String newMessage) {
|
||||
String[] messageString = messageStringSplitter(newMessage);
|
||||
if(messageString[0]=="1" && messageString.length == 7){
|
||||
String timeStamp = messageString[1];
|
||||
String deviceID = messageString[3];
|
||||
boolean sensorStatus = convertSensorStatus(messageString[4]);
|
||||
String sensorType = messageString[5];
|
||||
int sensorMassage = Integer.valueOf(messageString[6]);
|
||||
String timeStamp = messageString[0];
|
||||
String deviceID = messageString[1];
|
||||
boolean sensorStatus = convertSensorStatus(messageString[2]);
|
||||
String sensorType = messageString[3];
|
||||
int sensorMassage = Integer.valueOf(messageString[4]);
|
||||
|
||||
if (deviceID.equals(checkDeviceID(localDeviceUUID))) {
|
||||
return;
|
||||
}
|
||||
if (deviceID.equals(checkDeviceID(localDeviceUUID))) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(!connectedDevicesList.containsKey(deviceID)) {
|
||||
createNewDevice(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage);
|
||||
}
|
||||
else {
|
||||
updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);
|
||||
}
|
||||
if(!connectedDevicesList.containsKey(deviceID)) {
|
||||
createNewDevice(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage);
|
||||
}
|
||||
else {
|
||||
updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,14 +98,9 @@ public class DeviceRepository {
|
||||
StringBuilder message = new StringBuilder();
|
||||
Device device = connectedDevicesList.get(deviceID);
|
||||
if(device != null) {
|
||||
message.append("1")
|
||||
message.append(device.getTimeStamp())
|
||||
.append(delimiter)
|
||||
.append(device.getTimeStamp())
|
||||
.append(delimiter)
|
||||
.append("Gruppe3")
|
||||
.append(delimiter)
|
||||
//.append(device.getDeviceID())
|
||||
.append(getLocalIpAddress(deviceID))
|
||||
.append(device.getDeviceID())
|
||||
.append(delimiter)
|
||||
.append(device.getSensorStatus())
|
||||
.append(delimiter)
|
||||
@@ -131,10 +117,6 @@ public class DeviceRepository {
|
||||
.append(delimiter)
|
||||
.append("")
|
||||
.append(delimiter)
|
||||
.append("")
|
||||
.append(delimiter)
|
||||
.append("")
|
||||
.append(delimiter)
|
||||
.append("");
|
||||
}
|
||||
return message.toString();
|
||||
@@ -337,25 +319,4 @@ public class DeviceRepository {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public String getLocalIpAddress(String deviceID) {
|
||||
String checkedDeviceID = checkDeviceID(deviceID);
|
||||
Device device = connectedDevicesList.get(checkedDeviceID);
|
||||
if(device != null){
|
||||
try {
|
||||
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
|
||||
NetworkInterface networkInterface = (NetworkInterface) ((Enumeration<?>) en).nextElement();
|
||||
for (Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); addresses.hasMoreElements();) {
|
||||
InetAddress inetAddress = addresses.nextElement();
|
||||
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
|
||||
return inetAddress.getHostAddress();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SocketException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,86 +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();
|
||||
}
|
||||
if (mStartAlarmRecording == null) {
|
||||
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording();
|
||||
}
|
||||
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);
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user