forked from RedNax67/MMM-WunderGround
-
Notifications
You must be signed in to change notification settings - Fork 10
/
node_helper.js
212 lines (174 loc) · 5.96 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
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
/* Magic Mirror
* Module: MMM-Traffic
*
* By Sam Lewis https://github.com/SamLewis0602
* MIT Licensed.
*/
var NodeHelper = require('node_helper');
var request = require('request');
var moment = require('moment');
const exec = require('child_process').exec;
module.exports = NodeHelper.create({
start: function () {
console.log('MMM-WunderGround helper started ...');
this.fetcherRunning = false;
this.wunderPayload = "";
},
getWifi: function() {
var self = this;
console.log('Execute wlan probe');
exec('sudo iw wlan0 link', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
var wifilink=stdout.split("\n");
for( var i = 0; i < wifilink.length; ++i ) {
if (wifilink[i].indexOf('SSID:') !== -1) {
var apArr=wifilink[i].split(" ");
var ap=apArr[1];
if ( this.config.debug === 1 ) {
console.log(ap);
}
}
if (wifilink[i].indexOf('signal:') !== -1) {
var signalArr = wifilink[i].split(" ");
var signal = String(130 + parseInt(signalArr[1]));
if ( this.config.debug === 1 ) {
console.log(signal);
}
}
}
self.sendSocketNotification('WIFI_STRENGTH', {'wifi_strength':signal.toString(),'wifi_ap':ap});
});
},
getStorage: function() {
var self = this;
console.log('Execute wlan probe');
exec('df -h --output=size,used,avail,pcent /', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
var storage=stdout.split("\n");
var storageArr=storage[1].split(/\s+/);
self.sendSocketNotification('SYSTEM_STORAGE', {'store_size':storageArr[1],'store_used':storageArr[2],'store_avail':storageArr[3],'store_pcent':storageArr[4]});
});
},
getTemp: function() {
var self = this;
exec('/opt/vc/bin/vcgencmd measure_temp', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
var tempArr=stdout.split("=");
var temp=tempArr[1];
if ( this.config.debug === 1 ) {
console.log(temp);
}
self.sendSocketNotification('SYSTEM_TEMP', {'system_temp':temp});
});
},
getMem: function() {
var self = this;
exec('free -oh|grep Mem:', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
var memArr=stdout.split(/\s+/);
self.sendSocketNotification('SYSTEM_MEM', {'mem_size':String(memArr[1]),'mem_used':memArr[2],'mem_free':memArr[3]});
});
},
fetchWunderground: function() {
var self = this;
this.fetcherRunning = true;
var params = this.config.apikey;
var wulang = this.config.lang.toUpperCase();
if (wulang == "DE") {
wulang = "DL";
}
params +=
"/conditions/hourly/forecast10day/astronomy/alerts/lang:" +
wulang;
params += "/q/" + this.config.pws;
params += ".json";
var Wurl = this.config.apiBase + params;
if ( this.config.debug === 1 ) {
console.log(moment().format() + " 4 " + this.name + ": " + Wurl);
}
request({
url: Wurl,
method: 'GET'
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
this.wunderPayload = body;
// console.log(moment().format() + " 5 " + self.name + ": " + body);
self.sendSocketNotification('WUNDERGROUND',body);
} else {
console.log(moment().format() + " 6 " + self.name + ": " + error);
}
setTimeout(function() {
self.fetchWunderground();
}, self.config.updateInterval);
}
);
},
//Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
console.log(notification);
var self = this;
if(notification === "GET_WUNDERGROUND"){
this.config = payload;
// console.log(JSON.stringify(payload));
if ( this.config.debug === 1 ) {
console.log('Lets get WunderGround');
}
if (!this.fetcherRunning) {
this.fetchWunderground();
} else {
var self = this;
var params = this.config.apikey;
var wulang = this.config.lang.toUpperCase();
if (wulang == "DE") {
wulang = "DL";
}
params +=
"/conditions/hourly/forecast10day/astronomy/alerts/lang:" +
wulang;
params += "/q/" + this.config.pws;
params += ".json";
var Wurl = this.config.apiBase + params;
if ( this.config.debug === 1 ) {
console.log(moment().format() + " 3 " + this.name + ": " + Wurl);
}
request({
url: Wurl,
method: 'GET'
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
this.wunderPayload = body;
// console.log(moment().format() + " 1 " + self.name + ": " + body);
self.sendSocketNotification('WUNDERGROUND',body);
} else {
console.log(moment().format() + " 2 " + self.name + ": " + error);
}
}
);
}
}
if (notification === 'GET_WIFI') {
this.getWifi();
}
if (notification === 'GET_SYSTEM_TEMP') {
this.getTemp();
}
if (notification === 'GET_SYSTEM_MEM') {
this.getMem();
}
if (notification === 'GET_SYSTEM_STORAGE') {
this.getStorage();
}
}
});