Skip to content

Commit

Permalink
Merge pull request #53 from nightscout/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
bewest authored Dec 2, 2021
2 parents 81dcbdd + e04b5e6 commit 976fce4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
46 changes: 44 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,45 @@ var DIRECTIONS = {
, 'NOT COMPUTABLE': 8
, 'RATE OUT OF RANGE': 9
};
function stringLowerCaseNoSpaces(str) {
str = str.toLowerCase()
while(str.indexOf(' ')>-1)
str = str.replace(' ','');
return str;
}


function copyObjectWithLowercaseKeys(obj) {
// return a copy of obj but with each key transformed to lowercase
// and spaces removed
return Object.keys(obj).reduce(function (result, key) {
var newKey = stringLowerCaseNoSpaces(key);
result[newKey] = obj[key];
return result
}, {});
}

var LCDIRECTIONS = copyObjectWithLowercaseKeys(DIRECTIONS);


function matchTrend(trend) {
// attempt to match the trend based on
// a) it is a number
// b) it matches a key in DIRECTIONS
// c) it matches a key in LCDIRECTIONS if converted to lowercase and all spaces removed

if (typeof(trend) !== "string")
return trend;

if (trend in DIRECTIONS)
return DIRECTIONS[trend];

var lctrend = stringLowerCaseNoSpaces(trend);

if (lctrend in LCDIRECTIONS) return LCDIRECTIONS[lctrend];
return trend;
}

var Trends = (function ( ) {
var keys = Object.keys(DIRECTIONS);
var trends = keys.sort(function (a, b) {
Expand Down Expand Up @@ -202,12 +241,14 @@ function dex_to_entry (d) {
var regex = /\((.*)\)/;
var wall = parseInt(d.WT.match(regex)[1]);
var date = new Date(wall);
var trend = matchTrend(d.Trend);

var entry = {
sgv: d.Value
, date: wall
, dateString: date.toISOString( )
, trend: d.Trend
, direction: trendToDirection(d.Trend)
, trend: trend
, direction: trendToDirection(trend)
, device: 'share2'
, type: 'sgv'
};
Expand Down Expand Up @@ -270,6 +311,7 @@ function engine (opts) {

function refresh_token ( ) {
console.log('Fetching new token');
opts.login.accountId = null;
authorize(opts.login, function (err, res, body) {
if (!err && body && res && res.statusCode == 200) {
my.sessionID = body;
Expand Down
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "share2nightscout-bridge",
"version": "0.2.7",
"version": "0.2.8",
"description": "Fetches data from Dexcom's webservice and puts it in Nightscout.",
"main": "index.js",
"scripts": {
Expand Down
7 changes: 6 additions & 1 deletion tests/authorize_fetch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ describe("authorize_fetch", () => {
.be.an.instanceOf(Object);
glucose[0].DT.should.be.a.String().and.containEql("Date");
glucose[0].ST.should.be.a.String().and.containEql("Date");
glucose[0].Trend.should.be.a.Number();
glucose[0].Trend.should.exist();
if (typeof glucose[0].Trend === "string") {
glucose[0].Trend.should.be.a.String();
} else {
glucose[0].Trend.should.be.a.Number();
}
glucose[0].Value.should.be.a.Number();
glucose[0].WT.should.be.a.String().and.containEql("Date");

Expand Down

0 comments on commit 976fce4

Please sign in to comment.