Skip to content

Commit

Permalink
Merge pull request #188 from nightscout/kl.gapsyncfix
Browse files Browse the repository at this point in the history
Re-introduce gapsync with a hack
  • Loading branch information
ktind committed Jul 3, 2015
2 parents 19a8894 + f260325 commit 2ced199
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 65 deletions.
19 changes: 8 additions & 11 deletions app/src/main/java/com/nightscout/android/CollectorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,13 @@ public void onDestroy() {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Starting service");
// if (device == null) {
// Log.d(TAG, "Device is null!");
// ACRA.getErrorReporter().handleException(null);
// return START_STICKY;
// }
if (intent == null) {
Log.d(TAG, "Intent is null!");
// ACRA.getErrorReporter().handleException(null);
return START_STICKY;
}
// if (device.isConnected()) {
int numOfPages = intent.getIntExtra(NUM_PAGES, 2);
int syncType = intent.getStringExtra(SYNC_TYPE).equals(STD_SYNC) ? 0 : 1;
new AsyncDownload().execute(numOfPages, syncType);
// }
int numOfPages = intent.getIntExtra(NUM_PAGES, 2);
int syncType = intent.getStringExtra(SYNC_TYPE).equals(STD_SYNC) ? 0 : 1;
new AsyncDownload().execute(numOfPages, syncType);
return super.onStartCommand(intent, flags, startId);
}

