Skip to content

Instantly share code, notes, and snippets.

@siygle
Created June 6, 2012 16:17
Show Gist options
  • Select an option

  • Save siygle/2883003 to your computer and use it in GitHub Desktop.

Select an option

Save siygle/2883003 to your computer and use it in GitHub Desktop.
on{x} sample: notify to bring umbrella when it's rainy day
// Initializing variables
var reminder = "take an umbrella";
var weatherCondition = "rainy";
// End of variables initializing
console.log('Started script: Remind me to ' + reminder + ' every day the first time I unlock my phone, if it is going to be ' + weatherCondition);
// register callback to device unlocked event
device.screen.on('unlock', function () {
console.info('device unlocked');
var lastDateScreenUnlocked = device.localStorage.getItem('lastDateScreenUnlocked');
var today = new Date().toLocaleDateString();
// if it is the first time the device was unlocked today
if (!lastDateScreenUnlocked || lastDateScreenUnlocked !== today) {
// get current location
var locationListener = device.location.createListener('CELL', 2);
locationListener.on('changed', function(locSignal) {
locationListener.stop();
// get the weather forecast for the current location
feeds.weather.get(
{
location: locSignal.location.latitude + ',' + locSignal.location.longitude,
time: 0 // today's forecast
},
function onSuccess(weather, textStatus, response) {
console.info('Got the weather forecast for today:', JSON.stringify(weather.forecasts[0]));
// check the weather condition
var forecast = weather.forecasts[0];
if ((weatherCondition === 'rainy' && (forecast.rain > 50 || forecast.sky.toLowerCase() === 'rain')) || // rainy
(weatherCondition === 'sunny' && forecast.sky.toLowerCase() === 'clear') || // sunny
(weatherCondition === 'windy' && forecast.wind.speed >= 20 && forecast.wind.speed <= 30)) { // windy based on http://en.wikipedia.org/wiki/Beaufort_scale
// create a notification if the weather condition is met
var notification = device.notifications.createNotification(reminder);
notification.on('click', function () {
device.browser.launch(weather.forecastUrl);
});
notification.show();
} else {
console.info('weather is not ' + weatherCondition );
}
},
function onError(response, textStatus) {
console.error('Failed to get weather: ', textStatus);
});
});
locationListener.start();
// update the last time the screen was unlocked
device.localStorage.setItem('lastDateScreenUnlocked', today);
console.info('Stored the last time the screen was unlocked: ', today);
}
});
console.log('Completed script: Remind me to ' + reminder + ' every day the first time I unlock my phone, if it is going to be ' + weatherCondition);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment