Last active
October 13, 2015 10:12
-
-
Save bjoerge/91e65652cf8a02b54d04 to your computer and use it in GitHub Desktop.
heidrun-logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.DS_Store | |
npm-debug.log | |
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request') | |
var INTERVAL = 2 * 1000 | |
var REQUEST_TIMEOUT = 5 * 1000 | |
function tap(fn) { | |
return function (value) { | |
fn() | |
return value | |
} | |
} | |
function log(msg) { | |
return function () { | |
console.log(msg) | |
} | |
} | |
function getLogLine() { | |
var url = 'http://10.0.1.253/btnic.cgi?a'; | |
var req = new Promise(function(resolve, reject) { | |
request(url, function(err, response, body) { | |
if (err) { | |
return reject(err) | |
} | |
if (response.statusCode !== 200) { | |
return reject(new Error('HTTP error ' + response.statusCode + ': ' + body)) | |
} | |
resolve(JSON.parse(body)) | |
}) | |
}) | |
return timeout(req, REQUEST_TIMEOUT, function() { | |
return 'Got timeout while requesting ' + url | |
}) | |
} | |
var Firebase = require('firebase'); | |
var logRef = new Firebase('https://heidrun.firebaseio.com'); | |
function postToFirebase(logLine) { | |
return new Promise(function(resolve, reject) { | |
logRef.child(new Date().getTime()).set(logLine, function (err) { | |
if (err) { | |
return reject(err) | |
} | |
resolve(logLine) | |
}) | |
}) | |
} | |
function timeout(promise, ms, errorMsgFactory) { | |
return Promise.race([promise, delay(ms).then(function() { | |
var error = new Error(errorMsgFactory ? errorMsgFactory() : 'Operation timeout') | |
error.code = 'TIMEOUT' | |
throw error | |
})]) | |
} | |
function delay(time) { | |
return new Promise(function (resolve) { | |
setTimeout(resolve, time) | |
}) | |
} | |
function wait(time) { | |
return function() { | |
return delay(time) | |
} | |
} | |
function next() { | |
console.log('Fetching latest log line...') | |
getLogLine() | |
.then(tap(log('Got log line'))) | |
.then(postToFirebase) | |
.then(tap(log('Posted to firebase'))) | |
.catch(function(error) { | |
if (error.code === 'TIMEOUT') { | |
console.error(error.message) | |
return | |
} | |
console.error(error.stack) | |
}) | |
.then(wait(INTERVAL)) | |
.then(next) | |
} | |
next() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "heidrun-logger", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "node index.js" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"firebase": "^2.3.1", | |
"request": "^2.64.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment