@@ -16,62 +16,45 @@ module.exports = NodeHelper.create({
16
16
17
17
webServiceURL : "http://webservices.nextbus.com/service/publicJSONFeed" ,
18
18
agency : "ttc" ,
19
- dataRetriver : null ,
20
19
21
20
start : function ( ) {
22
21
console . log ( "Starting node_helper for module: " + this . name ) ;
23
- this . dataPollStarted = false ;
24
22
} ,
25
23
26
24
socketNotificationReceived : function ( notification , payload ) {
27
25
if ( notification === 'MMM-MYTTC-GET' ) {
28
26
29
- this . url = '' ;
30
- this . config = payload ;
27
+ var self = this ;
31
28
32
29
var builtURL = this . webServiceURL + "?&command=predictionsForMultiStops&a=" + this . agency ;
33
30
34
- var routes = this . config . routeList ;
31
+ var routes = payload . config . routeList ;
35
32
for ( var i = 0 ; i < routes . length ; i ++ ) {
36
33
builtURL += "&stops=" + routes [ i ] . routeNo + "|" + routes [ i ] . stop ;
37
34
}
38
35
39
- this . url = builtURL ;
40
- //console.log("=============>" + this.url);
36
+ // console.log("=============>" + builtURL);
41
37
42
- //first data pull
43
- this . getTTCTimes ( ) ;
44
38
45
- if ( ! this . dataPollStarted ) {
46
- this . dataPollStarted = true ;
47
-
39
+ var xmlHttp = new XMLHttpRequest ( ) ;
40
+ xmlHttp . onreadystatechange = function ( ) {
41
+ if ( xmlHttp . readyState == 4 && xmlHttp . status == 200 ) { //good
48
42
49
- //recurring data pull
50
- var self = this ;
51
- this . dataRetriver = setInterval ( function ( ) {
52
- self . getTTCTimes ( ) ;
53
- } , this . config . updateInterval ) ;
43
+ var processedData = self . processJSON ( xmlHttp . responseText , payload . config ) ;
44
+ self . sendSocketNotification ( 'MMM-MYTTC-RESPONSE' + payload . unique , processedData ) ;
45
+
46
+ } else if ( xmlHttp . readyState == 4 ) { //bad...
47
+ self . sendSocketNotification ( 'MMM-MYTTC-RESPONSE' + payload . unique , { data :null } ) ;
48
+ }
54
49
}
55
- }
56
- } ,
50
+ xmlHttp . open ( "GET" , builtURL , true ) ; // true for asynchronous
51
+ xmlHttp . send ( null ) ;
57
52
58
- getTTCTimes : function ( ) {
59
-
60
- var self = this ;
61
53
62
- var xmlHttp = new XMLHttpRequest ( ) ;
63
- xmlHttp . onreadystatechange = function ( ) {
64
- if ( xmlHttp . readyState == 4 && xmlHttp . status == 200 ) { //good
65
- self . processJSON ( xmlHttp . responseText ) ;
66
- } else if ( xmlHttp . readyState == 4 ) { //bad...
67
- self . sendSocketNotification ( 'MMM-MYTTC-RESPONSE' , { data :null } ) ;
68
- }
69
54
}
70
- xmlHttp . open ( "GET" , self . url , true ) ; // true for asynchronous
71
- xmlHttp . send ( null ) ;
72
-
73
55
} ,
74
56
57
+
75
58
formatTitle : function ( s ) {
76
59
var titlePieces = s . split ( " - " ) ;
77
60
var branchNo = titlePieces [ 1 ] . split ( " " ) [ 0 ] . toUpperCase ( ) ;
@@ -83,7 +66,7 @@ module.exports = NodeHelper.create({
83
66
return assembledTitle ;
84
67
} ,
85
68
86
- processJSON : function ( JSONText ) {
69
+ processJSON : function ( JSONText , config ) {
87
70
88
71
var resultList = new Array ;
89
72
var rawJSON = JSON . parse ( JSONText ) ;
@@ -163,14 +146,14 @@ module.exports = NodeHelper.create({
163
146
//reorder resultList to match config order
164
147
var self = this ;
165
148
var routeList = new Array ( ) ;
166
- for ( var i = 0 ; i < this . config . routeList . length ; i ++ ) {
149
+ for ( var i = 0 ; i < config . routeList . length ; i ++ ) {
167
150
var matchingElement = resultList . find ( function ( el ) {
168
- if ( el . routeNo == self . config . routeList [ i ] . routeNo && el . stopTag == self . config . routeList [ i ] . stop ) {
169
- if ( self . config . routeList [ i ] . label ) {
170
- el . routeTitle = self . config . routeList [ i ] . label ;
151
+ if ( el . routeNo == config . routeList [ i ] . routeNo && el . stopTag == config . routeList [ i ] . stop ) {
152
+ if ( config . routeList [ i ] . label ) {
153
+ el . routeTitle = config . routeList [ i ] . label ;
171
154
}
172
- if ( self . config . routeList [ i ] . color ) {
173
- el . color = self . config . routeList [ i ] . color ;
155
+ if ( config . routeList [ i ] . color ) {
156
+ el . color = config . routeList [ i ] . color ;
174
157
}
175
158
routeList . push ( el ) ;
176
159
return el ;
@@ -179,7 +162,7 @@ module.exports = NodeHelper.create({
179
162
}
180
163
181
164
//return the JSON object
182
- this . sendSocketNotification ( 'MMM-MYTTC-RESPONSE' , routeList ) ;
165
+ return routeList
183
166
184
167
}
185
168
0 commit comments