Skip to content

Commit

Permalink
Merge pull request nightscout#7231 from cpitchford/wip/cpitchford/syn…
Browse files Browse the repository at this point in the history
…chronize_dexcom_fetch_times

Optimize time between polling share2nightscout-bridge to reduce ingest lag
  • Loading branch information
Jared-Is-Coding committed Dec 21, 2021
2 parents df4acfa + dc312a8 commit a15722b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
47 changes: 43 additions & 4 deletions lib/plugins/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function bridged (entries) {
mostRecentRecord = glucose[i].date;
}
}
//console.log("DEXCOM: Most recent entry received; "+new Date(mostRecentRecord).toString());
}
entries.create(glucose, function stored (err) {
if (err) {
Expand All @@ -46,12 +47,12 @@ function options (env) {
, minutes: env.extendedSettings.bridge.minutes || 1440
};

var interval = env.extendedSettings.bridge.interval || 60000 * 2.5; // Default: 2.5 minutes
var interval = env.extendedSettings.bridge.interval || 60000 * 2.6; // Default: 2.6 minutes

if (interval < 1000 || interval > 300000) {
// Invalid interval range. Revert to default
console.error("Invalid interval set: [" + interval + "ms]. Defaulting to 2.5 minutes.")
interval = 60000 * 2.5 // 2.5 minutes
console.error("Invalid interval set: [" + interval + "ms]. Defaulting to 2.6 minutes.")
interval = 60000 * 2.6 // 2.6 minutes
}

return {
Expand All @@ -75,15 +76,53 @@ function create (env, bus) {

bridge.startEngine = function startEngine (entries) {


opts.callback = bridged(entries);

let last_run = new Date(0).getTime();
let last_ondemand = new Date(0).getTime();

function should_run() {
// Time we expect to have to collect again
const msRUN_AFTER = (300+20) * 1000;
const msNow = new Date().getTime();

const next_entry_expected = mostRecentRecord + msRUN_AFTER;

if (next_entry_expected > msNow) {
// we're not due to collect a new slot yet. Use interval
const ms_since_last_run = msNow - last_run;
if (ms_since_last_run < interval) {
return false;
}

last_run = msNow;
last_ondemand = new Date(0).getTime();
console.log("DEXCOM: Running poll");
return true;
}

const ms_since_last_run = msNow - last_ondemand;

if (ms_since_last_run < interval) {
return false;
}
last_run = msNow;
last_ondemand = msNow;
console.log("DEXCOM: Data due, running extra poll");
return true;
}

let timer = setInterval(function () {
if (!should_run()) return;


opts.fetch.minutes = parseInt((new Date() - mostRecentRecord) / 60000);
opts.fetch.maxCount = parseInt((opts.fetch.minutes / 5) + 1);
opts.firstFetchCount = opts.fetch.maxCount;
console.log("Fetching Share Data: ", 'minutes', opts.fetch.minutes, 'maxCount', opts.fetch.maxCount);
engine(opts);
}, interval);
}, 1000 /*interval*/);

if (bus) {
bus.on('teardown', function serverTeardown () {
Expand Down
6 changes: 3 additions & 3 deletions tests/bridge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('bridge', function ( ) {
var opts = bridge.options(tooLowInterval);
should.exist(opts);

opts.interval.should.equal(150000);
opts.interval.should.equal(156000);
});

it('set too high bridge interval option from env', function () {
Expand All @@ -64,7 +64,7 @@ describe('bridge', function ( ) {
var opts = bridge.options(tooHighInterval);
should.exist(opts);

opts.interval.should.equal(150000);
opts.interval.should.equal(156000);
});

it('set no bridge interval option from env', function () {
Expand All @@ -77,7 +77,7 @@ describe('bridge', function ( ) {
var opts = bridge.options(noInterval);
should.exist(opts);

opts.interval.should.equal(150000);
opts.interval.should.equal(156000);
});

});

0 comments on commit a15722b

Please sign in to comment.