1 ///////////////////////////////////////////////////////////////////////////////
2 // The FeedUpdateBroker class implements a simple RSS fetcher and parser.
4 var username = "zouba";
5 var password = "caf9r3ee";
7 var homeKey = "taivaanvuohentie%207%2Chelsinki";
8 var workKey = "it%E4merenkatu%2011%2Chelsinki";
22 // Feed update broker.
23 var feedUpdateBroker = null;
26 function FeedUpdateBroker()
32 // Fetches a feed from the specified URL and calls the callback when the feed
33 // has been fetched and parsed, or if the process results in an error.
34 FeedUpdateBroker.prototype.fetchFeed = function(feedURL, callback)
37 this.callback = callback;
39 // create new XML HTTP request
40 this.httpReq = new Ajax();
44 this.httpReq.onreadystatechange = function()
46 self.readyStateChanged();
49 // append the current time after the URL to bypass caches
50 var fullURL = feedURL;
51 if (fullURL.indexOf("?") == -1) {
57 fullURL += "nocache=" + (new Date().getTime());
59 // initiate the request
60 this.httpReq.open("GET", fullURL, true);
61 this.httpReq.send(null);
64 // Callback for ready-state change events in the XML HTTP request.
65 FeedUpdateBroker.prototype.readyStateChanged = function()
68 if (this.httpReq.readyState == 4) {
69 // attempt to get response status
70 var responseStatus = null;
72 responseStatus = this.httpReq.status;
74 catch (noStatusException) {
77 this.callback(responseStatus, this.httpReq.responseXML);
81 function parseRouteXML(responseStatus, xmlDoc)
83 if (responseStatus == 200 && xmlDoc != null) {
84 var routes = xmlDoc.getElementsByTagName("ROUTE");
86 for (var i = routes.length - 1; i >= 0; i--){
87 var lines = routes[i].getElementsByTagName("LINE");
88 var firstLine = lines[0];
89 var joreCode = firstLine.getAttribute("code");
91 var stop = firstLine.getElementsByTagName("STOP")[0];
92 var arrival = stop.getElementsByTagName("ARRIVAL")[0];
93 var arrivalTime = arrival.getAttribute("time");
94 addRow( joreCode, arrivalTime );
99 function addRow( joreCode, timeAtStop )
101 var timeTable = document.getElementById("timeTable");
103 var newRow = timeTable.insertRow(0);
104 var codeCell = newRow.insertCell(0);
105 var timeAtStopCell = newRow.insertCell(1);
106 codeCell.innerHTML = parseJORECode(joreCode);
107 timeAtStopCell.innerHTML = timeAtStop;
111 function parseHomeXML(responseStatus, xmlDoc)
113 if (responseStatus == 200 && xmlDoc != null) {
114 var location = xmlDoc.getElementsByTagName("LOC");
115 var firstLocation = location[0];
117 home.x = firstLocation.getAttribute("x");
118 home.y = firstLocation.getAttribute("y");
122 function parseWorkXML(responseStatus, xmlDoc)
124 if (responseStatus == 200 && xmlDoc != null) {
125 var location = xmlDoc.getElementsByTagName("LOC");
126 var firstLocation = location[0];
128 work.x = firstLocation.getAttribute("x");
129 work.y = firstLocation.getAttribute("y");
135 var feedURL = "http://api.reittiopas.fi/public-ytv/fi/api/?a=" + keyA.x + "," + keyA.y + "&b=" + keyB.x + "," + keyB.y + "&user=" + username + "&pass=" + password;
136 feedUpdateBroker.fetchFeed(feedURL, parseRouteXML);
139 function parseJORECode(joreCode)
141 var areaTransportTypeCode = joreCode.substring(0, 1);
142 var lineCode = joreCode.substring(1, 5);
143 var letterVariant = joreCode.substring(5, 6);
144 var letterNumberVariant = joreCode.substring(6, 7);
145 var direction = joreCode.substring(7, 8);
147 return "" + lineCode + letterVariant;
150 function emptyTable()
152 var table = document.getElementById("timeTable");
153 var rows = table.rows;
154 for (var i = rows.length - 1; i >= 0; i--){
157 document.getElementById("debug").innerHTML = "";
160 function takeMeHome()
170 function takeMeToWork()
183 //Retrieves the Service object to the ILocation interface
184 so = device.getServiceObject("Service.Location", "ILocation");
185 document.getElementById("debug").innerHTML = "after so";
187 document.getElementById("debug").innerHTML = ' ' +e;
191 // Gets the GPS position
192 function getLocationAsync()
194 document.getElementById("debug").innerHTML = "getLocationAsync()";
195 // This specifies update option used while retrieving location estimation.
196 var updateoptions = new Object();
197 // Setting PartialUpdates to 'FALSE' ensures that user get atleast
198 // BasicLocationInformation (Longitude, Lattitude, and Altitude.) is the default when no LocationInformationClass criteria is given.
199 updateoptions.PartialUpdates = false;
201 var criteria = new Object();
203 criteria.Updateoptions = updateoptions;
206 //Executes the GetLocation method and sets the callbackLocation as the callback function to be called.
207 so.ILocation.GetLocation(criteria,callbackLocation);
209 document.getElementById("debug").innerHTML = "getLocationAsync: " + e;
210 //alert ("getLocationAsync: " + e);
214 //Callback function that receives the result as parameter.
215 function callbackLocation(transId, eventCode, result)
217 var latitude = result.ReturnValue.Latitude;
218 var longitude = result.ReturnValue.Longitude;
219 document.getElementById("debug").innerHTML = latitude+":"+longitude;
224 feedUpdateBroker = new FeedUpdateBroker();
226 var homeURL = "http://api.reittiopas.fi/public-ytv/fi/api/?key=" + homeKey + "&user=" + username + "&pass=" + password;
227 feedUpdateBroker.fetchFeed(homeURL, parseHomeXML);
229 var workURL = "http://api.reittiopas.fi/public-ytv/fi/api/?key=" + workKey + "&user=" + username + "&pass=" + password;
230 feedUpdateBroker.fetchFeed(workURL, parseWorkXML);
232 //getLocationAsync();