From 5b042611a5148c8b4be050f2b723b4348cd8eba3 Mon Sep 17 00:00:00 2001 From: Miguel Siebenhaar Date: Wed, 21 Jun 2023 13:55:36 +0200 Subject: [PATCH] Kopie Master Version 1.2 UDP Socket merged with sensor --- .../Detection/DetectorService.java | 45 ++++++++++------ .../ueberwachungssystem/MainActivity.java | 53 ++++++++++++++++--- 2 files changed, 74 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectorService.java b/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectorService.java index ce657ad..dab5508 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectorService.java +++ b/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectorService.java @@ -31,6 +31,7 @@ public class DetectorService extends LifecycleService { WifiCommunication wifiCommunication; String dataFromWifi; + StringBuffer stringBufferFromWifi; @Override public int onStartCommand(Intent intent, int flags, int startId) { @@ -38,6 +39,17 @@ public class DetectorService extends LifecycleService { return START_NOT_STICKY; + /** Wifi Instanz Starten und Listener **/ + wifiCommunication = new WifiCommunication (1234); + wifiCommunication.setOnConnectionListener(new WifiCommunication.OnConnectionListener() { + @Override + public void onConnection(String data) { + dataFromWifi = data; + stringToStringbuffer(data); + } + + }); + /** Video Detection/Recorder **/ @@ -45,7 +57,8 @@ public class DetectorService extends LifecycleService { videoDetector.setOnDetectionListener(new Detector.OnDetectionListener() { @Override public void onDetection(@NonNull DetectionReport detectionReport) { - passToServiceListener(detectionReport); + //passToServiceListener(detectionReport); + wifiCommunication.sendTrue(detectionReport.toMessage()); } }); /** Motion Detection**/ @@ -54,7 +67,8 @@ public class DetectorService extends LifecycleService { motionDetector.setOnDetectionListener(new Detector.OnDetectionListener() { @Override public void onDetection(@NonNull DetectionReport detectionReport) { - passToServiceListener(detectionReport); + //passToServiceListener(detectionReport); + wifiCommunication.sendTrue(detectionReport.toMessage()); } }); /** Audio Detection **/ @@ -62,7 +76,8 @@ public class DetectorService extends LifecycleService { audioDetector.setOnDetectionListener(new Detector.OnDetectionListener() { @Override public void onDetection(@NonNull DetectionReport detectionReport) { - passToServiceListener(detectionReport); + //passToServiceListener(detectionReport); + wifiCommunication.sendTrue(detectionReport.toMessage()); } }); /** Audio Recorder**/ @@ -71,18 +86,7 @@ public class DetectorService extends LifecycleService { - - isServiceRunning = true; - - wifiCommunication = new WifiCommunication (1234); - wifiCommunication.setOnConnectionListener(new WifiCommunication.OnConnectionListener() { - @Override - public void onConnection(String data) { - dataFromWifi = data; - } - - }); return super.onStartCommand(intent, flags, startId); } @@ -108,18 +112,25 @@ public class DetectorService extends LifecycleService { /** Pass Detection Report to Service Detection Listener and trigger it */ - public void passToServiceListener(DetectionReport detectionReport) { + public void passToServiceListener(StringBuffer stringBuffer) { if (listener != null) { - listener.onDetection(detectionReport); + listener.onDetection(stringBuffer); } } /** On Detection Listener - runs when violation is reported */ public interface OnDetectionListener { - void onDetection(@NonNull DetectionReport detectionReport); + void onDetection(@NonNull StringBuffer stringBuffer); } public void setOnDetectionListener(@NonNull DetectorService.OnDetectionListener listener) { this.listener = listener; } + + /** string to stringbuffer **/ + public void stringToStringbuffer(String dataString){ + stringBufferFromWifi.append(dataString).append("\n"); + passToServiceListener(stringBufferFromWifi); + + } } diff --git a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java index 8167fc3..528725f 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java +++ b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java @@ -3,11 +3,16 @@ package com.example.ueberwachungssystem; import static android.content.ContentValues.TAG; import android.annotation.SuppressLint; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Bundle; +import android.os.IBinder; import android.util.Log; import android.view.MenuItem; import android.widget.Toast; @@ -18,13 +23,19 @@ import android.widget.Button; import android.widget.TextView; import android.widget.Toast; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; +import androidx.camera.core.ExperimentalGetImage; + +import com.example.ueberwachungssystem.Detection.DetectionReport; +import com.example.ueberwachungssystem.Detection.DetectorService; + import java.net.InetAddress; import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.nio.ByteOrder; -@SuppressLint("SetTextI18n") +@ExperimentalGetImage public class MainActivity extends AppCompatActivity { WifiCommunication communication; @@ -32,20 +43,47 @@ public class MainActivity extends AppCompatActivity { boolean communicationRunning = true; TextView textview1; + private DetectorService detectorService = new DetectorService(); + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - textview1 = findViewById(R.id.textView1); - Button button1 = findViewById(R.id.buttonSend); - button1.setOnClickListener(new View.OnClickListener() { + Intent serviceIntent = new Intent(this, DetectorService.class); + bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE); + startService(serviceIntent); + //textview1 = findViewById(R.id.textView1); + + //Button button1 = findViewById(R.id.buttonSend); + /*button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { communication.sendTrue("Testmessage"); } - }); + });*/ } + + private ServiceConnection serviceConnection = new ServiceConnection() { + @Override + public void onServiceConnected(ComponentName name, IBinder service) { + DetectorService.ServiceBinder binder = (DetectorService.ServiceBinder) service; + detectorService = binder.getBoundService(); + + detectorService.setOnDetectionListener(new DetectorService.OnDetectionListener() { + @Override + public void onDetection(StringBuffer stringBuffer) { + //textview für oli + Log.d("Debugger", stringBuffer.toString()); + } + }); + } + + @Override + public void onServiceDisconnected(ComponentName name) { + + } + }; @Override protected void onResume() { super.onResume(); @@ -78,7 +116,7 @@ public class MainActivity extends AppCompatActivity { getMenuInflater().inflate(R.menu.options_menu, menu); return true; } - public boolean onOptionsItemSelected(MenuItem item) { + /*public boolean onOptionsItemSelected(MenuItem item) { Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT).show(); PopUpClass popUpClass; switch (item.getItemId()) { @@ -100,6 +138,7 @@ public class MainActivity extends AppCompatActivity { default: return super.onOptionsItemSelected(item); } - } + }*/ + }