Added Alarm Handling to Project

This commit is contained in:
Christian Tinz 2023-06-18 22:01:52 +02:00
parent 8dd4c6d56a
commit 1094b0f75d
8 changed files with 100 additions and 8 deletions

View File

@ -70,7 +70,6 @@ public class AccelerometerActivity extends AppCompatActivity implements SensorEv
@Override @Override
public void onChanged(List<Device> devices) { public void onChanged(List<Device> devices) {
deviceListAdapter.setDevices(devices); deviceListAdapter.setDevices(devices);
Toast.makeText(AccelerometerActivity.this, "onChanged", Toast.LENGTH_LONG).show();
} }
}); });
@ -81,6 +80,18 @@ public class AccelerometerActivity extends AppCompatActivity implements SensorEv
} }
}); });
mAccelerometerViewModel.getStartAlarmRecording().observe(this, new Observer<Boolean>() {
@Override
public void onChanged(Boolean aBoolean) {
if (aBoolean) {
Toast.makeText(AccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(AccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
}
}
});
mAccelerometerViewModel.getMovementDetectedValue().observe(this, new Observer<Boolean>() { mAccelerometerViewModel.getMovementDetectedValue().observe(this, new Observer<Boolean>() {
@Override @Override
public void onChanged(Boolean aBoolean) { public void onChanged(Boolean aBoolean) {

View File

@ -62,7 +62,6 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
public void onChanged(List<Device> devices) { public void onChanged(List<Device> devices) {
deviceListAdapter.setDevices(devices); deviceListAdapter.setDevices(devices);
Toast.makeText(MainActivity.this, "onChanged", Toast.LENGTH_LONG).show();
} }
}); });
@ -73,6 +72,18 @@ public class MainActivity extends AppCompatActivity {
} }
}); });
mMainActivityViewModel.getStartAlarmRecording().observe(this, new Observer<Boolean>() {
@Override
public void onChanged(Boolean aBoolean) {
if (aBoolean) {
Toast.makeText(MainActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
}
}
});
audiodetectionButton.setOnClickListener(new View.OnClickListener() { audiodetectionButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
@ -89,7 +100,6 @@ public class MainActivity extends AppCompatActivity {
else { else {
mMainActivityViewModel.accessRequestCamera(MainActivity.this); mMainActivityViewModel.accessRequestCamera(MainActivity.this);
} }
} }
}); });
accelerometerButton.setOnClickListener(new View.OnClickListener() { accelerometerButton.setOnClickListener(new View.OnClickListener() {

View File

@ -62,7 +62,6 @@ public class VideodetectionActivity extends AppCompatActivity {
@Override @Override
public void onChanged(List<Device> devices) { public void onChanged(List<Device> devices) {
deviceListAdapter.setDevices(devices); deviceListAdapter.setDevices(devices);
Toast.makeText(VideodetectionActivity.this, "onChanged", Toast.LENGTH_LONG).show();
} }
}); });
@ -73,6 +72,18 @@ public class VideodetectionActivity extends AppCompatActivity {
} }
}); });
mVideoDetectionViewModel.getStartAlarmRecording().observe(this, new Observer<Boolean>() {
@Override
public void onChanged(Boolean aBoolean) {
if (aBoolean) {
Toast.makeText(VideodetectionActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(VideodetectionActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
}
}
});
mVideoDetectionViewModel.getVideoAlarmDetectedValue().observe(this, new Observer<Boolean>() { mVideoDetectionViewModel.getVideoAlarmDetectedValue().observe(this, new Observer<Boolean>() {
@Override @Override
public void onChanged(Boolean aBoolean) { public void onChanged(Boolean aBoolean) {

View File

@ -17,10 +17,12 @@ public class DeviceRepository {
private final int maxAlarmHistoryListSize = 20; private final int maxAlarmHistoryListSize = 20;
private final String delimiter = ", "; private final String delimiter = ", ";
private final String SensorStatusKey = "An"; private final String sensorStatusKey = "An";
private String localDeviceUUID; private String localDeviceUUID;
private static DeviceRepository deviceRepositoryInstance; private static DeviceRepository deviceRepositoryInstance;
private WiFiCommunication mWiFiCommunication; private WiFiCommunication mWiFiCommunication;
private boolean startAlarmRecordingValue;
private MutableLiveData<Boolean> startAlarmRecording = new MutableLiveData<>();
private MutableLiveData<List<Device>> deviceList = new MutableLiveData<>(); private MutableLiveData<List<Device>> deviceList = new MutableLiveData<>();
private MutableLiveData<List<Device>> alarmHistoryList = new MutableLiveData<>(); private MutableLiveData<List<Device>> alarmHistoryList = new MutableLiveData<>();
private HashMap<String, Device> connectedDevicesList = new HashMap<>(); private HashMap<String, Device> connectedDevicesList = new HashMap<>();
@ -29,6 +31,7 @@ public class DeviceRepository {
private DeviceRepository() { private DeviceRepository() {
setLocalDeviceUUID(); setLocalDeviceUUID();
startAlarmRecordingValue = false;
} }
public static synchronized DeviceRepository getInstance() { public static synchronized DeviceRepository getInstance() {
@ -52,11 +55,21 @@ public class DeviceRepository {
return alarmHistoryList; return alarmHistoryList;
} }
public MutableLiveData<Boolean> getStartAlarmRecording() {
setMutableLiveDataStartAlarmRecording();
return startAlarmRecording;
}
public void createNewDevice(String timeStamp, String deviceID, boolean sensorStatus, String sensorType, int sensorMassage){ public void createNewDevice(String timeStamp, String deviceID, boolean sensorStatus, String sensorType, int sensorMassage){
Device newDevice = new Device(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage); Device newDevice = new Device(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage);
if (sensorStatus) { if (sensorStatus) {
setAlarmHistoryDeviceList(newDevice); setAlarmHistoryDeviceList(newDevice);
} }
boolean newStartAlarmRecordingValue = checkDeviceStatus();
if (startAlarmRecordingValue != newStartAlarmRecordingValue) {
startAlarmRecordingValue = newStartAlarmRecordingValue;
setMutableLiveDataStartAlarmRecording();
}
addToConnectedDeviceList(newDevice.getDeviceID(), newDevice); addToConnectedDeviceList(newDevice.getDeviceID(), newDevice);
setMutableLiveDataDeviceList(); setMutableLiveDataDeviceList();
} }
@ -124,6 +137,11 @@ public class DeviceRepository {
setAlarmHistoryDeviceList(device); setAlarmHistoryDeviceList(device);
} }
device.setSensorStatus(sensorStatus); device.setSensorStatus(sensorStatus);
boolean newStartAlarmRecordingValue = checkDeviceStatus();
if (startAlarmRecordingValue != newStartAlarmRecordingValue) {
startAlarmRecordingValue = newStartAlarmRecordingValue;
setMutableLiveDataStartAlarmRecording();
}
addToConnectedDeviceList(checkedDeviceID, device); addToConnectedDeviceList(checkedDeviceID, device);
setMutableLiveDataDeviceList(); setMutableLiveDataDeviceList();
} }
@ -177,6 +195,11 @@ public class DeviceRepository {
setAlarmHistoryDeviceList(device); setAlarmHistoryDeviceList(device);
} }
device.setSensorStatus(sensorStatus); device.setSensorStatus(sensorStatus);
boolean newStartAlarmRecordingValue = checkDeviceStatus();
if (startAlarmRecordingValue != newStartAlarmRecordingValue) {
startAlarmRecordingValue = newStartAlarmRecordingValue;
setMutableLiveDataStartAlarmRecording();
}
addToConnectedDeviceList(checkedDeviceID, device); addToConnectedDeviceList(checkedDeviceID, device);
setMutableLiveDataDeviceList(); setMutableLiveDataDeviceList();
@ -245,6 +268,10 @@ public class DeviceRepository {
alarmHistoryList.setValue(alarmHistoryDeviceList); alarmHistoryList.setValue(alarmHistoryDeviceList);
} }
private void setMutableLiveDataStartAlarmRecording() {
startAlarmRecording.setValue(startAlarmRecordingValue);
}
private void addToConnectedDeviceList(String key, Device device) { private void addToConnectedDeviceList(String key, Device device) {
connectedDevicesList.put(key, device); connectedDevicesList.put(key, device);
if (key.equals(checkDeviceID(localDeviceUUID))) { if (key.equals(checkDeviceID(localDeviceUUID))) {
@ -272,7 +299,7 @@ public class DeviceRepository {
} }
private boolean convertSensorStatus(String status) { private boolean convertSensorStatus(String status) {
return status.equals(SensorStatusKey); return status.equals(sensorStatusKey);
} }
private void setAlarmHistoryDeviceList(Device device) { private void setAlarmHistoryDeviceList(Device device) {
@ -283,4 +310,13 @@ public class DeviceRepository {
alarmHistoryDeviceList.add(0, alarmHistoryDevice); alarmHistoryDeviceList.add(0, alarmHistoryDevice);
setMutableLiveDataAlarmHistoryList(); setMutableLiveDataAlarmHistoryList();
} }
private boolean checkDeviceStatus() {
for (Device device : connectedDevicesList.values()) {
if (device.getSensorStatus()) {
return true;
}
}
return false;
}
} }

View File

@ -17,6 +17,7 @@ public class AccelerometerViewModel extends ViewModel implements ViewModelInterf
private MutableLiveData<List<Device>> mDeviceList; private MutableLiveData<List<Device>> mDeviceList;
private MutableLiveData<Boolean> mMovementDetected = new MutableLiveData<>(); private MutableLiveData<Boolean> mMovementDetected = new MutableLiveData<>();
private MutableLiveData<List<Device>> mAlarmHistoryList; private MutableLiveData<List<Device>> mAlarmHistoryList;
private MutableLiveData<Boolean> mStartAlarmRecording;
private DeviceRepository mDeviceRepository; private DeviceRepository mDeviceRepository;
private ArrayList<Float> Gesamt_be; private ArrayList<Float> Gesamt_be;
int arraySize = 500; int arraySize = 500;
@ -32,6 +33,7 @@ public class AccelerometerViewModel extends ViewModel implements ViewModelInterf
mDeviceRepository = DeviceRepository.getInstance(); mDeviceRepository = DeviceRepository.getInstance();
mDeviceList = mDeviceRepository.getConnectedDeviceList(); mDeviceList = mDeviceRepository.getConnectedDeviceList();
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList(); mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList();
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording();
} }
initGesamtBE(); initGesamtBE();
mMovementDetected.setValue(false); mMovementDetected.setValue(false);
@ -96,6 +98,11 @@ public class AccelerometerViewModel extends ViewModel implements ViewModelInterf
return mAlarmHistoryList; return mAlarmHistoryList;
} }
@Override
public LiveData<Boolean> getStartAlarmRecording() {
return mStartAlarmRecording;
}
@Override @Override
public void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) { public void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) {
mDeviceRepository.updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage); mDeviceRepository.updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);

View File

@ -20,8 +20,9 @@ public class MainActivityViewModel extends ViewModel implements ViewModelInterfa
private MutableLiveData<List<Device>> mDeviceList; private MutableLiveData<List<Device>> mDeviceList;
private MutableLiveData<List<Device>> mAlarmHistoryList; private MutableLiveData<List<Device>> mAlarmHistoryList;
private MutableLiveData<Boolean> mStartAlarmRecording;
private DeviceRepository mDeviceRepository; private DeviceRepository mDeviceRepository;
private static final int RECHTEANFORDERUNG_KAMERA = 10; private static final int RIGHTS_REQUEST_CAMERA = 10;
@Override @Override
public void init() { public void init() {
@ -37,6 +38,7 @@ public class MainActivityViewModel extends ViewModel implements ViewModelInterfa
mDeviceRepository.createNewDevice(mDeviceRepository.getSystemTimeStamp(), mDeviceRepository.getLocalDeviceUUID(), false, "No Sensor selected", 0); mDeviceRepository.createNewDevice(mDeviceRepository.getSystemTimeStamp(), mDeviceRepository.getLocalDeviceUUID(), false, "No Sensor selected", 0);
mDeviceList = mDeviceRepository.getConnectedDeviceList(); mDeviceList = mDeviceRepository.getConnectedDeviceList();
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList(); mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList();
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording();
} }
public boolean isCameraAccessAllowed(Context context) { public boolean isCameraAccessAllowed(Context context) {
@ -44,7 +46,7 @@ public class MainActivityViewModel extends ViewModel implements ViewModelInterfa
} }
public void accessRequestCamera(Activity activity) { public void accessRequestCamera(Activity activity) {
ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.CAMERA}, RECHTEANFORDERUNG_KAMERA); ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.CAMERA}, RIGHTS_REQUEST_CAMERA);
} }
@Override @Override
@ -57,6 +59,11 @@ public class MainActivityViewModel extends ViewModel implements ViewModelInterfa
return mAlarmHistoryList; return mAlarmHistoryList;
} }
@Override
public LiveData<Boolean> getStartAlarmRecording() {
return mStartAlarmRecording;
}
@Override @Override
public void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) { public void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) {
mDeviceRepository.updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage); mDeviceRepository.updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);

