Skip to content

Commit 4750f13

Browse files
authored
Merge pull request #7231 from cpitchford/wip/cpitchford/synchronize_dexcom_fetch_times
Optimize time between polling share2nightscout-bridge to reduce ingest lag
2 parents df4acfa + dc312a8 commit 4750f13

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

lib/plugins/bridge.js

+43-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function bridged (entries) {
2424
mostRecentRecord = glucose[i].date;
2525
}
2626
}
27+
//console.log("DEXCOM: Most recent entry received; "+new Date(mostRecentRecord).toString());
2728
}
2829
entries.create(glucose, function stored (err) {
2930
if (err) {
@@ -46,12 +47,12 @@ function options (env) {
4647
, minutes: env.extendedSettings.bridge.minutes || 1440
4748
};
4849

49-
var interval = env.extendedSettings.bridge.interval || 60000 * 2.5; // Default: 2.5 minutes
50+
var interval = env.extendedSettings.bridge.interval || 60000 * 2.6; // Default: 2.6 minutes
5051

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

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

7677
bridge.startEngine = function startEngine (entries) {
7778

79+
7880
opts.callback = bridged(entries);
7981

82+
let last_run = new Date(0).getTime();
83+
let last_ondemand = new Date(0).getTime();
84+
85+
function should_run() {
86+
// Time we expect to have to collect again
87+
const msRUN_AFTER = (300+20) * 1000;
88+
const msNow = new Date().getTime();
89+
90+
const next_entry_expected = mostRecentRecord + msRUN_AFTER;
91+
92+
if (next_entry_expected > msNow) {
93+
// we're not due to collect a new slot yet. Use interval
94+
const ms_since_last_run = msNow - last_run;
95+
if (ms_since_last_run < interval) {
96+
return false;
97+
}
98+
99+
last_run = msNow;
100+
last_ondemand = new Date(0).getTime();
101+
console.log("DEXCOM: Running poll");
102+
return true;
103+
}
104+
105+
const ms_since_last_run = msNow - last_ondemand;
106+
107+
if (ms_since_last_run < interval) {
108+
return false;
109+
}
110+
last_run = msNow;
111+
last_ondemand = msNow;
112+
console.log("DEXCOM: Data due, running extra poll");
113+
return true;
114+
}
115+
80116
let timer = setInterval(function () {
117+
if (!should_run()) return;
118+
119+
81120
opts.fetch.minutes = parseInt((new Date() - mostRecentRecord) / 60000);
82121
opts.fetch.maxCount = parseInt((opts.fetch.minutes / 5) + 1);
83122
opts.firstFetchCount = opts.fetch.maxCount;
84123
console.log("Fetching Share Data: ", 'minutes', opts.fetch.minutes, 'maxCount', opts.fetch.maxCount);
85124
engine(opts);
86-
}, interval);
125+
}, 1000 /*interval*/);
87126

88127
if (bus) {
89128
bus.on('teardown', function serverTeardown () {

tests/bridge.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('bridge', function ( ) {
5151
var opts = bridge.options(tooLowInterval);
5252
should.exist(opts);
5353

54-
opts.interval.should.equal(150000);
54+
opts.interval.should.equal(156000);
5555
});
5656

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

67-
opts.interval.should.equal(150000);
67+
opts.interval.should.equal(156000);
6868
});
6969

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

80-
opts.interval.should.equal(150000);
80+
opts.interval.should.equal(156000);
8181
});
8282

8383
});

0 commit comments

Comments
 (0)