Skip to content

Instantly share code, notes, and snippets.

@mattbierner
Last active August 29, 2015 14:21
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 mattbierner/57d45eb40a900de0e816 to your computer and use it in GitHub Desktop.
Save mattbierner/57d45eb40a900de0e816 to your computer and use it in GitHub Desktop.
Blot're Weather Client Example
var Blotre = require('blotre');
var rp = require('request-promise');
var fs = require('fs');
var cron = require('cron');
var weatherSpectrum = new (require('colour-me-life'))();
weatherSpectrum.setSpectrum('blue', 'green', 'red');
weatherSpectrum.setNumberRange(0.0, 1.0);
var ZIP = "92328";
var getWeather = function(zip) {
return rp("http://api.openweathermap.org/data/2.5/weather?zip=" + zip + ",us")
.then(JSON.parse);
};
var getCurrentTemp = function(zip) {
return getWeather(zip)
.then(function(data) {
return data && data.main && data.main.temp;
});
};
var toC = function(temp) {
return temp - 273;
};
var tempToColor = function(temp) {
var max = 37;
var min = 0;
var position = Math.min(max, Math.max(min, toC(temp))) / max;
return weatherSpectrum.colorAt(position);
};
var colorToString = function(color) {
return "#" + color;
};
var askAuth = function(client) {
console.log("Code:", client.client.code);
};
var writeClient = function(client) {
fs.writeFileSync("data.json", JSON.stringify(client));
};
var readClient = function() {
try {
var data = JSON.parse(fs.readFileSync("data.json"));
return Blotre.create(data.client, data.creds, CONFIG);
} catch (e) {
return null;
}
};
var tryRedeem = function(client, callback) {
console.log("Press enter once you have authorized.")
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.once('data', function(chunk) {
client.redeemOnetimeCode()
.then(function(creds) {
client.setCreds(creds);
callback(client);
})
.catch(console.error);
});
};
var updateWeatherStream = function(client, color) {
return client.getStream(client.creds.user.rootStream)
.then(function(rootStream) {
return client.createStream({
name: "Weather",
uri: rootStream.uri + '/weather',
status: {
color: color
}
});
});
};
var updateWeather = function(client) {
getCurrentTemp(ZIP).then(function(temp) {
return updateWeatherStream(client, colorToString(tempToColor(temp)));
})
.then(function(data) {
console.log("Updated", new Date(data.updated), data.status.color);
})
.catch(console.error);
};
var CONFIG = {
onCredsChanged: writeClient
};
/**
* Start cron job
*/
var start = function(client) {
var currentJob = function() {
updateWeather(client);
};
currentJob();
cron.job("0 */10 * * * *", currentJob).start();
};
/**
* Main script
*/
var existingClient = readClient();
if (existingClient) {
console.log("Found existing client");
start(existingClient);
} else {
Blotre.createDisposable({
name: "The Global Toaster",
blurb: "Report of the current temp"
}, CONFIG)
.then(function(client) {
askAuth(client);
tryRedeem(client, start);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment