11
11
12
12
var NodeHelper = require ( "node_helper" ) ;
13
13
var XMLHttpRequest = require ( "xmlhttprequest" ) . XMLHttpRequest ;
14
- var convert = require ( 'xml-js' ) ;
15
14
16
15
module . exports = NodeHelper . create ( {
17
16
18
- webServiceURL : "http://webservices.nextbus.com/service/publicXMLFeed " ,
17
+ webServiceURL : "http://webservices.nextbus.com/service/publicJSONFeed " ,
19
18
agency : "ttc" ,
20
19
dataRetriver : null ,
21
20
22
21
start : function ( ) {
23
22
console . log ( "Starting node_helper for module: " + this . name ) ;
24
- this . started = false ;
23
+ this . dataPollStarted = false ;
25
24
} ,
26
25
27
26
socketNotificationReceived : function ( notification , payload ) {
@@ -38,12 +37,13 @@ module.exports = NodeHelper.create({
38
37
}
39
38
40
39
this . url = builtURL ;
40
+ //console.log("=============>" + this.url);
41
41
42
42
//first data pull
43
43
this . getTTCTimes ( ) ;
44
44
45
- if ( ! this . started ) {
46
- this . started = true ;
45
+ if ( ! this . dataPollStarted ) {
46
+ this . dataPollStarted = true ;
47
47
48
48
49
49
//recurring data pull
@@ -62,7 +62,7 @@ module.exports = NodeHelper.create({
62
62
var xmlHttp = new XMLHttpRequest ( ) ;
63
63
xmlHttp . onreadystatechange = function ( ) {
64
64
if ( xmlHttp . readyState == 4 && xmlHttp . status == 200 ) { //good
65
- self . processXML ( xmlHttp . responseText ) ;
65
+ self . processJSON ( xmlHttp . responseText ) ;
66
66
} else if ( xmlHttp . readyState == 4 ) { //bad...
67
67
self . sendSocketNotification ( 'MMM-MYTTC-RESPONSE' , { data :null } ) ;
68
68
}
@@ -83,50 +83,73 @@ module.exports = NodeHelper.create({
83
83
return assembledTitle ;
84
84
} ,
85
85
86
- processXML : function ( xmlText ) {
86
+ processJSON : function ( JSONText ) {
87
87
88
88
var resultList = new Array ;
89
+ var rawJSON = JSON . parse ( JSONText ) ;
90
+
91
+ //for some reason, the JSON feed does not place single child
92
+ //predictions in an array. So we need to fake it in order for
93
+ //iteration to work. Also repeated below for directions and
94
+ //predictionswithin directions.
95
+ var predictionsArray = new Array ( ) ;
96
+ if ( rawJSON . predictions . length ) {
97
+ predictionsArray = rawJSON . predictions
98
+ } else {
99
+ predictionsArray . push ( rawJSON . predictions ) ;
100
+ }
101
+ for ( var i = 0 ; i < predictionsArray . length ; i ++ ) {
89
102
90
- //convert XML to JSON object because I fucking hate working with XML
91
- var rawJSON = convert . xml2js ( xmlText , { compact : true , alwaysArray : true } ) ;
92
-
93
- for ( var i = 0 ; i < rawJSON . body [ 0 ] . predictions . length ; i ++ ) {
94
-
95
- var p = rawJSON . body [ 0 ] . predictions [ i ] ;
96
- var routeTitlePieces = p . _attributes . routeTitle . split ( "-" ) ;
103
+ var p = predictionsArray [ i ] ;
104
+ var routeTitlePieces = p . routeTitle . split ( "-" ) ;
97
105
routeTitlePieces . shift ( ) ; //remove the route number from the title
98
106
var route = new Object ( {
99
- routeNo : Number ( p . _attributes . routeTag ) ,
100
- stopTag : Number ( p . _attributes . stopTag ) ,
107
+ routeNo : Number ( p . routeTag ) ,
108
+ stopTag : Number ( p . stopTag ) ,
101
109
routeTitle : routeTitlePieces . join ( " " ) ,
102
- stopTitle : p . _attributes . stopTitle ,
110
+ stopTitle : p . stopTitle ,
103
111
} ) ;
104
112
105
113
route . branches = new Array ( ) ;
106
114
107
115
var assembledTitle ;
108
116
109
- if ( p . _attributes . dirTitleBecauseNoPredictions ) { //no data for this route
117
+ if ( p . dirTitleBecauseNoPredictions ) { //no data for this route
110
118
route . noSchedule = true ,
111
119
route . branches . push ( {
112
- title : this . formatTitle ( p . _attributes . dirTitleBecauseNoPredictions ) ,
120
+ title : this . formatTitle ( p . dirTitleBecauseNoPredictions ) ,
113
121
nextVehicles : [ ]
114
122
} )
115
-
116
123
} else {
117
- for ( var j = 0 ; j < p . direction . length ; j ++ ) {
118
- var d = p . direction [ j ] ;
124
+
125
+ var directionsArray = new Array ( ) ;
126
+ if ( p . direction . length ) {
127
+ directionsArray = p . direction
128
+ } else {
129
+ directionsArray . push ( p . direction ) ;
130
+ }
131
+
132
+ for ( var j = 0 ; j < directionsArray . length ; j ++ ) {
133
+ var d = directionsArray [ j ] ;
119
134
120
135
var minutesArray = new Array ;
121
- for ( var k = 0 ; k < d . prediction . length ; k ++ ) {
136
+
137
+ var dPredictionsArray = new Array ( ) ;
138
+ if ( d . prediction . length ) {
139
+ dPredictionsArray = d . prediction
140
+ } else {
141
+ dPredictionsArray . push ( d . prediction ) ;
142
+ }
143
+
144
+ for ( var k = 0 ; k < dPredictionsArray . length ; k ++ ) {
122
145
if ( k == 3 ) {
123
146
break ;
124
147
}
125
- minutesArray . push ( Number ( d . prediction [ k ] . _attributes . minutes ) ) ;
148
+ minutesArray . push ( Number ( dPredictionsArray [ k ] . minutes ) ) ;
126
149
}
127
150
128
151
route . branches . push ( {
129
- title : this . formatTitle ( d . _attributes . title ) ,
152
+ title : this . formatTitle ( d . title ) ,
130
153
nextVehicles : minutesArray
131
154
} )
132
155
@@ -149,13 +172,13 @@ module.exports = NodeHelper.create({
149
172
if ( self . config . routeList [ i ] . color ) {
150
173
el . color = self . config . routeList [ i ] . color ;
151
174
}
175
+ routeList . push ( el ) ;
152
176
return el ;
153
177
}
154
178
} ) ;
155
- routeList . push ( matchingElement ) ;
156
179
}
157
180
158
- //return the JSON object with index
181
+ //return the JSON object
159
182
this . sendSocketNotification ( 'MMM-MYTTC-RESPONSE' , routeList ) ;
160
183
161
184
}
0 commit comments