Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions app/src/main/java/io/pslab/activity/LuxMeterActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
((LuxMeterFragmentData) selectedFragment).stopSensorFetching();
invalidateOptionsMenu();
Long uniqueRef = realmPreferences.getLong("uniqueCount", 0);
selectedFragment.saveDataInRealm(uniqueRef, locationPref, gpsLogger);
CustomSnackBar.showSnackBar(coordinatorLayout, getString(R.string.exp_data_saved), null, null);
SharedPreferences.Editor editor = realmPreferences.edit();
editor.putLong("uniqueCount", uniqueRef + 1);
editor.commit();
if (selectedFragment.saveDataInRealm(uniqueRef, locationPref, gpsLogger)) {
CustomSnackBar.showSnackBar(coordinatorLayout, getString(R.string.exp_data_saved), null, null);
SharedPreferences.Editor editor = realmPreferences.edit();
editor.putLong("uniqueCount", uniqueRef + 1);
editor.commit();
} else {
CustomSnackBar.showSnackBar(coordinatorLayout, getString(R.string.no_data_fetched), null, null);
}
recordData = false;
} else {
if (locationPref) {
Expand Down
62 changes: 34 additions & 28 deletions app/src/main/java/io/pslab/fragment/LuxMeterFragmentData.java
Original file line number Diff line number Diff line change
Expand Up @@ -400,40 +400,46 @@ public void stopSensorFetching() {
}
}

public void saveDataInRealm(Long uniqueRef, boolean includeLocation, GPSLogger gpsLogger) {
realm.beginTransaction();

SensorLogged sensorLogged = realm.createObject(SensorLogged.class, uniqueRef);
sensorLogged.setSensor("Lux Meter");
sensorLogged.setDateTimeStart(startTime);
sensorLogged.setDateTimeEnd(endTime);
sensorLogged.setTimeZone(TimeZone.getDefault().getDisplayName());

if (includeLocation && gpsLogger != null) {
Location location = gpsLogger.getBestLocation();
if (location != null) {
sensorLogged.setLatitude(location.getLatitude());
sensorLogged.setLongitude(location.getLongitude());
public boolean saveDataInRealm(Long uniqueRef, boolean includeLocation, GPSLogger gpsLogger) {
boolean flag = luxRealmData.isEmpty();
if (!flag) {
realm.beginTransaction();

SensorLogged sensorLogged = realm.createObject(SensorLogged.class, uniqueRef);
sensorLogged.setSensor(getResources().getString(R.string.lux_meter));
sensorLogged.setDateTimeStart(startTime);
sensorLogged.setDateTimeEnd(endTime);
sensorLogged.setTimeZone(TimeZone.getDefault().getDisplayName());

if (includeLocation && gpsLogger != null) {
Location location = gpsLogger.getBestLocation();
if (location != null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we check location null in the 414 if block and reduce this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that but then I had to do this to prevent some null pointer exceptions.

sensorLogged.setLatitude(location.getLatitude());
sensorLogged.setLongitude(location.getLongitude());
} else {
sensorLogged.setLatitude(0.0);
sensorLogged.setLongitude(0.0);
}
gpsLogger.removeUpdate();
} else {
sensorLogged.setLatitude(0.0);
sensorLogged.setLongitude(0.0);
}
gpsLogger.removeUpdate();
} else {
sensorLogged.setLatitude(0.0);
sensorLogged.setLongitude(0.0);
}

for (int i = 0; i < luxRealmData.size(); i++) {
LuxData tempObject = luxRealmData.get(i);
tempObject.setId(i);
tempObject.setForeignKey(uniqueRef);
realm.copyToRealm(tempObject);
Log.i("dataResult", String.valueOf(tempObject.getLux()));
for (int i = 0; i < luxRealmData.size(); i++) {
LuxData tempObject = luxRealmData.get(i);
tempObject.setId(i);
tempObject.setForeignKey(uniqueRef);
realm.copyToRealm(tempObject);
Log.i("dataResult", String.valueOf(tempObject.getLux()));
}
realm.copyToRealm(sensorLogged);
realm.commitTransaction();
luxRealmData.clear();
return true;
} else {
return false;
}
realm.copyToRealm(sensorLogged);
realm.commitTransaction();
luxRealmData.clear();
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/io/pslab/others/GPSLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
public class GPSLogger {

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
private static final int UPDATE_INTERVAL_IN_MILLISECONDS = 1000;
private static final int UPDATE_INTERVAL_IN_MILLISECONDS = 400;
private static final int MIN_DISTANCE_CHANGE_FOR_UPDATES = 1;
private LocationManager locationManager;
private Context context;
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1036,5 +1036,6 @@
GND. GND is meant for Ground and any of the PSLab device GND pins can be used since they are common.\n\n
\u2022 Select sensor by going to the Configure tab from the bottom navigation bar and choose BHT-1750 in the drop down menu under Select Sensor.\n</string>
<string name="lux_meter_settings">Lux Meter Settings</string>
<string name="no_data_fetched">No Data Fetched</string>

</resources>