Skip to content
This repository has been archived by the owner on Oct 26, 2020. It is now read-only.

Commit

Permalink
Read config from env instead of file
Browse files Browse the repository at this point in the history
  • Loading branch information
mddub committed Oct 10, 2015
1 parent 6a9a7e3 commit beec517
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 20 deletions.
1 change: 0 additions & 1 deletion .gitignore
@@ -1,2 +1 @@
config.js
node_modules/
17 changes: 16 additions & 1 deletion README.md
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 0 additions & 8 deletions config.js.example

This file was deleted.

2 changes: 1 addition & 1 deletion logger.js
Expand Up @@ -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);
}
};
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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": {
Expand Down
35 changes: 27 additions & 8 deletions 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);
});
});
})();

0 comments on commit beec517

Please sign in to comment.