share2nightscout-bridge 0.2.6 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -66,6 +66,45 @@ var DIRECTIONS = {
66
66
  , 'NOT COMPUTABLE': 8
67
67
  , 'RATE OUT OF RANGE': 9
68
68
  };
69
+ function stringLowerCaseNoSpaces(str) {
70
+ str = str.toLowerCase()
71
+ while(str.indexOf(' ')>-1)
72
+ str = str.replace(' ','');
73
+ return str;
74
+ }
75
+
76
+
77
+ function copyObjectWithLowercaseKeys(obj) {
78
+ // return a copy of obj but with each key transformed to lowercase
79
+ // and spaces removed
80
+ return Object.keys(obj).reduce(function (result, key) {
81
+ var newKey = stringLowerCaseNoSpaces(key);
82
+ result[newKey] = obj[key];
83
+ return result
84
+ }, {});
85
+ }
86
+
87
+ var LCDIRECTIONS = copyObjectWithLowercaseKeys(DIRECTIONS);
88
+
89
+
90
+ function matchTrend(trend) {
91
+ // attempt to match the trend based on
92
+ // a) it is a number
93
+ // b) it matches a key in DIRECTIONS
94
+ // c) it matches a key in LCDIRECTIONS if converted to lowercase and all spaces removed
95
+
96
+ if (typeof(trend) !== "string")
97
+ return trend;
98
+
99
+ if (trend in DIRECTIONS)
100
+ return DIRECTIONS[trend];
101
+
102
+ var lctrend = stringLowerCaseNoSpaces(trend);
103
+
104
+ if (lctrend in LCDIRECTIONS) return LCDIRECTIONS[lctrend];
105
+ return trend;
106
+ }
107
+
69
108
  var Trends = (function ( ) {
70
109
  var keys = Object.keys(DIRECTIONS);
71
110
  var trends = keys.sort(function (a, b) {
@@ -142,7 +181,8 @@ function authorize (opts, then) {
142
181
  return request(req, then);
143
182
  } else {
144
183
  var responseStatus = res ? res.statusCode : "response not found";
145
- console.log("Cannot authorize account: ", err, responseStatus, body);
184
+ console.log("Cannot authorize account: ", err, responseStatus, accbody);
185
+ return process.nextTick(then, err, res, accbody);
146
186
  }
147
187
  });
148
188
  }
@@ -201,12 +241,14 @@ function dex_to_entry (d) {
201
241
  var regex = /\((.*)\)/;
202
242
  var wall = parseInt(d.WT.match(regex)[1]);
203
243
  var date = new Date(wall);
244
+ var trend = matchTrend(d.Trend);
245
+
204
246
  var entry = {
205
247
  sgv: d.Value
206
248
  , date: wall
207
249
  , dateString: date.toISOString( )
208
- , trend: d.Trend
209
- , direction: trendToDirection(d.Trend)
250
+ , trend: trend
251
+ , direction: trendToDirection(trend)
210
252
  , device: 'share2'
211
253
  , type: 'sgv'
212
254
  };
@@ -269,6 +311,7 @@ function engine (opts) {
269
311
 
270
312
  function refresh_token ( ) {
271
313
  console.log('Fetching new token');
314
+ opts.login.accountId = null;
272
315
  authorize(opts.login, function (err, res, body) {
273
316
  if (!err && body && res && res.statusCode == 200) {
274
317
  my.sessionID = body;