-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMMM-HTTPRequestDisplay.js
191 lines (162 loc) · 4.59 KB
/
MMM-HTTPRequestDisplay.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* Module */
/* Magic Mirror
* Module: MMM-HTTPRequestDisplay
*
* By Eunan Camilleri eunancamilleri@gmail.com
* v1.0 23/06/2016
* MIT Licensed.
*/
Module.register("MMM-HTTPRequestDisplay",{
// Default module config.
defaults: {
updateInterval: 10000, // every 10 seconds
initialLoadDelay: 2500, // 2.5 seconds delay
retryDelay: 2500, // retry delay
animationSpeed: 2500, // animation speed
httpRequestURL: ""
},
// Define start sequence.
start: function() {
Log.info("Starting module: " + this.name);
this.loaded = false;
this.scheduleUpdate(this.config.initialLoadDelay);
this.nodeNames = "";
this.updateTimer = null;
this.nodes = [];
this.currentValueIndex = 0;
this.failureFlag = "";
this.status = "";
this.requestComplete;
// Schedule update timer.
var self = this;
setInterval(function() {
self.updateDom(self.config.animationSpeed);
}, this.config.updateInterval);
self.updateRequest();
},
// Define required scripts.
getScripts: function() {
return ["httpreqdisplay.css"];
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("div");
var childNodes = document.createElement("div");
var span = document.createElement("span");
span.innerHTML = " ";
if(this.config.httpRequestURL === null || this.config.httpRequestURL === ""){
wrapper.innerHTML = "Please set your request URL target in your config file</br>See ReadMe for more information";
}
// Signals an issue with the HTTP Requeset
else if(this.failureFlag){
wrapper.innerHTML = "HTTP Request Failed. Status : " + this.status + "</br>Please check the request URL in the module config";
}
// Checks for results
else if(this.nodes == undefined || this.nodes.length === 0){
if(this.requestComplete){
wrapper.innerHTML = "No Results";
}
else {
wrapper.innerHTML = "Awaiting Results..."
}
}
else {
// Grabs the next value in our results
var nextValue = this.getNextValue();
// Wrap the title of the node and add it to the wrapper
wrapper.appendChild(this.wrapNodeTitle(nextValue[0]));
wrapper.appendChild(span);
//Add and display attributes
for(var x = 0; x < nextValue[1].length; x++){
wrapper.appendChild(this.wrapAttribute(nextValue[1][x]));
}
}
return wrapper;
},
wrapNodeTitle : function(node){
var nodeTitle, nodeValue, container;
container = document.createElement("div");
nodeTitle = node[0];
if(node[1] === null){
nodeValue = "";
}
else {
nodeValue = " : " + node[1];
}
container.className = "node-title center";
container.innerHTML = nodeTitle + nodeValue;
return container;
},
wrapAttribute : function(attribute){
var container, attributeTitle, attributeValue;
attributeTitle = attribute[0];
container = document.createElement("div");
if(attribute[1] === null){
attributeValue = "No Value";
}
else {
attributeValue = " : " + attribute[1];
}
container.className = "attribute-title bright small center";
container.innerHTML = attributeTitle + attributeValue;
return container;
},
getNextValue : function(){
var index = this.currentValueIndex;
if(this.currentValueIndex + 1 === this.nodes.length-1){
this.currentValueIndex = 0;
}
else {
this.currentValueIndex++;
}
return this.nodes[index];
},
updateRequest: function() {
var self = this;
var retry = true;
var xhttp = new XMLHttpRequest();
xhttp.open("GET", this.config.httpRequestURL, true);
xhttp.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status === 200) {
self.requestComplete = true;
self.processData(this.responseXML);
self.updateDom(self.config.animationSpeed);
}
else {
self.failureFlag = true;
self.status = this.status;
self.updateDom(self.config.animationSpeed);
}
if (retry) {
self.scheduleUpdate((self.loaded) ? -1 : self.config.retryDelay);
}
}
};
xhttp.send();
},
processData: function(data) {
this.nodes = [];
var x = data.documentElement.childNodes;
for (i = 0; i < x.length ;i++) {
var attributes = [];
for(j = 0; j < x[i].attributes.length; j++){
attributes.push([x[i].attributes[j].nodeName, x[i].attributes[j].nodeValue]);
}
this.nodes.push([[x[i].nodeName, x[i].nodeValue], attributes]);
}
this.loaded = true;
this.updateDom(this.config.animationSpeed);
},
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
clearTimeout(this.updateTimer);
this.updateTimer = setTimeout(function() {
self.updateRequest();
}, nextLoad);
}
});