Skip to content
Snippets Groups Projects
Unverified Commit 54221541 authored by Johannes Mockenhaupt's avatar Johannes Mockenhaupt
Browse files

Extract method scheduleNextPoll.

parent c0fd2dfd
Branches
Tags
No related merge requests found
...@@ -315,34 +315,38 @@ public class MedtronicCnlIntentService extends IntentService { ...@@ -315,34 +315,38 @@ public class MedtronicCnlIntentService extends IntentService {
sendToXDrip(); sendToXDrip();
uploadToNightscout(); uploadToNightscout();
// smart polling and pump-sensor poll clash detection scheduleNextPoll(timePollStarted, timeLastGoodSGV, pollInterval, df);
long lastActualPollTime = timePollStarted;
if (timeLastGoodSGV > 0) {
lastActualPollTime = timeLastGoodSGV + POLL_GRACE_PERIOD_MS + (POLL_PERIOD_MS * ((System.currentTimeMillis() - (timeLastGoodSGV + POLL_GRACE_PERIOD_MS)) / POLL_PERIOD_MS));
}
long nextActualPollTime = lastActualPollTime + POLL_PERIOD_MS;
long nextRequestedPollTime = lastActualPollTime + pollInterval;
if ((nextRequestedPollTime - System.currentTimeMillis()) < 10000L) {
nextRequestedPollTime = nextActualPollTime;
}
// extended unavailable SGV may be due to clash with the current polling time
// while we wait for a good SGV event, polling is auto adjusted by offsetting the next poll based on miss count
if (dataStore.getUnavailableSGVCount() > 0) {
if (timeLastGoodSGV == 0) {
nextRequestedPollTime += POLL_PERIOD_MS / 5L; // if there is a uploader/sensor poll clash on startup then this will push the next attempt out by 60 seconds
} else if (dataStore.getUnavailableSGVCount() > 2) {
sendStatus("Warning: No SGV available from pump for " + dataStore.getUnavailableSGVCount() + " attempts");
nextRequestedPollTime += ((long) ((dataStore.getUnavailableSGVCount() - 2) % 5)) * (POLL_PERIOD_MS / 10L); // adjust poll time in 1/10 steps to avoid potential poll clash (max adjustment at 5/10)
}
}
MedtronicCnlAlarmManager.setAlarm(nextRequestedPollTime);
sendStatus("Next poll due at: " + df.format(nextRequestedPollTime));
} }
} finally { } finally {
MedtronicCnlAlarmReceiver.completeWakefulIntent(intent); MedtronicCnlAlarmReceiver.completeWakefulIntent(intent);
} }
} }
private void scheduleNextPoll(long timePollStarted, long timeLastGoodSGV, long pollInterval, DateFormat df) {
// smart polling and pump-sensor poll clash detection
long lastActualPollTime = timePollStarted;
if (timeLastGoodSGV > 0) {
lastActualPollTime = timeLastGoodSGV + POLL_GRACE_PERIOD_MS + (POLL_PERIOD_MS * ((System.currentTimeMillis() - (timeLastGoodSGV + POLL_GRACE_PERIOD_MS)) / POLL_PERIOD_MS));
}
long nextActualPollTime = lastActualPollTime + POLL_PERIOD_MS;
long nextRequestedPollTime = lastActualPollTime + pollInterval;
if ((nextRequestedPollTime - System.currentTimeMillis()) < 10000L) {
nextRequestedPollTime = nextActualPollTime;
}
// extended unavailable SGV may be due to clash with the current polling time
// while we wait for a good SGV event, polling is auto adjusted by offsetting the next poll based on miss count
if (dataStore.getUnavailableSGVCount() > 0) {
if (timeLastGoodSGV == 0) {
nextRequestedPollTime += POLL_PERIOD_MS / 5L; // if there is a uploader/sensor poll clash on startup then this will push the next attempt out by 60 seconds
} else if (dataStore.getUnavailableSGVCount() > 2) {
sendStatus("Warning: No SGV available from pump for " + dataStore.getUnavailableSGVCount() + " attempts");
nextRequestedPollTime += ((long) ((dataStore.getUnavailableSGVCount() - 2) % 5)) * (POLL_PERIOD_MS / 10L); // adjust poll time in 1/10 steps to avoid potential poll clash (max adjustment at 5/10)
}
}
MedtronicCnlAlarmManager.setAlarm(nextRequestedPollTime);
sendStatus("Next poll due at: " + df.format(nextRequestedPollTime));
}
private boolean acquireUsbDevice() { private boolean acquireUsbDevice() {
if (!hasUsbHostFeature()) { if (!hasUsbHostFeature()) {
sendStatus("It appears that this device doesn't support USB OTG."); sendStatus("It appears that this device doesn't support USB OTG.");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment