-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspark_hose.js
More file actions
104 lines (93 loc) · 3.04 KB
/
spark_hose.js
File metadata and controls
104 lines (93 loc) · 3.04 KB
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
var fs, serverEvent, express, app, request, extend, events, eventEmitter;
fs = require('fs');
serverEvent = require('server-event');
request = require('request');
app = require('express')();
extend = require('xtend');
events = require('events');
eventEmitter = new events.EventEmitter();
fs = require('fs');
//get the access token from access_token.txt
var access_token = fs.readFileSync('./access_token.txt', 'utf8');
console.log("Access token: " + access_token);
app.listen(8080);
serverEvent = serverEvent({ express : app });
app.get('/events', serverEvent, function (req, res) {
req.socket.setTimeout(Infinity);
res.writeHead(200, {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
res.write(':ok \n\n');
eventEmitter.on('spark_event', function(data) {
res.sse(data);
});
});
app.get('/', function (req, res) {
var uid = req.params.uid,
path = req.params[0] ? req.params[0] : 'index.html';
res.sendfile(path, {root: './page'});
});
//get the SSE events from the Spark Hose
var requestObj = request({
uri: 'https://api.spark.io/v1/events?access_token=' + access_token,
timeout: 999999999,
method: "GET"
});
var gotData = false;
var chunks = [];
var appendToQueue = function(arr) {
for(var i=0;i<arr.length;i++) {
var line = (arr[i] || "").trim();
if (line == "") {
continue;
}
chunks.push(line);
if (line.indexOf("data:") == 0) {
processItem(chunks);
chunks = [];
}
}
};
var processItem = function(arr) {
var obj = {};
for(var i=0;i<arr.length;i++) {
var line = arr[i];
if (line.indexOf("event:") == 0) {
obj.name = line.replace("event:", "").trim();
}
else if (line.indexOf("data:") == 0) {
line = line.replace("data:", "");
obj = extend(obj, JSON.parse(line));
}
}
// eventEmitter.emit('spark_event', JSON.parse(JSON.stringify(obj)));
eventEmitter.emit('spark_event', obj);
gotData = true;
//console.log(JSON.stringify(obj));
};
var onError = function(error) {
var currentTime = new Date();
//console.log(currentTime.toString('yyyy/mm/dd hh:mm:ss'));
console.log(currentTime.toString('yyyy/mm/dd hh:mm:ss') + ": ERROR!");
process.exit(code=0);
};
var onData = function(event) {
var chunk = event.toString();
appendToQueue(chunk.split("\n"));
};
requestObj.on('error', onError);
requestObj.on('data', onData);
setInterval(function() {
//kind of a WDT, if we don't get data for 5 seconds let's kill ourselves and forever should restart us
var currentTime = new Date();
if (!gotData) {
//console.log(currentTime.toString('yyyy/mm/dd hh:mm:ss'));
console.log(currentTime.toString('yyyy/mm/dd hh:mm:ss') + ": NO DATA FOR 5 SECONDS!");
process.exit(code=0);
}
console.log(currentTime.toString('yyyy/mm/dd hh:mm:ss') + ": Kicked the dog");
gotData = false;
}, 5000);