Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

onx traffic recipe

By: a guest on Jun 5th, 2012  |  syntax: C++  |  size: 3.02 KB  |  views: 468  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // on{x} recipe for fastest route to work
  2. // created by jgaikwad@gmail.com
  3. // version 1
  4. // last modified 6/5/2012
  5. // define variables
  6. var time = "07:35 AM";
  7. var wps = [];
  8. var fastestroute = '';
  9. var route1Duration = '';
  10. var route2Duration = '';
  11. var route1Name = 'i635 route';
  12. var route2Name = 'downtown route';
  13.  
  14. // get fastest route
  15. var getFastestRoute = function () {
  16.     // first get route 1
  17.         wps[0] = '39.173773,-94.559544';
  18.         wps[1] = '39.037003,-94.682968';
  19.         feeds.traffic.get(
  20.                 {wps: wps, unittype: 'i'},
  21.                 function(traffic, textStatus, response) {
  22.                    console.info('\n****The traffic warnings are for route2 ' + traffic.warnings[0]);
  23.                    // duration is given in seconds, modify to minutes
  24.                    route1Duration = traffic.totalTravelDuration/60;
  25.                 },
  26.                 function(response, textStatus) {
  27.                    console.info('got error :  ' + textStatus);
  28.                 }
  29.         );
  30.         // then get route 2
  31.         wps[0] = '39.172709,-94.552313';
  32.         wps[1] = '39.037003,-94.682968';
  33.         feeds.traffic.get(
  34.                 {wps: wps, unittype: 'i'},
  35.                 function(traffic, textStatus, response) {
  36.                    console.info('\n****The traffic warnings are for route1 ' + traffic.warnings[0]);
  37.                    // duration is given in seconds, modify to minutes
  38.                    route2Duration = traffic.totalTravelDuration/60;
  39.                    // compare route durations
  40.                    if (route1Duration < route2Duration) {
  41.                            fastestroute = route1Name;
  42.                    } else {
  43.                            fastestroute = route2Name;
  44.                    }
  45.                    // notify of fastest route
  46.                    var notification = device.notifications.createNotification('Traffic info');
  47.                    notification.content = 'Fastest route=' + fastestroute;
  48.                    notification.show();
  49.                 },
  50.                 function(response, textStatus) {
  51.                    console.info('got error :  ' + textStatus);
  52.                 }
  53.         );
  54. };
  55.  
  56. var parseTime = function (timeString) {
  57.         if (timeString === '') {
  58.             return null;
  59.         }
  60.  
  61.         var time = timeString.match(/^(\d+)(:(\d\d))?\s*((a|(p))m?)?$/i);
  62.         if (time === null) {
  63.             return null;
  64.         }
  65.  
  66.         var hours = parseInt(time[1], 10);
  67.         if (hours === 12 && !time[6]) {
  68.             hours = 0;
  69.         } else {
  70.             hours += (hours < 12 && time[6]) ? 12 : 0;
  71.         }
  72.         var d = new Date();
  73.         d.setHours(hours);
  74.         d.setMinutes(parseInt(time[3], 10) || 0);
  75.         d.setSeconds(0, 0);
  76.         return d;
  77.     };
  78.  
  79.     // get the fastest route every day at the specified time
  80.     var firstTime = parseTime(time);
  81.     if (firstTime === null) {
  82.         console.error('Invalid time format: ' + time + '. Expected: hh:mm or hh:mm AM/PM');
  83.     } else {
  84.         var now = new Date();
  85.         if (firstTime.getTime() < now) {
  86.             firstTime.setDate(firstTime.getDate() + 1);
  87.         }
  88.         console.info('Setting scheduled task to start at ' + firstTime);
  89.         device.scheduler.setTimer({
  90.                 name: 'dailyTrafficTimer',
  91.                 time: firstTime.getTime(),
  92.                 interval: 'day',
  93.                 repeat: true,
  94.                 exact: true
  95.             },
  96.             getFastestRoute);
  97.     }
clone this paste RAW Paste Data