View File

@ -19,6 +19,7 @@ public class VideodetectionViewModel extends ViewModel implements ViewModelInter
private MutableLiveData<List<Device>> mDeviceList; private MutableLiveData<List<Device>> mDeviceList;
private MutableLiveData<Boolean> mVideoAlarmDetected; private MutableLiveData<Boolean> mVideoAlarmDetected;
private MutableLiveData<List<Device>> mAlarmHistoryList; private MutableLiveData<List<Device>> mAlarmHistoryList;
private MutableLiveData<Boolean> mStartAlarmRecording;
private DeviceRepository mDeviceRepository; private DeviceRepository mDeviceRepository;
private CameraSensor mCameraSensor; private CameraSensor mCameraSensor;
@ -41,6 +42,9 @@ public class VideodetectionViewModel extends ViewModel implements ViewModelInter
if (mAlarmHistoryList == null) { if (mAlarmHistoryList == null) {
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList(); mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList();
} }
if (mStartAlarmRecording == null) {
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording();
}
if (mVideoAlarmDetected == null) { if (mVideoAlarmDetected == null) {
mVideoAlarmDetected = mCameraSensor.getVideoAlarmDetectedValue(); mVideoAlarmDetected = mCameraSensor.getVideoAlarmDetectedValue();
} }
@ -64,6 +68,11 @@ public class VideodetectionViewModel extends ViewModel implements ViewModelInter
return mAlarmHistoryList; return mAlarmHistoryList;
} }
@Override
public LiveData<Boolean> getStartAlarmRecording() {
return mStartAlarmRecording;
}
@Override @Override
public void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) { public void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) {
mDeviceRepository.updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage); mDeviceRepository.updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);

View File

@ -10,6 +10,7 @@ public interface ViewModelInterface {
void init(); void init();
LiveData<List<Device>> getConnectedDeviceList(); LiveData<List<Device>> getConnectedDeviceList();
LiveData<List<Device>> getAlarmHistoryList(); LiveData<List<Device>> getAlarmHistoryList();
LiveData<Boolean> getStartAlarmRecording();
void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage); void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage);
void setTimeStamp(String deviceID, String timeStamp); void setTimeStamp(String deviceID, String timeStamp);
String getTimeStamp(String deviceID); String getTimeStamp(String deviceID);