Added menu option to show/hide messages table.
[ptas] / zouba / wrt / basic.js
1 ///////////////////////////////////////////////////////////////////////////////
2 // The FeedUpdateBroker class implements a simple RSS fetcher and parser.
3
4 var username = "zouba";
5 var password = "caf9r3ee";
6
7 var homeKey = "taivaanvuohentie%207%2Chelsinki";
8 var workKey = "it%E4merenkatu%2011%2Chelsinki";
9
10 var work = {
11     x: 2551042,
12     y: 6672829
13 };
14 var home = {
15     x: 2549183,
16     y: 6672570
17 };
18
19 var keyA = work;
20 var keyB = home;
21
22 // Feed update broker.
23 var feedUpdateBroker = null;
24
25 // Constructor.
26 function FeedUpdateBroker()
27 {
28     this.httpReq = null;
29     this.callback = null;
30 }
31
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)
35 {
36     // remember callback
37     this.callback = callback;
38     
39     // create new XML HTTP request
40     this.httpReq = new Ajax();
41     
42     // set callback
43     var self = this;
44     this.httpReq.onreadystatechange = function()
45     {
46         self.readyStateChanged();
47     };
48     
49     // append the current time after the URL to bypass caches
50     var fullURL = feedURL;
51     if (fullURL.indexOf("?") == -1) {
52         fullURL += "?";
53     }
54     else {
55         fullURL += "&";
56     }
57     fullURL += "nocache=" + (new Date().getTime());
58     
59     // initiate the request
60     this.httpReq.open("GET", fullURL, true);
61     this.httpReq.send(null);
62 }
63
64 // Callback for ready-state change events in the XML HTTP request.
65 FeedUpdateBroker.prototype.readyStateChanged = function()
66 {
67     // complete request?
68     if (this.httpReq.readyState == 4) {
69         // attempt to get response status
70         var responseStatus = null;
71         try {
72             responseStatus = this.httpReq.status;
73         } 
74         catch (noStatusException) {
75         }
76         
77         this.callback(responseStatus, this.httpReq.responseXML);
78     }
79 }
80
81 function parseRouteXML(responseStatus, xmlDoc)
82 {
83   if (responseStatus == 200 && xmlDoc != null) {
84     var routes = xmlDoc.getElementsByTagName("ROUTE");
85
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");
90
91       var stop = firstLine.getElementsByTagName("STOP")[0];
92       var arrival = stop.getElementsByTagName("ARRIVAL")[0];
93       var arrivalTime = arrival.getAttribute("time");
94       addRow( joreCode, arrivalTime );
95     };          
96   }
97 }
98
99 function addRow( joreCode, timeAtStop )
100 {
101   var timeTable = document.getElementById("timeTable");
102
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;
108 }
109
110
111 function parseHomeXML(responseStatus, xmlDoc)
112 {
113     if (responseStatus == 200 && xmlDoc != null) {
114         var location = xmlDoc.getElementsByTagName("LOC");
115         var firstLocation = location[0];
116         
117         home.x = firstLocation.getAttribute("x");
118         home.y = firstLocation.getAttribute("y");
119     }
120 }
121
122 function parseWorkXML(responseStatus, xmlDoc)
123 {
124     if (responseStatus == 200 && xmlDoc != null) {
125         var location = xmlDoc.getElementsByTagName("LOC");
126         var firstLocation = location[0];
127         
128         work.x = firstLocation.getAttribute("x");
129         work.y = firstLocation.getAttribute("y");
130     }
131 }
132
133 function getRoute()
134 {
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);
137 }
138
139 function parseJORECode(joreCode)
140 {
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);
146     
147     return "" + lineCode + letterVariant;
148 }
149
150 function emptyTable()
151 {
152   var table = document.getElementById("timeTable");
153   var rows = table.rows;
154   for (var i = rows.length - 1; i >= 0; i--){
155     table.deleteRow(i);
156   }
157   document.getElementById("debug").innerHTML = "";
158 }
159
160 function takeMeHome()
161 {
162     emptyTable();
163
164     keyA = work;
165     keyB = home;
166     
167     getRoute();
168 }
169
170 function takeMeToWork()
171 {
172     emptyTable();
173
174     keyA = home;
175     keyB = work;
176     
177     getRoute();
178 }
179
180 var so;
181
182 try {
183     //Retrieves the Service object to the ILocation interface
184     so = device.getServiceObject("Service.Location", "ILocation");
185     document.getElementById("debug").innerHTML = "after so";
186 } catch (e) {
187     document.getElementById("debug").innerHTML = ' ' +e;
188     //alert(' ' +e);
189 }
190  
191 // Gets the GPS position
192 function getLocationAsync()
193 {
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;
200  
201     var criteria = new Object();
202  
203     criteria.Updateoptions = updateoptions;
204  
205     try {
206     //Executes the GetLocation method and sets the callbackLocation as the callback function to be called.
207     so.ILocation.GetLocation(criteria,callbackLocation);
208     } catch (e) {
209         document.getElementById("debug").innerHTML = "getLocationAsync: " + e;
210         //alert ("getLocationAsync: " + e);
211     }
212 }
213
214 //Callback function that receives the result as parameter.
215 function callbackLocation(transId, eventCode, result)
216 {
217     var latitude = result.ReturnValue.Latitude;
218     var longitude = result.ReturnValue.Longitude;
219     document.getElementById("debug").innerHTML = latitude+":"+longitude;
220 }
221
222 function init()
223 {
224     feedUpdateBroker = new FeedUpdateBroker();
225     
226     var homeURL = "http://api.reittiopas.fi/public-ytv/fi/api/?key=" + homeKey + "&user=" + username + "&pass=" + password;
227     feedUpdateBroker.fetchFeed(homeURL, parseHomeXML);
228     
229     var workURL = "http://api.reittiopas.fi/public-ytv/fi/api/?key=" + workKey + "&user=" + username + "&pass=" + password;
230     feedUpdateBroker.fetchFeed(workURL, parseWorkXML);
231
232     //getLocationAsync();
233 }