Skip to content

Instantly share code, notes, and snippets.

@dalanmiller
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dalanmiller/29ffcc3394c41d70ca8b to your computer and use it in GitHub Desktop.
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.
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