From 25ceb131f2f7d3a77e87b90f3bcda1c00eab9c7e Mon Sep 17 00:00:00 2001 From: Kevin Lee Date: Sun, 14 Jun 2015 19:10:19 -0500 Subject: [PATCH 1/2] Hack to fix gap sync --- .../nightscout/android/CollectorService.java | 19 ++- .../nightscout/android/ProcessorService.java | 8 +- .../preferences/AndroidPreferences.java | 68 +++++++++ .../android/ui/MonitorFragment.java | 9 -- .../ui/NightscoutNavigationDrawer.java | 38 ++++- .../nightscout/android/upload/Uploader.java | 47 ++++++- app/src/main/res/values/pref_keys.xml | 6 + .../com/nightscout/core/drivers/DexcomG4.java | 39 ++---- .../preferences/NightscoutPreferences.java | 40 ++++++ .../core/preferences/TestPreferences.java | 132 +++++++++++++++++- .../nightscout/core/upload/BaseUploader.java | 2 + .../core/upload/RestV1Uploader.java | 11 +- 12 files changed, 354 insertions(+), 65 deletions(-) diff --git a/app/src/main/java/com/nightscout/android/CollectorService.java b/app/src/main/java/com/nightscout/android/CollectorService.java index 96ddce83..943fafb2 100755 --- a/app/src/main/java/com/nightscout/android/CollectorService.java +++ b/app/src/main/java/com/nightscout/android/CollectorService.java @@ -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); } @@ -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; } @@ -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; } @@ -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; } @@ -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); } diff --git a/app/src/main/java/com/nightscout/android/ProcessorService.java b/app/src/main/java/com/nightscout/android/ProcessorService.java index 7a111e08..49b57bca 100755 --- a/app/src/main/java/com/nightscout/android/ProcessorService.java +++ b/app/src/main/java/com/nightscout/android/ProcessorService.java @@ -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; @@ -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) @@ -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"); diff --git a/app/src/main/java/com/nightscout/android/preferences/AndroidPreferences.java b/app/src/main/java/com/nightscout/android/preferences/AndroidPreferences.java index c1ff45b5..b44750f7 100755 --- a/app/src/main/java/com/nightscout/android/preferences/AndroidPreferences.java +++ b/app/src/main/java/com/nightscout/android/preferences/AndroidPreferences.java @@ -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); diff --git a/app/src/main/java/com/nightscout/android/ui/MonitorFragment.java b/app/src/main/java/com/nightscout/android/ui/MonitorFragment.java index 469f3878..281ba196 100755 --- a/app/src/main/java/com/nightscout/android/ui/MonitorFragment.java +++ b/app/src/main/java/com/nightscout/android/ui/MonitorFragment.java @@ -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) { @@ -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 diff --git a/app/src/main/java/com/nightscout/android/ui/NightscoutNavigationDrawer.java b/app/src/main/java/com/nightscout/android/ui/NightscoutNavigationDrawer.java index db465eec..98c4fd32 100644 --- a/app/src/main/java/com/nightscout/android/ui/NightscoutNavigationDrawer.java +++ b/app/src/main/java/com/nightscout/android/ui/NightscoutNavigationDrawer.java @@ -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; @@ -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); @@ -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() { diff --git a/app/src/main/java/com/nightscout/android/upload/Uploader.java b/app/src/main/java/com/nightscout/android/upload/Uploader.java index c8eb1a68..dcda0fb1 100644 --- a/app/src/main/java/com/nightscout/android/upload/Uploader.java +++ b/app/src/main/java/com/nightscout/android/upload/Uploader.java @@ -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; @@ -180,6 +181,7 @@ public boolean upload(G4Download download) { List 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 glucoseDataSets, @@ -192,10 +194,36 @@ private boolean upload(List 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 filteredGlucoseDataSet = filterRecords(glucoseDataSets, lastGlucoseDataSetUpload); + List filteredMeterRecords = filterRecords(meterRecords, lastMeterRecordsUpload); + List filteredCalRecords = filterRecords(calRecords, lastCalRecordsUpload); + List 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) { @@ -219,4 +247,19 @@ public List getUploaders() { protected boolean areAllUploadersInitialized() { return allUploadersInitalized; } + + private List filterRecords(List recordList, long lastSysTime) { + List 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; + } + } diff --git a/app/src/main/res/values/pref_keys.xml b/app/src/main/res/values/pref_keys.xml index 703ecfac..c43f6d2f 100644 --- a/app/src/main/res/values/pref_keys.xml +++ b/app/src/main/res/values/pref_keys.xml @@ -19,6 +19,12 @@ last_mqtt_sensor_time last_mqtt_cal_time last_mqtt_meter_time + last_mqtt_ins_time + last_uploader_egv_time + last_uploader_sensor_time + last_uploader_cal_time + last_uploader_meter_time + last_uploader_ins_time enable_labs root_support_enabled data_donate diff --git a/core/src/main/java/com/nightscout/core/drivers/DexcomG4.java b/core/src/main/java/com/nightscout/core/drivers/DexcomG4.java index f3aa5f53..3a480be0 100755 --- a/core/src/main/java/com/nightscout/core/drivers/DexcomG4.java +++ b/core/src/main/java/com/nightscout/core/drivers/DexcomG4.java @@ -95,16 +95,6 @@ public String getReceiverId() { public void onConnect() { super.onConnect(); log.debug("onConnect Called DexcomG4 connection"); -// ReadData readData = new ReadData(transport); -// try { -// receiverId = readData.readSerialNumber(); -// log.debug("ReceiverId: {}", receiverId); -//// transmitterId = readData.readTrasmitterId(); -//// log.debug("TransmitterId: {}", transmitterId); -// } catch (IOException e) { -// log.error("Exception {}", e); -// e.printStackTrace(); -// } } @Override @@ -152,6 +142,13 @@ protected G4Download doDownload() { } systemTime = readData.readSystemTime(); + // FIXME: readData.readBatteryLevel() seems to flake out on battery level reads via serial. + // Removing for now. + if (preferences.getDeviceType() == SupportedDevices.DEXCOM_G4) { + batLevel = 100; + } else if (preferences.getDeviceType() == SupportedDevices.DEXCOM_G4_SHARE2) { + batLevel = readData.readBatteryLevel(); + } dateTime = new DateTime(); recentRecords = readData.getRecentEGVsPages(numOfPages, systemTime, dateTime.getMillis()); @@ -161,7 +158,6 @@ protected G4Download doDownload() { boolean hasSensorData = (lastEgvRecord.getRawSystemTimeSeconds() > preferences.getLastEgvSysTime()); preferences.setLastEgvSysTime(lastEgvRecord.getRawSystemTimeSeconds()); - UIDownload uiDownload = new UIDownload(); uiDownload.download = downloadBuilder.sgv(cookieMonsterG4SGVs) .download_timestamp(dateTime.toString()) @@ -170,7 +166,7 @@ protected G4Download doDownload() { bus.post(uiDownload); if ((preferences.isRawEnabled() && hasSensorData) || (preferences.isRawEnabled() && lastSensorRecords == null)) { - sensorRecords = readData.getRecentSensorRecords(numOfPages, systemTime, dateTime.getMillis()); + sensorRecords = readData.getRecentSensorRecords(numOfPages * 2, systemTime, dateTime.getMillis()); lastSensorRecords = sensorRecords; } else { sensorRecords = lastSensorRecords; @@ -184,8 +180,14 @@ protected G4Download doDownload() { .download_timestamp(dateTime.toString()) .download_status(status) .receiver_id(receiverId) + .receiver_battery(batLevel) .transmitter_id(transmitterId) .build(); + // FIXME - hack put in place to get data to the UI as soon as possible. + // Problem was it would take 1+ minutes for BLE to respond with all datasets + // enabled. This gets the data to the user as quickly as possible but + // spreads the bus posts across multiple classes. This should be managed by + // the collector service and not the download implementation. bus.post(download); } boolean hasCalData = false; @@ -194,7 +196,6 @@ protected G4Download doDownload() { if (meterRecords.size() > 0) { MeterRecord lastMeterRecord = meterRecords.get(meterRecords.size() - 1); hasCalData = (lastMeterRecord.getRawSystemTimeSeconds() > preferences.getLastMeterSysTime()); -// hasCalData = true; preferences.setLastMeterSysTime(lastMeterRecord.getRawSystemTimeSeconds()); } } else { @@ -218,13 +219,6 @@ protected G4Download doDownload() { status = DownloadStatus.NO_DATA; } - // FIXME: readData.readBatteryLevel() seems to flake out on battery level reads via serial. - // Removing for now. - if (preferences.getDeviceType() == SupportedDevices.DEXCOM_G4) { - batLevel = 100; - } else if (preferences.getDeviceType() == SupportedDevices.DEXCOM_G4_SHARE2) { - batLevel = readData.readBatteryLevel(); - } // TODO pull in other exceptions once we have the analytics/acra reporters } catch (IOException e) { //TODO record this in the event log later @@ -246,9 +240,6 @@ protected G4Download doDownload() { reporter.report(EventType.DEVICE, EventSeverity.ERROR, "CRC failed " + e); } -// List cookieMonsterG4SGVs = EGVRecord.toProtobufList(recentRecords); -// List cookieMonsterG4Sensors = SensorRecord.toProtobufList(sensorRecords); - List cookieMonsterG4Cals = CalRecord.toProtobufList(calRecords); List cookieMonsterG4Meters = MeterRecord.toProtobufList(meterRecords); List cookieMonsterG4Inserts = @@ -256,7 +247,7 @@ protected G4Download doDownload() { log.debug("Number of insertion records (protobuf): {}", cookieMonsterG4Inserts.size()); -// downloadBuilder.sgv(cookieMonsterG4SGVs) +// downloadBuilder = new G4Download.Builder(); downloadBuilder.cal(cookieMonsterG4Cals) .meter(cookieMonsterG4Meters) .insert(cookieMonsterG4Inserts) diff --git a/core/src/main/java/com/nightscout/core/preferences/NightscoutPreferences.java b/core/src/main/java/com/nightscout/core/preferences/NightscoutPreferences.java index 8f6b0a73..8ebee212 100644 --- a/core/src/main/java/com/nightscout/core/preferences/NightscoutPreferences.java +++ b/core/src/main/java/com/nightscout/core/preferences/NightscoutPreferences.java @@ -100,4 +100,44 @@ public interface NightscoutPreferences { long getLastEgvSysTime(); void setLastEgvSysTime(long egvSysTime); + + public void setLastEgvMqttUpload(long timestamp); + + public void setLastSensorMqttUpload(long timestamp); + + public void setLastCalMqttUpload(long timestamp); + + public void setLastMeterMqttUpload(long timestamp); + + public void setLastInsMqttUpload(long timestamp); + + public long getLastEgvMqttUpload(); + + public long getLastSensorMqttUpload(); + + public long getLastCalMqttUpload(); + + public long getLastMeterMqttUpload(); + + public long getLastInsMqttUpload(); + + public void setLastEgvBaseUpload(long timestamp, String postfix); + + public void setLastSensorBaseUpload(long timestamp, String postfix); + + public void setLastCalBaseUpload(long timestamp, String postfix); + + public void setLastMeterBaseUpload(long timestamp, String postfix); + + public void setLastInsBaseUpload(long timestamp, String postfix); + + public long getLastEgvBaseUpload(String postfix); + + public long getLastSensorBaseUpload(String postfix); + + public long getLastCalBaseUpload(String postfix); + + public long getLastMeterBaseUpload(String postfix); + + public long getLastInsBaseUpload(String postfix); } diff --git a/core/src/main/java/com/nightscout/core/preferences/TestPreferences.java b/core/src/main/java/com/nightscout/core/preferences/TestPreferences.java index 0a48fdc9..0ee164de 100644 --- a/core/src/main/java/com/nightscout/core/preferences/TestPreferences.java +++ b/core/src/main/java/com/nightscout/core/preferences/TestPreferences.java @@ -4,6 +4,7 @@ import com.nightscout.core.model.GlucoseUnit; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; public class TestPreferences implements NightscoutPreferences { @@ -27,6 +28,8 @@ public class TestPreferences implements NightscoutPreferences { private boolean mgbUploadEnabled = false; private String btAddress = null; private String btDeviceName = null; + private HashMap> lastUpload; + private SupportedDevices deviceType = SupportedDevices.UNKNOWN; @Override public boolean isRestApiEnabled() { @@ -193,20 +196,21 @@ public void setAskedForData(boolean askedForData) { this.askedForData = askedForData; } - // TODO Implement these @Override public SupportedDevices getDeviceType() { - return SupportedDevices.UNKNOWN; + return deviceType; + } + + public void setDeviceType(SupportedDevices deviceType) { + this.deviceType = deviceType; } - // TODO Implement these @Override public void setBluetoothDevice(String btDeviceName, String btAddress) { this.btAddress = btAddress; this.btDeviceName = btDeviceName; } - // TODO Implement these @Override public String getBtAddress() { return this.btAddress; @@ -244,22 +248,136 @@ public void setInsertionUploadEnabled(boolean enabled) { @Override public long getLastMeterSysTime() { - return 0; + return getLastBaseUpload("ui", "meter"); } @Override public void setLastMeterSysTime(long meterSysTime) { - + setLastBaseUpload(meterSysTime, "ui", "meter"); } @Override public long getLastEgvSysTime() { - return 0; + return getLastBaseUpload("ui", "egv"); } @Override public void setLastEgvSysTime(long egvSysTime) { + setLastBaseUpload(egvSysTime, "ui", "egv"); + } + + @Override + public void setLastEgvMqttUpload(long timestamp) { + setLastBaseUpload(timestamp, "mqtt", "egv"); + } + + @Override + public void setLastSensorMqttUpload(long timestamp) { + setLastBaseUpload(timestamp, "mqtt", "sensor"); + } + + @Override + public void setLastCalMqttUpload(long timestamp) { + setLastBaseUpload(timestamp, "mqtt", "cal"); + } + + @Override + public void setLastMeterMqttUpload(long timestamp) { + setLastBaseUpload(timestamp, "mqtt", "meter"); + } + + @Override + public void setLastInsMqttUpload(long timestamp) { + setLastBaseUpload(timestamp, "mqtt", "ins"); + } + + @Override + public long getLastEgvMqttUpload() { + return getLastBaseUpload("mqtt", "egv"); + } + + @Override + public long getLastSensorMqttUpload() { + return getLastBaseUpload("mqtt", "sensor"); + } + + @Override + public long getLastCalMqttUpload() { + return getLastBaseUpload("mqtt", "cal"); + } + + @Override + public long getLastMeterMqttUpload() { + return getLastBaseUpload("mqtt", "meter"); + } + + @Override + public long getLastInsMqttUpload() { + return getLastBaseUpload("mqtt", "ins"); + } + + @Override + public void setLastEgvBaseUpload(long timestamp, String postfix) { + setLastBaseUpload(timestamp, postfix, "egv"); + } + + @Override + public void setLastSensorBaseUpload(long timestamp, String postfix) { + setLastBaseUpload(timestamp, postfix, "sensor"); + } + + @Override + public void setLastCalBaseUpload(long timestamp, String postfix) { + setLastBaseUpload(timestamp, postfix, "cal"); + } + + @Override + public void setLastMeterBaseUpload(long timestamp, String postfix) { + setLastBaseUpload(timestamp, postfix, "meter"); + } + + @Override + public void setLastInsBaseUpload(long timestamp, String postfix) { + setLastBaseUpload(timestamp, postfix, "ins"); + } + + @Override + public long getLastEgvBaseUpload(String postfix) { + return getLastBaseUpload(postfix, "egv"); + } + + @Override + public long getLastSensorBaseUpload(String postfix) { + return getLastBaseUpload(postfix, "sensor"); + } + + @Override + public long getLastCalBaseUpload(String postfix) { + return getLastBaseUpload(postfix, "cal"); + } + + @Override + public long getLastMeterBaseUpload(String postfix) { + return getLastBaseUpload(postfix, "meter"); + } + + @Override + public long getLastInsBaseUpload(String postfix) { + return getLastBaseUpload(postfix, "ins"); + } + + private void setLastBaseUpload(long value, String postfix, String recordType) { + HashMap record = new HashMap<>(); + record.put(recordType, value); + lastUpload.put(postfix, record); + } + private long getLastBaseUpload(String postfix, String recordType) { + if (lastUpload.containsKey(postfix) && lastUpload.get(postfix).containsKey(recordType)) { + return lastUpload.get(postfix).get(recordType); + } else { + return 0; + } } @Override diff --git a/core/src/main/java/com/nightscout/core/upload/BaseUploader.java b/core/src/main/java/com/nightscout/core/upload/BaseUploader.java index 1bc60462..79b389a6 100644 --- a/core/src/main/java/com/nightscout/core/upload/BaseUploader.java +++ b/core/src/main/java/com/nightscout/core/upload/BaseUploader.java @@ -57,7 +57,9 @@ public final boolean uploadGlucoseDataSets(List glucoseDataSets) return true; } boolean output = true; + int counter = 0; for (GlucoseDataSet glucoseDataSet : glucoseDataSets) { + counter += 1; try { output &= doUpload(glucoseDataSet); } catch (IOException e) { diff --git a/core/src/main/java/com/nightscout/core/upload/RestV1Uploader.java b/core/src/main/java/com/nightscout/core/upload/RestV1Uploader.java index bab65630..ddc26171 100755 --- a/core/src/main/java/com/nightscout/core/upload/RestV1Uploader.java +++ b/core/src/main/java/com/nightscout/core/upload/RestV1Uploader.java @@ -124,22 +124,19 @@ protected boolean doUpload(GlucoseDataSet glucoseDataSet) throws IOException { JSONObject json = toJSONObjectEgv(glucoseDataSet); log.error("Json: {}", json); if (!preferences.isRawEnabled()) { - log.error("Not enabled Json: {}", json); + log.debug("Raw not enabled. JSON: {}", json); return doPost("entries", json); } else { if (glucoseDataSet.areRecordsMatched()) { - log.error("Records matched Json: {}", json); - return doPost("entries", toJSONObjectSensor(glucoseDataSet, json)); + json = toJSONObjectSensor(glucoseDataSet, json); + log.debug("Records matched Json: {}", json); + return doPost("entries", json); } else { log.error("Records not matched Json: {}", json); boolean result = doPost("entries", json); - json = new JSONObject(); - json.put("type", "sgv"); - result &= doPost("entries", json); return result; } } -// return doPost("entries", toJSONObject(glucoseDataSet)); } catch (JSONException e) { log.error("Could not create JSON object for rest v1 glucose data set.", e); return false; From f260325e5767e21b057379ac788caf1624c5d829 Mon Sep 17 00:00:00 2001 From: Kevin Lee Date: Thu, 25 Jun 2015 21:22:54 -0500 Subject: [PATCH 2/2] Upload uploader battery on initial post to mqtt --- core/src/main/java/com/nightscout/core/drivers/DexcomG4.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/main/java/com/nightscout/core/drivers/DexcomG4.java b/core/src/main/java/com/nightscout/core/drivers/DexcomG4.java index 3a480be0..486f5c22 100755 --- a/core/src/main/java/com/nightscout/core/drivers/DexcomG4.java +++ b/core/src/main/java/com/nightscout/core/drivers/DexcomG4.java @@ -181,6 +181,7 @@ protected G4Download doDownload() { .download_status(status) .receiver_id(receiverId) .receiver_battery(batLevel) + .uploader_battery(uploaderDevice.getBatteryLevel()) .transmitter_id(transmitterId) .build(); // FIXME - hack put in place to get data to the UI as soon as possible.