Version 1. working

This commit is contained in:
Miguel Siebenhaar 2023-06-21 15:06:57 +02:00
parent d17f455ae1
commit 8e002107b5
3 changed files with 51 additions and 13 deletions

View File

@ -3,6 +3,7 @@ package com.example.ueberwachungssystem.Detection;
import android.content.Intent; import android.content.Intent;
import android.os.Binder; import android.os.Binder;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log;
import android.widget.ImageView; import android.widget.ImageView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -10,6 +11,8 @@ import androidx.annotation.Nullable;
import androidx.camera.core.ExperimentalGetImage; import androidx.camera.core.ExperimentalGetImage;
import androidx.lifecycle.LifecycleService; import androidx.lifecycle.LifecycleService;
import com.example.ueberwachungssystem.WifiCommunication;
import java.io.File; import java.io.File;
@ExperimentalGetImage @ExperimentalGetImage
@ -24,12 +27,34 @@ public class DetectorService extends LifecycleService {
public Accelerometer motionDetector = null; public Accelerometer motionDetector = null;
public MicrophoneDetector audioDetector = null; public MicrophoneDetector audioDetector = null;
public WifiCommunication wifiCommunication;
String wifiData;
StringBuffer stringBufferWifi = new StringBuffer();
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
if (isServiceRunning) if (isServiceRunning)
return START_NOT_STICKY; return START_NOT_STICKY;
/** Wifi Instanz **/
wifiCommunication = new WifiCommunication(1234);
wifiCommunication.sendTrue("TEst");
wifiCommunication.setOnConnectionListener(new WifiCommunication.OnConnectionListener() {
@Override
public void onConnection(String data) {
Log.d("Listener", data);
wifiData = data;
stringToStringbuffer(data);
Log.d("buffer",stringBufferWifi.toString());
passToServiceListener(stringBufferWifi);
}
});
@ -38,7 +63,8 @@ public class DetectorService extends LifecycleService {
videoDetector.setOnDetectionListener(new Detector.OnDetectionListener() { videoDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
@Override @Override
public void onDetection(@NonNull DetectionReport detectionReport) { public void onDetection(@NonNull DetectionReport detectionReport) {
passToServiceListener(detectionReport); //passToServiceListener(detectionReport);
wifiCommunication.sendTrue(detectionReport.toMessage());
} }
}); });
/** Motion Detection**/ /** Motion Detection**/
@ -47,7 +73,8 @@ public class DetectorService extends LifecycleService {
motionDetector.setOnDetectionListener(new Detector.OnDetectionListener() { motionDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
@Override @Override
public void onDetection(@NonNull DetectionReport detectionReport) { public void onDetection(@NonNull DetectionReport detectionReport) {
passToServiceListener(detectionReport); //passToServiceListener(detectionReport);
wifiCommunication.sendTrue(detectionReport.toMessage());
} }
}); });
/** Audio Detection **/ /** Audio Detection **/
@ -55,7 +82,8 @@ public class DetectorService extends LifecycleService {
audioDetector.setOnDetectionListener(new Detector.OnDetectionListener() { audioDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
@Override @Override
public void onDetection(@NonNull DetectionReport detectionReport) { public void onDetection(@NonNull DetectionReport detectionReport) {
passToServiceListener(detectionReport); //passToServiceListener(detectionReport);
wifiCommunication.sendTrue(detectionReport.toMessage());
} }
}); });
/** Audio Recorder**/ /** Audio Recorder**/
@ -92,18 +120,24 @@ public class DetectorService extends LifecycleService {
/** Pass Detection Report to Service Detection Listener and trigger it */ /** Pass Detection Report to Service Detection Listener and trigger it */
public void passToServiceListener(DetectionReport detectionReport) { public void passToServiceListener(StringBuffer stringBuffer) {
if (listener != null) { if (listener != null) {
listener.onDetection(detectionReport); listener.onDetection(stringBuffer);
} }
} }
/** On Detection Listener - runs when violation is reported */ /** On Detection Listener - runs when violation is reported */
public interface OnDetectionListener { public interface OnDetectionListener {
void onDetection(@NonNull DetectionReport detectionReport); void onDetection(@NonNull StringBuffer stringBuffer);
} }
public void setOnDetectionListener(@NonNull DetectorService.OnDetectionListener listener) { public void setOnDetectionListener(@NonNull DetectorService.OnDetectionListener listener) {
this.listener = listener; this.listener = listener;
} }
public void stringToStringbuffer(String string){
if(string != null) {
stringBufferWifi.append(string).append("\n");
}
}
} }

View File

@ -103,8 +103,8 @@ public class MainActivity extends AppCompatActivity {
detectorService.setOnDetectionListener(new DetectorService.OnDetectionListener() { detectorService.setOnDetectionListener(new DetectorService.OnDetectionListener() {
@Override @Override
public void onDetection(@NonNull DetectionReport detectionReport) { public void onDetection(@NonNull StringBuffer stringBuffer) {
Log.d("onDetection", detectionReport.toMessage()); Log.d("onDetection", stringBuffer.toString());
} }
}); });
} }

View File

@ -1,5 +1,6 @@
package com.example.ueberwachungssystem; package com.example.ueberwachungssystem;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -51,15 +52,16 @@ public class WifiCommunication {
} }
public interface OnConnectionListener { public interface OnConnectionListener {
void onConnection(StringBuffer data); void onConnection(String data);
} }
public void setOnConnectionListener(@NonNull OnConnectionListener listener) { public void setOnConnectionListener(@NonNull OnConnectionListener listener) {
this.listener = listener; this.listener = listener;
} }
public void sendWifiData(StringBuffer wifiMessage) { public void sendWifiData(String wifiMessage) {
if (listener != null) { if (listener != null) {
listener.onConnection(wifiMessage); listener.onConnection(wifiMessage);
} }
} }
@ -78,8 +80,9 @@ public class WifiCommunication {
rxString = new String(receiveData, 0, rxPacket.getLength()); rxString = new String(receiveData, 0, rxPacket.getLength());
String[] splitrxString = rxString.split(","); String[] splitrxString = rxString.split(",");
if(!previousRxString.equals(rxString) && splitrxString[0].equals("1") && splitrxString.length==7) { if(!previousRxString.equals(rxString) && splitrxString[0].equals("1") && splitrxString.length==7) {
rxStringBuffer.append(rxString).append("\n"); //rxStringBuffer.append(rxString).append("\n");
sendWifiData(rxStringBuffer); Log.d("empfangen", rxString);
sendWifiData(rxString);
//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer)); //mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
previousRxString = rxString; previousRxString = rxString;
} }
@ -103,7 +106,8 @@ public class WifiCommunication {
Date curDate = new Date(System.currentTimeMillis()); Date curDate = new Date(System.currentTimeMillis());
String str = formatter.format(curDate); String str = formatter.format(curDate);
byte[] send_Data = new byte[512]; byte[] send_Data = new byte[512];
String txString = ("1," +str+ ",Gruppe2," + getLocalIpAddress() + ",An,Video," +messageToSend); String txString = (messageToSend); //"1," +str+ ",Gruppe2," + getLocalIpAddress() + ",An,Video,"
Log.d("send", txString);
send_Data = txString.getBytes(); send_Data = txString.getBytes();
DatagramPacket txPacket = new DatagramPacket(send_Data, txString.length(), address, port); DatagramPacket txPacket = new DatagramPacket(send_Data, txString.length(), address, port);