Browse Source

Version 1. working

ms_service_copie
Miguel Siebenhaar 1 year ago
parent
commit
8e002107b5

+ 40
- 6
app/src/main/java/com/example/ueberwachungssystem/Detection/DetectorService.java View File

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

}
});








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**/
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 **/
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**/




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

+ 2
- 2
app/src/main/java/com/example/ueberwachungssystem/MainActivity.java View File



detectorService.setOnDetectionListener(new DetectorService.OnDetectionListener() { detectorService.setOnDetectionListener(new DetectorService.OnDetectionListener() {
@Override @Override
public void onDetection(@NonNull DetectionReport detectionReport) {
Log.d("onDetection", detectionReport.toMessage());
public void onDetection(@NonNull StringBuffer stringBuffer) {
Log.d("onDetection", stringBuffer.toString());
} }
}); });
} }

+ 9
- 5
app/src/main/java/com/example/ueberwachungssystem/WifiCommunication.java View File

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


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

} }
} }


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");
sendWifiData(rxStringBuffer);
//rxStringBuffer.append(rxString).append("\n");
Log.d("empfangen", rxString);
sendWifiData(rxString);
//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer)); //mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
previousRxString = rxString; previousRxString = rxString;
} }
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);

Loading…
Cancel
Save