Skip to content
Closed
Changes from 1 commit
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
Next Next commit
detect api version, if v1 use sgv field instead of bg to be compatabl…
…e to direct mongo upload, if not v1 assume legacy and send data the way project-glu expects it
  • Loading branch information
Jason Calabrese authored and bewest committed Jul 18, 2014
commit 2829daaf0ab6adf39992aca9cc900a6c098ecd2e
46 changes: 39 additions & 7 deletions src/com/nightscout/android/upload/UploadHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ protected void onPostExecute(Long result) {

private void doRESTUpload(SharedPreferences prefs, EGVRecord... records) {
try {
String baseURL = prefs.getString("API Base URL", "");
String postURL = baseURL + (baseURL.endsWith("/") ? "" : "/") + "entries";
String baseURLSetting = prefs.getString("API Base URL", "");
String baseURL = baseURLSetting + (baseURLSetting.endsWith("/") ? "" : "/");

int apiVersion = 0;
if (baseURL.endsWith("/v1/")) apiVersion = 1;

String postURL = baseURL + "entries";
Log.i(TAG, "postURL: " + postURL);

HttpParams params = new BasicHttpParams();
Expand All @@ -76,12 +81,23 @@ private void doRESTUpload(SharedPreferences prefs, EGVRecord... records) {
HttpPost post = new HttpPost(postURL);

for (EGVRecord record : records) {
Date date = DATE_FORMAT.parse(record.displayTime);
JSONObject json = new JSONObject();
json.put("device", "dexcom");
json.put("timestamp", date.getTime());
json.put("bg", Integer.parseInt(record.bGValue));
json.put("direction", record.trend);

try {
switch (apiVersion) {
case 1: // community API v1
populateV1APIEntry(json, record);
break;
case 0: //legacy API (project-glu)
//fall through
default:
populateLegacyAPIEntry(json, record);
break;
}
} catch (Exception e) {
Log.w(TAG, "Unable to populate entry, apiVersion: " + apiVersion, e);
continue;
}

String jsonString = json.toString();

Expand All @@ -104,6 +120,22 @@ private void doRESTUpload(SharedPreferences prefs, EGVRecord... records) {
}
}

private void populateV1APIEntry(JSONObject json, EGVRecord record) throws Exception {
Date date = DATE_FORMAT.parse(record.displayTime);
json.put("device", "dexcom");
json.put("timestamp", date.getTime());
json.put("sgv", Integer.parseInt(record.bGValue));
json.put("direction", record.trend);
}

private void populateLegacyAPIEntry(JSONObject json, EGVRecord record) throws Exception {
Date date = DATE_FORMAT.parse(record.displayTime);
json.put("device", "dexcom");
json.put("timestamp", date.getTime());
json.put("bg", Integer.parseInt(record.bGValue));
json.put("direction", record.trend);
}

private void doMongoUpload(SharedPreferences prefs, EGVRecord... records) {

String dbURI = prefs.getString("MongoDB URI", null);
Expand Down