forked from xdrip-js/Lookout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xDripAPS.js
113 lines (103 loc) · 3.2 KB
/
xDripAPS.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// const http = require("http");
const os = require("os");
const request = require("request")
module.exports = () => {
return {
// API (public) functions
post: (glucose) => {
// log error and ignore errant glucose values
if (glucose.glucose > 800 || glucose.glucose < 20) {
console.log('Invalid glucose value received from transmitter, ignoring');
return;
}
let direction;
if (glucose.trend <= -30) {
direction = 'DoubleDown';
} else if (glucose.trend <= -20) {
direction = 'SingeDown';
} else if (glucose.trend <= -10) {
direction = 'FortyFiveDown';
} else if (glucose.trend < 10) {
direction = 'Flat';
} else if (glucose.trend < 20) {
direction = 'FortyFiveUp';
} else if (glucose.trend < 30) {
direction = 'SingleUp';
} else {
direction = 'DoubleUp';
}
const entry = [{
'device': 'openaps://' + os.hostname(),
'date': glucose.readDate,
'dateString': new Date(glucose.readDate).toISOString(),
'sgv': glucose.glucose,
'direction': direction,
'type': 'sgv',
'filtered': glucose.filtered,
'unfiltered': glucose.unfiltered,
'rssi': "100", // TODO: consider reading this on connection and reporting
'noise': "1",
'trend': glucose.trend,
'glucose': glucose.glucose
}];
const data = JSON.stringify(entry);
const ns_url = process.env.NIGHTSCOUT_HOST;
const secret = process.env.API_SECRET;
// // first post to localhost
// let options = {
// hostname: '127.0.0.1', // could also try localhost ?
// port: 5000,
// path: '/api/v1/entries',
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// 'Content-Length': Buffer.byteLength(data),
// 'API-SECRET': secret
// }
// };
//
// let req = http.request(options);
//
// req.on('error', function(e) {
// console.log('problem with request: ' + e.message);
// });
//
// req.write(data);
// req.end();
const optionsNS = {
url: 'http://second15.herokuapp.com/api/v1/entries',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'API-SECRET': secret
},
body: entry,
json: true
};
request(optionsNS, function (error, response, body) {
if (error) {
console.error('error posting json: ', error)
} else {
console.log('uploaded to NS, statusCode = ' + response.statusCode);
}
})
const optionsX = {
url: 'http://127.0.0.1:5000/api/v1/entries',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'API-SECRET': secret
},
body: entry,
json: true
};
request(optionsX, function (error, response, body) {
if (error) {
console.error('error posting json: ', error)
} else {
console.log('uploaded to xDripAPS, statusCode = ' + response.statusCode);
}
})
}
};
};