Weather Condition every day the first time I unlock my phone

Have the weather condition in a toast (setOn{X}) every day at the first time you unlock your phone.

Inspired by :
Show me the weather forecast every day at 8:00 AM if the expected temperature is below 50 Fahrenheit
created by the on{X} team

To use this on{X} Recipe, simply copy the code below and modify if needed.

	var weatherCondition;
    
    console.log('Started script: Weather Condition every day the first time I unlock my phone');

    // register callback to device unlocked event
    device.screen.on('unlock', function () {
        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(
                    {
                        time: 0,    // today's forecast
                        unittype:  'm',  // degré celcius
                        location: locSignal.location.latitude + ',' + locSignal.location.longitude
                    },
                    function onSuccess(weather, textStatus, response) {
                        // check the weather condition
                        var forecast = weather.forecasts[0];
                        
                        if (forecast.rain > 50 || forecast.sky.toLowerCase() === 'rain')
                            weatherCondition = 'rainy';
                        if (forecast.sky.toLowerCase() === 'clear')
                            weatherCondition = 'shiny'; 
                        if (forecast.wind.speed  >= 20 && forecast.wind.speed <= 30)
                            weatherCondition = 'wind';
                        
                        var params = { message:'toast', text:weatherCondition + ', température: ' + weather.now.temperature + '°C.' };
                        device.applications.launch('setonx', params, null);
                        
                    },
                    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.log('Started script: Weather Condition every day the first time I unlock my phone');

Recipe Wall

1,452

Views