Gruppe 1
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DetectorService.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package com.example.ueberwachungssystem.Detection;
  2. import android.content.Intent;
  3. import android.os.Binder;
  4. import android.os.IBinder;
  5. import android.util.Log;
  6. import android.widget.ImageView;
  7. import androidx.annotation.NonNull;
  8. import androidx.annotation.Nullable;
  9. import androidx.camera.core.ExperimentalGetImage;
  10. import androidx.lifecycle.LifecycleService;
  11. import com.example.ueberwachungssystem.WifiCommunication;
  12. import java.io.File;
  13. @ExperimentalGetImage
  14. public class DetectorService extends LifecycleService {
  15. public ServiceBinder serviceBinder = new ServiceBinder();
  16. private DetectorService.OnDetectionListener listener;
  17. private boolean isServiceRunning = false;
  18. // Used Objects:
  19. public VideoDetector videoDetector = null;
  20. public AudioRecorder audioRecorder = null;
  21. public Accelerometer motionDetector = null;
  22. public MicrophoneDetector audioDetector = null;
  23. public WifiCommunication wifiCommunication;
  24. // Recording logic
  25. private boolean detectedVideo = false;
  26. private boolean detectedAudio = false;
  27. private boolean detectedMotion = false;
  28. String wifiData;
  29. StringBuffer stringBufferWifi = new StringBuffer();
  30. String typOfAlarm;
  31. @Override
  32. public int onStartCommand(Intent intent, int flags, int startId) {
  33. if (isServiceRunning)
  34. return START_NOT_STICKY;
  35. /** Wifi Instanz **/
  36. wifiCommunication = new WifiCommunication(1234);
  37. wifiCommunication.sendTrue("TEst");
  38. wifiCommunication.setOnConnectionListener(new WifiCommunication.OnConnectionListener() {
  39. @Override
  40. public void onConnection(String data) {
  41. Log.d("Listener", data);
  42. wifiData = data;
  43. stringToStringbuffer(data);
  44. Log.d("buffer",stringBufferWifi.toString());
  45. passToServiceListener(stringBufferWifi);
  46. checkState(data);
  47. checkTyp(data);
  48. }
  49. });
  50. /** Video Detection/Recorder **/
  51. videoDetector = new VideoDetector(this);
  52. videoDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
  53. @Override
  54. public void onDetection(@NonNull DetectionReport detectionReport) {
  55. //passToServiceListener(detectionReport);
  56. if(detectionReport.detectionState){
  57. detectedVideo = true;
  58. videoDetector.startRecording();
  59. } else {
  60. detectedVideo = false;
  61. if(!(detectedVideo || detectedAudio || detectedMotion)) {
  62. videoDetector.stopRecording();
  63. }
  64. }
  65. wifiCommunication.sendTrue(detectionReport.toMessage());
  66. }
  67. });
  68. /** Motion Detection**/
  69. motionDetector = new Accelerometer(this);
  70. motionDetector.getSensor();
  71. motionDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
  72. @Override
  73. public void onDetection(@NonNull DetectionReport detectionReport) {
  74. //passToServiceListener(detectionReport);
  75. wifiCommunication.sendTrue(detectionReport.toMessage());
  76. }
  77. });
  78. /** Audio Detection **/
  79. audioDetector = new MicrophoneDetector(this);
  80. audioDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
  81. @Override
  82. public void onDetection(@NonNull DetectionReport detectionReport) {
  83. //passToServiceListener(detectionReport);
  84. wifiCommunication.sendTrue(detectionReport.toMessage());
  85. }
  86. });
  87. /** Audio Recorder**/
  88. audioRecorder = new AudioRecorder(this);
  89. isServiceRunning = true;
  90. return super.onStartCommand(intent, flags, startId);
  91. }
  92. @Override
  93. public void onDestroy() {
  94. super.onDestroy();
  95. isServiceRunning = false;
  96. }
  97. /** Service methods */
  98. public class ServiceBinder extends Binder {
  99. public DetectorService getBoundService() {
  100. // Return an instance of the TestService
  101. return DetectorService.this;
  102. }
  103. }
  104. @Nullable
  105. @Override
  106. public IBinder onBind(Intent intent) {
  107. super.onBind(intent);
  108. return serviceBinder;
  109. }
  110. /** Pass Detection Report to Service Detection Listener and trigger it */
  111. public void passToServiceListener(StringBuffer stringBuffer) {
  112. if (listener != null) {
  113. listener.onDetection(stringBuffer);
  114. }
  115. }
  116. /** On Detection Listener - runs when violation is reported */
  117. public interface OnDetectionListener {
  118. void onDetection(@NonNull StringBuffer stringBuffer);
  119. }
  120. public void setOnDetectionListener(@NonNull DetectorService.OnDetectionListener listener) {
  121. this.listener = listener;
  122. }
  123. public void stringToStringbuffer(String string){
  124. if(string != null) {
  125. stringBufferWifi.insert(0,string + "\n");
  126. }
  127. }
  128. public String[] splitString(String string){
  129. String[] splitrxString = string.split(",");
  130. return splitrxString; //splitrxString[0] = "1",splitrxString[1] = "HH:MM:SS", splitrxString[0].equals("1")
  131. }
  132. public boolean checkState(String string){
  133. Log.d("state", String.valueOf(splitString(string)[4].equals("An")));
  134. return splitString(string)[4].equals("An");
  135. }
  136. public String checkTyp(String string){
  137. if (splitString(string)[5] != null) {
  138. typOfAlarm = splitString(string)[5];
  139. Log.d("Type", typOfAlarm);
  140. }
  141. return typOfAlarm;
  142. }
  143. }