Advertisement
melaniep

on{x} recipe to get traffic duration every morning

Jun 8th, 2012
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Initializing variables
  2.  
  3.     var time = "7:00 AM";
  4.     var wps = [];
  5.    // get the traffic for I-90 ramp in Bellevue to I-5
  6.     wps[0] = '47.580694,-122.177238';
  7.     wps[1] = '47.894125,-122.320404';    
  8.  
  9.     // End of variables initializing
  10.  
  11.     console.log('Started script: Show me the traffic every day at ' + time);
  12.    
  13.     // get the traffic feed
  14.     console.info('about to call traffic feeds');
  15.     feeds.traffic.get(
  16.         {wps: wps, unittype: 'm'},
  17.         function(traffic, textStatus, response) {
  18.            console.info('\n****The traffic warnings are ' + traffic.warnings[0]);
  19.            // duration is given in seconds, modify to minutes - edited to round to whole minutes
  20.            var duration = Math.round(traffic.totalTravelDuration/60);
  21.            var notification = device.notifications.createNotification('Traffic info');
  22.            notification.content = 'Driving distance=' + traffic.totalTravelDistance + ' duration=' + duration + 'minutes';
  23.            notification.show();
  24.         },
  25.         function(rese, textStatus) {
  26.            console.info('got error :  ' + textStatus);
  27.             }
  28. );
  29.  
  30.  
  31.     var parseTime = function (timeString) {
  32.         if (timeString === '') {
  33.             return null;
  34.         }
  35.  
  36.         var time = timeString.match(/^(\d+)(:(\d\d))?\s*((a|(p))m?)?$/i);
  37.         if (time === null) {
  38.             return null;
  39.         }
  40.  
  41.         var hours = parseInt(time[1], 10);
  42.         if (hours === 12 && !time[6]) {
  43.             hours = 0;
  44.         } else {
  45.             hours += (hours < 12 && time[6]) ? 12 : 0;
  46.         }
  47.         var d = new Date();
  48.         d.setHours(hours);
  49.         d.setMinutes(parseInt(time[3], 10) || 0);
  50.         d.setSeconds(0, 0);
  51.         return d;
  52.     };
  53.  
  54.     // get the alarm every day at the specified time
  55.     var firstTime = parseTime(time);
  56.     if (firstTime === null) {
  57.         console.error('Invalid time format: ' + time + '. Expected: hh:mm or hh:mm AM/PM');
  58.     } else {
  59.         var now = new Date();
  60.         if (firstTime.getTime() < now) {
  61.             firstTime.setDate(firstTime.getDate() + 1);
  62.         }
  63.         console.info('Setting scheduled task to start at ' + firstTime);
  64.         device.scheduler.setTimer({
  65.                 name: 'dailyHoroscopeTimer',
  66.                 time: firstTime.getTime(),
  67.                 interval: 'day',
  68.                 repeat: true,
  69.                 exact: true
  70.             },
  71.             showHoroscope);
  72.     }
  73.  
  74.     console.log('Completed script: Show me the horoscope every day at ' + time);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement