Skip to main content

Initializing variables

// Initializing variables

    var time = "7:00 AM";
    var wps = [];
   // get the traffic for I-90 ramp in Bellevue to I-5
    wps[0] = '47.580694,-122.177238';
    wps[1] = '47.894125,-122.320404';    

// End of variables initializing

console.log('Started script: Show me the traffic every day at ' + time);
   
    // get the traffic feed
    console.info('about to call traffic feeds');
    feeds.traffic.get(
        {wps: wps, unittype: 'm'},
        function(traffic, textStatus, response) {
           console.info('\n****The traffic warnings are ' + traffic.warnings[0]);
           // duration is given in seconds, modify to minutes - edited to round to whole minutes
           var duration = Math.round(traffic.totalTravelDuration/60);
           var notification = device.notifications.createNotification('Traffic info');
           notification.content = 'Driving distance=' + traffic.totalTravelDistance + ' duration=' + duration + 'minutes';
           notification.show();
        },
        function(rese, textStatus) {
           console.info('got error :  ' + textStatus);
            }
);


    var parseTime = function (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 specified time
    var firstTime = parseTime(time);
    if (firstTime === null) {
        console.error('Invalid time format: ' + time + '. Expected: hh:mm or hh:mm AM/PM');
    } else {
        var now = new Date();
        if (firstTime.getTime() < now) {
            firstTime.setDate(firstTime.getDate() + 1);
        }
        console.info('Setting scheduled task to start at ' + firstTime);
        device.scheduler.setTimer({
                name: 'dailyHoroscopeTimer',
                time: firstTime.getTime(),
                interval: 'day',
                repeat: true,
                exact: true
            },
            showHoroscope);
    }

    console.log('Completed script: Show me the horoscope every day at ' + time);