Expand All @@ -219,6 +211,7 @@ protected G4Download doInBackground(Integer... params) {
Log.i(TAG, "DEXCOM_G4 was opened for download");
} catch (IOException e) {
Log.e(TAG, "Unable to open DEXCOM_G4, will keep trying", e);
Log.w(TAG, "Next poll setting to default #1");
setNextPoll(nextUploadTime);
return null;
}
Expand All @@ -229,6 +222,7 @@ protected G4Download doInBackground(Integer... params) {
Log.i(TAG, "Device was opened for download");
} catch (IOException e) {
Log.e(TAG, "Unable to open device, will keep trying", e);
Log.w(TAG, "Next poll setting to default #2");
setNextPoll(nextUploadTime);
return null;
}
Expand All @@ -248,6 +242,7 @@ protected G4Download doInBackground(Integer... params) {
download = (G4Download) device.download();
if (download == null || download.download_timestamp == null || download.receiver_system_time_sec == null) {
Log.e(TAG, "Bad download, will try again");
Log.w(TAG, "Next poll setting to default #3");
setNextPoll(nextUploadTime);
return null;
}
Expand Down Expand Up @@ -329,7 +324,9 @@ protected G4Download doInBackground(Integer... params) {
long refTime = DateTime.parse(download.download_timestamp).getMillis();
EGVRecord lastEgvRecord = new EGVRecord(download.sgv.get(download.sgv.size() - 1), download.receiver_system_time_sec, refTime);
nextUploadTime = Duration.standardSeconds(Minutes.minutes(5).toStandardSeconds().getSeconds() - ((rcvrTime - lastEgvRecord.getRawSystemTimeSeconds()) % Minutes.minutes(5).toStandardSeconds().getSeconds())).getMillis();
Log.w(TAG, "Actually set next poll");
}
Log.w(TAG, "Next poll setting to a value #1");
setNextPoll(nextUploadTime);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ public void incomingData(G4Download download) {
// TODO - Eventually collapse all of these to a single loop to process the download.
// Requires a single interface for everything to determine how to process a download.
boolean uploadSuccess = false;
if (uploader != null) {
uploadSuccess = uploader.upload(download, 1);
if (uploader != null && uploader.getUploaders().size() > 0) {
uploadSuccess = uploader.upload(download);
}
if (download.sgv.size() <= 0) {
return;
Expand All @@ -261,7 +261,7 @@ public void incomingData(G4Download download) {
.sgv(filterRecords(download.sgv, SensorGlucoseValueEntry.class, preferences.getLastEgvMqttUpload()))
.cal(filterRecords(download.cal, CalibrationEntry.class, preferences.getLastCalMqttUpload()))
.meter(filterRecords(download.meter, MeterEntry.class, preferences.getLastMeterMqttUpload()))
.insert(filterRecords(download.insert, InsertionEntry.class, preferences.getLastRecordTime("ins", "mqtt")))
.insert(filterRecords(download.insert, InsertionEntry.class, preferences.getLastInsMqttUpload()))
.receiver_battery(download.receiver_battery)
.receiver_id(download.receiver_id)
.receiver_system_time_sec(download.receiver_system_time_sec)
Expand Down Expand Up @@ -298,7 +298,7 @@ public void incomingData(G4Download download) {
}
if (filteredDownload.insert.size() > 0) {
Log.d(TAG, "Publishing " + filteredDownload.insert.size() + " insert records");
preferences.setLastRecordTime("ins", "mqtt", filteredDownload.cal.get(filteredDownload.insert.size() - 1).sys_timestamp_sec);
preferences.setLastInsMqttUpload(filteredDownload.cal.get(filteredDownload.insert.size() - 1).sys_timestamp_sec);
}
} else {
reporter.report(EventType.UPLOADER, EventSeverity.ERROR, "Expected MQTT to be connected but it is not");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,38 +247,106 @@ public void setRootEnabled(boolean enabled) {
preferences.edit().putBoolean(context.getString(R.string.root_enable), enabled).apply();
}

@Override
public void setLastEgvMqttUpload(long timestamp) {
preferences.edit().putLong(context.getString(R.string.last_mqtt_egv_time), timestamp).apply();
}

@Override
public void setLastSensorMqttUpload(long timestamp) {
preferences.edit().putLong(context.getString(R.string.last_mqtt_sensor_time), timestamp).apply();
}

@Override
public void setLastCalMqttUpload(long timestamp) {
preferences.edit().putLong(context.getString(R.string.last_mqtt_cal_time), timestamp).apply();
}

@Override
public void setLastMeterMqttUpload(long timestamp) {
preferences.edit().putLong(context.getString(R.string.last_mqtt_meter_time), timestamp).apply();
}

@Override
public void setLastInsMqttUpload(long timestamp) {
preferences.edit().putLong(context.getString(R.string.last_mqtt_ins_time), timestamp).apply();
}

@Override
public long getLastEgvMqttUpload() {
return preferences.getLong(context.getString(R.string.last_mqtt_egv_time), 0);
}

@Override
public long getLastSensorMqttUpload() {
return preferences.getLong(context.getString(R.string.last_mqtt_sensor_time), 0);
}

@Override
public long getLastCalMqttUpload() {
return preferences.getLong(context.getString(R.string.last_mqtt_cal_time), 0);
}

@Override
public long getLastMeterMqttUpload() {
return preferences.getLong(context.getString(R.string.last_mqtt_meter_time), 0);
}

@Override
public long getLastInsMqttUpload() {
return preferences.getLong(context.getString(R.string.last_mqtt_ins_time), 0);
}

@Override
public void setLastEgvBaseUpload(long timestamp, String postfix) {
preferences.edit().putLong(context.getString(R.string.last_uploader_egv_time_prefix) + postfix, timestamp).apply();
}

@Override
public void setLastSensorBaseUpload(long timestamp, String postfix) {
preferences.edit().putLong(context.getString(R.string.last_uploader_sensor_time_prefix) + postfix, timestamp).apply();
}

@Override
public void setLastCalBaseUpload(long timestamp, String postfix) {
preferences.edit().putLong(context.getString(R.string.last_uploader_cal_time_prefix) + postfix, timestamp).apply();
}

@Override
public void setLastMeterBaseUpload(long timestamp, String postfix) {
preferences.edit().putLong(context.getString(R.string.last_uploader_meter_time_prefix) + postfix, timestamp).apply();
}

@Override
public void setLastInsBaseUpload(long timestamp, String postfix) {
preferences.edit().putLong(context.getString(R.string.last_uploader_ins_time_prefix) + postfix, timestamp).apply();
}

@Override
public long getLastEgvBaseUpload(String postfix) {
return preferences.getLong(context.getString(R.string.last_uploader_egv_time_prefix) + postfix, 0);
}

@Override
public long getLastSensorBaseUpload(String postfix) {
return preferences.getLong(context.getString(R.string.last_uploader_sensor_time_prefix) + postfix, 0);
}

@Override
public long getLastCalBaseUpload(String postfix) {
return preferences.getLong(context.getString(R.string.last_uploader_cal_time_prefix) + postfix, 0);
}

@Override
public long getLastMeterBaseUpload(String postfix) {
return preferences.getLong(context.getString(R.string.last_uploader_meter_time_prefix) + postfix, 0);
}

@Override
public long getLastInsBaseUpload(String postfix) {
return preferences.getLong(context.getString(R.string.last_uploader_ins_time_prefix) + postfix, 0);
}

public long getLastRecordTime(String recType, String uploadType) {
String key = "last_" + uploadType + "_" + recType + "_time";
return preferences.getLong(key, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ private void refreshUnits() {
private Runnable updateProgress = new Runnable() {
@Override
public void run() {
log.debug("Updating progress bar");
Intent intent = new Intent(getActivity(), CollectorService.class);
getActivity().bindService(intent, mCollectorConnection, Context.BIND_AUTO_CREATE);
if (mBound) {
Expand Down Expand Up @@ -282,14 +281,6 @@ public void onSaveInstanceState(Bundle outState) {
mWebView.saveState(outState);
outState.putString("saveTextSGV", mTextSGV.getText().toString());
outState.putString("saveTextTimestamp", mTextTimestamp.getText().toString());
if (receiverButton.getTag() != null) {
log.info("Saving device state");
outState.putInt("deviceState", (int) receiverButton.getTag());
}
if (uploadButton.getTag() != null) {
log.info("Saving sync state");
outState.putInt("syncState", (int) uploadButton.getTag());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.nightscout.core.events.EventSeverity;
import com.nightscout.core.events.EventType;

import java.net.URI;

import javax.inject.Inject;

import it.neokree.materialnavigationdrawer.MaterialNavigationDrawer;
Expand Down Expand Up @@ -78,7 +80,7 @@ public void onClick(MaterialSection materialSection) {
MaterialSection gapSync = newSection("Gap sync", new MaterialSectionListener() {
@Override
public void onClick(MaterialSection materialSection) {
Log.d(TAG, "Sync requested");
setupGapSync();
Intent syncIntent = new Intent(getApplicationContext(), CollectorService.class);
syncIntent.setAction(CollectorService.ACTION_POLL);
syncIntent.putExtra(CollectorService.NUM_PAGES, 20);
Expand Down Expand Up @@ -133,6 +135,40 @@ public void onClick(MaterialSection materialSection) {

}

// FIXME - Hack to enable full gap sync
// Sets the last upload time for each counter to 0 so that everything is uploaded
private void setupGapSync() {
preferences.setLastEgvSysTime(0);
preferences.setLastMeterSysTime(0);
if (preferences.isMqttEnabled()) {
preferences.setLastEgvMqttUpload(0);
preferences.setLastMeterMqttUpload(0);
preferences.setLastSensorMqttUpload(0);
preferences.setLastCalMqttUpload(0);
preferences.setLastInsMqttUpload(0);
}
// Note: A service on a different port could potentially write to a different
// database but we're only going to treat each each unique host as a different endpoint.
if (preferences.isRestApiEnabled()) {
for (String endPoint : preferences.getRestApiBaseUris()) {
URI uri = URI.create(endPoint);
String id = uri.getHost();
preferences.setLastEgvBaseUpload(0, id);
preferences.setLastMeterBaseUpload(0, id);
preferences.setLastSensorBaseUpload(0, id);
preferences.setLastCalBaseUpload(0, id);
preferences.setLastInsBaseUpload(0, id);
}
}
if (preferences.isMongoUploadEnabled()) {
preferences.setLastEgvBaseUpload(0, "MongoDB");
preferences.setLastMeterBaseUpload(0, "MongoDB");
preferences.setLastSensorBaseUpload(0, "MongoDB");
preferences.setLastCalBaseUpload(0, "MongoDB");
preferences.setLastInsBaseUpload(0, "MongoDB");
}
}


@Override
protected void onStop() {
Expand Down
47 changes: 45 additions & 2 deletions app/src/main/java/com/nightscout/android/upload/Uploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.nightscout.android.events.AndroidEventReporter;
import com.nightscout.core.dexcom.Utils;
import com.nightscout.core.dexcom.records.CalRecord;
import com.nightscout.core.dexcom.records.GenericTimestampRecord;
import com.nightscout.core.dexcom.records.GlucoseDataSet;
import com.nightscout.core.dexcom.records.InsertionRecord;
import com.nightscout.core.dexcom.records.MeterRecord;
Expand Down Expand Up @@ -180,6 +181,7 @@ public boolean upload(G4Download download) {
List<InsertionRecord> insertionRecords = asRecordList(download.insert, InsertionRecord.class, download.receiver_system_time_sec, refTime);

return upload(glucoseDataSets, meterRecords, calRecords, insertionRecords, download.receiver_battery);
// return upload(glucoseDataSets, meterRecords, calRecords, insertionRecords, 0);
}

private boolean upload(List<GlucoseDataSet> glucoseDataSets,
Expand All @@ -192,10 +194,36 @@ private boolean upload(List<GlucoseDataSet> glucoseDataSets,
boolean successful = true;
for (BaseUploader uploader : uploaders) {
try {
successful = uploader.uploadRecords(glucoseDataSets, meterRecords, calRecords, insertionRecords, deviceStatus, rcvrBat);
String id = uploader.getIdentifier();
long lastGlucoseDataSetUpload = preferences.getLastEgvBaseUpload(id);
long lastMeterRecordsUpload = preferences.getLastMeterBaseUpload(id);
long lastCalRecordsUpload = preferences.getLastCalBaseUpload(id);
long lastInsRecordsUpload = preferences.getLastInsBaseUpload(id);
List<GlucoseDataSet> filteredGlucoseDataSet = filterRecords(glucoseDataSets, lastGlucoseDataSetUpload);
List<MeterRecord> filteredMeterRecords = filterRecords(meterRecords, lastMeterRecordsUpload);
List<CalRecord> filteredCalRecords = filterRecords(calRecords, lastCalRecordsUpload);
List<InsertionRecord> filteredInsRecords = filterRecords(insertionRecords, lastInsRecordsUpload);
successful = uploader.uploadRecords(filteredGlucoseDataSet, filteredMeterRecords, filteredCalRecords, filteredInsRecords, deviceStatus, rcvrBat);
if (successful) {
if (filteredGlucoseDataSet.size() > 0) {
log.debug("Uploaded {} merged records (EGV and Sensor)", filteredGlucoseDataSet.size());
preferences.setLastEgvBaseUpload(filteredGlucoseDataSet.get(filteredGlucoseDataSet.size() - 1).getRawSysemTimeEgv(), id);
}
if (filteredMeterRecords.size() > 0) {
log.debug("Uploaded {} meter records", filteredMeterRecords.size());
preferences.setLastMeterBaseUpload(filteredMeterRecords.get(filteredMeterRecords.size() - 1).getRawSystemTimeSeconds(), id);
}
if (filteredCalRecords.size() > 0) {
log.debug("Uploaded {} calibration records", filteredCalRecords.size());
preferences.setLastCalBaseUpload(filteredCalRecords.get(filteredCalRecords.size() - 1).getRawSystemTimeSeconds(), id);
}
if (filteredInsRecords.size() > 0) {
log.debug("Uploaded {} insertion records", filteredInsRecords.size());
preferences.setLastInsBaseUpload(filteredInsRecords.get(filteredInsRecords.size() - 1).getRawSystemTimeSeconds(), id);
}
}
allSuccessful &= successful;
} catch (Exception e) {
// Bad practice but need to capture any uploader exceptions here..
allSuccessful &= false;
}
if (successful) {
Expand All @@ -219,4 +247,19 @@ public List<BaseUploader> getUploaders() {
protected boolean areAllUploadersInitialized() {
return allUploadersInitalized;
}

private <T extends GenericTimestampRecord> List<T> filterRecords(List<? extends GenericTimestampRecord> recordList, long lastSysTime) {
List<T> results = new ArrayList<>();
for (GenericTimestampRecord record : recordList) {
log.error("Comparing record time stamp {} to last recorded {}", record.getRawSystemTimeSeconds(), lastSysTime);
if (record.getRawSystemTimeSeconds() > lastSysTime) {
log.error("Comparing: Adding record");
results.add((T) record);
} else {
log.error("Comparing: Not adding record");
}
}
return results;
}

}
6 changes: 6 additions & 0 deletions app/src/main/res/values/pref_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
<string name="last_mqtt_sensor_time" translatable="false">last_mqtt_sensor_time</string>
<string name="last_mqtt_cal_time" translatable="false">last_mqtt_cal_time</string>
<string name="last_mqtt_meter_time" translatable="false">last_mqtt_meter_time</string>
<string name="last_mqtt_ins_time" translatable="false">last_mqtt_ins_time</string>
<string name="last_uploader_egv_time_prefix" translatable="false">last_uploader_egv_time</string>
<string name="last_uploader_sensor_time_prefix" translatable="false">last_uploader_sensor_time</string>
<string name="last_uploader_cal_time_prefix" translatable="false">last_uploader_cal_time</string>
<string name="last_uploader_meter_time_prefix" translatable="false">last_uploader_meter_time</string>
<string name="last_uploader_ins_time_prefix" translatable="false">last_uploader_ins_time</string>
<string name="labs_enable" translatable="false">enable_labs</string>
<string name="root_enable" translatable="false">root_support_enabled</string>
<string name="data_donate" translatable="false">data_donate</string>
Expand Down
Loading

0 comments on commit 2ced199

Please sign in to comment.