diff --git a/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectionReport.java b/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectionReport.java index 96f9dbf..6e80a74 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectionReport.java +++ b/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectionReport.java @@ -1,8 +1,13 @@ package com.example.ueberwachungssystem.Detection; +import android.annotation.SuppressLint; import android.util.Log; +import com.example.ueberwachungssystem.WifiCommunication; + +import java.text.SimpleDateFormat; import java.util.Calendar; +import java.util.Date; /** Detection Report Class */ public class DetectionReport { @@ -12,7 +17,11 @@ public class DetectionReport { public boolean detectionState; public DetectionReport(boolean detectionState, String detectionType, float detectedAmplitude) { - this.timeStamp = String.valueOf(Calendar.getInstance().getTime()); + // New Date Format + @SuppressLint("SimpleDateFormat") SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); + Date curDate = new Date(System.currentTimeMillis()); + this.timeStamp = formatter.format(curDate); + //Old Date Format: this.timeStamp = String.valueOf(Calendar.getInstance().getTime()); this.detectionType = detectionType; this.detectedValue = detectedAmplitude; this.detectionState = detectionState; @@ -31,6 +40,16 @@ public class DetectionReport { return String.join("\t", state, time, type, value); } + public String toMessage() { + String state; + if(detectionState) + state = "An"; + else + state = "Aus"; + + return String.join(",", "1", timeStamp, "Gruppe2", WifiCommunication.getLocalIpAddress(), state, detectionType, String.valueOf(detectedValue)); + } + /** Debug Report */ public void log(String tag) { Log.d(tag, this.toString()); 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 e15f707..ce657ad 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectorService.java +++ b/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectorService.java @@ -22,10 +22,11 @@ public class DetectorService extends LifecycleService { private DetectorService.OnDetectionListener listener; private boolean isServiceRunning = false; - VideoDetector videoDetector = null; - AudioRecorder audioRecorder = null; - - /** Communication **/ + // Used Objects: + public VideoDetector videoDetector = null; + public AudioRecorder audioRecorder = null; + public Accelerometer motionDetector = null; + public MicrophoneDetector audioDetector = null; WifiCommunication wifiCommunication; @@ -37,7 +38,9 @@ public class DetectorService extends LifecycleService { return START_NOT_STICKY; - // Setup Service classes: + + + /** Video Detection/Recorder **/ videoDetector = new VideoDetector(this); videoDetector.setOnDetectionListener(new Detector.OnDetectionListener() { @Override @@ -45,10 +48,31 @@ public class DetectorService extends LifecycleService { passToServiceListener(detectionReport); } }); - + /** Motion Detection**/ + motionDetector = new Accelerometer(this); + motionDetector.getSensor(); + motionDetector.setOnDetectionListener(new Detector.OnDetectionListener() { + @Override + public void onDetection(@NonNull DetectionReport detectionReport) { + passToServiceListener(detectionReport); + } + }); + /** Audio Detection **/ + audioDetector = new MicrophoneDetector(this); + audioDetector.setOnDetectionListener(new Detector.OnDetectionListener() { + @Override + public void onDetection(@NonNull DetectionReport detectionReport) { + passToServiceListener(detectionReport); + } + }); + /** Audio Recorder**/ audioRecorder = new AudioRecorder(this); + + + + isServiceRunning = true; wifiCommunication = new WifiCommunication (1234); @@ -61,6 +85,7 @@ public class DetectorService extends LifecycleService { }); return super.onStartCommand(intent, flags, startId); } + @Override public void onDestroy() { super.onDestroy(); @@ -82,81 +107,7 @@ public class DetectorService extends LifecycleService { } - /** Video Detection */ - public void startVideoDetection() { - if(videoDetector != null) - videoDetector.startDetection(); - } - public void stopVideoDetection() { - if(videoDetector != null) - videoDetector.stopDetection(); - } - public boolean isVideoDetectionRunning() { - if(videoDetector != null) - return videoDetector.isDetecting(); - return false; - } - public void debugVideoProcessing(ImageView input, ImageView output) { - if(videoDetector != null) - videoDetector.debugProcessing(input, output); - } - - /** Audio Detection */ - public void startAudioDetection() { - - } - public void stopAudioDetection() { - - } - - /** Motion Detection */ - public void startMotionDetection() { - - } - public void stopMotionDetection() { - - } - - /** Video Recording */ - public void startVideoRecording() { - if(videoDetector != null) - videoDetector.startRecording(); - } - public void stopVideoRecording() { - if(videoDetector != null) - videoDetector.stopRecording(); - } - public boolean isVideoRecordingRunning() { - if(videoDetector != null) - return videoDetector.isRecording(); - return false; - } - public void setVideoRecordingDir(File outputDir) { - if (videoDetector != null) - videoDetector.setOutputDir(outputDir); - } - - /** Audio Recording */ - public void startAudioRecording() { - if(audioRecorder != null) - audioRecorder.startRecording(); - } - public void stopAudioRecording() { - if(audioRecorder != null) - audioRecorder.stopRecording(); - } - public boolean isAudioRecordingRunning() { - if(videoDetector != null) - return audioRecorder.isRecording(); - return false; - } - public void setAudioRecordingDir(File outputDir) { - if (audioRecorder != null) - audioRecorder.setOutputDir(outputDir); - } - - - /** 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) { if (listener != null) { listener.onDetection(detectionReport); diff --git a/app/src/main/java/com/example/ueberwachungssystem/Detection/MicrophoneDetector.java b/app/src/main/java/com/example/ueberwachungssystem/Detection/MicrophoneDetector.java index 6a9e8ba..697a732 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/Detection/MicrophoneDetector.java +++ b/app/src/main/java/com/example/ueberwachungssystem/Detection/MicrophoneDetector.java @@ -2,24 +2,16 @@ package com.example.ueberwachungssystem.Detection; import static java.lang.Math.*; -import android.Manifest; import android.annotation.SuppressLint; -import android.app.Activity; import android.content.Context; -import android.content.pm.PackageManager; import android.media.AudioFormat; import android.media.AudioRecord; import android.media.MediaRecorder; import android.os.AsyncTask; import android.util.Log; -import androidx.core.app.ActivityCompat; -import androidx.core.content.ContextCompat; - import com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex; import com.example.ueberwachungssystem.Detection.Signalverarbeitung.FFT; -import com.example.ueberwachungssystem.Detection.DetectionReport; -import com.example.ueberwachungssystem.Detection.Detector; public class MicrophoneDetector extends Detector { /** @@ -28,11 +20,9 @@ public class MicrophoneDetector extends Detector { * @param context */ - private static final int RECHTEANFORDERUNG_MIKROFON = 1; - private AufnahmeTask aufnahmeTask; public boolean armed = false; - public int Schwellwert_Alarm = 100; + public int schwellwertAlarm = 100; private Context context; public MicrophoneDetector(Context context) { @@ -58,12 +48,17 @@ public class MicrophoneDetector extends Detector { private final int sampleRateInHz = 44100; private final int channelConfig = AudioFormat.CHANNEL_IN_MONO; private final int audioFormat = AudioFormat.ENCODING_PCM_16BIT; + + private final int startDelay = 20000; + private final int threadSleeptime = 10; private int minPufferGroesseInBytes; private int pufferGroesseInBytes; private RingPuffer ringPuffer = new RingPuffer(10); private float kalibierWert; private com.example.ueberwachungssystem.Detection.DetectionReport detectionReport; + + @SuppressLint("MissingPermission") AufnahmeTask() { minPufferGroesseInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat); @@ -112,8 +107,6 @@ public class MicrophoneDetector extends Detector { Log.d("0","Konfiguration: "+ s); - int pufferGroesseInAnzahlAbtastwerten = pufferGroesseInBytes / anzahlBytesProAbtastwert; - } @Override @@ -129,7 +122,7 @@ public class MicrophoneDetector extends Detector { //Kalibrierung try { - Thread.sleep(3000); // Time to lay down the phone + Thread.sleep(startDelay); // Time to lay down the phone } catch (InterruptedException e) { e.printStackTrace(); } @@ -139,7 +132,7 @@ public class MicrophoneDetector extends Detector { Verarbeitungsergebnis kalibrierErgebnis = verarbeiten(puffer, n); kalibierWert += kalibrierErgebnis.maxAmp; try { - Thread.sleep(50); + Thread.sleep(threadSleeptime); } catch (InterruptedException e) { e.printStackTrace(); } @@ -193,7 +186,7 @@ public class MicrophoneDetector extends Detector { publishProgress(ergebnis); try { - Thread.sleep(10); + Thread.sleep(threadSleeptime); } catch (InterruptedException e) { e.printStackTrace(); } @@ -221,7 +214,7 @@ public class MicrophoneDetector extends Detector { ringPuffer.hinzufuegen(max); maxAmp = ringPuffer.maximum(); - if (maxAmp <= Schwellwert_Alarm+kalibierWert) { + if (maxAmp <= schwellwertAlarm + kalibierWert) { armed = true; } } @@ -234,10 +227,10 @@ public class MicrophoneDetector extends Detector { super.onProgressUpdate(progress); float maxAmpPrint = round(20*log10(abs(progress[0].maxAmp/1.0))); float kalibierWertPrint = round(20*log10(abs(kalibierWert))); - Log.d("0","VR, Max, Kal:" + progress[0].verarbeitungsrate + ", " + maxAmpPrint - + " dB, " + kalibierWertPrint + " dB"); + Log.d("alarmAudio","VR: " + progress[0].verarbeitungsrate + ", Amp: " + maxAmpPrint + + " dB, Kal: " + kalibierWertPrint + " dB"); - if (progress[0].maxAmp >= Schwellwert_Alarm+kalibierWert && armed == true) { + if (progress[0].maxAmp >= schwellwertAlarm + kalibierWert && armed == true) { armed = false; detectionReport = new DetectionReport(true, "Audio", maxAmpPrint); reportViolation("Audio", maxAmpPrint); @@ -359,16 +352,6 @@ public class MicrophoneDetector extends Detector { this.wichtungAlterWert = 1 - this.wichtungNeuerWert; } - float MittelwertPuffer(short[] puffer) { - - for (int i = 0; i < puffer.length; i++) { - mittelwert = Math.abs(puffer[i]); - } - mittelwert = mittelwert/puffer.length; - - return mittelwert; - } - float mittel(float wert) { if (istMittelwertGesetzt) { mittelwert = wert * wichtungNeuerWert + mittelwert * wichtungAlterWert; diff --git a/app/src/main/java/com/example/ueberwachungssystem/Detection/VideoDetector.java b/app/src/main/java/com/example/ueberwachungssystem/Detection/VideoDetector.java index 9018ddf..75ca0fb 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/Detection/VideoDetector.java +++ b/app/src/main/java/com/example/ueberwachungssystem/Detection/VideoDetector.java @@ -42,8 +42,6 @@ import java.util.concurrent.ExecutionException; /** * Video Detector inherits some methods from abstract Detector class (more info there) - * USE FROM MAIN ACTIVITY: - * VideoDetector vd = new VideoDetector(this); * */ @@ -175,7 +173,10 @@ public class VideoDetector extends Detector { public void stopDetection() { if (!isDetecting || imageAnalysis == null) return; - cameraProvider.unbind(imageAnalysis); + if (!isRecording) + cameraProvider.unbindAll(); + else + cameraProvider.unbind(imageAnalysis); isDetecting = false; allowReportViolation = false; } @@ -187,7 +188,11 @@ public class VideoDetector extends Detector { return; videoCapture.stopRecording(); - cameraProvider.unbind(videoCapture); + + if (!isDetecting()) + cameraProvider.unbindAll(); + else + cameraProvider.unbind(videoCapture); isRecording = false; } @@ -219,10 +224,10 @@ public class VideoDetector extends Detector { int n = OpenCVHelper.countNonZeroPixels(processed); int pixelCount = image.getWidth() * image.getHeight(); - float percentChanged = (float) n / pixelCount; + float percentChanged = ((float) n / pixelCount) * 100; // Violation Condition - if (percentChanged * 100 > ALARM_THRESHOLD) { + if (percentChanged> ALARM_THRESHOLD) { if (allowReportViolation) reportViolation("Video", percentChanged); } @@ -310,7 +315,7 @@ public class VideoDetector extends Detector { /** Start delay until Violation Report is allowed */ private void startViolationTimer(float setupTime) { - new CountDownTimer((long) (START_DELAY), 100) { + new CountDownTimer((long) (setupTime), 100) { @Override public void onTick(long millisUntilFinished) { } diff --git a/app/src/main/java/com/example/ueberwachungssystem/Fragments/Fragment1.java b/app/src/main/java/com/example/ueberwachungssystem/Fragments/Fragment1.java new file mode 100644 index 0000000..7441b12 --- /dev/null +++ b/app/src/main/java/com/example/ueberwachungssystem/Fragments/Fragment1.java @@ -0,0 +1,48 @@ +package com.example.ueberwachungssystem.Fragments; + +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.fragment.app.Fragment; + +import com.example.ueberwachungssystem.R; + +public class Fragment1 extends Fragment { + private String text; + private final static String KEY_TEXT = "KEY_TEXT"; + + private void log(String nachricht) { + Log.d(this.getClass().getSimpleName(), nachricht); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) { + log("onCreateView"); + View view = inflater.inflate(R.layout.fragment1, container, false); + TextView Sensor = (TextView) view.findViewById(R.id.Sensor); + Sensor.setText(text); + return view; + } + public static Fragment1 erstellen(String text) { + Fragment1 fragment = new Fragment1(); + Bundle b = new Bundle(); + b.putString(KEY_TEXT, text); + fragment.setArguments(b); + return fragment; + } + @Override + public void onCreate(Bundle bundle) { + super .onCreate(bundle); + Bundle args = getArguments(); + if (args != null ) { + text = args.getString(KEY_TEXT); + log("onCreate: text=" + text); + } else { + log("onCreate"); + } + } +} diff --git a/app/src/main/java/com/example/ueberwachungssystem/Fragments/Fragment2.java b/app/src/main/java/com/example/ueberwachungssystem/Fragments/Fragment2.java new file mode 100644 index 0000000..782b841 --- /dev/null +++ b/app/src/main/java/com/example/ueberwachungssystem/Fragments/Fragment2.java @@ -0,0 +1,46 @@ +package com.example.ueberwachungssystem.Fragments; + +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.fragment.app.Fragment; + +import com.example.ueberwachungssystem.R; + +public class Fragment2 extends Fragment { + private String text; + private final static String KEY_TEXT = "KEY_TEXT" ; + private void log(String nachricht) { + Log.d(this.getClass().getSimpleName(), nachricht); + } + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) { + log( "onCreateView" ); + View view = inflater.inflate(R.layout.fragment2, container, false ); + TextView Sensor = (TextView) view.findViewById(R.id.Sensor); + Sensor.setText(text); + return view; + } + public static Fragment2 erstellen(String text) { + Fragment2 fragment = new Fragment2(); + Bundle b = new Bundle(); + b.putString(KEY_TEXT, text); + fragment.setArguments(b); + return fragment; + } + @Override + public void onCreate(Bundle bundle) { + super.onCreate(bundle); + Bundle args = getArguments(); + if (args != null) { + text = args.getString(KEY_TEXT); + log("onCreate: text=" + text); + } else { + log("onCreate"); + } + } +} diff --git a/app/src/main/java/com/example/ueberwachungssystem/Fragments/Fragment3.java b/app/src/main/java/com/example/ueberwachungssystem/Fragments/Fragment3.java new file mode 100644 index 0000000..61eed11 --- /dev/null +++ b/app/src/main/java/com/example/ueberwachungssystem/Fragments/Fragment3.java @@ -0,0 +1,46 @@ +package com.example.ueberwachungssystem.Fragments; + +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.fragment.app.Fragment; + +import com.example.ueberwachungssystem.R; + +public class Fragment3 extends Fragment { + private String text; + private final static String KEY_TEXT = "KEY_TEXT" ; + private void log(String nachricht) { + Log.d(this.getClass().getSimpleName(), nachricht); + } + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) { + log( "onCreateView" ); + View view = inflater.inflate(R.layout.fragment2, container, false ); + TextView Sensor = (TextView) view.findViewById(R.id.Sensor); + Sensor.setText(text); + return view; + } + public static Fragment3 erstellen(String text) { + Fragment3 fragment = new Fragment3(); + Bundle b = new Bundle(); + b.putString(KEY_TEXT, text); + fragment.setArguments(b); + return fragment; + } + @Override + public void onCreate(Bundle bundle) { + super.onCreate(bundle); + Bundle args = getArguments(); + if (args != null) { + text = args.getString(KEY_TEXT); + log("onCreate: text=" + text); + } else { + log("onCreate"); + } + } +} diff --git a/app/src/main/java/com/example/ueberwachungssystem/Logger/Logger.java b/app/src/main/java/com/example/ueberwachungssystem/Logger/Logger.java new file mode 100644 index 0000000..924f5aa --- /dev/null +++ b/app/src/main/java/com/example/ueberwachungssystem/Logger/Logger.java @@ -0,0 +1,44 @@ +package com.example.ueberwachungssystem.Logger; + +import android.util.Log; +import android.widget.TextView; + +import java.io.PrintWriter; +import java.io.StringWriter; + +public class Logger { + private TextView textView; + private StringBuffer sb = new StringBuffer(); + private String tag; + + public Logger(String tag, TextView textView, String logInitText) { + this.tag = tag; + this.textView = textView; + sb.append(logInitText); + } + + public void log(String s) { + Log.d(tag, s); + sb.append(s).append("\n"); + if (textView != null) { + textView.setText(sb.toString()); + } + } + + public void log(Exception e) { + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw)); + log(sw.toString()); + } + + public void clearLog() { + sb.setLength(0); + if (textView != null) { + textView.setText(""); + } + } + + public String getLoggedText() { + return sb.toString(); + } +} diff --git a/app/src/main/res/layout-land/activity_main_land.xml b/app/src/main/res/layout-land/activity_main_land.xml new file mode 100644 index 0000000..41d2ef1 --- /dev/null +++ b/app/src/main/res/layout-land/activity_main_land.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 7969f40..c5abe55 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -38,16 +38,4 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" /> - - -