Got event editor working and manual reporting of seizures. Fixed a few crashes from event upload so should probably check the logic because I don't think it should have crashed!

This commit is contained in:
Graham Jones
2022-03-23 21:14:23 +00:00
parent 7042891ef2
commit 883b22a634
4 changed files with 73 additions and 61 deletions

View File

@@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="uk.org.openseizuredetector"
android:versionCode="9"
android:versionCode="99"
android:versionName="4.1.0">
<!-- android:allowBackup="false" -->
<uses-permission android:name="android.permission.BLUETOOTH" />

View File

@@ -287,15 +287,7 @@ public class LogManager {
if (sdData.alarmState != 0) {
Log.i(TAG, "writeDatapointToLocalDb(): adding event to local DB");
// Write Datapoint to database
SQLStr = "INSERT INTO " + mEventsTableName
+ "(dataTime, status)"
+ " VALUES("
+ "'" + dateStr + "',"
+ sdData.alarmState
+ ")";
mOsdDb.execSQL(SQLStr);
Log.v(TAG, "writeDatapointToLocalDb(): event written to database");
createLocalEvent(dateStr, sdData.alarmState);
}
} catch (SQLException e) {
Log.e(TAG, "writeToLocalDb(): Error Writing Data: " + e.toString());
@@ -305,6 +297,20 @@ public class LogManager {
}
}
public boolean createLocalEvent(String dataTime, long status) {
// Expects dataTime to be in format: SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Log.d(TAG,"createLocalEvent() - dataTime="+dataTime+", status="+status);
// Write Datapoint to database
String SQLStr = "INSERT INTO " + mEventsTableName
+ "(dataTime, status)"
+ " VALUES("
+ "'" + dataTime + "',"
+ status
+ ")";
mOsdDb.execSQL(SQLStr);
return true;
}
/**
* Returns a json representation of locally stored event 'id'.
*
@@ -872,6 +878,7 @@ public class LogManager {
// datapointCallback is called when the upload is complete.
public void uploadNextDatapoint() {
//Log.v(TAG, "uploadNextDatapoint()");
if (mDatapointsToUploadList != null) {
if (mDatapointsToUploadList.size() > 0) {
mUploadInProgress = true;
try {
@@ -891,6 +898,9 @@ public class LogManager {
setEventToUploaded(mCurrentEventLocalId, mCurrentEventRemoteId);
finishUpload();
}
} else {
Log.w(TAG,"uploadNextDatapoint - mDatapointsToUploadList is null - I don't thin this should have happened!");
}
}
// Called by WebApiConnection when a new datapoint is created. It assumes that we have just created
@@ -898,9 +908,13 @@ public class LogManager {
// to upload the next one.
public void datapointCallback(String datapointStr) {
Log.v(TAG, "datapointCallback() dataPointId="+mCurrentDatapointId+" remote datapointID=" + datapointStr + ", mCurrentEventId=" + mCurrentEventRemoteId);
if (mDatapointsToUploadList != null) {
if (mDatapointsToUploadList.size() > 0) {
mDatapointsToUploadList.remove(0);
}
} else {
Log.w(TAG,"datapointCallback - mDatapointsToUploadList is null - I don't thin this should have happened!");
}
setDatapointToUploaded(mCurrentDatapointId, mCurrentEventRemoteId);
uploadNextDatapoint();
}

View File

@@ -17,6 +17,7 @@ import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
@@ -142,22 +143,12 @@ public class ReportSeizureActivity extends AppCompatActivity {
@Override
public void onClick(View view) {
Log.v(TAG, "onOk");
//SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr=String.format("%4d-%02d-%02d %02d:%02d:30",mYear,mMonth+1,mDay, mHour, mMinute);
Log.v(TAG, "onOk() - dateSTr="+dateStr);
mMsg = "Finding Nearest Datapoint to Date/Time "+dateStr+"...";
mLm.getNearestDatapointToDate(dateStr, (Long id) -> {
mMsg = mMsg + "\nNearest Datapoint is "+id;
Log.v(TAG, "onOK() - nearest datapoint is "+id);
if (id!=-1) {
mLm.setDatapointStatus(id,5);
mMsg = mMsg + "\nSet Datapoint to Manual Alarm Status";
mUtil.showToast(getString(R.string.createdNewEvent));
mLm.createLocalEvent(dateStr,5);
mUtil.showToast("Seizure Event Created");
finish();
} else {
mMsg = mMsg + "\n*** Datapoint not found - not doing anything ***";
mUtil.showToast(getString(R.string.DatapointNotFound));
}
});
}
};
View.OnClickListener onCancel =

View File

@@ -95,10 +95,15 @@ public class WebApiConnection {
public boolean isLoggedIn() {
FirebaseAuth auth = FirebaseAuth.getInstance();
if (auth != null) {
Log.v(TAG, "isLoggedIn(): Firebase Logged in OK");
if (auth.getCurrentUser() != null) {
//Log.v(TAG, "isLoggedIn(): Firebase Logged in OK");
return (true);
} else {
Log.v(TAG, "isLoggedIn(): Firebase not logged in");
//Log.v(TAG, "isLoggedIn(): Current user is null - Firebase not logged in");
return (false);
}
} else {
//Log.v(TAG, "isLoggedIn(): Firebase not logged in");
return (false);
}
}
@@ -205,6 +210,11 @@ public class WebApiConnection {
Log.w(TAG, "getEvents() - mDb is null - not doing anything");
return false;
}
if (!isLoggedIn()) {
Log.w(TAG, "getEvents() - not logged in - not doing anything");
return false;
}
String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
mDb.collection("Events") //.where("userId", "==", userId)
.whereEqualTo("userId", userId)
@@ -240,21 +250,6 @@ public class WebApiConnection {
return (true);
}
Map<String, Object> jsonObjectToMap(JSONObject obj) {
try {
Map<String, Object> retMap = new HashMap<String, Object>();
for (Iterator<String> it = obj.keys(); it.hasNext(); ) {
String keyStr = it.next();
Log.v(TAG, "jsonObjecToMap()- keyStr=" + keyStr + ", obj=" + obj.get(keyStr));
retMap.put(keyStr, obj.get(keyStr));
}
return (retMap);
} catch (JSONException e) {
Log.e(TAG, "jsonObjectToMap() - Error Converting JSONObject" + obj.toString() + ": " + e.toString());
return (null);
}
}
public boolean updateEvent(final JSONObject eventObj, JSONObjectCallback callback) {
String eventId;
Log.v(TAG, "updateEvent()");
@@ -272,7 +267,19 @@ public class WebApiConnection {
}
final String dataStr = eventObj.toString();
Log.v(TAG, "updateEvent - data=" + dataStr);
Map<String, Object> eventMap = jsonObjectToMap(eventObj);
Map<String, Object> eventMap = new HashMap<>();
try {
eventMap.put("dataTime", eventObj.getLong("dataTime"));
eventMap.put("osdAlarmState", eventObj.getInt("osdAlarmState"));
eventMap.put("desc", eventObj.getString("desc"));
eventMap.put("type", eventObj.getString("type"));
eventMap.put("subType", eventObj.getString("subType"));
eventMap.put("userId", eventObj.getString("userId"));
} catch (JSONException e) {
Log.e(TAG, "updateEvent(): Error data from eventObj." + e.toString());
e.printStackTrace();
return false;
}
Log.v(TAG, "updateEvent - map=" + eventMap.toString());
try {