This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
MMM-json-feed.js
210 lines (179 loc) · 5.41 KB
/
MMM-json-feed.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"use strict";
Module.register("MMM-json-feed", {
firstUpdate: true,
result: {},
defaults: {
prettyName: true,
stripName: true,
title: "JSON Feed",
url: "", // Deprecated
urls: [], // Added as a new parameter to maintain backwards compatibility
updateInterval: 600000,
singleLine: false,
values: [],
replaceName: [],
arrayName: "",
arraySize: 999
},
start: function() {
this.getStats();
this.scheduleUpdate();
},
isEmpty: function(obj) {
for(var key in obj) {
if(obj.hasOwnProperty(key)) {
return false;
}
}
return true;
},
getDom: function() {
var wrapper = document.createElement("ticker");
wrapper.className = "dimmed small";
var data = this.result;
var statElement = document.createElement("header");
var title = this.config.title;
statElement.innerHTML = title;
wrapper.appendChild(statElement);
if (data && !this.isEmpty(data)) {
var tableElement = document.createElement("table");
var values = this.config.values;
if (this.config.arrayName.length > 0) {
try {
data = this.byString(data, this.config.arrayName);
if (data && data.length) {
for (var i = 0; i < data.length && i < this.config.arraySize; i++) {
this.addValues(data[i], values, tableElement);
if (i < data.length - 1) {
var hr = document.createElement("hr");
hr.style = "border-color: #444;"
tableElement.appendChild(hr);
}
}
} else {
this.addValues(data, values, tableElement);
}
} catch (e) {
console.error(e);
this.addValues(data, values, tableElement);
}
} else {
this.addValues(data, values, tableElement);
}
wrapper.appendChild(tableElement);
} else {
var error = document.createElement("span");
error.innerHTML = this.firstUpdate ? "Fetching stats" : "Error fetching stats.";
wrapper.appendChild(error);
}
return wrapper;
},
addValues: function(data, values, tableElement) {
console.log(data);
if (values.length > 0) {
for (var i = 0; i < values.length; i++) {
var val = this.getValue(data, values[i]);
if (val) {
tableElement.appendChild(this.addValue(values[i], val));
}
}
} else {
for (var key in data) {
if (data.hasOwnProperty(key)) {
tableElement.appendChild(this.addValue(key, data[key]));
}
}
}
},
getValue: function(data, value) {
if (data && value) {
var split = value.split(".");
var current = data;
while (split.length > 0) {
current = current[split.shift()];
}
return current;
}
return null;
},
addValue: function(name, value) {
// This is a nasty hack, don't do this in prod kids
var row = this.config.singleLine ? document.createElement("span") : document.createElement("tr");
var split = name.split(".");
var strippedName = split[split.length - 1];
if (this.config.stripName) {
name = strippedName;
}
// Replace overrides not stripping the name
if (this.matchesReplace(strippedName)) {
name = this.replaceName(strippedName);
} else if (this.config.prettyName) {
name = name.replace(/([A-Z])/g, function($1){return "_"+$1.toLowerCase();});
name = name.split("_").join(" ");
name = name.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
row.innerHTML = "";
if (name.length > 0) {
name = name.replace(/['"]+/g, '');
row.innerHTML = name + ": ";
}
row.innerHTML += JSON.stringify(value).replace(/['"]+/g, '');
return row;
},
matchesReplace: function(name) {
for (var i = 0; i < this.config.replaceName.length; i++) {
var n = this.config.replaceName[i];
if (n[0].toLowerCase() === name.toLowerCase()) {
console.log("matched")
return true;
}
}
return false;
},
replaceName: function(name) {
for (var i = 0; i < this.config.replaceName.length; i++) {
var n = this.config.replaceName[i];
if (n[0].toLowerCase() === name.toLowerCase()) {
return n[1];
}
}
return name;
},
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setInterval(function() {
self.getStats();
}, nextLoad);
},
getStats: function () {
var url = (this.config.url.length > 0) ? [this.config.url] : [];
var allUrls = this.config.urls.concat(url);
this.sendSocketNotification("GET_STATS", allUrls);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "STATS_RESULT") {
this.result = payload;
this.firstUpdate = false;
this.updateDom(500); // 500 is fade
}
},
// function from https://stackoverflow.com/questions/6491463
byString: function(o, s) {
s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
s = s.replace(/^\./, ''); // strip a leading dot
var a = s.split('.');
for (var i = 0, n = a.length; i < n; ++i) {
var k = a[i];
if (k in o) {
o = o[k];
} else {
return;
}
}
return o;
}
});