Skip to content

Instantly share code, notes, and snippets.

@error454
Created September 9, 2012 22:52
Show Gist options
  • Save error454/3687788 to your computer and use it in GitHub Desktop.
Save error454/3687788 to your computer and use it in GitHub Desktop.
on{X} Morning Umbrella & Coat Check
var time = "6:30 AM";
var temperatureBar = "below";
var temperature = "55";
var unitType = "i" /* Fahrenheit */;
var weatherCondition = "rainy";
function showForecast() {
// Get the current location
var locationListener = device.location.createListener('CELL', 2);
locationListener.on('changed', function (locSignal) {
locationListener.stop();
var weatherOptions = {
location: locSignal.location.latitude + ',' + locSignal.location.longitude,
days: 0,
unittype: unitType
};
//Get the weather feed
feeds.weather.get(
weatherOptions,
function onSuccess(weather, textStatus, response) {
var degrees = parseInt(temperature, 10);
var lowTemp = weather.forecasts[0].temperature.low;
var highTemp = weather.forecasts[0].temperature.high;
var forecast = weather.forecasts[0];
var rainy = isRainy(forecast);
var tempAlert = (temperatureBar === 'above' && highTemp >= degrees) || (temperatureBar === 'below' && lowTemp <= degrees);
//create notification
var text = "";
if(rainy && tempAlert){
text += "Pack an umbrella and a coat!";
}
else if(!rainy && tempAlert){
text += "Pack a coat!";
}
else if(!rainy && !tempAlert){
text += "Looks like a beautiful day!";
}
var notification = device.notifications.createNotification("Weather report");
notification.content = text;
notification.on('click', function () {
feeds.weather.show(weatherOptions);
});
notification.show();
},
function onError(response, textStatus) {
console.error('Failed to get weather: ', textStatus);
}
);
});
locationListener.start();
}
function isRainy(forecast){
if(forecast === null){
return false;
}
var sky = forecast.sky.toLowerCase();
var rain = forecast.rain;
return (rain > 0 && (sky === 't-storms' || sky === 'scattered thunderstorms')) ||
(rain > 50 && (sky === 'rain' || sky === 'showers' || sky === 'showers / clear' || sky === 'sprinkles' || sky === 'showers' || sky === 'sleet' || sky === 'light rain'));
}
function parseTime(timeString) {
if (timeString === '') {
return null;
}
var time = timeString.match(/^(\d+)(:(\d\d))?\s*((a|(p))m?)?$/i);
if (time === null) {
return null;
}
var hours = parseInt(time[1],10);
if (hours === 12 && !time[6]) {
hours = 0;
} else {
hours += (hours < 12 && time[6]) ? 12 : 0;
}
var d = new Date();
d.setHours(hours);
d.setMinutes(parseInt(time[3], 10) || 0);
d.setSeconds(0, 0);
return d;
}
// get the alarm every day at the given hour
var now = new Date(),
timeString = time,
firstTime = parseTime(timeString);
if (firstTime === null) {
console.error('Invalid time format: ' + timeString + '. Expected: hh:mm or hh:mm AM/PM');
}
else {
if (firstTime.getTime() < now) {
firstTime.setDate(firstTime.getDate() + 1);
}
device.scheduler.setTimer(
{
name: 'dailyWeatherTimer',
time: firstTime.getTime(),
interval: 'day',
repeat: true,
exact: true
},
showForecast);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment