-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnode_helper.js
133 lines (123 loc) · 4.79 KB
/
node_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var request = require("request");
var NodeHelper = require("node_helper");
module.exports = NodeHelper.create({
// Process socket notification from MMM-CTA.js
start: function() {
this.config = null;
this.instance = null;
},
getData: function(payload) {
var self = this;
var busReady = false;
var trainReady = false;
this.instance = "MMM-CTA-DATA" + payload.modInstance;
// Set myUrlBus based on what is set, first, if "top" is set, then if not set
if (payload.urlBus !== null && payload.key !== null && payload.stpid !== null && payload.maxRes !== null) {
var myUrlBus = payload.urlBus + "?key=" + payload.key + "&stpid=" + payload.stpid + "&top=" + payload.maxRes + "&format=json";
busReady = true;
} else if (payload.urlBus !== null && payload.key !== null && payload.stpid !== null) {
var myUrlBus = payload.urlBus + "?key=" + payload.key + "&stpid=" + payload.stpid + "&format=json";
busReady = true;
} else { console.log("MMM-CTA: Bus config incomplete!!!"); };
if (payload.urlTrain !== null && payload.keyTrain !== null && payload.maxResTrain !== null && payload.idTrain !== null) {
var myUrlTrain = payload.urlTrain + "?key=" + payload.keyTrain + "&max="+ payload.maxResTrain + "&mapid=" + payload.idTrain + "&outputType=json";
trainReady = true;
} else if (payload.urlTrain !== null && payload.keyTrain !== null && payload.idTrain !== null) { // Max result will be 4 by default
var myUrlTrain = payload.urlTrain + "?key=" + payload.keyTrain + "&max=5" + "&mapid=" + payload.idTrain + "&outputType=json";
trainReady = true;
} else { console.log("MMM-CTA: Train config incomplete!!!"); };
// Functions for bus & train, bus, or train only
if (busReady && trainReady) {
self.getBusTrain(myUrlBus, myUrlTrain);
} else if (busReady) {
self.getBus(myUrlBus);
} else if (trainReady) {
self.getTrain(myUrlTrain);
} else {
console.log("No complete bus or train configs found!!!");
};
},
// console.log(myUrl); // for debugging
getBusTrain: function(myUrlBus, myUrlTrain) {
var self = this;
console.log(self.instance);
var bodyJs = {
bus: null,
train: null
};
request({url: myUrlBus}, function (error, response, body) {
// console.log("CTA request fired."); // for debugging
// Following line for building, delete when able to get DOM to show
// self.sendSocketNotification("MMM-CTA-DATA", "TESTING");
// Delete above when solved
if (!error && response.statusCode == 200) {
bodyJs.bus = JSON.parse(body);
console.log(bodyJs.bus); // For testing purposes;
request({url: myUrlTrain}, function (error, response, body) {
console.log("CTA request fired train."); // for debugging
// Following line for building, delete when able to get DOM to show
// self.sendSocketNotification("MMM-CTA-DATA", "TESTING");
// Delete above when solved
if (!error && response.statusCode == 200) {
bodyJs.train = JSON.parse(body);
console.log(bodyJs.train); // For testing purposes;
self.sendSocketNotification(self.instance, bodyJs)
}
});
} else {
// If no data from bus or error from bus still run train!
request({url: myUrlTrain}, function (error, response, body) {
console.log("CTA request fired train."); // for debugging
// If no error, store in bodyJs
if (!error && response.statusCode == 200) {
bodyJs.train = JSON.parse(body);
console.log(bodyJs.train); // For testing purposes;
self.sendSocketNotification(self.instance, bodyJs)
};
});
}
});
},
getBus: function(myUrlBus) {
var self = this;
var bodyJs = {
bus: null,
train: null
};
request({url: myUrlBus}, function (error, response, body) {
// console.log("CTA request fired."); // for debugging
// Following line for building, delete when able to get DOM to show
// self.sendSocketNotification("MMM-CTA-DATA", "TESTING");
// Delete above when solved
if (!error && response.statusCode == 200) {
bodyJs.bus = JSON.parse(body);
self.sendSocketNotification(self.instance, bodyJs);
console.log(bodyJs);
};
});
},
getTrain: function(myUrlTrain) {
var self = this;
var bodyJs = {
bus: null,
train: null
};
request({url: myUrlTrain}, function (error, response, body) {
console.log("CTA request fired train."); // for debugging
// If no error, store in bodyJs
if (!error && response.statusCode == 200) {
bodyJs.train = JSON.parse(body);
console.log(bodyJs.train); // For testing purposes;
self.sendSocketNotification(self.instance, bodyJs)
};
});
},
socketNotificationReceived: function(notification, payload) { // Payload is 'request' from MMM-CTA
var self = this;
if (notification === "CTA-REQUEST" ) {
// console.log("Running CTA-REQUEST"); // for debugging
self.config = payload;
self.getData(payload);
}
}
});