-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
74 lines (65 loc) · 1.93 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
/*
* Magic Mirror
* Module: MMM-AirQuality
*
* By Smart'Gic
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const fetch = require('node-fetch');
module.exports = NodeHelper.create({
userAgent: 'MMM-AirQuality',
start() {
console.log('MMM-AirQuality helper started');
},
getMetrics() {
const parent = this;
const mq2Url = this.config.abstractApiUrl + '/v1/gas/' + this.config.sensor
fetch(mq2Url, {
headers: {'Accept': 'application/json',
'Content-Type': 'application/json',
'User-Agent': this.userAgent}
})
.then(res => res.json())
.then(json => parent.sendSocketNotification('DATA_MQ2', json));
},
sendAlert() {
let led = {
gpio: this.config.ledGpio,
frequency: this.config.ledFrequency,
speed: this.config.ledSpeed,
step: this.config.ledStep,
duration: this.config.ledDuration
};
let speech = {
utterance: this.config.speech,
lang: this.config.speechLang
};
const ledUrl = this.config.abstractApiUrl + '/v1/leds/led'
fetch(ledUrl, {
method: 'POST',
body: JSON.stringify(led),
headers: {'Accept': 'application/json',
'Content-Type': 'application/json',
'User-Agent': this.userAgent}
});
const speechUrl = this.config.mycroftApiUrl + '/v1/voice/speech'
fetch(speechUrl, {
method: 'POST',
body: JSON.stringify(speech),
headers: {'Accept': 'application/json',
'Content-Type': 'application/json',
'User-Agent': this.userAgent,
'Authorization': 'Bearer ' + this.config.mycroftApiToken}
});
},
socketNotificationReceived(notification, payload) {
if (notification == 'GET_MQ2') {
this.getMetrics();
} else if (notification == 'INIT_MQ2') {
this.config = payload;
} else if (notification == 'ALERT_MQ2') {
this.sendAlert();
}
}
});