Skip to content

Commit 0153cc1

Browse files
committed
Fixed data mechanism to support multipe instances of module
1 parent 694486d commit 0153cc1

File tree

2 files changed

+37
-43
lines changed

2 files changed

+37
-43
lines changed

MMM-MyTTC.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,27 @@ Module.register('MMM-MyTTC', {
5454

5555
Log.info('Starting module: ' + this.name);
5656

57+
this.UNIQUE_STRING = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5);
58+
5759
this.loaded = false;
5860
this.ttcData = null;
5961

60-
//strat getting data
61-
this.sendSocketNotification("MMM-MYTTC-GET", this.config);
62+
//start getting data
63+
this.getData();
64+
65+
var self = this;
66+
setInterval(function() {
67+
self.getData();
68+
}, this.config.updateInterval);
69+
},
70+
71+
getData: function() {
72+
this.sendSocketNotification("MMM-MYTTC-GET", {unique: this.UNIQUE_STRING, config:this.config});
6273
},
6374

6475
socketNotificationReceived: function(notification, payload) {
6576
//only update if a data set is returned. Otherwise leave stale data on the screen.
66-
if ( notification === 'MMM-MYTTC-RESPONSE' && payload != null) {
77+
if ( notification === 'MMM-MYTTC-RESPONSE' + this.UNIQUE_STRING && payload != null) {
6778
this.ttcData = payload;
6879

6980
//only fade in tye module after the first data pull

node_helper.js

+23-40
Original file line numberDiff line numberDiff line change
@@ -16,62 +16,45 @@ module.exports = NodeHelper.create({
1616

1717
webServiceURL: "http://webservices.nextbus.com/service/publicJSONFeed",
1818
agency: "ttc",
19-
dataRetriver: null,
2019

2120
start: function() {
2221
console.log("Starting node_helper for module: " + this.name);
23-
this.dataPollStarted = false;
2422
},
2523

2624
socketNotificationReceived: function(notification, payload){
2725
if (notification === 'MMM-MYTTC-GET') {
2826

29-
this.url = '';
30-
this.config = payload;
27+
var self = this;
3128

3229
var builtURL = this.webServiceURL + "?&command=predictionsForMultiStops&a=" + this.agency;
3330

34-
var routes = this.config.routeList;
31+
var routes = payload.config.routeList;
3532
for (var i = 0; i < routes.length; i++) {
3633
builtURL += "&stops=" + routes[i].routeNo + "|" + routes[i].stop;
3734
}
3835

39-
this.url = builtURL;
40-
//console.log("=============>" + this.url);
36+
// console.log("=============>" + builtURL);
4137

42-
//first data pull
43-
this.getTTCTimes();
4438

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
4842

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+
}
5449
}
55-
}
56-
},
50+
xmlHttp.open("GET", builtURL, true); // true for asynchronous
51+
xmlHttp.send(null);
5752

58-
getTTCTimes: function() {
59-
60-
var self = this;
6153

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-
}
6954
}
70-
xmlHttp.open("GET", self.url, true); // true for asynchronous
71-
xmlHttp.send(null);
72-
7355
},
7456

57+
7558
formatTitle: function(s) {
7659
var titlePieces = s.split(" - ");
7760
var branchNo = titlePieces[1].split(" ")[0].toUpperCase();
@@ -83,7 +66,7 @@ module.exports = NodeHelper.create({
8366
return assembledTitle;
8467
},
8568

86-
processJSON: function(JSONText) {
69+
processJSON: function(JSONText, config) {
8770

8871
var resultList = new Array;
8972
var rawJSON = JSON.parse(JSONText);
@@ -163,14 +146,14 @@ module.exports = NodeHelper.create({
163146
//reorder resultList to match config order
164147
var self = this;
165148
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++) {
167150
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;
171154
}
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;
174157
}
175158
routeList.push(el);
176159
return el;
@@ -179,7 +162,7 @@ module.exports = NodeHelper.create({
179162
}
180163

181164
//return the JSON object
182-
this.sendSocketNotification('MMM-MYTTC-RESPONSE', routeList);
165+
return routeList
183166

184167
}
185168

0 commit comments

Comments
 (0)