Skip to content

Commit 05b9c23

Browse files
committed
Changed to use the JSON feed instead of XML. I fucking hate XML.
1 parent d210ea4 commit 05b9c23

27 files changed

+83
-3477
lines changed

MMM-MyTTC.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
position: relative;
33
padding: 1px;
44
border-bottom: solid 1px #222;
5-
padding-bottom: 8px;
6-
margin-bottom: 8px;
5+
padding-bottom: 6px;
6+
margin-bottom: 12px;
77
}
88

99
.MMM-MyTTC .route-title {

MMM-MyTTC.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Module.register('MMM-MyTTC', {
8383
wrapper.classList.add("wrapper");
8484

8585
if (!this.loaded) {
86-
wrapper.innerHTML = "Loading...";
86+
wrapper.innerHTML = this.translate('LOADING');
8787
wrapper.className = "dimmed light small";
8888
return wrapper;
8989
} else if (this.ttcData == null) { //should never get here, but just in case.

node_helper.js

+50-27
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,16 @@
1111

1212
var NodeHelper = require("node_helper");
1313
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
14-
var convert = require('xml-js');
1514

1615
module.exports = NodeHelper.create({
1716

18-
webServiceURL: "http://webservices.nextbus.com/service/publicXMLFeed",
17+
webServiceURL: "http://webservices.nextbus.com/service/publicJSONFeed",
1918
agency: "ttc",
2019
dataRetriver: null,
2120

2221
start: function() {
2322
console.log("Starting node_helper for module: " + this.name);
24-
this.started = false;
23+
this.dataPollStarted = false;
2524
},
2625

2726
socketNotificationReceived: function(notification, payload){
@@ -38,12 +37,13 @@ module.exports = NodeHelper.create({
3837
}
3938

4039
this.url = builtURL;
40+
//console.log("=============>" + this.url);
4141

4242
//first data pull
4343
this.getTTCTimes();
4444

45-
if (!this.started) {
46-
this.started = true;
45+
if (!this.dataPollStarted) {
46+
this.dataPollStarted = true;
4747

4848

4949
//recurring data pull
@@ -62,7 +62,7 @@ module.exports = NodeHelper.create({
6262
var xmlHttp = new XMLHttpRequest();
6363
xmlHttp.onreadystatechange = function() {
6464
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { //good
65-
self.processXML(xmlHttp.responseText);
65+
self.processJSON(xmlHttp.responseText);
6666
} else if (xmlHttp.readyState == 4) { //bad...
6767
self.sendSocketNotification('MMM-MYTTC-RESPONSE', {data:null});
6868
}
@@ -83,50 +83,73 @@ module.exports = NodeHelper.create({
8383
return assembledTitle;
8484
},
8585

86-
processXML: function(xmlText) {
86+
processJSON: function(JSONText) {
8787

8888
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++) {
89102

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("-");
97105
routeTitlePieces.shift(); //remove the route number from the title
98106
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),
101109
routeTitle : routeTitlePieces.join(" "),
102-
stopTitle : p._attributes.stopTitle,
110+
stopTitle : p.stopTitle,
103111
});
104112

105113
route.branches = new Array();
106114

107115
var assembledTitle;
108116

109-
if (p._attributes.dirTitleBecauseNoPredictions) { //no data for this route
117+
if (p.dirTitleBecauseNoPredictions) { //no data for this route
110118
route.noSchedule = true,
111119
route.branches.push({
112-
title: this.formatTitle(p._attributes.dirTitleBecauseNoPredictions),
120+
title: this.formatTitle(p.dirTitleBecauseNoPredictions),
113121
nextVehicles: []
114122
})
115-
116123
} 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];
119134

120135
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++) {
122145
if (k == 3) {
123146
break;
124147
}
125-
minutesArray.push(Number(d.prediction[k]._attributes.minutes));
148+
minutesArray.push(Number(dPredictionsArray[k].minutes));
126149
}
127150

128151
route.branches.push({
129-
title: this.formatTitle(d._attributes.title),
152+
title: this.formatTitle(d.title),
130153
nextVehicles: minutesArray
131154
})
132155

@@ -149,13 +172,13 @@ module.exports = NodeHelper.create({
149172
if (self.config.routeList[i].color) {
150173
el.color = self.config.routeList[i].color;
151174
}
175+
routeList.push(el);
152176
return el;
153177
}
154178
});
155-
routeList.push(matchingElement);
156179
}
157180

158-
//return the JSON object with index
181+
//return the JSON object
159182
this.sendSocketNotification('MMM-MYTTC-RESPONSE', routeList);
160183

161184
}

node_modules/sax/LICENSE

-41
This file was deleted.

0 commit comments

Comments
 (0)