Skip to content

Commit

Permalink
Only uploading if there are new measurements, introducing "single sho…
Browse files Browse the repository at this point in the history
…t"-mode, allow disabling of HTTPS (useful for docker deployments)
  • Loading branch information
timoschlueter committed Oct 4, 2022
1 parent 6023fea commit c624a86
Showing 1 changed file with 43 additions and 20 deletions.
63 changes: 43 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,16 @@ function getLibreLinkUpUrl(region) {
/**
* NightScout API
*/
const NIGHT_SCOUT_URL = process.env.NIGHTSCOUT_URL;
const NIGHT_SCOUT_API_TOKEN = process.env.NIGHTSCOUT_API_TOKEN;
const NIGHTSCOUT_URL = process.env.NIGHTSCOUT_URL;
const NIGHTSCOUT_API_TOKEN = process.env.NIGHTSCOUT_API_TOKEN;
const NIGHTSCOUT_DISABLE_HTTPS = process.env.NIGHTSCOUT_DISABLE_HTTPS || false;

function getNightscoutUrl() {
if (NIGHTSCOUT_DISABLE_HTTPS === "true") {
return "http://" + NIGHTSCOUT_URL;
}
return "https://" + NIGHTSCOUT_URL;
}

/**
* last known authTicket
Expand All @@ -82,14 +90,18 @@ const libreLinkUpHttpHeaders = {
}

const nightScoutHttpHeaders = {
"api-secret": NIGHT_SCOUT_API_TOKEN,
"api-secret": NIGHTSCOUT_API_TOKEN,
"User-Agent": USER_AGENT,
"Content-Type": "application/json",
}

const schedule = "*/" + (process.env.LINK_UP_TIME_INTERVAL || 5) + " * * * *";
logger.info("Starting cron schedule: " + schedule)
cron.schedule(schedule, () => {main();}, {});
if (process.env.SINGLE_SHOT === "true") {
main().then();
} else {
const schedule = "*/" + (process.env.LINK_UP_TIME_INTERVAL || 5) + " * * * *";
logger.info("Starting cron schedule: " + schedule)
cron.schedule(schedule, () => { main().then() }, {});
}

async function main() {
if (hasValidAuthentication() === false) {
Expand Down Expand Up @@ -139,8 +151,6 @@ async function getGlucoseMeasurements() {
headers: getLluAuthHeaders()
});

logger.info("Received blood glucose measurement items");

await uploadToNightScout(response.data.data);
} catch (error) {
logger.error("Error getting glucose measurements", error);
Expand Down Expand Up @@ -194,7 +204,7 @@ async function getLibreLinkUpConnection() {
}

async function lastEntryDate() {
const url = "https://" + NIGHT_SCOUT_URL + "/api/v1/entries?count=1"
const url = getNightscoutUrl() + "/api/v1/entries?count=1"
const response = await axios.get(
url,
{
Expand Down Expand Up @@ -239,18 +249,31 @@ async function uploadToNightScout(measurementData) {
}
});

try {
const url = "https://" + NIGHT_SCOUT_URL + "/api/v1/entries"
await axios.post(
url,
formattedMeasurements,
if (formattedMeasurements.length > 0)
{
logger.info("Trying to upload " + formattedMeasurements.length + " glucose measurement items to Nightscout");
try
{
const url = getNightscoutUrl() + "/api/v1/entries"
const response = await axios.post(
url,
formattedMeasurements,
{
headers: nightScoutHttpHeaders
});
if (response.status !== 200)
{
headers: nightScoutHttpHeaders
});

logger.info("Upload of " + formattedMeasurements.length + " measurements to NightScout succeeded");
} catch (error) {
logger.error("Upload to NightScout failed ", error);
logger.error("Upload to NightScout failed ", response.statusText);
} else
{
logger.info("Upload of " + formattedMeasurements.length + " measurements to Nightscout succeeded");
}
} catch (error)
{
logger.error("Upload to NightScout failed ", error);
}
} else {
logger.info("No new measurements to upload");
}
}

Expand Down

0 comments on commit c624a86

Please sign in to comment.