Compare commits

...

3 Commits

Author SHA1 Message Date
0423a56117 Senden der Geräte-IP-Adresse (in Device Repository)
Senden der zusammengesetzten Nachricht vom Message String Builder --> Teile der alten Testnachricht in WiFiCommunication entfernt
2023-06-20 10:42:04 +02:00
a13f4e295b Änderungen in:
messageStringBuilder: Hinzufügen der Nachricht-ID (immer 1) und der Gruppen-ID (immer Gruppe3)
getNewReceivedMessage: Zuweisung der neuen Array-Felder von messageString an die jeweiligen Variablen, Einbinden der if-Bedingung: nur die Funktion nach dem Message Split weiter ausführen, wenn erstes Array-Feld ="1" und Array-Länge=7 ist
WICHTIG: Datentyp von sensorMassage (Zeile 84) ist int --> zeigen wir empfangenen float Wert als Int an? Gleiches in getSensorMassage-Funktion (Z.257)
2023-06-19 23:23:27 +02:00
c57d277dfb Test 2023-06-19 22:01:35 +02:00
4 changed files with 106 additions and 55 deletions

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) {

View File

@ -1,5 +1,7 @@
package com.example.greenwatch.communication;
import android.util.Log;
import com.example.greenwatch.repositories.DeviceRepository;
import java.io.IOException;
@ -56,23 +58,6 @@ 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();
@ -89,7 +74,7 @@ public class WiFiCommunication {
{
isNewMessage = false;
//todo: adapt send String
String txString = getLocalIpAddress() + " sends. #" + tmpCnt++ + sendMessage;
String txString = sendMessage;
byte[] txBuffer = txString.getBytes();
DatagramPacket txPacket = new DatagramPacket(txBuffer, txBuffer.length, address, port);

View File

@ -1,14 +1,21 @@
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;
@ -76,21 +83,23 @@ public class DeviceRepository {
public void getNewReceivedMessage(String newMessage) {
String[] messageString = messageStringSplitter(newMessage);
String timeStamp = messageString[0];
String deviceID = messageString[1];
boolean sensorStatus = convertSensorStatus(messageString[2]);
String sensorType = messageString[3];
int sensorMassage = Integer.valueOf(messageString[4]);
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]);
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);
}
}
}
@ -98,9 +107,14 @@ public class DeviceRepository {
StringBuilder message = new StringBuilder();
Device device = connectedDevicesList.get(deviceID);
if(device != null) {
message.append(device.getTimeStamp())
message.append("1")
.append(delimiter)
.append(device.getDeviceID())
.append(device.getTimeStamp())
.append(delimiter)
.append("Gruppe3")
.append(delimiter)
//.append(device.getDeviceID())
.append(getLocalIpAddress(deviceID))
.append(delimiter)
.append(device.getSensorStatus())
.append(delimiter)
@ -117,6 +131,10 @@ public class DeviceRepository {
.append(delimiter)
.append("")
.append(delimiter)
.append("")
.append(delimiter)
.append("")
.append(delimiter)
.append("");
}
return message.toString();
@ -319,4 +337,25 @@ 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 "";
}
}

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