-
Notifications
You must be signed in to change notification settings - Fork 3
/
check-share.js
49 lines (42 loc) · 1.53 KB
/
check-share.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
const request = require('request');
var parseString = require('xml2js').parseString;
const twit = require('./twit');
const fb = require('./fb');
const shareUS = 'https://share1.dexcom.com'
// const shareOUS = 'https://shareous1.dexcom.com'
// from https://github.com/nightscout/share2nightscout-bridge/issues/15
const systemTime = '/ShareWebServices/Services/General/SystemUtcTime'
const redis = require('redis');
const client = redis.createClient(process.env.REDIS_URL);
const now = new Date();
const nowString = now.toISOString();
const broadcastStatus = status => {
const message = `Share has changed status to ${status.up ? 'UP' : 'DOWN'}. http://shareup2.herokuapp.com`;
twit.tweet(message);
fb.post(message);
};
client.get('status', (err, val) => {
let status = JSON.parse(val);
status.at = nowString;
request(`${shareUS}${systemTime}`, function (error, response, body) {
// console.log('errorOUS:', error); // Print the error if one occurred
// console.log('statusCodeOUS:', response && response.statusCode); // Print the response status code if a response was received
parseString(body, function (err, result) {
if (err) {
if (status.up) {
status.up = false;
status.since = nowString;
broadcastStatus(status);
}
} else {
if (!status.up) {
status.up = true;
status.since = nowString;
broadcastStatus(status);
}
}
client.set('status', JSON.stringify(status), redis.print);
client.quit();
});
});
});