Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fad1ed3937 | ||
|
|
8dd4c6d56a | ||
|
|
317bef2b59 | ||
|
|
032ab2788d |
No files matched your search
@@ -35,4 +35,10 @@ dependencies {
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
||||
|
||||
def camerax_version = "1.2.3"
|
||||
implementation "androidx.camera:camera-camera2:${camerax_version}"
|
||||
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
|
||||
implementation "androidx.camera:camera-view:${camerax_version}"
|
||||
|
||||
}
|
||||
@@ -3,6 +3,9 @@
|
||||
package="com.example.greenwatch">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.CAMERA"/>
|
||||
|
||||
<uses-feature android:name="android.hardware.camera"/>
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.example.greenwatch;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
@@ -14,6 +16,8 @@ import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.greenwatch.adapters.AlarmHistoryListAdapter;
|
||||
import com.example.greenwatch.adapters.DeviceListAdapter;
|
||||
import com.example.greenwatch.models.Device;
|
||||
import com.example.greenwatch.viewmodels.AccelerometerViewModel;
|
||||
|
||||
@@ -39,6 +43,20 @@ public class AccelerometerActivity extends AppCompatActivity implements SensorEv
|
||||
accelerometerWarningTV = (TextView) findViewById(R.id.tvAccelerometerWarning);
|
||||
backToMainActivityButton = (Button) findViewById(R.id.accelerometerBackToMainActivity);
|
||||
|
||||
RecyclerView recyclerView = findViewById(R.id.deviceListRecyclerView);
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||
recyclerView.setHasFixedSize(true);
|
||||
|
||||
final DeviceListAdapter deviceListAdapter = new DeviceListAdapter();
|
||||
recyclerView.setAdapter(deviceListAdapter);
|
||||
|
||||
RecyclerView alarmHistoryListRecyclerView = findViewById(R.id.alarmHistoryListRecyclerView);
|
||||
alarmHistoryListRecyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||
alarmHistoryListRecyclerView.setHasFixedSize(true);
|
||||
|
||||
final AlarmHistoryListAdapter alarmHistoryListAdapter = new AlarmHistoryListAdapter();
|
||||
alarmHistoryListRecyclerView.setAdapter(alarmHistoryListAdapter);
|
||||
|
||||
backToMainActivityButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
@@ -51,34 +69,26 @@ public class AccelerometerActivity extends AppCompatActivity implements SensorEv
|
||||
mAccelerometerViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() {
|
||||
@Override
|
||||
public void onChanged(List<Device> devices) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Time Stamp: ");
|
||||
sb.append(mAccelerometerViewModel.getTimeStamp(mAccelerometerViewModel.getLocalDeviceUUID()));
|
||||
sb.append(";\n");
|
||||
sb.append("DeviceID: ");
|
||||
sb.append(mAccelerometerViewModel.getDeviceID(mAccelerometerViewModel.getLocalDeviceUUID()));
|
||||
sb.append(";\n");
|
||||
sb.append("Sensor Status: ");
|
||||
sb.append(mAccelerometerViewModel.getSensorStatus(mAccelerometerViewModel.getLocalDeviceUUID()));
|
||||
sb.append(";\n");
|
||||
sb.append("Sensor Type: ");
|
||||
sb.append(mAccelerometerViewModel.getSensorType(mAccelerometerViewModel.getLocalDeviceUUID()));
|
||||
sb.append(";\n");
|
||||
sb.append("Sensor Massage: ");
|
||||
sb.append(mAccelerometerViewModel.getSensorMassage(mAccelerometerViewModel.getLocalDeviceUUID()));
|
||||
accelerometerStatusMessage.setText(sb);
|
||||
deviceListAdapter.setDevices(devices);
|
||||
Toast.makeText(AccelerometerActivity.this, "onChanged", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
|
||||
mAccelerometerViewModel.getAlarmHistoryList().observe(this, new Observer<List<Device>>() {
|
||||
@Override
|
||||
public void onChanged(List<Device> devices) {
|
||||
alarmHistoryListAdapter.setAlarmHistoryList(devices);
|
||||
}
|
||||
});
|
||||
|
||||
mAccelerometerViewModel.getMovementDetectedValue().observe(this, new Observer<Boolean>() {
|
||||
@Override
|
||||
public void onChanged(Boolean aBoolean) {
|
||||
if (aBoolean) {
|
||||
mAccelerometerViewModel.setDevice(mAccelerometerViewModel.getLocalDeviceUUID(), mAccelerometerViewModel.getSystemTimeStamp(), true, "Accelerometer", 10);
|
||||
mAccelerometerViewModel.updateDevice(mAccelerometerViewModel.getLocalDeviceUUID(), mAccelerometerViewModel.getSystemTimeStamp(), true, "Accelerometer", 10);
|
||||
}
|
||||
else {
|
||||
mAccelerometerViewModel.setDevice(mAccelerometerViewModel.getLocalDeviceUUID(), mAccelerometerViewModel.getSystemTimeStamp(), false, "Accelerometer", 0);
|
||||
mAccelerometerViewModel.updateDevice(mAccelerometerViewModel.getLocalDeviceUUID(), mAccelerometerViewModel.getSystemTimeStamp(), false, "Accelerometer", 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,9 +10,9 @@ import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.greenwatch.adapters.AlarmHistoryListAdapter;
|
||||
import com.example.greenwatch.adapters.DeviceListAdapter;
|
||||
import com.example.greenwatch.models.Device;
|
||||
import com.example.greenwatch.viewmodels.MainActivityViewModel;
|
||||
@@ -21,7 +21,6 @@ import java.util.List;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private TextView tvstatusmessage;
|
||||
private Button audiodetectionButton;
|
||||
private Button videodetectionButton;
|
||||
private Button accelerometerButton;
|
||||
@@ -35,7 +34,6 @@ public class MainActivity extends AppCompatActivity {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
tvstatusmessage = (TextView) findViewById(R.id.tvStatusmessage);
|
||||
audiodetectionButton = (Button) findViewById(R.id.audiodetectionButton);
|
||||
videodetectionButton = (Button) findViewById(R.id.videodetectionButton);
|
||||
accelerometerButton = (Button) findViewById(R.id.accelerometerButton);
|
||||
@@ -43,53 +41,55 @@ public class MainActivity extends AppCompatActivity {
|
||||
videodetectionAndAccelerometerButton = (Button) findViewById(R.id.videodetectionAndAccelerometerButton);
|
||||
connectionButton = (Button) findViewById(R.id.connectionButton);
|
||||
|
||||
RecyclerView recyclerView = findViewById(R.id.deviceListRecyclerView);
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||
recyclerView.setHasFixedSize(true);
|
||||
RecyclerView deviceListRecyclerView = findViewById(R.id.deviceListRecyclerView);
|
||||
deviceListRecyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||
deviceListRecyclerView.setHasFixedSize(true);
|
||||
|
||||
final DeviceListAdapter deviceListAdapter = new DeviceListAdapter();
|
||||
recyclerView.setAdapter(deviceListAdapter);
|
||||
deviceListRecyclerView.setAdapter(deviceListAdapter);
|
||||
|
||||
RecyclerView alarmHistoryListRecyclerView = findViewById(R.id.alarmHistoryListRecyclerView);
|
||||
alarmHistoryListRecyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||
alarmHistoryListRecyclerView.setHasFixedSize(true);
|
||||
|
||||
final AlarmHistoryListAdapter alarmHistoryListAdapter = new AlarmHistoryListAdapter();
|
||||
alarmHistoryListRecyclerView.setAdapter(alarmHistoryListAdapter);
|
||||
|
||||
mMainActivityViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class);
|
||||
mMainActivityViewModel.init();
|
||||
mMainActivityViewModel.accessRequestCamera(this);
|
||||
mMainActivityViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() {
|
||||
@Override
|
||||
public void onChanged(List<Device> devices) {
|
||||
deviceListAdapter.setDevices(devices);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Time Stamp: ");
|
||||
sb.append(mMainActivityViewModel.getTimeStamp(mMainActivityViewModel.getLocalDeviceUUID()));
|
||||
sb.append(";\n");
|
||||
sb.append("DeviceID: ");
|
||||
sb.append(mMainActivityViewModel.getDeviceID(mMainActivityViewModel.getLocalDeviceUUID()));
|
||||
sb.append(";\n");
|
||||
sb.append("Sensor Status: ");
|
||||
sb.append(mMainActivityViewModel.getSensorStatus(mMainActivityViewModel.getLocalDeviceUUID()));
|
||||
sb.append(";\n");
|
||||
sb.append("Sensor Type: ");
|
||||
sb.append(mMainActivityViewModel.getSensorType(mMainActivityViewModel.getLocalDeviceUUID()));
|
||||
sb.append(";\n");
|
||||
sb.append("Sensor Massage: ");
|
||||
sb.append(mMainActivityViewModel.getSensorMassage(mMainActivityViewModel.getLocalDeviceUUID()));
|
||||
tvstatusmessage.setText(sb);
|
||||
Toast.makeText(MainActivity.this, "onChanged", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
|
||||
mMainActivityViewModel.getAlarmHistoryList().observe(this, new Observer<List<Device>>() {
|
||||
@Override
|
||||
public void onChanged(List<Device> devices) {
|
||||
alarmHistoryListAdapter.setAlarmHistoryList(devices);
|
||||
}
|
||||
});
|
||||
|
||||
audiodetectionButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
//openAudiodetectionActivity();
|
||||
mMainActivityViewModel.setDevice(mMainActivityViewModel.getLocalDeviceUUID(), "10:51", false, "Audio", 10);
|
||||
mMainActivityViewModel.updateDevice(mMainActivityViewModel.getLocalDeviceUUID(), "10:51", false, "Audio", 10);
|
||||
}
|
||||
});
|
||||
videodetectionButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
//openVideodetectionActivity();
|
||||
mMainActivityViewModel.setSensorType(mMainActivityViewModel.getLocalDeviceUUID(), "Video");
|
||||
mMainActivityViewModel.setTimeStamp(mMainActivityViewModel.getLocalDeviceUUID(), "12:50");
|
||||
mMainActivityViewModel.setDeviceID(mMainActivityViewModel.getLocalDeviceUUID(), "12345");
|
||||
if (mMainActivityViewModel.isCameraAccessAllowed(MainActivity.this)) {
|
||||
openVideodetectionActivity();
|
||||
}
|
||||
else {
|
||||
mMainActivityViewModel.accessRequestCamera(MainActivity.this);
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
accelerometerButton.setOnClickListener(new View.OnClickListener() {
|
||||
@@ -140,12 +140,12 @@ public class MainActivity extends AppCompatActivity {
|
||||
public void openVideodetectionAndAccelerometerActivity(){
|
||||
//Intent intent = new Intent(this, VideodetectionAndAccelerometerActivity.class);
|
||||
//startActivity(intent);
|
||||
mMainActivityViewModel.setDevice(mMainActivityViewModel.getLocalDeviceUUID(), "24:00", false, "Video", 0);
|
||||
mMainActivityViewModel.updateDevice(mMainActivityViewModel.getLocalDeviceUUID(), "24:00", false, "Video", 0);
|
||||
}
|
||||
public void openConnectionActivity(){
|
||||
//Intent intent = new Intent(this, ConnectionActivity.class);
|
||||
//startActivity(intent);
|
||||
mMainActivityViewModel.setDevice(mMainActivityViewModel.getLocalDeviceUUID(), "10:51", true, "Audio", 10);
|
||||
mMainActivityViewModel.updateDevice(mMainActivityViewModel.getLocalDeviceUUID(), "10:51", true, "Audio", 10);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +1,32 @@
|
||||
package com.example.greenwatch;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.camera.lifecycle.ProcessCameraProvider;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.greenwatch.adapters.AlarmHistoryListAdapter;
|
||||
import com.example.greenwatch.adapters.DeviceListAdapter;
|
||||
import com.example.greenwatch.models.Device;
|
||||
import com.example.greenwatch.viewmodels.AccelerometerViewModel;
|
||||
import com.example.greenwatch.viewmodels.VideodetectionViewModel;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class VideodetectionActivity extends AppCompatActivity {
|
||||
|
||||
private Button backToMainActivity;
|
||||
private VideodetectionViewModel mVideoDetectionViewModel;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -17,11 +35,64 @@ public class VideodetectionActivity extends AppCompatActivity {
|
||||
|
||||
backToMainActivity = (Button) findViewById(R.id.videodetectorBackToMainActivity);
|
||||
|
||||
RecyclerView recyclerView = findViewById(R.id.deviceListRecyclerView);
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||
recyclerView.setHasFixedSize(true);
|
||||
|
||||
final DeviceListAdapter deviceListAdapter = new DeviceListAdapter();
|
||||
recyclerView.setAdapter(deviceListAdapter);
|
||||
|
||||
RecyclerView alarmHistoryListRecyclerView = findViewById(R.id.alarmHistoryListRecyclerView);
|
||||
alarmHistoryListRecyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||
alarmHistoryListRecyclerView.setHasFixedSize(true);
|
||||
|
||||
final AlarmHistoryListAdapter alarmHistoryListAdapter = new AlarmHistoryListAdapter();
|
||||
alarmHistoryListRecyclerView.setAdapter(alarmHistoryListAdapter);
|
||||
|
||||
backToMainActivity.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
|
||||
mVideoDetectionViewModel = new ViewModelProvider(this).get(VideodetectionViewModel.class);
|
||||
mVideoDetectionViewModel.init();
|
||||
mVideoDetectionViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() {
|
||||
@Override
|
||||
public void onChanged(List<Device> devices) {
|
||||
deviceListAdapter.setDevices(devices);
|
||||
Toast.makeText(VideodetectionActivity.this, "onChanged", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
|
||||
mVideoDetectionViewModel.getAlarmHistoryList().observe(this, new Observer<List<Device>>() {
|
||||
@Override
|
||||
public void onChanged(List<Device> devices) {
|
||||
alarmHistoryListAdapter.setAlarmHistoryList(devices);
|
||||
}
|
||||
});
|
||||
|
||||
mVideoDetectionViewModel.getVideoAlarmDetectedValue().observe(this, new Observer<Boolean>() {
|
||||
@Override
|
||||
public void onChanged(Boolean aBoolean) {
|
||||
if (aBoolean) {
|
||||
mVideoDetectionViewModel.updateDevice(mVideoDetectionViewModel.getLocalDeviceUUID(), mVideoDetectionViewModel.getSystemTimeStamp(), true, "Video", 10);
|
||||
}
|
||||
else {
|
||||
mVideoDetectionViewModel.updateDevice(mVideoDetectionViewModel.getLocalDeviceUUID(), mVideoDetectionViewModel.getSystemTimeStamp(), false, "Video", 0);
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
|
||||
cameraProviderFuture.addListener(() -> {
|
||||
try {
|
||||
mVideoDetectionViewModel.bindImageAnalysis(cameraProviderFuture.get(), this, this);
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, ContextCompat.getMainExecutor(this));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.example.greenwatch.adapters;
|
||||
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.example.greenwatch.R;
|
||||
import com.example.greenwatch.models.Device;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class AlarmHistoryListAdapter extends RecyclerView.Adapter<AlarmHistoryListAdapter.AlarmHistoryListHolder> {
|
||||
private List<Device> alarmHistoryList = new ArrayList<>();
|
||||
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public AlarmHistoryListHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
View itemView = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.alarm_history_item, parent, false);
|
||||
return new AlarmHistoryListHolder(itemView);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull AlarmHistoryListHolder holder, int position) {
|
||||
Device currentDevice = alarmHistoryList.get(position);
|
||||
holder.textViewSensorType.setText(holder.itemView.getContext().getString(R.string.sensor_type_placeholder, currentDevice.getSensorType()));
|
||||
holder.textViewDeviceID.setText(holder.itemView.getContext().getString(R.string.deviceID_placeholder, currentDevice.getDeviceID()));
|
||||
holder.textViewTimeStamp.setText(holder.itemView.getContext().getString(R.string.sensor_time_stamp_placeholder, currentDevice.getTimeStamp()));
|
||||
holder.textViewSensorMessage.setText(holder.itemView.getContext().getString(R.string.sensor_message_placeholder, currentDevice.getSensorMassage()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return alarmHistoryList.size();
|
||||
}
|
||||
|
||||
public void setAlarmHistoryList(List<Device> alarmHistoryList) {
|
||||
this.alarmHistoryList = alarmHistoryList;
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
class AlarmHistoryListHolder extends RecyclerView.ViewHolder {
|
||||
private TextView textViewSensorType;
|
||||
private TextView textViewDeviceID;
|
||||
private TextView textViewTimeStamp;
|
||||
private TextView textViewSensorMessage;
|
||||
|
||||
public AlarmHistoryListHolder(View itemView) {
|
||||
super(itemView);
|
||||
textViewSensorType = (TextView) itemView.findViewById(R.id.tvAlarmHistoryDeviceSensorType);
|
||||
textViewDeviceID = (TextView) itemView.findViewById(R.id.tvAlarmHistoryDeviceID);
|
||||
textViewTimeStamp = (TextView) itemView.findViewById(R.id.tvAlarmHistoryDeviceTimeStamp);
|
||||
textViewSensorMessage = (TextView) itemView.findViewById(R.id.tvAlarmHistoryDeviceSensorMassage);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,21 +6,26 @@ import androidx.lifecycle.MutableLiveData;
|
||||
import com.example.greenwatch.models.Device;
|
||||
import com.example.greenwatch.communication.WiFiCommunication;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class DeviceRepository {
|
||||
|
||||
private final int maxAlarmHistoryListSize = 20;
|
||||
private final String delimiter = ", ";
|
||||
private final String SensorStatusKey = "An";
|
||||
private String localDeviceUUID;
|
||||
private static DeviceRepository deviceRepositoryInstance;
|
||||
private WiFiCommunication mWiFiCommunication;
|
||||
private MutableLiveData<List<Device>> deviceList = new MutableLiveData<>();
|
||||
private MutableLiveData<List<Device>> alarmHistoryList = new MutableLiveData<>();
|
||||
private HashMap<String, Device> connectedDevicesList = new HashMap<>();
|
||||
private HashMap<String, String> deviceIDMapper = new HashMap<>();
|
||||
private List<Device> alarmHistoryDeviceList = new ArrayList<>();
|
||||
|
||||
private DeviceRepository() {
|
||||
setLocalDeviceUUID();
|
||||
@@ -38,15 +43,42 @@ public class DeviceRepository {
|
||||
}
|
||||
|
||||
public MutableLiveData<List<Device>> getConnectedDeviceList() {
|
||||
setMutableLiveDataValue();
|
||||
setMutableLiveDataDeviceList();
|
||||
return deviceList;
|
||||
}
|
||||
|
||||
public MutableLiveData<List<Device>> getAlarmHistoryDeviceList() {
|
||||
setMutableLiveDataAlarmHistoryList();
|
||||
return alarmHistoryList;
|
||||
}
|
||||
|
||||
public void createNewDevice(String timeStamp, String deviceID, boolean sensorStatus, String sensorType, int sensorMassage){
|
||||
Device newDevice = new Device(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage);
|
||||
setDeviceIDMapper(deviceID);
|
||||
if (sensorStatus) {
|
||||
setAlarmHistoryDeviceList(newDevice);
|
||||
}
|
||||
addToConnectedDeviceList(newDevice.getDeviceID(), newDevice);
|
||||
setMutableLiveDataValue();
|
||||
setMutableLiveDataDeviceList();
|
||||
}
|
||||
|
||||
public void getNewReceivedMessage(String newMessage) {
|
||||
String[] messageString = messageStringSplitter(newMessage);
|
||||
String timeStamp = messageString[0];
|
||||
String deviceID = messageString[1];
|
||||
boolean sensorStatus = convertSensorStatus(messageString[2]);
|
||||
String sensorType = messageString[3];
|
||||
int sensorMassage = Integer.valueOf(messageString[4]);
|
||||
|
||||
if (deviceID.equals(checkDeviceID(localDeviceUUID))) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(!connectedDevicesList.containsKey(deviceID)) {
|
||||
createNewDevice(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage);
|
||||
}
|
||||
else {
|
||||
updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);
|
||||
}
|
||||
}
|
||||
|
||||
private String messageStringBuilder(String deviceID) {
|
||||
@@ -62,7 +94,6 @@ public class DeviceRepository {
|
||||
.append(device.getSensorType())
|
||||
.append(delimiter)
|
||||
.append(device.getSensorMassage());
|
||||
return message.toString();
|
||||
}
|
||||
else {
|
||||
message.append("")
|
||||
@@ -74,8 +105,8 @@ public class DeviceRepository {
|
||||
.append("")
|
||||
.append(delimiter)
|
||||
.append("");
|
||||
return message.toString();
|
||||
}
|
||||
return message.toString();
|
||||
}
|
||||
|
||||
public String getLocalDeviceUUID() {
|
||||
@@ -87,11 +118,14 @@ public class DeviceRepository {
|
||||
Device device = connectedDevicesList.get(checkedDeviceID);
|
||||
if(device != null) {
|
||||
device.setTimeStamp(timeStamp);
|
||||
device.setSensorStatus(sensorStatus);
|
||||
device.setSensorType(sensorType);
|
||||
device.setSensorMassage(sensorMassage);
|
||||
if (!device.getSensorStatus() && sensorStatus) {
|
||||
setAlarmHistoryDeviceList(device);
|
||||
}
|
||||
device.setSensorStatus(sensorStatus);
|
||||
addToConnectedDeviceList(checkedDeviceID, device);
|
||||
setMutableLiveDataValue();
|
||||
setMutableLiveDataDeviceList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,7 +135,7 @@ public class DeviceRepository {
|
||||
if(device != null) {
|
||||
device.setTimeStamp(timeStamp);
|
||||
addToConnectedDeviceList(checkedDeviceID, device);
|
||||
setMutableLiveDataValue();
|
||||
setMutableLiveDataDeviceList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,7 +156,7 @@ public class DeviceRepository {
|
||||
setDeviceIDMapper(newDeviceID);
|
||||
connectedDevicesList.remove(checkedDeviceID);
|
||||
addToConnectedDeviceList(newDeviceID, device);
|
||||
setMutableLiveDataValue();
|
||||
setMutableLiveDataDeviceList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,9 +173,13 @@ public class DeviceRepository {
|
||||
String checkedDeviceID = checkDeviceID(deviceID);
|
||||
Device device = connectedDevicesList.get(checkedDeviceID);
|
||||
if(device != null) {
|
||||
if (!device.getSensorStatus() && sensorStatus) {
|
||||
setAlarmHistoryDeviceList(device);
|
||||
}
|
||||
device.setSensorStatus(sensorStatus);
|
||||
addToConnectedDeviceList(checkedDeviceID, device);
|
||||
setMutableLiveDataValue();
|
||||
setMutableLiveDataDeviceList();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,7 +198,7 @@ public class DeviceRepository {
|
||||
if(device != null) {
|
||||
device.setSensorType(sensorType);
|
||||
addToConnectedDeviceList(checkedDeviceID, device);
|
||||
setMutableLiveDataValue();
|
||||
setMutableLiveDataDeviceList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,7 +217,7 @@ public class DeviceRepository {
|
||||
if(device != null) {
|
||||
device.setSensorMassage(sensorMessage);
|
||||
addToConnectedDeviceList(checkedDeviceID, device);
|
||||
setMutableLiveDataValue();
|
||||
setMutableLiveDataDeviceList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,11 +230,21 @@ public class DeviceRepository {
|
||||
return 0;
|
||||
}
|
||||
|
||||
private void setMutableLiveDataValue() {
|
||||
public String getSystemTimeStamp() {
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||
Date date = new Date(System.currentTimeMillis());
|
||||
return formatter.format(date);
|
||||
}
|
||||
|
||||
private void setMutableLiveDataDeviceList() {
|
||||
List<Device> list = new ArrayList<>(connectedDevicesList.values());
|
||||
deviceList.setValue(list);
|
||||
}
|
||||
|
||||
private void setMutableLiveDataAlarmHistoryList() {
|
||||
alarmHistoryList.setValue(alarmHistoryDeviceList);
|
||||
}
|
||||
|
||||
private void addToConnectedDeviceList(String key, Device device) {
|
||||
connectedDevicesList.put(key, device);
|
||||
if (key.equals(checkDeviceID(localDeviceUUID))) {
|
||||
@@ -223,27 +271,16 @@ public class DeviceRepository {
|
||||
return message.split(delimiter);
|
||||
}
|
||||
|
||||
public void getNewReceivedMessage(String newMessage) {
|
||||
String[] messageString = messageStringSplitter(newMessage);
|
||||
String timeStamp = messageString[0];
|
||||
String deviceID = messageString[1];
|
||||
boolean sensorStatus = convertSensorStatus(messageString[2]);
|
||||
String sensorType = messageString[3];
|
||||
int sensorMassage = Integer.valueOf(messageString[4]);
|
||||
|
||||
if (deviceID.equals(checkDeviceID(localDeviceUUID))) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(!connectedDevicesList.containsKey(deviceID)) {
|
||||
createNewDevice(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage);
|
||||
}
|
||||
else {
|
||||
updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean convertSensorStatus(String status) {
|
||||
return status.equals(SensorStatusKey);
|
||||
}
|
||||
|
||||
private void setAlarmHistoryDeviceList(Device device) {
|
||||
if (alarmHistoryDeviceList.size() == maxAlarmHistoryListSize) {
|
||||
alarmHistoryDeviceList.remove(alarmHistoryDeviceList.size() -1);
|
||||
}
|
||||
Device alarmHistoryDevice = new Device(device.getTimeStamp(), device.getDeviceID(), device.getSensorStatus(), device.getSensorType(), device.getSensorMassage());
|
||||
alarmHistoryDeviceList.add(0, alarmHistoryDevice);
|
||||
setMutableLiveDataAlarmHistoryList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
package com.example.greenwatch.sensors;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.ImageFormat;
|
||||
import android.media.Image;
|
||||
import android.os.Handler;
|
||||
import android.util.Size;
|
||||
|
||||
import androidx.camera.core.CameraSelector;
|
||||
import androidx.camera.core.ImageAnalysis;
|
||||
import androidx.camera.lifecycle.ProcessCameraProvider;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class CameraSensor {
|
||||
private final MutableLiveData<Boolean> mVideoAlarmDetected = new MutableLiveData<>();
|
||||
private static CameraSensor cameraSensorInstance;
|
||||
private boolean videoAlarmDetected;
|
||||
private boolean isMotionDetected;
|
||||
private ByteBuffer previousBuffer;
|
||||
private int previousWidth;
|
||||
private int previousHeight;
|
||||
private final int threshold = 50;
|
||||
private int startX;
|
||||
private int startY;
|
||||
private int endX;
|
||||
private int endY;
|
||||
|
||||
private static final long ALARM_RESET_DELAY = 5000;
|
||||
private Runnable alarmResetRunnable;
|
||||
private final Handler alarmResetHandler = new Handler();
|
||||
|
||||
private CameraSensor() {
|
||||
videoAlarmDetected = false;
|
||||
}
|
||||
|
||||
public static synchronized CameraSensor getInstance() {
|
||||
if (cameraSensorInstance == null){
|
||||
cameraSensorInstance = new CameraSensor();
|
||||
}
|
||||
return cameraSensorInstance;
|
||||
}
|
||||
|
||||
public MutableLiveData<Boolean> getVideoAlarmDetectedValue() {
|
||||
setMutableLiveDataVideoMovementDetected();
|
||||
return mVideoAlarmDetected;
|
||||
}
|
||||
|
||||
private void setMutableLiveDataVideoMovementDetected() {
|
||||
mVideoAlarmDetected.setValue(videoAlarmDetected);
|
||||
}
|
||||
|
||||
public void bindImageAnalysis(ProcessCameraProvider cameraProvider, LifecycleOwner lifecycleOwner, Context context) {
|
||||
ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
|
||||
builder.setTargetResolution(new Size(640, 480));
|
||||
builder.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST);
|
||||
ImageAnalysis imageAnalysis = builder.build();
|
||||
imageAnalysis.setAnalyzer(
|
||||
ContextCompat.getMainExecutor(context),
|
||||
imageProxy -> {
|
||||
|
||||
int imageFormat = imageProxy.getFormat();
|
||||
|
||||
if (imageFormat == ImageFormat.YUV_420_888) {
|
||||
|
||||
Image currentImage = imageProxy.getImage();
|
||||
|
||||
if (previousHeight != 0) {
|
||||
assert currentImage != null;
|
||||
isMotionDetected = compareFrames(currentImage);
|
||||
}
|
||||
|
||||
assert currentImage != null;
|
||||
Image.Plane[] planes = currentImage.getPlanes();
|
||||
ByteBuffer buffer = planes[0].getBuffer().duplicate();
|
||||
previousBuffer = ByteBuffer.allocateDirect(buffer.remaining());
|
||||
previousBuffer.put(buffer);
|
||||
previousWidth = currentImage.getWidth();
|
||||
previousHeight = currentImage.getHeight();
|
||||
|
||||
currentImage.close();
|
||||
|
||||
if (isMotionDetected) {
|
||||
|
||||
videoAlarmDetected = true;
|
||||
|
||||
if(alarmResetRunnable != null) {
|
||||
alarmResetHandler.removeCallbacks(alarmResetRunnable);
|
||||
}
|
||||
|
||||
alarmResetRunnable = this::resetAlarmStatus;
|
||||
alarmResetHandler.postDelayed(alarmResetRunnable, ALARM_RESET_DELAY);
|
||||
}
|
||||
|
||||
checkAlarmCondition();
|
||||
|
||||
}
|
||||
|
||||
imageProxy.close();
|
||||
});
|
||||
|
||||
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
|
||||
|
||||
cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, imageAnalysis);
|
||||
}
|
||||
|
||||
private boolean compareFrames(Image currentImage) {
|
||||
Image.Plane[] planes = currentImage.getPlanes();
|
||||
ByteBuffer currentBuffer = planes[0].getBuffer();
|
||||
int currentWidth = currentImage.getWidth();
|
||||
int currentHeight = currentImage.getHeight();
|
||||
int yRowStride = planes[0].getRowStride();
|
||||
int yPixelStride = planes[0].getPixelStride();
|
||||
|
||||
if (previousWidth != currentWidth || previousHeight != currentHeight) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int blockSize = kleinstesQuadrat(previousHeight, previousWidth) / 8;
|
||||
|
||||
int numBlocksX = currentWidth / blockSize;
|
||||
int numBlocksY = currentHeight / blockSize;
|
||||
|
||||
for (int blockY = 0; blockY < numBlocksY; blockY++) {
|
||||
for (int blockX = 0; blockX < numBlocksX; blockX++) {
|
||||
startX = blockX * blockSize;
|
||||
startY = blockY * blockSize;
|
||||
endX = startX + blockSize;
|
||||
endY = startY + blockSize;
|
||||
|
||||
float currentLuminance = berechneMittlereLuminanz(currentBuffer, yRowStride, yPixelStride);
|
||||
float previousLuminance = berechneMittlereLuminanz(previousBuffer, yRowStride, yPixelStride);
|
||||
|
||||
int pixelDifference = Math.abs((int) previousLuminance - (int) currentLuminance);
|
||||
|
||||
if (pixelDifference > threshold) {
|
||||
System.out.println(pixelDifference);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private float berechneMittlereLuminanz(ByteBuffer buffer, int rowStride, int pixelStride) {
|
||||
int sumLuminance = 0;
|
||||
int countPixels = 0;
|
||||
|
||||
for (int row = startY; row < endY; row++) {
|
||||
for (int col = startX; col < endX; col++) {
|
||||
int bufferIndex = row * rowStride + col * pixelStride;
|
||||
int currentPixel = buffer.get(bufferIndex) & 0xFF;
|
||||
|
||||
sumLuminance += currentPixel;
|
||||
countPixels++;
|
||||
}
|
||||
}
|
||||
|
||||
return (float) sumLuminance / countPixels;
|
||||
}
|
||||
|
||||
private int kleinstesQuadrat(int height, int width) {
|
||||
if (height == width) {
|
||||
return height;
|
||||
} else if (height > width) {
|
||||
return kleinstesQuadrat(height - width, width);
|
||||
} else {
|
||||
return kleinstesQuadrat(height, width - height);
|
||||
}
|
||||
}
|
||||
|
||||
private void resetAlarmStatus() {
|
||||
videoAlarmDetected = false;
|
||||
alarmResetRunnable = null;
|
||||
}
|
||||
|
||||
public void checkAlarmCondition() {
|
||||
if (videoAlarmDetected && !mVideoAlarmDetected.getValue()) {
|
||||
setMutableLiveDataVideoMovementDetected();
|
||||
}
|
||||
else if (!videoAlarmDetected && mVideoAlarmDetected.getValue()){
|
||||
setMutableLiveDataVideoMovementDetected();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,10 +12,11 @@ import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class AccelerometerViewModel extends ViewModel {
|
||||
public class AccelerometerViewModel extends ViewModel implements ViewModelInterface {
|
||||
|
||||
private MutableLiveData<List<Device>> mDeviceList;
|
||||
private MutableLiveData<Boolean> mMovementDetected = new MutableLiveData<>();
|
||||
private MutableLiveData<List<Device>> mAlarmHistoryList;
|
||||
private DeviceRepository mDeviceRepository;
|
||||
private ArrayList<Float> Gesamt_be;
|
||||
int arraySize = 500;
|
||||
@@ -25,10 +26,12 @@ public class AccelerometerViewModel extends ViewModel {
|
||||
float thresholdValue;
|
||||
boolean startMeasuring;
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
if (mDeviceList == null) {
|
||||
mDeviceRepository = DeviceRepository.getInstance();
|
||||
mDeviceList = mDeviceRepository.getConnectedDeviceList();
|
||||
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList();
|
||||
}
|
||||
initGesamtBE();
|
||||
mMovementDetected.setValue(false);
|
||||
@@ -79,65 +82,82 @@ public class AccelerometerViewModel extends ViewModel {
|
||||
}
|
||||
}
|
||||
|
||||
public String getSystemTimeStamp() {
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||
Date date = new Date(System.currentTimeMillis());
|
||||
return formatter.format(date);
|
||||
}
|
||||
|
||||
public LiveData<Boolean> getMovementDetectedValue() {
|
||||
return mMovementDetected;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LiveData<List<Device>> getConnectedDeviceList() {
|
||||
return mDeviceList;
|
||||
}
|
||||
|
||||
public void setDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) {
|
||||
@Override
|
||||
public LiveData<List<Device>> getAlarmHistoryList() {
|
||||
return mAlarmHistoryList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) {
|
||||
mDeviceRepository.updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTimeStamp(String deviceID, String timeStamp) {
|
||||
mDeviceRepository.setTimeStamp(deviceID, timeStamp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTimeStamp(String deviceID) {
|
||||
return mDeviceRepository.getTimeStamp(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDeviceID(String deviceID, String newDeviceID) {
|
||||
mDeviceRepository.setDeviceID(deviceID, newDeviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDeviceID(String deviceID) {
|
||||
return mDeviceRepository.getDeviceID(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSensorStatus(String deviceID, boolean sensorStatus) {
|
||||
mDeviceRepository.setSensorStatus(deviceID, sensorStatus);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getSensorStatus(String deviceID) {
|
||||
return mDeviceRepository.getSensorStatus(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSensorType(String deviceID, String sensorType) {
|
||||
mDeviceRepository.setSensorType(deviceID, sensorType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSensorType(String deviceID) {
|
||||
return mDeviceRepository.getSensorType(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSensorMassage(String deviceID, int sensorMessage) {
|
||||
mDeviceRepository.setSensorMassage(deviceID, sensorMessage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSensorMassage(String deviceID) {
|
||||
return mDeviceRepository.getSensorMassage(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalDeviceUUID() {
|
||||
return mDeviceRepository.getLocalDeviceUUID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSystemTimeStamp() {
|
||||
return mDeviceRepository.getSystemTimeStamp();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,11 @@
|
||||
package com.example.greenwatch.viewmodels;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
@@ -10,12 +16,14 @@ import com.example.greenwatch.repositories.DeviceRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MainActivityViewModel extends ViewModel {
|
||||
public class MainActivityViewModel extends ViewModel implements ViewModelInterface {
|
||||
|
||||
private MutableLiveData<List<Device>> mDeviceList;
|
||||
private MutableLiveData<List<Device>> mAlarmHistoryList;
|
||||
private DeviceRepository mDeviceRepository;
|
||||
private static final int RECHTEANFORDERUNG_KAMERA = 10;
|
||||
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
WiFiCommunication mWiFiCommunication;
|
||||
if(mDeviceList != null) {
|
||||
@@ -26,59 +34,91 @@ public class MainActivityViewModel extends ViewModel {
|
||||
mWiFiCommunication = WiFiCommunication.getInstance();
|
||||
mDeviceRepository.setWiFiCommunication(mWiFiCommunication);
|
||||
mWiFiCommunication.setDeviceRepository(mDeviceRepository);
|
||||
mDeviceRepository.createNewDevice("", mDeviceRepository.getLocalDeviceUUID(), false, "No Sensor selected", 0);
|
||||
mDeviceRepository.createNewDevice(mDeviceRepository.getSystemTimeStamp(), mDeviceRepository.getLocalDeviceUUID(), false, "No Sensor selected", 0);
|
||||
mDeviceList = mDeviceRepository.getConnectedDeviceList();
|
||||
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList();
|
||||
}
|
||||
|
||||
public boolean isCameraAccessAllowed(Context context) {
|
||||
return ContextCompat.checkSelfPermission(context, android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
|
||||
}
|
||||
|
||||
public void accessRequestCamera(Activity activity) {
|
||||
ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.CAMERA}, RECHTEANFORDERUNG_KAMERA);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LiveData<List<Device>> getConnectedDeviceList() {
|
||||
return mDeviceList;
|
||||
}
|
||||
|
||||
public void setDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) {
|
||||
@Override
|
||||
public LiveData<List<Device>> getAlarmHistoryList() {
|
||||
return mAlarmHistoryList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) {
|
||||
mDeviceRepository.updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTimeStamp(String deviceID, String timeStamp) {
|
||||
mDeviceRepository.setTimeStamp(deviceID, timeStamp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTimeStamp(String deviceID) {
|
||||
return mDeviceRepository.getTimeStamp(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDeviceID(String deviceID, String newDeviceID) {
|
||||
mDeviceRepository.setDeviceID(deviceID, newDeviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDeviceID(String deviceID) {
|
||||
return mDeviceRepository.getDeviceID(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSensorStatus(String deviceID, boolean sensorStatus) {
|
||||
mDeviceRepository.setSensorStatus(deviceID, sensorStatus);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getSensorStatus(String deviceID) {
|
||||
return mDeviceRepository.getSensorStatus(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSensorType(String deviceID, String sensorType) {
|
||||
mDeviceRepository.setSensorType(deviceID, sensorType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSensorType(String deviceID) {
|
||||
return mDeviceRepository.getSensorType(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSensorMassage(String deviceID, int sensorMessage) {
|
||||
mDeviceRepository.setSensorMassage(deviceID, sensorMessage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSensorMassage(String deviceID) {
|
||||
return mDeviceRepository.getSensorMassage(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalDeviceUUID() {
|
||||
return mDeviceRepository.getLocalDeviceUUID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSystemTimeStamp() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package com.example.greenwatch.viewmodels;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.camera.lifecycle.ProcessCameraProvider;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
import com.example.greenwatch.models.Device;
|
||||
import com.example.greenwatch.repositories.DeviceRepository;
|
||||
import com.example.greenwatch.sensors.CameraSensor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class VideodetectionViewModel extends ViewModel implements ViewModelInterface {
|
||||
|
||||
private MutableLiveData<List<Device>> mDeviceList;
|
||||
private MutableLiveData<Boolean> mVideoAlarmDetected;
|
||||
private MutableLiveData<List<Device>> mAlarmHistoryList;
|
||||
private DeviceRepository mDeviceRepository;
|
||||
private CameraSensor mCameraSensor;
|
||||
|
||||
|
||||
public LiveData<Boolean> getVideoMovementDetectedValue() {
|
||||
return mVideoAlarmDetected;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
if (mDeviceRepository == null) {
|
||||
mDeviceRepository = DeviceRepository.getInstance();
|
||||
}
|
||||
if (mCameraSensor == null) {
|
||||
mCameraSensor = CameraSensor.getInstance();
|
||||
}
|
||||
if (mDeviceList == null) {
|
||||
mDeviceList = mDeviceRepository.getConnectedDeviceList();
|
||||
}
|
||||
if (mAlarmHistoryList == null) {
|
||||
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList();
|
||||
}
|
||||
if (mVideoAlarmDetected == null) {
|
||||
mVideoAlarmDetected = mCameraSensor.getVideoAlarmDetectedValue();
|
||||
}
|
||||
}
|
||||
|
||||
public void bindImageAnalysis(ProcessCameraProvider cameraProvider, LifecycleOwner lifecycleOwner, Context context) {
|
||||
mCameraSensor.bindImageAnalysis(cameraProvider, lifecycleOwner, context);
|
||||
}
|
||||
|
||||
public LiveData<Boolean> getVideoAlarmDetectedValue() {
|
||||
return mVideoAlarmDetected;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LiveData<List<Device>> getConnectedDeviceList() {
|
||||
return mDeviceList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LiveData<List<Device>> getAlarmHistoryList() {
|
||||
return mAlarmHistoryList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) {
|
||||
mDeviceRepository.updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTimeStamp(String deviceID, String timeStamp) {
|
||||
mDeviceRepository.setTimeStamp(deviceID, timeStamp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTimeStamp(String deviceID) {
|
||||
return mDeviceRepository.getTimeStamp(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDeviceID(String deviceID, String newDeviceID) {
|
||||
mDeviceRepository.setDeviceID(deviceID, newDeviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDeviceID(String deviceID) {
|
||||
return mDeviceRepository.getDeviceID(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSensorStatus(String deviceID, boolean sensorStatus) {
|
||||
mDeviceRepository.setSensorStatus(deviceID, sensorStatus);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getSensorStatus(String deviceID) {
|
||||
return mDeviceRepository.getSensorStatus(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSensorType(String deviceID, String sensorType) {
|
||||
mDeviceRepository.setSensorType(deviceID, sensorType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSensorType(String deviceID) {
|
||||
return mDeviceRepository.getSensorType(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSensorMassage(String deviceID, int sensorMessage) {
|
||||
mDeviceRepository.setSensorMassage(deviceID, sensorMessage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSensorMassage(String deviceID) {
|
||||
return mDeviceRepository.getSensorMassage(deviceID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalDeviceUUID() {
|
||||
return mDeviceRepository.getLocalDeviceUUID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSystemTimeStamp() {
|
||||
return mDeviceRepository.getSystemTimeStamp();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.example.greenwatch.viewmodels;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
|
||||
import com.example.greenwatch.models.Device;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ViewModelInterface {
|
||||
void init();
|
||||
LiveData<List<Device>> getConnectedDeviceList();
|
||||
LiveData<List<Device>> getAlarmHistoryList();
|
||||
void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage);
|
||||
void setTimeStamp(String deviceID, String timeStamp);
|
||||
String getTimeStamp(String deviceID);
|
||||
void setDeviceID(String deviceID, String newDeviceID);
|
||||
String getDeviceID(String deviceID);
|
||||
void setSensorStatus(String deviceID, boolean sensorStatus);
|
||||
boolean getSensorStatus(String deviceID);
|
||||
void setSensorType(String deviceID, String sensorType);
|
||||
String getSensorType(String deviceID);
|
||||
void setSensorMassage(String deviceID, int sensorMessage);
|
||||
int getSensorMassage(String deviceID);
|
||||
String getLocalDeviceUUID();
|
||||
String getSystemTimeStamp();
|
||||
}
|
||||
@@ -36,4 +36,27 @@
|
||||
android:text="Back to MainActivity">
|
||||
</Button>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/deviceListRecyclerView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:listitem="@layout/device_item"
|
||||
android:layout_weight="1">
|
||||
</androidx.recyclerview.widget.RecyclerView>
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/alarmHistoryListRecyclerView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:listitem="@layout/alarm_history_item"
|
||||
android:layout_weight="1">
|
||||
</androidx.recyclerview.widget.RecyclerView>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
@@ -8,19 +8,19 @@
|
||||
android:padding="10dp"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvStatusmessage"
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/alarmHistoryListRecyclerView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:text="Hello World!"
|
||||
android:layout_weight="4">
|
||||
</TextView>
|
||||
android:layout_weight="3"
|
||||
tools:listitem="@layout/alarm_history_item">
|
||||
</androidx.recyclerview.widget.RecyclerView>
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/deviceListRecyclerView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="2"
|
||||
android:layout_weight="3"
|
||||
tools:listitem="@layout/device_item">
|
||||
</androidx.recyclerview.widget.RecyclerView>
|
||||
|
||||
|
||||
@@ -22,4 +22,27 @@
|
||||
android:text="Back to MainActivity">
|
||||
</Button>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/deviceListRecyclerView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:listitem="@layout/device_item"
|
||||
android:layout_weight="1">
|
||||
</androidx.recyclerview.widget.RecyclerView>
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/alarmHistoryListRecyclerView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:listitem="@layout/alarm_history_item"
|
||||
android:layout_weight="1">
|
||||
</androidx.recyclerview.widget.RecyclerView>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
@@ -0,0 +1,51 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="8dp">
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="8dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvAlarmHistoryDeviceSensorType"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/sensor_type_placeholder"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Large"
|
||||
android:maxLines="1"
|
||||
android:layout_alignParentStart="true"
|
||||
android:ellipsize="end">
|
||||
</TextView>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvAlarmHistoryDeviceID"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@id/tvAlarmHistoryDeviceSensorType"
|
||||
android:text="@string/deviceID_placeholder">
|
||||
</TextView>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvAlarmHistoryDeviceTimeStamp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@id/tvAlarmHistoryDeviceID"
|
||||
android:text="@string/sensor_time_stamp_placeholder">
|
||||
</TextView>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvAlarmHistoryDeviceSensorMassage"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@id/tvAlarmHistoryDeviceTimeStamp"
|
||||
android:text="@string/sensor_message_placeholder">
|
||||
</TextView>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
</androidx.cardview.widget.CardView>
|
||||
Reference in new issue
Block a user