From a34a58ba434b11e0f1d9cf1a0e93da77b0b6766f Mon Sep 17 00:00:00 2001 From: Chris Pitchford <2324225+cpitchford@users.noreply.github.com> Date: Thu, 9 Dec 2021 22:55:52 +0000 Subject: [PATCH 1/2] Synchronise the dexcom collection times to reduce refresh lag When collecting date from Dexcom via share2bridge-dexcom before we would poll at regular intervals. Dexcom transmitters / apps refresh every 5 minutes give/take the inaccuracy in the transmitters clock. If we poll every 5 minutes, we will randomly introduce up to 5 minutes of lag before we refresh. For example, if we poll 4:59s after dexcom refreshed we will face a 4:59s lag seeing the latest data arrive This patch introduces a refresh adjustment. We look at the timestamp of the most recent data item and aim to refresh 5 minutes plus a buffer after that timestamp. for example, 5:20s after the most recent data time. the additional buffer gives dexcom a chance to push the data through their application/infrastructure (after the app sends the data, it appears in testing to be around 20s before it is available in the API) This reduces the lag time to a reliable 20 seconds regardless of when nightscout was started and keeps it in track as it drifts forwards and back. --- lib/plugins/bridge.js | 47 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/lib/plugins/bridge.js b/lib/plugins/bridge.js index 50851e6df74..5241e9734dc 100644 --- a/lib/plugins/bridge.js +++ b/lib/plugins/bridge.js @@ -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) { @@ -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 { @@ -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 () { From dc312a8b8265561edbd83be07a82ed4f77583910 Mon Sep 17 00:00:00 2001 From: Chris Pitchford <2324225+cpitchford@users.noreply.github.com> Date: Fri, 10 Dec 2021 00:46:34 +0000 Subject: [PATCH 2/2] Update test for new default interval The default interval is increasd from 2.5 minutes to 2.6 minutes This change incorporates the lag between the app uploading and the data being available in share2nightscout-bridge This change also accomodates the optimisation of the refresh interval based on the last collected data timestamp --- tests/bridge.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/bridge.test.js b/tests/bridge.test.js index 66b69f64c3a..60afacd1b79 100644 --- a/tests/bridge.test.js +++ b/tests/bridge.test.js @@ -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 () { @@ -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 () { @@ -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); }); });