Fix layar API
Fix layar API
Add current time/timing period to heading of panel to change those settings

<?php <?php
function include_header($pageTitle, $pageType, $opendiv = true, $geolocate = false) function include_header($pageTitle, $pageType, $opendiv = true, $geolocate = false)
{ {
echo ' echo '
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>' . $pageTitle . '</title>'; <title>' . $pageTitle . '</title>';
if (isDebugServer()) echo '<link rel="stylesheet" href="css/jquery-mobile-1.0a3.css" /> if (isDebugServer()) echo '<link rel="stylesheet" href="css/jquery-mobile-1.0a3.css" />
<script type="text/javascript" src="js/jquery-1.5.js"></script> <script type="text/javascript" src="js/jquery-1.5.js"></script>
<script type="text/javascript" src="js/jquery-mobile-1.0a3.js"></script>'; <script type="text/javascript" src="js/jquery-mobile-1.0a3.js"></script>';
else echo '<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.css" /> else echo '<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.js"></script> <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.js"></script>'; <script type="text/javascript" src="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.js"></script>';
echo ' echo '
<link rel="stylesheet" href="css/jquery.ui.datepicker.mobile.css" /> <link rel="stylesheet" href="css/jquery.ui.datepicker.mobile.css" />
<script> <script>
//reset type=date inputs to text //reset type=date inputs to text
$( document ).bind( "mobileinit", function(){ $( document ).bind( "mobileinit", function(){
$.mobile.page.prototype.options.degradeInputs.date = true; $.mobile.page.prototype.options.degradeInputs.date = true;
}); });
</script> </script>
<script src="js/jQuery.ui.datepicker.js"></script> <script src="js/jQuery.ui.datepicker.js"></script>
<script src="js/jquery.ui.datepicker.mobile.js"></script> <script src="js/jquery.ui.datepicker.mobile.js"></script>
<style type="text/css"> <style type="text/css">
.ui-navbar { .ui-navbar {
width: 100%; width: 100%;
} }
.ui-btn-inner { .ui-btn-inner {
white-space: normal !important; white-space: normal !important;
} }
.ui-li-heading { .ui-li-heading {
white-space: normal !important; white-space: normal !important;
} }
.ui-listview-filter { .ui-listview-filter {
margin: 0 !important; margin: 0 !important;
} }
.ui-icon-navigation { .ui-icon-navigation {
background-image: url(css/images/113-navigation.png); background-image: url(css/images/113-navigation.png);
background-position: 1px 0; background-position: 1px 0;
} }
#footer { #footer {
text-size: 0.75em; text-size: 0.75em;
text-align: center; text-align: center;
} }
body { body {
background-color: #F0F0F0; background-color: #F0F0F0;
} }
</style> </style>
<meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" /> <meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="apple-touch-startup-image" href="startup.png" /> <link rel="apple-touch-startup-image" href="startup.png" />
<link rel="apple-touch-icon" href="apple-touch-icon.png" />'; <link rel="apple-touch-icon" href="apple-touch-icon.png" />';
if ($geolocate) { if ($geolocate) {
echo "<script> echo "<script>
   
function success(position) { function success(position) {
$('#geolocate').val(position.coords.latitude+','+position.coords.longitude); $('#geolocate').val(position.coords.latitude+','+position.coords.longitude);
$.ajax({ url: \"common.inc.php?geolocate=yes&lat=\"+position.coords.latitude+\"&lon=\"+position.coords.longitude }); $.ajax({ url: \"common.inc.php?geolocate=yes&lat=\"+position.coords.latitude+\"&lon=\"+position.coords.longitude });
$('#here').click(function(event) { $('#geolocate').val(doAJAXrequestForGeolocSessionHere()); return false;}); $('#here').click(function(event) { $('#geolocate').val(doAJAXrequestForGeolocSessionHere()); return false;});
$('#here').show(); $('#here').show();
} }
function error(msg) { function error(msg) {
console.log(msg); console.log(msg);
} }
   
if (navigator.geolocation) { if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error); navigator.geolocation.getCurrentPosition(success, error);
} }
   
</script> "; </script> ";
} }
echo '</head> echo '</head>
<body> <body>
'; ';
if (isMetricsOn()) { if (isMetricsOn()) {
require_once ('owa/owa_env.php'); require_once ('owa/owa_env.php');
require_once (OWA_DIR . 'owa_php.php'); require_once (OWA_DIR . 'owa_php.php');
$owa = new owa_php(); $owa = new owa_php();
global $owaSiteID; global $owaSiteID;
$owa->setSiteId($owaSiteID); $owa->setSiteId($owaSiteID);
$owa->setPageTitle($pageTitle); $owa->setPageTitle($pageTitle);
$owa->setPageType($pageType); $owa->setPageType($pageType);
$owa->trackPageView(); $owa->trackPageView();
$owa->placeHelperPageTags(); $owa->placeHelperPageTags();
} }
if ($opendiv) { if ($opendiv) {
echo '<div data-role="page"> echo '<div data-role="page">
<script> <script>
$(document).ready(function () $(document).ready(function ()
{ {
document.title = "' . $pageTitle . '"; document.title = "' . $pageTitle . '";
}); });
</script> </script>
<div data-role="header"> <div data-role="header">
<h1>' . $pageTitle . '</h1> <h1>' . $pageTitle . '</h1>
</div><!-- /header --> </div><!-- /header -->
<div data-role="content"> '; <div data-role="content"> ';
} }
} }
function include_footer() function include_footer()
{ {
if ($geolocate && isset($_SESSION['lat'])) { if ($geolocate && isset($_SESSION['lat'])) {
echo "<script> echo "<script>
$('#here').click(function(event) { $('#geolocate').val(doAJAXrequestForGeolocSessionHere()); return false;}); $('#here').click(function(event) { $('#geolocate').val(doAJAXrequestForGeolocSessionHere()); return false;});
$('#here').show(); $('#here').show();
</script>"; </script>";
} }
echo '<div id="footer"><a href="about.php">About/Contact Us</a>&nbsp;<a href="feedback.php">Feedback/Bug Report</a></a>'; echo '<div id="footer"><a href="about.php">About/Contact Us</a>&nbsp;<a href="feedback.php">Feedback/Bug Report</a></a>';
echo '</div>'; echo '</div>';
} }
function timePlaceSettings($geolocate = false) function timePlaceSettings($geolocate = false)
{ {
global $service_periods; global $service_periods;
$geoerror = false; $geoerror = false;
if ($geolocate == true) { if ($geolocate == true) {
$geoerror = !isset($_SESSION['lat']) || !isset($_SESSION['lat']) || $_SESSION['lat'] == "" || $_SESSION['lon'] == ""; $geoerror = !isset($_SESSION['lat']) || !isset($_SESSION['lat']) || $_SESSION['lat'] == "" || $_SESSION['lon'] == "";
} }
if ($geoerror) { if ($geoerror) {
echo '<div class="error">Sorry, but your location could not currently be detected. echo '<div class="error">Sorry, but your location could not currently be detected.
Please allow location permission, wait for your location to be detected, Please allow location permission, wait for your location to be detected,
or enter an address/co-ordinates in the box below.</div>'; or enter an address/co-ordinates in the box below.</div>';
} }
echo '<div data-role="collapsible" data-collapsed="' . !$geoerror . '"> echo '<div data-role="collapsible" data-collapsed="' . !$geoerror . '">
<h3>Change Time/Place...</h3> <h3>Change Time/Place (' . $_SESSION['time'] . ' '.ucwords(service_period()).')...</h3>
<form action="" method="post"> <form action="" method="post">
<div class="ui-body"> <div class="ui-body">
<div data-role="fieldcontain"> <div data-role="fieldcontain">
<label for="geolocate"> Current Location: </label> <label for="geolocate"> Current Location: </label>
<input type="text" id="geolocate" name="geolocate" value="' . (isset($_SESSION['lat']) && isset($_SESSION['lon']) ? $_SESSION['lat'] . "," . $_SESSION['lon'] : "Enter co-ordinates or address here") . '"/> <a href="#" style="display:none" name="here" id="here"/>Here?</a> <input type="text" id="geolocate" name="geolocate" value="' . (isset($_SESSION['lat']) && isset($_SESSION['lon']) ? $_SESSION['lat'] . "," . $_SESSION['lon'] : "Enter co-ordinates or address here") . '"/> <a href="#" style="display:none" name="here" id="here"/>Here?</a>
</div> </div>
<div data-role="fieldcontain"> <div data-role="fieldcontain">
<label for="time"> Time: </label> <label for="time"> Time: </label>
<input type="time" name="time" id="time" value="' . (isset($_SESSION['time']) ? $_SESSION['time'] : date("H:i")) . '"/> <a href="#" name="currentTime" id="currentTime"/>Current Time?</a> <input type="time" name="time" id="time" value="' . (isset($_SESSION['time']) ? $_SESSION['time'] : date("H:i")) . '"/> <a href="#" name="currentTime" id="currentTime"/>Current Time?</a>
</div> </div>
<div data-role="fieldcontain"> <div data-role="fieldcontain">
<label for="service_period"> Service Period: </label> <label for="service_period"> Service Period: </label>
<select name="service_period">'; <select name="service_period">';
foreach ($service_periods as $service_period) { foreach ($service_periods as $service_period) {
echo "<option value=\"$service_period\"" . (service_period() === $service_period ? "SELECTED" : "") . '>' . ucwords($service_period) . '</option>'; echo "<option value=\"$service_period\"" . (service_period() === $service_period ? "SELECTED" : "") . '>' . ucwords($service_period) . '</option>';
} }
echo '</select> echo '</select>
<a href="#" style="display:none" name="currentPeriod" id="currentPeriod"/>Current Period?</a> <a href="#" style="display:none" name="currentPeriod" id="currentPeriod"/>Current Period?</a>
</div> </div>
<input type="submit" value="Update"/> <input type="submit" value="Update"/>
</form> </form>
</div></div>'; </div></div>';
} }
?> ?>
<?php <?php
include ('common.inc.php'); include ('common.inc.php');
$output = Array(); $output = Array();
$output['hotspots'] = Array(); $output['hotspots'] = Array();
$output['layer'] = "canberrabusstops"; $output['layer'] = "canberrabusstops";
$max_page = 10; $max_page = 10;
$max_results = 50; $max_results = 50;
$page_start = 0 + filter_var($_REQUEST['pageKey'], FILTER_SANITIZE_NUMBER_INT); $page_start = 0 + filter_var($_REQUEST['pageKey'], FILTER_SANITIZE_NUMBER_INT);
$page_end = $max_page + filter_var($_REQUEST['pageKey'], FILTER_SANITIZE_NUMBER_INT); $page_end = $max_page + filter_var($_REQUEST['pageKey'], FILTER_SANITIZE_NUMBER_INT);
$lat = filter_var($_REQUEST['lat'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); $lat = filter_var($_REQUEST['lat'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
$lon = filter_var($_REQUEST['lon'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); $lon = filter_var($_REQUEST['lon'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
if (isset($_REQUEST['radius'])) $radius = filter_var($_REQUEST['radius'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);  
$url = $APIurl . "/json/neareststops?lat=$lat&lon=$lon&limit=50"; $url = $APIurl . "/json/neareststops?lat=$lat&lon=$lon&limit=50";
$contents = json_decode(getPage($url)); $contents = json_decode(getPage($url));
debug(print_r($contents, true)); debug(print_r($contents, true));
$stopNum = 0; $stopNum = 0;
foreach ($contents as $row) { foreach ($contents as $row) {
   
$stopNum++; $stopNum++;
if ($stopNum > $page_start && $stopNum <= $page_end) { if ($stopNum > $page_start && $stopNum <= $page_end) {
   
$hotspot = Array(); $hotspot = Array();
$hotspot['id'] = $row[0]; $hotspot['id'] = $row[0];
$hotspot['title'] = $row[1]; $hotspot['title'] = $row[1];
$hotspot['type'] = 0; $hotspot['type'] = 0;
$hotspot['lat'] = floor($row[2] * 1000000); $hotspot['lat'] = floor($row[2] * 1000000);
$hotspot['lon'] = floor($row[3] * 1000000); $hotspot['lon'] = floor($row[3] * 1000000);
$hotspot['distance'] = distance($row[2], $row[3], $_REQUEST['lat'], $_REQUEST['lon']); $hotspot['distance'] = distance($row[2], $row[3], $_REQUEST['lat'], $_REQUEST['lon']);
if (!isset($_REQUEST['radius']) || $hotspot['distance'] < $radius) { if (!isset($_REQUEST['radius']) || $hotspot['distance'] < $radius) {
   
$hotspot['actions'] = Array( $hotspot['actions'] = Array(
Array( Array(
"label" => 'View more trips/information', "label" => 'View more trips/information',
'uri' => 'http://bus.lambdacomplex.org/' . 'stop.php?stopid=' . $row[0] 'uri' => 'http://bus.lambdacomplex.org/' . 'stop.php?stopid=' . $row[0]
) )
); );
$url = $APIurl . "/json/stoptrips?stop=" . $row[0] . "&time=" . midnight_seconds() . "&service_period=" . service_period() . "&limit=4&time_range=" . str(90 * 60); $url = $APIurl . "/json/stoptrips?stop=" . $row[0] . "&time=" . midnight_seconds() . "&service_period=" . service_period() . "&limit=4&time_range=" . strval(90 * 60);
$trips = json_decode(getPage($url)); $trips = json_decode(getPage($url));
debug(print_r($trips, true)); debug(print_r($trips, true));
foreach ($trips as $key => $row) { foreach ($trips as $key => $row) {
if ($key < 3) { if ($key < 3) {
$hotspot['line' . strval($key + 2) ] = $row[1][1] . ' @ ' . midnight_seconds_to_time($row[0]); $hotspot['line' . strval($key + 2) ] = $row[1][1] . ' @ ' . midnight_seconds_to_time($row[0]);
} }
} }
if (sizeof($trips) == 0) $hotspot['line2'] = 'No trips in the near future.'; if (sizeof($trips) == 0) $hotspot['line2'] = 'No trips in the near future.';
$output['hotspots'][] = $hotspot; $output['hotspots'][] = $hotspot;
} }
} }
} }
if (sizeof($hotspot) > 0) { if (sizeof($hotspot) > 0) {
$output['errorString'] = 'ok'; $output['errorString'] = 'ok';
$output['errorCode'] = 0; $output['errorCode'] = 0;
} }
else { else {
$output['errorString'] = 'no results, try increasing range'; $output['errorString'] = 'no results, try increasing range';
$output['errorCode'] = 21; $output['errorCode'] = 21;
} }
if ($page_end >= $max_results || sizeof($hotspot) < $max_page) { if ($page_end >= $max_results || sizeof($hotspot) < $max_page) {
$output["morePages"] = false; $output["morePages"] = false;
$output["nextPageKey"] = null; $output["nextPageKey"] = null;
} }
else { else {
$output["morePages"] = true; $output["morePages"] = true;
$output["nextPageKey"] = $page_end; $output["nextPageKey"] = $page_end;
} }
echo json_encode($output); echo json_encode($output);
?> ?>
#!/usr/bin/python2.5 #!/usr/bin/python2.5
   
# Copyright (C) 2007 Google Inc. # Copyright (C) 2007 Google Inc.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
   
""" """
An example application that uses the transitfeed module. An example application that uses the transitfeed module.
   
You must provide a Google Maps API key. You must provide a Google Maps API key.
""" """
   
   
import BaseHTTPServer, sys, urlparse import BaseHTTPServer, sys, urlparse
import bisect import bisect
from gtfsscheduleviewer.marey_graph import MareyGraph from gtfsscheduleviewer.marey_graph import MareyGraph
import gtfsscheduleviewer import gtfsscheduleviewer
import mimetypes import mimetypes
import os.path import os.path
import re import re
import signal import signal
import simplejson import simplejson
import socket import socket
import time import time
import datetime import datetime
import transitfeed import transitfeed
from transitfeed import util from transitfeed import util
import urllib import urllib
   
   
# By default Windows kills Python with Ctrl+Break. Instead make Ctrl+Break # By default Windows kills Python with Ctrl+Break. Instead make Ctrl+Break
# raise a KeyboardInterrupt. # raise a KeyboardInterrupt.
if hasattr(signal, 'SIGBREAK'): if hasattr(signal, 'SIGBREAK'):
signal.signal(signal.SIGBREAK, signal.default_int_handler) signal.signal(signal.SIGBREAK, signal.default_int_handler)
   
   
mimetypes.add_type('text/plain', '.vbs') mimetypes.add_type('text/plain', '.vbs')
   
   
class ResultEncoder(simplejson.JSONEncoder): class ResultEncoder(simplejson.JSONEncoder):
def default(self, obj): def default(self, obj):
try: try:
iterable = iter(obj) iterable = iter(obj)
except TypeError: except TypeError:
pass pass
else: else:
return list(iterable) return list(iterable)
return simplejson.JSONEncoder.default(self, obj) return simplejson.JSONEncoder.default(self, obj)
   
# Code taken from # Code taken from
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/425210/index_txt # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/425210/index_txt
# An alternate approach is shown at # An alternate approach is shown at
# http://mail.python.org/pipermail/python-list/2003-July/212751.html # http://mail.python.org/pipermail/python-list/2003-July/212751.html
# but it requires multiple threads. A sqlite object can only be used from one # but it requires multiple threads. A sqlite object can only be used from one
# thread. # thread.
class StoppableHTTPServer(BaseHTTPServer.HTTPServer): class StoppableHTTPServer(BaseHTTPServer.HTTPServer):
def server_bind(self): def server_bind(self):
BaseHTTPServer.HTTPServer.server_bind(self) BaseHTTPServer.HTTPServer.server_bind(self)
self.socket.settimeout(1) self.socket.settimeout(1)
self._run = True self._run = True
   
def get_request(self): def get_request(self):
while self._run: while self._run:
try: try:
sock, addr = self.socket.accept() sock, addr = self.socket.accept()
sock.settimeout(None) sock.settimeout(None)
return (sock, addr) return (sock, addr)
except socket.timeout: except socket.timeout:
pass pass
   
def stop(self): def stop(self):
self._run = False self._run = False
   
def serve(self): def serve(self):
while self._run: while self._run:
self.handle_request() self.handle_request()
   
   
def StopToTuple(stop): def StopToTuple(stop):
"""Return tuple as expected by javascript function addStopMarkerFromList""" """Return tuple as expected by javascript function addStopMarkerFromList"""
return (stop.stop_id, stop.stop_name, float(stop.stop_lat), return (stop.stop_id, stop.stop_name, float(stop.stop_lat),
float(stop.stop_lon), stop.location_type, stop.stop_code) float(stop.stop_lon), stop.location_type, stop.stop_code)
def StopZoneToTuple(stop): def StopZoneToTuple(stop):
"""Return tuple as expected by javascript function addStopMarkerFromList""" """Return tuple as expected by javascript function addStopMarkerFromList"""
return (stop.stop_id, stop.stop_name, float(stop.stop_lat), return (stop.stop_id, stop.stop_name, float(stop.stop_lat),
float(stop.stop_lon), stop.location_type, stop.stop_code, stop.zone_id) float(stop.stop_lon), stop.location_type, stop.stop_code, stop.zone_id)
   
class ScheduleRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): class ScheduleRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
cache = {} cache = {}
def do_GET(self): def do_GET(self):
scheme, host, path, x, params, fragment = urlparse.urlparse(self.path) scheme, host, path, x, params, fragment = urlparse.urlparse(self.path)
parsed_params = {} parsed_params = {}
for k in params.split('&'): for k in params.split('&'):
k = urllib.unquote(k) k = urllib.unquote(k)
if '=' in k: if '=' in k:
k, v = k.split('=', 1) k, v = k.split('=', 1)
parsed_params[k] = unicode(v, 'utf8') parsed_params[k] = unicode(v, 'utf8')
else: else:
parsed_params[k] = '' parsed_params[k] = ''
   
if path == '/': if path == '/':
return self.handle_GET_home() return self.handle_GET_home()