Kopie Master Version 1.2 UDP Socket merged with sensor

This commit is contained in:
Miguel Siebenhaar 2023-06-21 13:55:36 +02:00
parent 5a0aab0176
commit 5b042611a5
2 changed files with 74 additions and 24 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}
}*/
}