Last active
August 29, 2015 14:26
-
-
Save dalanmiller/29ffcc3394c41d70ca8b to your computer and use it in GitHub Desktop.
A js file which watches a RethinkDB changefeed and pushes messages to PushBullet.
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 r = require("rethinkdb"); | |
var Promise = require("bluebird"); | |
var fs = require("fs"); | |
var PushBullet = require('pushbullet'); | |
Promise.promisifyAll(fs); | |
fs.readFileAsync("token").then(function(val) { | |
PUSHER = new PushBullet(val.toString().trim()); | |
}) | |
.then(function(){ | |
return r.connect({ | |
host: "dalanmiller-pi.local", | |
port: 28015, | |
db: "telemetry_pi" | |
}); | |
}) | |
.then(function(conn){ | |
console.log("Successful RethinkDB connection made, starting changefeed...") | |
return r.table("observations").pluck("datetime", "temp").changes()("new_val").run(conn); | |
}) | |
.then(function(changes) { | |
changes.each(function(err, change){ | |
formattedTemp = change.temp.toFixed(5).toString() | |
console.log("TEMPERATURE: " + change.temp + formattedTemp); | |
if(change.temp > 30){ | |
console.log("Too hot!"); | |
PUSHER.note("", "It's burning up!", formattedTemp, handleResponse); | |
} else if(change.temp < 5){ | |
console.log("Too cold!"); | |
PUSHER.note("", "It's freezing!", formattedTemp, handleResponse); | |
} else { | |
console.log("Just fine!"); | |
PUSHER.note("", "Temperature Nominal!", formattedTemp, handleResponse); | |
} | |
}); | |
}); | |
handleResponse = function(response){ | |
console.log(response); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment