From beec517777a6a3fed863e967ca2df1359e624b32 Mon Sep 17 00:00:00 2001 From: Mark Wilson Date: Sat, 10 Oct 2015 16:25:43 -0700 Subject: [PATCH] Read config from env instead of file --- .gitignore | 1 - README.md | 17 ++++++++++++++++- config.js.example | 8 -------- logger.js | 2 +- package.json | 2 +- run.js | 35 +++++++++++++++++++++++++++-------- 6 files changed, 45 insertions(+), 20 deletions(-) delete mode 100644 config.js.example diff --git a/.gitignore b/.gitignore index 170b4bc..c2658d7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -config.js node_modules/ diff --git a/README.md b/README.md index 4809fcb..23517a3 100644 --- a/README.md +++ b/README.md @@ -17,12 +17,26 @@ 1. Install [Node]. 1. Clone this repository or [download a zip] with the latest version. -1. Create a `config.js` file based on the provided `config.js.example`. 1. `npm install` to install dependencies. +1. Set environment variables (see below). 1. `npm start` and leave it running. **Coming soon:** Deploy instructions for Heroku +### Required environment variables + +* `CARELINK_USERNAME` - your username for [CareLink][carelink] +* `CARELINK_PASSWORD` - your password for [CareLink][carelink] +* `CARELINK_PUMP_TIMEZONE` - a timezone offset string like `"-0700"` which represents the pump's offset from UTC (**TODO:** use time zone names like `America/Los_Angeles` instead, since offsets vary depending on DST) +* `API_SECRET` - the value you use for `API_SECRET` on your Nightscout website +* `WEBSITE_HOSTNAME` - the hostname for your Nightscout instance, which looks like `your.host.com`. If you are running this script in the same Azure environment as Nightscout, there is no need to set this, as it will [already be set by Azure][azure-environment]. If you set `NS` (see below), you do not need to set this. + +### Optional environment variables + +* `CARELINK_REQUEST_INTERVAL` - number of milliseconds to wait between requests to the CareLink server (default: 60000) +* `CARELINK_SGV_LIMIT` - maximum number of recent sensor glucose values to send to Nightscout (default: 24) +* `NS` - a fully-qualified Nightscout URL (e.g. `https://sitename.azurewebsites.net`) which overrides `WEBSITE_HOSTNAME` + ## Currently supported data * Sensor glucose values @@ -49,6 +63,7 @@ This project is intended for educational and informational purposes only. It rel [cgm-remote-monitor]: https://github.com/nightscout/cgm-remote-monitor [Node]: https://nodejs.org [download a zip]: https://github.com/mddub/minimed-connect-to-nightscout/archive/master.zip +[azure-environment]: https://github.com/projectkudu/kudu/wiki/Azure-runtime-environment [this sensor-disabled gist]: https://gist.github.com/mddub/b033ec0c800deec02471 [this sensor-enabled gist]: https://gist.github.com/mddub/dc1baf74eda772dcb164 [get in touch]: mailto:mark@warkmilson.com diff --git a/config.js.example b/config.js.example deleted file mode 100644 index ca44467..0000000 --- a/config.js.example +++ /dev/null @@ -1,8 +0,0 @@ -exports.CARELINK_USERNAME = 'username'; -exports.CARELINK_PASSWORD = 'password'; -exports.PUMP_TIMEZONE = '-0700'; // UTC time offset -exports.CARELINK_REQUEST_INTERVAL = 60 * 1000; // milliseconds -exports.NUM_RECORDS_TO_SUBMIT = 20; // carelink gives ~300 sgv records at a time, but there's no need to send them all to nightscout - -exports.NIGHTSCOUT_HOST = 'https://yoursitename.azurewebsites.net'; -exports.NIGHTSCOUT_API_SECRET = 'your api secret'; diff --git a/logger.js b/logger.js index ebcfb1b..80ee924 100644 --- a/logger.js +++ b/logger.js @@ -2,7 +2,7 @@ "use strict"; module.exports.log = function(str) { - if (process.env['MINIMED_CONNECT_VERBOSE']) { + if (process.env['CARELINK_VERBOSE']) { console.log(new Date() + ' ' + str); } }; diff --git a/package.json b/package.json index 321bdab..b0d50bd 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ }, "main": "index.js", "scripts": { - "start": "MINIMED_CONNECT_VERBOSE=true node run.js" + "start": "CARELINK_VERBOSE=true node run.js" }, "license": "MIT", "dependencies": { diff --git a/run.js b/run.js index e227040..e4b1461 100644 --- a/run.js +++ b/run.js @@ -1,19 +1,38 @@ /* jshint node: true */ "use strict"; -var config = require('./config'), - carelink = require('./carelink'), +var carelink = require('./carelink'), nightscout = require('./nightscout'); -var client = carelink.Client({username: config.CARELINK_USERNAME, password: config.CARELINK_PASSWORD}), - endpoint = config.NIGHTSCOUT_HOST + '/api/v1/entries.json', - secret = config.NIGHTSCOUT_API_SECRET; +function readEnv(key, defaultVal) { + var val = process.env[key] || + process.env[key.toLowerCase()] || + // Azure prefixes environment variables with this + process.env['CUSTOMCONNSTR_' + key] || + process.env['CUSTOMCONNSTR_' + key.toLowerCase()]; + return val !== undefined ? val : defaultVal; +} + +var config = { + username: readEnv('CARELINK_USERNAME'), + password: readEnv('CARELINK_PASSWORD'), + pumpTimezone: readEnv('CARELINK_PUMP_TIMEZONE'), + nsHost: readEnv('WEBSITE_HOSTNAME'), + nsBaseUrl: readEnv('NS'), + nsSecret: readEnv('API_SECRET'), + interval: parseInt(readEnv('CARELINK_REQUEST_INTERVAL', 60 * 1000)), + sgvLimit: parseInt(readEnv('CARELINK_SGV_LIMIT', 24)) +}; + +var client = carelink.Client({username: config.username, password: config.password}); + +var endpoint = (config.nsBaseUrl ? config.nsBaseUrl : 'https://' + config.nsHost) + '/api/v1/entries.json'; (function requestLoop() { client.fetch(function(data) { - var entries = nightscout.transform(data, config.PUMP_TIMEZONE, config.NUM_RECORDS_TO_SUBMIT); - nightscout.upload(entries, endpoint, secret, function(response) { - setTimeout(requestLoop, config.CARELINK_REQUEST_INTERVAL); + var entries = nightscout.transform(data, config.pumpTimezone, config.sgvLimit); + nightscout.upload(entries, endpoint, config.nsSecret, function(response) { + setTimeout(requestLoop, config.interval); }); }); })();