forked from thebookins/shareup
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck-share.js
44 lines (38 loc) · 1.63 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
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 client = require('redis').createClient(process.env.REDIS_URL);
const now = new Date();
const nowString = now.toISOString();
client.get('status', (err, val) => {
let status = JSON.parse(val);
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) {
client.set('status', JSON.stringify({at: nowString, up: false, since: nowString}));
twit.tweet(`Share is down at ${now}.`);
fb.post(`Share is down at ${now}.`);
} else {
client.set('status', JSON.stringify({at: nowString, up: false, since: status.since}));
}
} else {
if (status.up) {
client.set('status', JSON.stringify({at: nowString, up: true, since: status.since}));
} else {
client.set('status', JSON.stringify({at: nowString, up: true, since: nowString}));
twit.tweet(`Share is up at ${now}.`);
fb.post(`Share is up at ${now}.`);
}
}
client.quit();
});
});
});