-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMMM-SnmpIntSpeed.js
224 lines (204 loc) · 7.02 KB
/
MMM-SnmpIntSpeed.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
var download, upload;
Module.register("MMM-SnmpIntSpeed", {
// Default module config.
defaults: {
customStyle: null,
gaugeType: "minimal",
text: "",
dataFile: 'data/snmp.json',
interval: 5,
community: 'public',
version: 2,
host: "localhost",
index: 1
},
payload: [],
/**
* Main entry point from MagicMirror
*
* @return {void}
*/
start() {
Log.info("Starting module: " + this.name)
let speed = (this.config.hasOwnProperty("speed") ? (!/^(auto|0)$/i.test(this.config.speed) ? this.config.speed : false) : false)
this.nodeHelperConfig = {
gaugeType: this.config.gaugeType,
community: this.config.community,
version: this.config.version,
interval: this.config.interval,
host: this.config.host,
index: this.config.index,
speed: speed
}
this.initializeUpdate()
this.setUpdateInterval()
},
/**
* Creates a "timer" to update the page on a interval
*
* @return {void}
*/
setUpdateInterval() {
this.updater = setInterval(() => {
this.initializeUpdate()
}, this.config.interval * 1000)
},
/**
* Send config to node helper to wait on the retrieval of new posts
*
* @return {void}
*/
initializeUpdate() {
this.sendSocketNotification('SNMP_POOL_SERVERS', { config: this.nodeHelperConfig })
},
/**
* Load javascripts
*
* @return {void}
*/
getScripts() {
return [this.file('js/justgage-1.2.2/justgage.js'), this.file('js/justgage-1.2.2/raphael-2.1.4.min.js'), this.file('js/jquery.js')]
},
/**
* Load CSS styles
*
* @return {void}
*/
getStyles() {
var styles = [this.file("css/style.css")]
if (this.config.customStyle != null) { styles.push(this.file(this.config.customStyle)) }
return styles
},
/**
* Returns an HTML DOM object with the elements to be added to the page
*
* @return {object} wrapper an HTML DOM object with the elements to be added to the page
*/
getDom() {
var wrapper = document.createElement("div")
var downloadSpeedGauge = document.createElement("div")
downloadSpeedGauge.id = 'downloadSpeedGauge'
var uploadSpeedGauge = document.createElement("div")
uploadSpeedGauge.id = 'uploadSpeedGauge'
wrapper.appendChild(downloadSpeedGauge)
wrapper.appendChild(uploadSpeedGauge)
return wrapper
},
/**
* Process notifications from the application or other modules
*
* @return {void}
*/
notificationReceived(notification, payload, sender) {
if (notification == 'DOM_OBJECTS_CREATED') {
this.addScript()
}
},
/**
* Process socket notification from node_helper
*
* @return {void}
*/
socketNotificationReceived(notification, payload) {
Log.log(this.name + " socketNotificationReceived:" + notification)
if (notification == "SNMP_POOL_RESPONSE") {
var down = payload.download
var up = payload.upload
var max = (this.nodeHelperConfig.speed != 0 ? this.nodeHelperConfig.speed : payload.highSpeed)
upload.refresh(up, max)
download.refresh(down, max)
}
},
/**
* Adds javascript to the page. This needs to be called after the
* download and upload DIVs have been added to the page
*
* @return {void}
*/
addScript(mode) {
ifText = ""
var script = document.createElement('script')
if (this.config.gaugeType == 'minimal') {
script.innerHTML = '' + //'var download, upload;' +
'download = new JustGage({' +
'id: "downloadSpeedGauge",' +
'value: 50,' +
'min: 0,' +
'max: 100,' +
'title: ' + ifText + ' "Download",' +
'refreshAnimationType:"linear",' +
'gaugeWidthScale: "0.8",' +
'valueFontColor: "#fff",' +
'valueFontFamily: "Roboto Condensed",' +
'titleFontFamily: "Roboto Condensed",' +
'titleFontColor: "#aaa",' +
'gaugeColor: "#000",' +
'levelColors: ["#fff"],' +
'hideInnerShadow: true,' +
'hideMinMax: false,' +
'decimals: 2,' +
'label: "bps",' +
'humanFriendly: true,' +
'symbol: " "});' +
'upload = new JustGage({' +
'id: "uploadSpeedGauge",' +
'value: ' + 0 + ',' +
'min: 0,' +
'max: 100,' +
'title: ' + ifText + ' "Upload",' +
'refreshAnimationType:"linear",' +
'gaugeWidthScale: "0.8",' +
'valueFontColor: "#fff",' +
'valueFontFamily: "Roboto Condensed",' +
'titleFontFamily: "Roboto Condensed",' +
'titleFontColor: "#aaa",' +
'gaugeColor: "#000",' +
'levelColors: ["#fff"],' +
'hideInnerShadow: true,' +
'hideMinMax: false,' +
'decimals: 2,' +
'label: "bps",' +
'humanFriendly: true,' +
'symbol: " "});'
} else {
script.innerHTML = 'var download, upload;' +
'download = new JustGage({' +
'id: "downloadSpeedGauge",' +
'value: ' + 0 + ',' +
'min: 0,' +
'max: 100,' +
'title: ' + ifText + ' "Download",' +
'refreshAnimationType:"linear",' +
'gaugeWidthScale: "0.8",' +
'valueFontColor: "#fff",' +
'valueFontFamily: "Roboto Condensed",' +
'titleFontFamily: "Roboto Condensed",' +
'titleFontColor: "#aaa",' +
'formatNumber: true,' +
'hideMinMax: false,' +
'decimals: 2,' +
'label: "bps",' +
'humanFriendly: true,' +
'symbol: " "});' +
'upload = new JustGage({' +
'id: "uploadSpeedGauge",' +
'value: ' + 0 + ',' +
'min: 0,' +
'max: 100,' +
'title: ' + ifText + ' "Upload",' +
'refreshAnimationType:"linear",' +
'gaugeWidthScale: "0.8",' +
'valueFontColor: "#fff",' +
'valueFontFamily: "Roboto Condensed",' +
'titleFontFamily: "Roboto Condensed",' +
'titleFontColor: "#aaa",' +
'formatNumber: true,' +
'hideMinMax: false,' +
'decimals: 2,' +
'label: "bps",' +
'humanFriendly: true,' +
'symbol: " "});'
}
$(script).appendTo('body')
}
})